Решение на Регулярни изрази от Александър Стоичков

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

Към профила на Александър Стоичков

Резултати

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

Код

import re
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):
replace_phone = "[PHONE]"
replace_email = "[EMAIL]"
pattern_phone = r'\+?\d+'
pattern_email = r'[\w*]+@\S*'
if self.preserve_phone_country_code is True:
pattern_phone = r'(?<=\+359)\d*'
if len(re.findall(pattern_phone, self.text)) == 0:
pattern_phone = r'\+?\d+'
else:
replace_phone = " [FILTERED]"
if self.preserve_email_hostname is True:
pattern_email = r'[\w*]+(?=@)'
replace_email = "[FILTERED]"
if self.partially_preserve_email_username is True:
pattern_email = r'[\w*]+(?=@)'
if len(re.match(pattern_email, self.text).group()) >= 6:
pattern_email = r'(?<=\w{3})[\w*]+(?=@)'
replace_email = "[FILTERED]"
else:
pattern_email = r'[\w*]+(?=@)'
replace_email = "[FILTERED]"
self.text = re.sub(pattern_email, replace_email, self.text)
self.text = re.sub(pattern_phone, replace_phone, self.text)
return self.text
class Validations:
def is_email(value):
is_email = False
pattern = r'[\S*]{1,200}\@([\w-]*)\.([\w-]*)'
if re.match(pattern, value, flags=0) is not None:
is_email = True
return is_email
def is_phone(value):
is_phone = False
local_pattern = r'0[1-9](\d\-?\s?){5,11}'
international_pattern = r'\+359\s?\-?(\d\s?\-?){6,11}'
international_zero_pattern = r'00\s?\-?(\d\s?\-?){6,11}'
if re.match(local_pattern, value, flags=0) is not None or \
re.match(international_pattern, value, flags=0) is not None or \
re.match(international_zero_pattern, value, flags=0) is not None:
is_phone = True
return is_phone
def is_hostname(value):
is_hostname = False
pattern = r'([\w-]*)\.([\w-]*)'
if re.match(pattern, value, flags=0) is not None:
is_hostname = True
return is_hostname
def is_ip_address(value):
is_ip_address = False
pattern = r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
if re.match(pattern, value, flags=0) is not None:
is_ip_address = True
return is_ip_address
def is_number(value):
is_number = False
pattern_zero = r'\-?0[0-9]*\.?[0-9]*'
pattern_no_zero = r'\-?[1-9]+\.?[0-9]*'
if re.match(pattern_zero, value, flags=0) is not None or \
re.match(pattern_no_zero, value, flags=0) is not None:
is_number = True
return is_number
def is_integer(value):
is_integer = False
pattern = r'\-?[1-9][0-9]*'
if re.match(pattern, value, flags=0):
is_integer = True
return is_integer
def is_date(value):
is_date = False
pattern = r'[0-9]{4}-(1[0-2]|0[0-9])-(0[0-9]|[1-3][0-9])'
if re.match(pattern, value, flags=0):
is_date = True
return is_date
def is_time(value):
is_time = False
pattern = r'(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):\2'
if re.match(pattern, value, flags=0):
is_time = True
return is_time
def is_datetime(value):
is_datetime = False
pattern = r'[0-9]{4}-(1[0-2]|0[0-9])-(0[0-9]|[1-3][0-9])\s(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])'
if re.match(pattern, value, flags=0):
is_datetime = True
return is_datetime

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

F.EFFF.F.FF....FF.FFFFFFFFFF...FF.FF...
======================================================================
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-jtlx23/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-jtlx23/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-jtlx23/solution.py", line 24, in filtered
    if len(re.match(pattern_email, self.text).group()) >= 6:
AttributeError: 'NoneType' object has no attribute 'group'

======================================================================
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-jtlx23/test.py", line 55, in test_allows_email_hostname_to_be_preserved
    self.assertEqual('[FILTERED]@exa.mple.com', self.filter_email_usernames('some12-+3@exa.mple.com'))
AssertionError: '[FILTERED]@exa.mple.com' != 'some[PHONE]-+[FILTERED]@exa.mple.com'
- [FILTERED]@exa.mple.com
+ some[PHONE]-+[FILTERED]@exa.mple.com
? +++++++++++++


======================================================================
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-jtlx23/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-jtlx23/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '+1555 123, 55555' != '[PHONE] [PHONE], [PHONE]'
- +1555 123, 55555
+ [PHONE] [PHONE], [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-jtlx23/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '[PHONE] ([PHONE]) [PHONE]-[PHONE]-[PHONE]'
- [PHONE]
+ [PHONE] ([PHONE]) [PHONE]-[PHONE]-[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-jtlx23/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact: [EMAIL],[EMAIL]' != 'Contact: [EMAIL]'
- Contact: [EMAIL],[EMAIL]
?          --------
+ Contact: [EMAIL]


======================================================================
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-jtlx23/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...' != ' [PHONE]-[PHONE]-[PHONE] [PHONE] or...'
-  [PHONE] or...
+  [PHONE]-[PHONE]-[PHONE] [PHONE] 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-jtlx23/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] )[PHONE] [PHONE]'
- Phone: +25 [FILTERED]
+ Phone: [PHONE]( [PHONE] )[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-jtlx23/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-jtlx23/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_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-jtlx23/test.py", line 255, in test_does_not_allow_invalid_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('2012-06-32'))
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-jtlx23/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_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-jtlx23/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-jtlx23/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-jtlx23/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-jtlx23/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_multiline_strings_in_integer_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-jtlx23/test.py", line 233, in test_handles_multiline_strings_in_integer_validation_properly
    self.assertFalse(solution.Validations.is_number("42\n24"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_multiline_strings_in_numbers_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-jtlx23/test.py", line 215, in test_handles_multiline_strings_in_numbers_validation_properly
    self.assertFalse(solution.Validations.is_number("42\n24"))
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-jtlx23/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-jtlx23/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_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-jtlx23/test.py", line 278, in test_validates_datetime_values
    self.assertTrue(solution.Validations.is_datetime('2012-11-19T19:00:00'))
AssertionError: False is not true

======================================================================
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-jtlx23/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_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-jtlx23/test.py", line 224, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('-42 '))
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-jtlx23/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.044s

FAILED (failures=23, errors=1)

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

Александър обнови решението на 23.04.2014 01:35 (преди около 10 години)

+import re
+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):
+ replace_phone = "[PHONE]"
+ replace_email = "[EMAIL]"
+ pattern_phone = r'\+?\d+'
+ pattern_email = r'[\w*]+@\S*'
+ if self.preserve_phone_country_code is True:
+ pattern_phone = r'(?<=\+359)\d*'
+ if len(re.findall(pattern_phone, self.text)) == 0:
+ pattern_phone = r'\+?\d+'
+ else:
+ replace_phone = " [FILTERED]"
+ if self.preserve_email_hostname is True:
+ pattern_email = r'[\w*]+(?=@)'
+ replace_email = "[FILTERED]"
+ if self.partially_preserve_email_username is True:
+ pattern_email = r'[\w*]+(?=@)'
+ if len(re.match(pattern_email, self.text).group()) >= 6:
+ pattern_email = r'(?<=\w{3})[\w*]+(?=@)'
+ replace_email = "[FILTERED]"
+ else:
+ pattern_email = r'[\w*]+(?=@)'
+ replace_email = "[FILTERED]"
+ self.text = re.sub(pattern_email, replace_email, self.text)
+ self.text = re.sub(pattern_phone, replace_phone, self.text)
+ return self.text
+class Validations:
+ def is_email(value):
+ is_email = False
+ pattern = r'[\S*]{1,200}\@([\w-]*)\.([\w-]*)'
+ if re.match(pattern, value, flags=0) is not None:
+ is_email = True
+ return is_email
+ def is_phone(value):
+ is_phone = False
+ local_pattern = r'0[1-9](\d\-?\s?){5,11}'
+ international_pattern = r'\+359\s?\-?(\d\s?\-?){6,11}'
+ international_zero_pattern = r'00\s?\-?(\d\s?\-?){6,11}'
+ if re.match(local_pattern, value, flags=0) is not None or \
+ re.match(international_pattern, value, flags=0) is not None or \
+ re.match(international_zero_pattern, value, flags=0) is not None:
+ is_phone = True
+ return is_phone
+ def is_hostname(value):
+ is_hostname = False
+ pattern = r'([\w-]*)\.([\w-]*)'
+ if re.match(pattern, value, flags=0) is not None:
+ is_hostname = True
+ return is_hostname
+ def is_ip_address(value):
+ is_ip_address = False
+ pattern = r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
+ if re.match(pattern, value, flags=0) is not None:
+ is_ip_address = True
+ return is_ip_address
+ def is_number(value):
+ is_number = False
+ pattern_zero = r'\-?0[0-9]*\.?[0-9]*'
+ pattern_no_zero = r'\-?[1-9]+\.?[0-9]*'
+ if re.match(pattern_zero, value, flags=0) is not None or \
+ re.match(pattern_no_zero, value, flags=0) is not None:
+ is_number = True
+ return is_number
+ def is_integer(value):
+ is_integer = False
+ pattern = r'\-?[1-9][0-9]*'
+ if re.match(pattern, value, flags=0):
+ is_integer = True
+ return is_integer
+ def is_date(value):
+ is_date = False
+ pattern = r'[0-9]{4}-(1[0-2]|0[0-9])-(0[0-9]|[1-3][0-9])'
+ if re.match(pattern, value, flags=0):
+ is_date = True
+ return is_date
+ def is_time(value):
+ is_time = False
+ pattern = r'(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):\2'
+ if re.match(pattern, value, flags=0):
+ is_time = True
+ return is_time
+ def is_datetime(value):
+ is_datetime = False
+ pattern = r'[0-9]{4}-(1[0-2]|0[0-9])-(0[0-9]|[1-3][0-9])\s(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])'
+ if re.match(pattern, value, flags=0):
+ is_datetime = True
+ return is_datetime