Решение на Регулярни изрази от Георги Харизанов

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

Към профила на Георги Харизанов

Резултати

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

Код

import re
class Validations:
email_filter = r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$"
local_phone_filter_plus =r"\+[1-9]{1}[0-9]{1,2}([0-9][-\(\) ]{0,2}){6,11}$"
local_phone_filter =r"0[1-9]{1}[0-9]{1,2}([0-9][-\(\) ]{0,2}){6,11}$"
international_phone_filter =r"00[0-9]{1,3}([0-9][-\(\) ]{0,2}){6,11}$"
hostname_filter = r"^[A-Za-z0-9]+(\.[A-Za-z])+"
ip_address_filter = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
number_filter = r"^-{0,1}[0-9]+\.{0,1}[0-9]*$"
integer_filter = r"^-{0,1}[0-9]+$"
date_filter = r"^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
time_filter = r"^[0-9]{2}:[0-9]{2}:[0-9]{2}$"
date_time_filter = r"^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$"
def is_email(value):
return re.match(Validations.email_filter, value) is not None
def is_phone(value):
return (re.search(Validations.local_phone_filter, value) is not None
or re.search(Validations.local_phone_filter_plus, value) is not None
or re.search(Validations.international_phone_filter, value) is not None)
def is_hostname(value):
return re.match(Validations.hostname_filter, value) is not None
def is_ip_address(value):
return re.match(Validations.ip_address_filter, value) is not None
def is_number(value):
return re.match(Validations.number_filter, value) is not None
def is_integer(value):
return re.match(Validations.integer_filter, value) is not None
def is_date(value):
return re.match(Validations.date_filter, value) is not None
def is_time(value):
return re.match(Validations.time_filter, value) is not None
def is_date_time(value):
return re.match(Validations.date_time_filter, value) is not None
class PrivacyFilter:
email_filter_preserving_hostname = "^[A-Za-z0-9\.\+_-]+@"
def __init__(self, text_to_filter):
self.text_to_filter = text_to_filter;
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
if(Validations.is_email(self.text_to_filter)):
if self.preserve_email_hostname:
return re.sub(self.email_filter_preserving_hostname,
"[FILTERED]@", self.text_to_filter)
else:
return re.sub(Validations.email_filter,
"[EMAIL]", self.text_to_filter)
if(Validations.is_phone(self.text_to_filter)):
if self.preserve_phone_country_code:
return re.sub("[0-9]{8}$", " [FILTERED]", self.text_to_filter)
else:
return re.sub("\+[0-9]{11}$", "[PHONE]", self.text_to_filter)

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

.FFFFFFFFFFF...FFFFF..FF..FF.F.EF.FF...
======================================================================
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-7ntzul/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'

======================================================================
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-7ntzul/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-7ntzul/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' != None

======================================================================
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-7ntzul/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' != None

======================================================================
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-7ntzul/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Reach me at: 0885123' != None

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

======================================================================
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-7ntzul/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-7ntzul/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact: [EMAIL],[EMAIL]' != None

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

======================================================================
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-7ntzul/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...' != None

======================================================================
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-7ntzul/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_works_with_blank_or_whitespace_strings_and_preserves_whitespace (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-7ntzul/test.py", line 19, in test_works_with_blank_or_whitespace_strings_and_preserves_whitespace
    self.assertEqual('', solution.PrivacyFilter('').filtered())
AssertionError: '' != None

======================================================================
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-7ntzul/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-7ntzul/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_invalid_hours_minutes_or_seconds (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-7ntzul/test.py", line 270, in test_does_not_allow_invalid_hours_minutes_or_seconds
    self.assertFalse(solution.Validations.is_time('24:00:00'))
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-7ntzul/test.py", line 254, in test_does_not_allow_invalid_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('2012-13-01'))
AssertionError: True is not false

======================================================================
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-7ntzul/test.py", line 249, in test_does_not_allow_zero_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('1000-00-01'))
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-7ntzul/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-7ntzul/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-7ntzul/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-7ntzul/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_IP_addresses (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-7ntzul/test.py", line 184, in test_validates_IP_addresses
    self.assertFalse(solution.Validations.is_ip_address('300.2.3.4'))
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-7ntzul/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-7ntzul/test.py", line 225, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('00'))
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-7ntzul/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.050s

FAILED (failures=24, errors=1)

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

Георги обнови решението на 23.04.2014 16:50 (преди над 10 години)

+import re
+
+class Validations:
+ email_filter = r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$"
+ local_phone_filter_plus =r"\+[1-9]{1}[0-9]{1,2}([0-9][-\(\) ]{0,2}){6,11}$"
+ local_phone_filter =r"0[1-9]{1}[0-9]{1,2}([0-9][-\(\) ]{0,2}){6,11}$"
+ international_phone_filter =r"00[0-9]{1,3}([0-9][-\(\) ]{0,2}){6,11}$"
+ hostname_filter = r"^[A-Za-z0-9]+(\.[A-Za-z])+"
+ ip_address_filter = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
+ number_filter = r"^-{0,1}[0-9]+\.{0,1}[0-9]*$"
+ integer_filter = r"^-{0,1}[0-9]+$"
+ date_filter = r"^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
+ time_filter = r"^[0-9]{2}:[0-9]{2}:[0-9]{2}$"
+ date_time_filter = r"^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$"
+
+ def is_email(value):
+ return re.match(Validations.email_filter, value) is not None
+
+ def is_phone(value):
+ return (re.search(Validations.local_phone_filter, value) is not None
+ or re.search(Validations.local_phone_filter_plus, value) is not None
+ or re.search(Validations.international_phone_filter, value) is not None)
+
+ def is_hostname(value):
+ return re.match(Validations.hostname_filter, value) is not None
+
+ def is_ip_address(value):
+ return re.match(Validations.ip_address_filter, value) is not None
+
+ def is_number(value):
+ return re.match(Validations.number_filter, value) is not None
+
+ def is_integer(value):
+ return re.match(Validations.integer_filter, value) is not None
+
+ def is_date(value):
+ return re.match(Validations.date_filter, value) is not None
+
+ def is_time(value):
+ return re.match(Validations.time_filter, value) is not None
+
+ def is_date_time(value):
+ return re.match(Validations.date_time_filter, value) is not None
+
+
+class PrivacyFilter:
+ email_filter_preserving_hostname = "^[A-Za-z0-9\.\+_-]+@"
+
+
+ def __init__(self, text_to_filter):
+ self.text_to_filter = text_to_filter;
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ if(Validations.is_email(self.text_to_filter)):
+ if self.preserve_email_hostname:
+ return re.sub(self.email_filter_preserving_hostname,
+ "[FILTERED]@", self.text_to_filter)
+ else:
+ return re.sub(Validations.email_filter,
+ "[EMAIL]", self.text_to_filter)
+
+ if(Validations.is_phone(self.text_to_filter)):
+ if self.preserve_phone_country_code:
+ return re.sub("[0-9]{8}$", " [FILTERED]", self.text_to_filter)
+ else:
+ return re.sub("\+[0-9]{11}$", "[PHONE]", self.text_to_filter)