Решение на Регулярни изрази от Теодора Добрева

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

Към профила на Теодора Добрева

Резултати

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

Код

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):
return self.__filter_phones(self.__filter_emails(self.__text))
def __filter_emails(self, text):
return re.sub(
r'\w[\w_+.\-]{,200}\@(\w(\w|-){,61}\w?\.)+([a-z]|[A-Z]){2,3}(\.([a-z]|[A-Z]){2,3})?',
'[EMAIL]', text)
def __filter_phones(self, text):
return re.sub(r'(00|\+)[1-9][0-9]{,2}([\s\-\(\)]{,2}[0-9]){6,11}',
'[PHONE]',
re.sub(r'0([\s\-\(\)]{,2}[0-9]){6,11}', '[PHONE]', text))
class Validations:
@staticmethod
def id_username(value):
return re.match(r'^\w[\w_+.\-]{,200}', value) is not None
@classmethod
def is_email(cls, value):
splitted = re.split(r'@', value)
username = splitted[0]
hostname = ''
if len(splitted) > 1:
hostname = splitted[1]
return cls.id_username(username) and cls.is_hostname(hostname)
@staticmethod
def is_local_phone(value):
return re.match(
r'^0([\s\-\(\)]{,2}[0-9]){6,11}$', value) is not None
@staticmethod
def is_international_phone(value):
return re.match(
r'^(00|\+)[1-9][0-9]{,2}([\s\-\(\)]{,2}[0-9]){6,11}$', value)\
is not None
@classmethod
def is_phone(cls, value):
return cls.is_local_phone(value) or cls.is_international_phone(value)
@staticmethod
def is_hostname(value):
return re.match(
r'(\w(\w|-){,61}\w?\.)+([a-z]|[A-Z]){2,3}(\.([a-z]|[A-Z]){2,3})?$',
value) is not None
@staticmethod
def is_ip_address(value):
return re.match(
r'^(([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$',
value) is not None
@staticmethod
def is_number(value):
return re.match(
r'^[-]?(0?|[1-9][0-9]*)[\.[0-9]+]?$', value) is not None
@staticmethod
def is_integer(value):
return re.match(r'^[-]?(0?|[1-9][0-9]*)$', value) is not None
@staticmethod
def is_date(value):
return re.match(
r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$', value)\
is not None
@staticmethod
def is_time(value):
return re.match(
r'^([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$', value) is not None
@classmethod
def is_datetime(cls, value):
splitted = re.split(r'[ |T]', value)
date = splitted[0]
time = ''
if len(splitted) > 1:
time = splitted[1]
return cls.is_date(date) and cls.is_time(time)

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

FFFFF.F...F....FF.....FF..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-1tbsppw/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-1tbsppw/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-1tbsppw/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-1tbsppw/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-1tbsppw/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '+1555 123, 55555' != '[PHONE], 55555'
- +1555 123, 55555
+ [PHONE], 55555


======================================================================
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-1tbsppw/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_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-1tbsppw/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: [PHONE]'
- Phone: +25 [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-1tbsppw/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-1tbsppw/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-1tbsppw/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-1tbsppw/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-1tbsppw/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-1tbsppw/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-1tbsppw/test.py", line 174, in test_validates_hostnames
    self.assertFalse(solution.Validations.is_hostname('not-a-hostname-.com'))
AssertionError: True is not false

======================================================================
FAIL: test_validates_more_complex_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-1tbsppw/test.py", line 205, in test_validates_more_complex_numbers
    self.assertFalse(solution.Validations.is_number('00'))
AssertionError: True is not false

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

FAILED (failures=15)

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

Теодора обнови решението на 23.04.2014 16:40 (преди над 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):
+ pass
+
+ def __filter_emails(self, text):
+ pass
+
+ def __filter_phones(self, text):
+ pass
+
+
+class Validations:
+
+ @staticmethod
+ def id_username(value):
+ return re.match(r'^\w[\w_+.\-]{,200}$', value) is not None
+
+ @classmethod
+ def is_email(cls, value):
+ splitted = re.split(r'@', value)
+ username = splitted[0]
+ hostname = ''
+ if len(splitted) > 1:
+ hostname = splitted[1]
+ return cls.id_username(username) and cls.is_hostname(hostname)
+
+ @staticmethod
+ def is_local_phone(value):
+ return re.match(
+ r'^0([\s\-\(\)]{,2}[0-9]){6,11}$', value) is not None
+
+ @staticmethod
+ def is_international_phone(value):
+ return re.match(
+ r'^(00|\+)[1-9][0-9]{,2}([\s\-\(\)]{,2}[0-9]){6,11}$', value)\
+ is not None
+
+ @classmethod
+ def is_phone(cls, value):
+ return cls.is_local_phone(value) or cls.is_international_phone(value)
+
+ @staticmethod
+ def is_hostname(value):
+ return re.match(
+ r'(\w(\w|-){,61}\w?\.)+([a-z]|[A-Z]){2,3}(\.([a-z]|[A-Z]){2,3})?$',
+ value) is not None
+
+ @staticmethod
+ def is_ip_address(value):
+ return re.match(
+ r'^(([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$',
+ value) is not None
+
+ @staticmethod
+ def is_number(value):
+ return re.match(
+ r'^[-]?(0?|[1-9][0-9]*)[\.[0-9]+]?$', value) is not None
+
+ @staticmethod
+ def is_integer(value):
+ return re.match(r'^[-]?(0?|[1-9][0-9]*)$', value) is not None
+
+ @staticmethod
+ def is_date(value):
+ return re.match(
+ r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$', value)\
+ is not None
+
+ @staticmethod
+ def is_time(value):
+ return re.match(
+ r'^([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$', value) is not None
+
+ @classmethod
+ def is_datetime(cls, value):
+ splitted = re.split(r'[ |T]', value)
+ date = splitted[0]
+ time = ''
+ if len(splitted) > 1:
+ time = splitted[1]
+ return cls.is_date(date) and cls.is_time(time)

Теодора обнови решението на 23.04.2014 16:55 (преди над 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):
- pass
+ return self.__filter_phones(self.__filter_emails(self.__text))
def __filter_emails(self, text):
- pass
+ return re.sub(
+ r'\w[\w_+.\-]{,200}\@(\w(\w|-){,61}\w?\.)+([a-z]|[A-Z]){2,3}(\.([a-z]|[A-Z]){2,3})?',
+ '[EMAIL]', text)
def __filter_phones(self, text):
- pass
+ return re.sub(r'(00|\+)[1-9][0-9]{,2}([\s\-\(\)]{,2}[0-9]){6,11}',
+ '[PHONE]',
+ re.sub(r'0([\s\-\(\)]{,2}[0-9]){6,11}', '[PHONE]', text))
class Validations:
@staticmethod
def id_username(value):
- return re.match(r'^\w[\w_+.\-]{,200}$', value) is not None
+ return re.match(r'^\w[\w_+.\-]{,200}', value) is not None
@classmethod
def is_email(cls, value):
splitted = re.split(r'@', value)
username = splitted[0]
hostname = ''
if len(splitted) > 1:
hostname = splitted[1]
return cls.id_username(username) and cls.is_hostname(hostname)
@staticmethod
def is_local_phone(value):
return re.match(
r'^0([\s\-\(\)]{,2}[0-9]){6,11}$', value) is not None
@staticmethod
def is_international_phone(value):
return re.match(
r'^(00|\+)[1-9][0-9]{,2}([\s\-\(\)]{,2}[0-9]){6,11}$', value)\
is not None
@classmethod
def is_phone(cls, value):
return cls.is_local_phone(value) or cls.is_international_phone(value)
@staticmethod
def is_hostname(value):
return re.match(
r'(\w(\w|-){,61}\w?\.)+([a-z]|[A-Z]){2,3}(\.([a-z]|[A-Z]){2,3})?$',
value) is not None
@staticmethod
def is_ip_address(value):
return re.match(
r'^(([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$',
value) is not None
@staticmethod
def is_number(value):
return re.match(
r'^[-]?(0?|[1-9][0-9]*)[\.[0-9]+]?$', value) is not None
@staticmethod
def is_integer(value):
return re.match(r'^[-]?(0?|[1-9][0-9]*)$', value) is not None
@staticmethod
def is_date(value):
return re.match(
r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$', value)\
is not None
@staticmethod
def is_time(value):
return re.match(
r'^([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$', value) is not None
@classmethod
def is_datetime(cls, value):
splitted = re.split(r'[ |T]', value)
date = splitted[0]
time = ''
if len(splitted) > 1:
time = splitted[1]
return cls.is_date(date) and cls.is_time(time)