Решение на Регулярни изрази от Михаил Панайотов

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

Към профила на Михаил Панайотов

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 17 успешни тест(а)
  • 22 неуспешни тест(а)

Код

import re
class PrivacyFilter:
def __init__(self, information):
self._information = information
self._EMAIL_REGEX = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
processed_info = self._information
if self.partially_preserve_email_username:
grouped = processed_info.group().split("@")
if len(grouped[0]) <= 6:
grouped[0] = "[FILTERED]"
else:
grouped[0] = grouped[0][0] + grouped[0][1] + grouped[0][2] + "[FILTERED]"
joined = '@'.join(grouped)
match = re.search(self._EMAIL_REGEX, joined)
processed_info = match
elif self.preserve_email_hostname:
grouped = processed_info.group().split("@")
grouped[0] = "[FILTERED]"
joined = '@'.join(grouped)
match = re.search(self._EMAIL_REGEX, joined)
processed_info = match
return processed_info
class Validations:
@classmethod
def is_integer(self, text):
if re.match("-?(0|[1-9][0-9]*)$", text):
return True
else:
return False
@classmethod
def is_number(self, text):
if re.match("-?(0|[1-9][0-9]*)(\.[0-9]*)?$", text):
return True
else:
return False
@classmethod
def is_date(self, text):
if re.match("[0-9]{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])$", text):
return True
else:
return False
@classmethod
def is_time(self, text):
if re.match("([01][0-9]|2[0-3]):([0-5][0-9]|60):([0-5][0-9]|60)$", text):
return True
else:
return False
@classmethod
def is_ip_address(self, text):
ip_address = text.split(".")
length = len(ip_address)
if length != 4:
return False
else:
for sub_address in ip_address:
if not re.match("(1?[0-9]?[0-9]$)|(2[0-4][0-9]$)|(25[0-5]$)", sub_address):
return False
return True
@classmethod
def is_phone(self, text):
if re.match("((^0)|([+(^00)][1-9][0-9]{1,2}))([ \-()]{0,2}[0-9]){6,11}$", text):
return True
else:
return False

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

EEEE.FEFFFF..E.EF..FE.FE..FFE..EE......
======================================================================
ERROR: 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-1e4jgdq/test.py", line 54, in test_allows_email_hostname_to_be_preserved
    self.assertEqual('[FILTERED]@example.com', self.filter_email_usernames('someone@example.com'))
  File "/tmp/d20140513-11348-1e4jgdq/test.py", line 11, in filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1e4jgdq/solution.py", line 25, in filtered
    grouped = processed_info.group().split("@")
AttributeError: 'str' object has no attribute 'group'

======================================================================
ERROR: 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-1e4jgdq/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'))
  File "/tmp/d20140513-11348-1e4jgdq/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1e4jgdq/solution.py", line 15, in filtered
    grouped = processed_info.group().split("@")
AttributeError: 'str' object has no attribute 'group'

======================================================================
ERROR: 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-1e4jgdq/test.py", line 64, in test_does_not_brake_with_unicode
    self.assertEqual('За връзка: [FILTERED]@example.com', self.partially_filter_email_usernames('За връзка: me@example.com'))
  File "/tmp/d20140513-11348-1e4jgdq/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1e4jgdq/solution.py", line 15, in filtered
    grouped = processed_info.group().split("@")
AttributeError: 'str' object has no attribute 'group'

======================================================================
ERROR: 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-1e4jgdq/test.py", line 50, in test_does_not_filter_invalid_emails
    self.assertEqual(text, self.filter_email_usernames(text))
  File "/tmp/d20140513-11348-1e4jgdq/test.py", line 11, in filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1e4jgdq/solution.py", line 25, in filtered
    grouped = processed_info.group().split("@")
AttributeError: 'str' object has no attribute 'group'

======================================================================
ERROR: 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-1e4jgdq/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'))
  File "/tmp/d20140513-11348-1e4jgdq/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1e4jgdq/solution.py", line 15, in filtered
    grouped = processed_info.group().split("@")
AttributeError: 'str' object has no attribute 'group'

======================================================================
ERROR: test_allows_validation_for_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-1e4jgdq/test.py", line 105, in test_allows_validation_for_emails
    self.assertTrue(solution.Validations.is_email('foo@bar.com'))
AttributeError: type object 'Validations' has no attribute 'is_email'

======================================================================
ERROR: 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-1e4jgdq/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), valid)
AttributeError: type object 'Validations' has no attribute 'is_email'

======================================================================
ERROR: 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-1e4jgdq/test.py", line 127, in test_does_not_break_on_emails_in_multiline_strings
    self.assertFalse(solution.Validations.is_email("foo@bar.com\nwat?"))
AttributeError: type object 'Validations' has no attribute 'is_email'

======================================================================
ERROR: 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-1e4jgdq/test.py", line 179, in test_handles_multiline_strings_in_hostname_validation_properly
    self.assertFalse(solution.Validations.is_hostname("foo.com\n"))
AttributeError: type object 'Validations' has no attribute 'is_hostname'

======================================================================
ERROR: test_returns_boolean_True_or_False (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-1e4jgdq/test.py", line 109, in test_returns_boolean_True_or_False
    self.assertIs(solution.Validations.is_email('foo@bar.com'), True)
AttributeError: type object 'Validations' has no attribute 'is_email'

======================================================================
ERROR: test_validates_datetime_values (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-1e4jgdq/test.py", line 277, in test_validates_datetime_values
    self.assertTrue(solution.Validations.is_datetime('2012-11-19 19:00:00'))
AttributeError: type object 'Validations' has no attribute 'is_datetime'

======================================================================
ERROR: 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-1e4jgdq/test.py", line 166, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('domain.tld'))
AttributeError: type object 'Validations' has no attribute 'is_hostname'

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


======================================================================
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-1e4jgdq/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact:[EMAIL]' != 'Contact:someone@example.com'
- Contact:[EMAIL]
+ Contact:someone@example.com


======================================================================
FAIL: test_obfuscates_simple_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-1e4jgdq/test.py", line 24, in test_obfuscates_simple_emails
    self.assertEqual('Contact: [EMAIL]', solution.PrivacyFilter('Contact: someone@example.com').filtered())
AssertionError: 'Contact: [EMAIL]' != 'Contact: someone@example.com'
- Contact: [EMAIL]
+ Contact: someone@example.com


======================================================================
FAIL: test_preserves_whitespace_around_phones (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-1e4jgdq/test.py", line 89, in test_preserves_whitespace_around_phones
    self.assertEqual(' [PHONE] or...', solution.PrivacyFilter(' +359881212-12-1 2 or...').filtered())
AssertionError: ' [PHONE] or...' != ' +359881212-12-1 2 or...'
-  [PHONE] or...
+  +359881212-12-1 2 or...


======================================================================
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-1e4jgdq/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
AssertionError: 'Phone: +25 [FILTERED]' != 'Phone: +25( 55 )12 12255'
- Phone: +25 [FILTERED]
+ Phone: +25( 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-1e4jgdq/test.py", line 160, in test_can_validate_more_complex_phone_numbers
    self.assertIs(solution.Validations.is_phone(phone), valid)
AssertionError: False is not True

======================================================================
FAIL: test_does_not_allow_zero_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-1e4jgdq/test.py", line 250, in test_does_not_allow_zero_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('1000-01-00'))
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-1e4jgdq/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_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-1e4jgdq/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-1e4jgdq/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

----------------------------------------------------------------------
Ran 39 tests in 0.035s

FAILED (failures=10, errors=12)

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

Михаил обнови решението на 23.04.2014 15:07 (преди над 10 години)

+import re
+
+class PrivacyFilter:
+ def __init__(self, information):
+ self._information = information
+ self._EMAIL_REGEX = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ processed_info = self._information
+
+ if self.partially_preserve_email_username:
+ grouped = processed_info.group().split("@")
+ if len(grouped[0]) <= 6:
+ grouped[0] = "[FILTERED]"
+ else:
+ grouped[0] = grouped[0][0] + grouped[0][1] + grouped[0][2] + "[FILTERED]"
+ joined = '@'.join(grouped)
+ match = re.search(self._EMAIL_REGEX, joined)
+ processed_info = match
+
+ elif self.preserve_email_hostname:
+ grouped = processed_info.group().split("@")
+ grouped[0] = "[FILTERED]"
+ joined = '@'.join(grouped)
+ match = re.search(self._EMAIL_REGEX, joined)
+ processed_info = match
+
+ return processed_info
+
+class Validations:
+ @classmethod
+ def is_integer(self, text):
+ if re.match("-?(0|[1-9][0-9]*)$", text):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_number(self, text):
+ if re.match("-?(0|[1-9][0-9]*)(\.[0-9]*)?$", text):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_date(self, text):
+ if re.match("[0-9]{4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])$", text):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_time(self, text):
+ if re.match("([01][0-9]|2[0-3]):([0-5][0-9]|60):([0-5][0-9]|60)$", text):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_ip_address(self, text):
+ ip_address = text.split(".")
+ length = len(ip_address)
+ if length != 4:
+ return False
+ else:
+ for sub_address in ip_address:
+ if not re.match("(1?[0-9]?[0-9]$)|(2[0-4][0-9]$)|(25[0-5]$)", sub_address):
+ return False
+ return True
+
+ @classmethod
+ def is_phone(self, text):
+ if re.match("((^0)|([+(^00)][1-9][0-9]{1,2}))([ \-()]{0,2}[0-9]){6,11}$", text):
+ return True
+ else:
+ return False