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

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

Към профила на Марио Даскалов

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 29 успешни тест(а)
  • 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):
email_regex = re.compile(Validations.EMAIL_REGEX)
email_filtered_text = email_regex.sub(self.__replace_email, self.text)
phone_regex = re.compile(Validations.PHONE_REGEX)
return phone_regex.sub(self.__replace_phone, email_filtered_text)
def __replace_phone(self, match):
if self.preserve_phone_country_code:
return match.group(1) + ' ' + '[FILTERED]'
else:
return '[PHONE]'
def __replace_email(self, match):
if self.partially_preserve_email_username:
username = match.group(1)
filtered_username = '' if len(username) < 6 else username[:3]
return filtered_username + '[FILTERED]' + '@' + match.group(2)
elif self.preserve_email_hostname:
return '[FILTERED]@' + match.group(2)
else:
return '[EMAIL]'
class Validations:
INTEGER_REGEX = r'-?(0|[1-9]\d*)'
NUMBER_REGEX = r'-?(0|[1-9]\d*)(.\d+)?'
DATE_REGEX = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])'
TIME_REGEX = r'([0-1]\d|2[0-3]):([0-5]\d):([0-5]\d)'
DATETIME_REGEX = DATE_REGEX + r'[- T]' + TIME_REGEX
PHONE_REGEX = r'(0(?!0)|\+[1-9]\d{0,2}|\b00)([\(\) -]{0,2}\d){6,11}'
HOSTNAME_REGEX = (r'(:?[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\.)+' +
r'(:?[a-zA-Z]{2,3}(?:[a-zA-Z]{2,3})?)')
EMAIL_REGEX = (r'([a-zA-Z0-9](?:\w|[\+\.-]){0,200})@' +
'(' +
HOSTNAME_REGEX +
')')
@classmethod
def __is_full_match(cls, regex, string):
return re.match(r'^' + regex + r'$', string) is not None
@classmethod
def is_integer(cls, string):
return cls.__is_full_match(cls.INTEGER_REGEX, string)
@classmethod
def is_number(cls, string):
return cls.__is_full_match(cls.NUMBER_REGEX, string)
@classmethod
def is_date(cls, string):
return cls.__is_full_match(cls.DATE_REGEX, string)
@classmethod
def is_time(cls, string):
return cls.__is_full_match(cls.TIME_REGEX, string)
@classmethod
def is_datetime(cls, string):
return cls.__is_full_match(cls.DATETIME_REGEX, string)
@classmethod
def is_phone(cls, string):
return cls.__is_full_match(cls.PHONE_REGEX, string)
@classmethod
def is_hostname(cls, string):
return cls.__is_full_match(cls.HOSTNAME_REGEX, string)
@classmethod
def is_email(cls, string):
return cls.__is_full_match(cls.EMAIL_REGEX, string)
@classmethod
def is_ip_address(cls, string):
valid_bits_group = '|'.join(map(str, range(256)))
ip_address_regex = '^((?:{0}).){{3}}({0})$'.format(valid_bits_group)
return cls.__is_full_match(ip_address_regex, string)

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

...FF..F..F.....F.....FF..FF....F......
======================================================================
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-17gatg8/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-17gatg8/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_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-17gatg8/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-17gatg8/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: 00 [FILTERED]'
- Phone: 0025 [FILTERED]
?          --
+ Phone: 00 [FILTERED]


======================================================================
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-17gatg8/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_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-17gatg8/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-17gatg8/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-17gatg8/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-17gatg8/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-17gatg8/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

----------------------------------------------------------------------
Ran 39 tests in 0.068s

FAILED (failures=10)

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

Марио обнови решението на 23.04.2014 14:56 (преди над 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):
+ email_regex = re.compile(Validations.EMAIL_REGEX)
+ email_filtered_text = email_regex.sub(self.__replace_email, self.text)
+ phone_regex = re.compile(Validations.PHONE_REGEX)
+ return phone_regex.sub(self.__replace_phone, email_filtered_text)
+
+ def __replace_phone(self, match):
+ if self.preserve_phone_country_code:
+ return match.group(1) + ' ' + '[FILTERED]'
+ else:
+ return '[PHONE]'
+
+ def __replace_email(self, match):
+ if self.partially_preserve_email_username:
+ username = match.group(1)
+ filtered_username = '' if len(username) < 6 else username[:3]
+ return filtered_username + '[FILTERED]' + '@' + match.group(2)
+ elif self.preserve_email_hostname:
+ return '[FILTERED]@' + match.group(2)
+ else:
+ return '[EMAIL]'
+
+
+class Validations:
+ INTEGER_REGEX = r'-?(0|[1-9]\d*)'
+ NUMBER_REGEX = r'-?(0|[1-9]\d*)(.\d+)?'
+ DATE_REGEX = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])'
+ TIME_REGEX = r'([0-1]\d|2[0-3]):([0-5]\d):([0-5]\d)'
+ DATETIME_REGEX = DATE_REGEX + r'[- T]' + TIME_REGEX
+ PHONE_REGEX = r'(0(?!0)|\+[1-9]\d{0,2}|\b00)([\(\) -]{0,2}\d){6,11}'
+ HOSTNAME_REGEX = (r'(:?[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\.)+' +
+ r'(:?[a-zA-Z]{2,3}(?:[a-zA-Z]{2,3})?)')
+ EMAIL_REGEX = (r'([a-zA-Z0-9](?:\w|[\+\.-]){0,200})@' +
+ '(' +
+ HOSTNAME_REGEX +
+ ')')
+
+ @classmethod
+ def __is_full_match(cls, regex, string):
+ return re.match(r'^' + regex + r'$', string) is not None
+
+ @classmethod
+ def is_integer(cls, string):
+ return cls.__is_full_match(cls.INTEGER_REGEX, string)
+
+ @classmethod
+ def is_number(cls, string):
+ return cls.__is_full_match(cls.NUMBER_REGEX, string)
+
+ @classmethod
+ def is_date(cls, string):
+ return cls.__is_full_match(cls.DATE_REGEX, string)
+
+ @classmethod
+ def is_time(cls, string):
+ return cls.__is_full_match(cls.TIME_REGEX, string)
+
+ @classmethod
+ def is_datetime(cls, string):
+ return cls.__is_full_match(cls.DATETIME_REGEX, string)
+
+ @classmethod
+ def is_phone(cls, string):
+ return cls.__is_full_match(cls.PHONE_REGEX, string)
+
+ @classmethod
+ def is_hostname(cls, string):
+ return cls.__is_full_match(cls.HOSTNAME_REGEX, string)
+
+ @classmethod
+ def is_email(cls, string):
+ return cls.__is_full_match(cls.EMAIL_REGEX, string)
+
+ @classmethod
+ def is_ip_address(cls, string):
+ valid_bits_group = '|'.join(map(str, range(256)))
+ ip_address_regex = '^((?:{0}).){{3}}({0})$'.format(valid_bits_group)
+ return cls.__is_full_match(ip_address_regex, string)