Решение на Регулярни изрази от Велина Дойчева

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

Към профила на Велина Дойчева

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 20 успешни тест(а)
  • 19 неуспешни тест(а)

Код

import re
tld_pattern = r'\b[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
hostname_part_pattern = r'\b[0-9A-Za-z]([0-9a-z\-]{,61}[0-9A-Za-z])?\b'
domain_pattern = hostname_part_pattern + r'\.' + tld_pattern
hostname_pattern = hostname_part_pattern + r'\.' + tld_pattern
email_pattern = r'(?P<username>[a-z0-9][\w_\-+\.]{,200})@' + hostname_pattern
phone_number_pattern_local = r'(?<![\w\+])0(([\ \-\(\)]{,2}[0-9]){6,11})'
phone_number_pattern_inter = (r'(?<![\w\+])(0{2}|\+)[\ \-\(\)]{,2}'
r'(?P<code>[1-9][0-9]{1,2})'
r'([\ \-\(\)]{,2}[0-9]){6,11}')
ip_address_pattern = (r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}'
r'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
number_pattern = r'^\-?(0|([1-9][0-9]*))(\.[\d]+)?$'
integer_pattern = r'^\-?(0|([1-9][0-9][^\.]*))$'
date_pattern = (r'[0-9]{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])'
'|(3[01]))')
time_pattern = r'(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]'
date_time_pattern = date_pattern + r'[T\s\b\-]' + time_pattern
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
def filtered(self):
text = self.text
text = re.sub(email_pattern, '[EMAIL]', text)
text = re.sub(phone_number_pattern_inter, '[PHONE]', text)
text = re.sub(phone_number_pattern_local, '[PHONE]', text)
return text
class Validations:
def is_email(value):
match = re.match(email_pattern, value)
if match is None:
return False
else:
return True
def is_phone(value):
match = re.match(phone_number_pattern_inter, value)
match_local = re.match(phone_number_pattern_local, value)
if match is None and match_local is None:
return False
else:
return True
def is_hostname(value):
match = re.match(hostname_pattern, value)
if match is None:
return False
else:
return True
def is_ip_address(value):
match = re.match(ip_address_pattern, value)
if match is None:
return False
else:
return True
def is_number(value):
match = re.match(number_pattern, str(value))
if match is None:
return False
else:
return True
def is_integer(value):
match = re.match(integer_pattern, value)
if match is None:
return False
else:
return True
def is_date(value):
match = re.match(date_pattern, value)
if match is None:
return False
else:
return True
def is_time(value):
match = re.match(time_pattern, value)
if match is None:
return False
else:
return True
def is_datetime(value):
match = re.match(date_time_pattern, value)
if match is None:
return False
else:
return True

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

FFFFFFFF..F....FF...FFFF..FF....F.F....
======================================================================
FAIL: test_allows_email_hostname_to_be_preserved (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-z7jrgm/test.py", line 54, in test_allows_email_hostname_to_be_preserved
    self.assertEqual('[FILTERED]@example.com', self.filter_email_usernames('someone@example.com'))
AssertionError: '[FILTERED]@example.com' != '[EMAIL]'
- [FILTERED]@example.com
+ [EMAIL]


======================================================================
FAIL: test_allows_email_usernames_to_be_partially_preserved (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-z7jrgm/test.py", line 58, in test_allows_email_usernames_to_be_partially_preserved
    self.assertEqual('som[FILTERED]@example.com', self.partially_filter_email_usernames('someone@example.com'))
AssertionError: 'som[FILTERED]@example.com' != '[EMAIL]'
- som[FILTERED]@example.com
+ [EMAIL]


======================================================================
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-z7jrgm/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' != 'За връзка: [EMAIL]'
- За връзка: [FILTERED]@example.com
+ За връзка: [EMAIL]


======================================================================
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-z7jrgm/test.py", line 48, in test_does_not_filter_invalid_emails
    self.assertEqual(text, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact me here: _invalid@email.com' != 'Contact me here: _[EMAIL]'
- Contact me here: _invalid@email.com
+ Contact me here: _[EMAIL]


======================================================================
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-z7jrgm/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-z7jrgm/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '+1 555 123-456'
- [PHONE]
+ +1 555 123-456


======================================================================
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-z7jrgm/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' != '[EMAIL]'
- [FILTERED]@example.com
+ [EMAIL]


======================================================================
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-z7jrgm/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact: [EMAIL],[EMAIL]' != 'Contact: [EMAIL],someone.new@sub.example123.co.uk'
- Contact: [EMAIL],[EMAIL]
+ Contact: [EMAIL],someone.new@sub.example123.co.uk


======================================================================
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-z7jrgm/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: [PHONE]'
- Phone: 0025 [FILTERED]
+ Phone: [PHONE]


======================================================================
FAIL: test_can_validate_more_complex_emails (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-z7jrgm/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), valid)
AssertionError: True is not False

======================================================================
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-z7jrgm/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_break_on_emails_in_multiline_strings (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-z7jrgm/test.py", line 127, in test_does_not_break_on_emails_in_multiline_strings
    self.assertFalse(solution.Validations.is_email("foo@bar.com\nwat?"))
AssertionError: True is not false

======================================================================
FAIL: test_does_not_break_on_phones_in_multiline_strings (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-z7jrgm/test.py", line 163, in test_does_not_break_on_phones_in_multiline_strings
    self.assertFalse(solution.Validations.is_phone("0885123123\nwat?"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_multiline_strings_in_IP_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-z7jrgm/test.py", line 189, in test_handles_multiline_strings_in_IP_validation_properly
    self.assertFalse(solution.Validations.is_ip_address("8.8.8.8\n"))
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-z7jrgm/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-z7jrgm/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-z7jrgm/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-z7jrgm/test.py", line 167, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('some.long-subdomain.domain.co.ul'))
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-z7jrgm/test.py", line 224, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('-42 '))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 39 tests in 0.045s

FAILED (failures=19)

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

Велина обнови решението на 23.04.2014 16:05 (преди почти 11 години)

+import re
+
+
+tld_pattern = r'\b[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
+hostname_part_pattern = r'\b[0-9A-Za-z]([0-9a-z\-]{,61}[0-9A-Za-z])?\b'
+domain_pattern = hostname_part_pattern + r'\.' + tld_pattern
+hostname_pattern = hostname_part_pattern + r'\.' + tld_pattern
+email_pattern = r'(?P<username>[a-z0-9][\w_\-+\.]{,200})@' + hostname_pattern
+phone_number_pattern_local = r'(?<![\w\+])0(([\ \-\(\)]{,2}[0-9]){6,11})'
+phone_number_pattern_inter = (r'(?<![\w\+])(0{2}|\+)[\ \-\(\)]{,2}'
+ r'(?P<code>[1-9][0-9]{1,2})'
+ r'([\ \-\(\)]{,2}[0-9]){6,11}')
+ip_address_pattern = (r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}'
+ r'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
+number_pattern = r'^\-?(0|([1-9][0-9]*))(\.[\d]+)?$'
+integer_pattern = r'^\-?(0|([1-9][0-9][^\.]*))$'
+date_pattern = (r'[0-9]{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])'
+ '|(3[01]))')
+time_pattern = r'(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]'
+date_time_pattern = date_pattern + r'[T\s\b\-]' + time_pattern
+
+
+class PrivacyFilter:
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+
+ def filtered(self):
+ text = self.text
+ text = re.sub(email_pattern, '[EMAIL]', text)
+ text = re.sub(phone_number_pattern_inter, '[PHONE]', text)
+ text = re.sub(phone_number_pattern_local, '[PHONE]', text)
+ return text
+
+
+class Validations:
+ def is_email(value):
+ match = re.match(email_pattern, value)
+ if match is None:
+ return False
+ else:
+ return True
+
+ def is_phone(value):
+ match = re.match(phone_number_pattern_inter, value)
+ match_local = re.match(phone_number_pattern_local, value)
+ if match is None and match_local is None:
+ return False
+ else:
+ return True
+
+ def is_hostname(value):
+ match = re.match(hostname_pattern, value)
+ if match is None:
+ return False
+ else:
+ return True
+
+ def is_ip_address(value):
+ match = re.match(ip_address_pattern, value)
+ if match is None:
+ return False
+ else:
+ return True
+
+ def is_number(value):
+ match = re.match(number_pattern, str(value))
+ if match is None:
+ return False
+ else:
+ return True
+
+ def is_integer(value):
+ match = re.match(integer_pattern, value)
+ if match is None:
+ return False
+ else:
+ return True
+
+ def is_date(value):
+ match = re.match(date_pattern, value)
+ if match is None:
+ return False
+ else:
+ return True
+
+ def is_time(value):
+ match = re.match(time_pattern, value)
+ if match is None:
+ return False
+ else:
+ return True
+
+ def is_datetime(value):
+ match = re.match(date_time_pattern, value)
+ if match is None:
+ return False
+ else:
+ return True