Решение на Регулярни изрази от Николай Масларски

Обратно към всички решения

Към профила на Николай Масларски

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 23 успешни тест(а)
  • 16 неуспешни тест(а)

Код

import re
class PrivacyFilter:
def __init__(self, text):
self.text = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
filtered_text = self.text
if self.preserve_email_hostname:
filtered_text =\
re.sub(r'\b[a-zA-Z0-9]([\w\+-\.]{1,60}(?=@([a-zA-Z0-9]'
'[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]{2,3}'
'(\.[a-zA-Z]{2})*))',
'[FILTERED]', filtered_text)
elif self.partially_preserve_email_username:
filtered_text =\
re.sub(r'(?<=\b[a-zA-Z0-9][\w\+-\.][\w\+-\.])([\w\+-\.]{1,60}'
'(?=@([a-zA-Z0-9]'
'[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]{2,3}(\.)?'
'(\.[a-zA-Z]{2})*))',
'[FILTERED]', filtered_text)
else:
filtered_text =\
re.sub(r'\b[a-zA-Z0-9]([\w\+-\.]{1,60}@([a-zA-Z0-9]'
'[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]'
'{2,3}(\.[a-zA-Z]{2})*)',
'[EMAIL]', filtered_text)
if self.preserve_phone_country_code:
filtered_text = re.sub(r'((?<=\+[1-9]\d\d)|(?<=\b 00))'
'([- \(\)]{,2}\d){6,11}',
' [FILTERED]', filtered_text)
filtered_text = re.sub(r'(?<=\+[1-9]\d)'
'([- \(\)]{,2}\d){6,11}',
' [FILTERED]', filtered_text)
filtered_text = re.sub(r'(?<=\+[1-9])'
'([- \(\)]{,2}\d){6,11}',
' [FILTERED]', filtered_text)
filtered_text = re.sub(r'((\+|\b 00)[1-9]\d{,2}|\b0(?!0))'
'([- \(\)]{,2}\d){6,11}',
'[PHONE]', filtered_text)
return filtered_text
class Validations:
@staticmethod
def is_email(text):
return bool(re.match(r'\b[a-zA-Z0-9]([\w\+-\.]{1,60}@([a-zA-Z0-9]'
'[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]{2,3}'
'(\.[a-zA-Z]{2})*)$',
text))
@staticmethod
def is_phone(text):
return bool(re.match(r'((\+|00)[1-9]\d{,2}|\b0(?!0))'
'([- \(\)]{,2}\d){6,11}$', text))
@staticmethod
def is_hostname(text):
return bool(re.match(r'([a-zA-Z0-9]'
'[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]'
'{2,3}(\.[a-zA-Z]{2})*$', text))
@staticmethod
def is_ip_address(text):
return bool(re.match(r'[0-2]?[0-5]?[0-5](\.[0-2]?[0-5]?[0-5]){3,3}$',
text))
@staticmethod
def is_number(text):
return bool(re.match(r'-?(0|[1-9]\d)(\.\d+)?$', text))
@staticmethod
def is_integer(text):
return bool(re.match(r'-?(0|[1-9]\d)$', text))
@staticmethod
def is_date(text):
return bool(re.match(r'\d{4}-(0[1-9]|1[0-2])-'
'(0[1-9]|[1-2][0-9]|3[0-2])$', text))
@staticmethod
def is_time(text):
return bool(re.match(r'(0[1-9]|1[0-9]|2[0-3])'
'(\:[0-5][0-9]){2}$', text))
@staticmethod
def is_datetime(text):
return Validations.is_date(text[:10]) and\
Validations.is_time(text[11:]) and text[10] in [' ', 'T']

Лог от изпълнението

..FFFFFF..F.....F.F....F..FF....F.F.F.F
======================================================================
FAIL: test_does_not_brake_with_unicode (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 64, in test_does_not_brake_with_unicode
    self.assertEqual('За връзка: [FILTERED]@example.com', self.partially_filter_email_usernames('За връзка: me@example.com'))
AssertionError: 'За връзка: [FILTERED]@example.com' != 'За връзка: me@example.com'
- За връзка: [FILTERED]@example.com
?            ^^^^^^^^^^
+ За връзка: me@example.com
?            ^^


======================================================================
FAIL: test_does_not_filter_invalid_emails (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 48, in test_does_not_filter_invalid_emails
    self.assertEqual(text, solution.PrivacyFilter(text).filtered())
AssertionError: 'And more: someone@invalid.domaintld' != 'And more: [EMAIL]aintld'
- And more: someone@invalid.domaintld
+ And more: [EMAIL]aintld


======================================================================
FAIL: test_does_not_filter_invalid_phone_numbers (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Reach me at: 0885123' != 'Reach me at: [PHONE]'
- Reach me at: 0885123
+ Reach me at: [PHONE]


======================================================================
FAIL: test_filters_more_complex_phone_numbers (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '004412125543'
- [PHONE]
+ 004412125543


======================================================================
FAIL: test_filters_whole_email_usernames_if_too_short (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 61, in test_filters_whole_email_usernames_if_too_short
    self.assertEqual('[FILTERED]@example.com', self.partially_filter_email_usernames('me@example.com'))
AssertionError: '[FILTERED]@example.com' != 'me@example.com'
- [FILTERED]@example.com
+ me@example.com


======================================================================
FAIL: test_obfuscates_more_complicated_emails (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[EMAIL]' != 'larodi@x.com'
- [EMAIL]
+ larodi@x.com


======================================================================
FAIL: test_separates_preserved_country_code_from_filtered_phone_with_a_space (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
AssertionError: 'Phone: 0025 [FILTERED]' != 'Phone: 0025(55) 12 12255'
- Phone: 0025 [FILTERED]
+ Phone: 0025(55) 12 12255


======================================================================
FAIL: test_can_validate_more_complex_phone_numbers (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 160, in test_can_validate_more_complex_phone_numbers
    self.assertIs(solution.Validations.is_phone(phone), valid)
AssertionError: True is not False

======================================================================
FAIL: test_does_not_allow_invalid_months_or_days_in_dates (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 255, in test_does_not_allow_invalid_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('2012-06-32'))
AssertionError: True is not false

======================================================================
FAIL: test_handles_multiline_strings_in_hostname_validation_properly (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 179, in test_handles_multiline_strings_in_hostname_validation_properly
    self.assertFalse(solution.Validations.is_hostname("foo.com\n"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_newlines_in_date_validation (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 259, in test_handles_newlines_in_date_validation
    self.assertFalse(solution.Validations.is_date("2012-11-19\n"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_newlines_in_time_and_datetime_validation (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 288, in test_handles_newlines_in_time_and_datetime_validation
    self.assertFalse(solution.Validations.is_time("12:01:01\n"))
AssertionError: True is not false

======================================================================
FAIL: test_validates_hostnames (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 169, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('1.2.3.4.xip.io'))
AssertionError: False is not true

======================================================================
FAIL: test_validates_more_complex_integers (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 227, in test_validates_more_complex_integers
    self.assertTrue(solution.Validations.is_integer('9'))
AssertionError: False is not true

======================================================================
FAIL: test_validates_numbers (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 197, in test_validates_numbers
    self.assertTrue(solution.Validations.is_number('9'))
AssertionError: False is not true

======================================================================
FAIL: test_validates_times (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1k2htmd/test.py", line 265, in test_validates_times
    self.assertTrue(solution.Validations.is_time('00:00:00'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 39 tests in 0.065s

FAILED (failures=16)

История (1 версия и 0 коментара)

Николай обнови решението на 22.04.2014 13:15 (преди около 10 години)

+import re
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ filtered_text = self.text
+
+ if self.preserve_email_hostname:
+ filtered_text =\
+ re.sub(r'\b[a-zA-Z0-9]([\w\+-\.]{1,60}(?=@([a-zA-Z0-9]'
+ '[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]{2,3}'
+ '(\.[a-zA-Z]{2})*))',
+ '[FILTERED]', filtered_text)
+
+ elif self.partially_preserve_email_username:
+ filtered_text =\
+ re.sub(r'(?<=\b[a-zA-Z0-9][\w\+-\.][\w\+-\.])([\w\+-\.]{1,60}'
+ '(?=@([a-zA-Z0-9]'
+ '[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]{2,3}(\.)?'
+ '(\.[a-zA-Z]{2})*))',
+ '[FILTERED]', filtered_text)
+ else:
+ filtered_text =\
+ re.sub(r'\b[a-zA-Z0-9]([\w\+-\.]{1,60}@([a-zA-Z0-9]'
+ '[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]'
+ '{2,3}(\.[a-zA-Z]{2})*)',
+ '[EMAIL]', filtered_text)
+
+ if self.preserve_phone_country_code:
+ filtered_text = re.sub(r'((?<=\+[1-9]\d\d)|(?<=\b 00))'
+ '([- \(\)]{,2}\d){6,11}',
+ ' [FILTERED]', filtered_text)
+
+ filtered_text = re.sub(r'(?<=\+[1-9]\d)'
+ '([- \(\)]{,2}\d){6,11}',
+ ' [FILTERED]', filtered_text)
+
+ filtered_text = re.sub(r'(?<=\+[1-9])'
+ '([- \(\)]{,2}\d){6,11}',
+ ' [FILTERED]', filtered_text)
+
+ filtered_text = re.sub(r'((\+|\b 00)[1-9]\d{,2}|\b0(?!0))'
+ '([- \(\)]{,2}\d){6,11}',
+ '[PHONE]', filtered_text)
+
+ return filtered_text
+
+
+class Validations:
+
+ @staticmethod
+ def is_email(text):
+ return bool(re.match(r'\b[a-zA-Z0-9]([\w\+-\.]{1,60}@([a-zA-Z0-9]'
+ '[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]{2,3}'
+ '(\.[a-zA-Z]{2})*)$',
+ text))
+
+ @staticmethod
+ def is_phone(text):
+ return bool(re.match(r'((\+|00)[1-9]\d{,2}|\b0(?!0))'
+ '([- \(\)]{,2}\d){6,11}$', text))
+
+ @staticmethod
+ def is_hostname(text):
+ return bool(re.match(r'([a-zA-Z0-9]'
+ '[a-zA-Z0-9-]{,61}[a-zA-Z0-9]\.)+[a-zA-Z]'
+ '{2,3}(\.[a-zA-Z]{2})*$', text))
+
+ @staticmethod
+ def is_ip_address(text):
+ return bool(re.match(r'[0-2]?[0-5]?[0-5](\.[0-2]?[0-5]?[0-5]){3,3}$',
+ text))
+
+ @staticmethod
+ def is_number(text):
+ return bool(re.match(r'-?(0|[1-9]\d)(\.\d+)?$', text))
+
+ @staticmethod
+ def is_integer(text):
+ return bool(re.match(r'-?(0|[1-9]\d)$', text))
+
+ @staticmethod
+ def is_date(text):
+ return bool(re.match(r'\d{4}-(0[1-9]|1[0-2])-'
+ '(0[1-9]|[1-2][0-9]|3[0-2])$', text))
+
+ @staticmethod
+ def is_time(text):
+ return bool(re.match(r'(0[1-9]|1[0-9]|2[0-3])'
+ '(\:[0-5][0-9]){2}$', text))
+
+ @staticmethod
+ def is_datetime(text):
+ return Validations.is_date(text[:10]) and\
+ Validations.is_time(text[11:]) and text[10] in [' ', 'T']