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

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

Към профила на Петър Парушев

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 19 успешни тест(а)
  • 20 неуспешни тест(а)

Код

import re
email_replacement = '[EMAIL]'
phone_replacement = '[PHONE]'
filtered_replacement = '[FILTERED]'
#И аз съм тъжен за дължината на следващия ред
hostname_pattern = r'([a-zA-Z0-9]([a-zA-Z0-9-](?!-\.)){,62}\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3)?'
email_split_pattern = r'(.*)@(.*)'
email_pattern = r'^[a-z][a-zA-Z0-9_\+\.-]{,200}'
phone_pattern = r'(0(?!0)|(\s|\+)[1-9]\d{0,2})(( |-|\(|\)){0,2}\d){6,11}'
global_phone_pattern = r'((\s|\+)[1-9]\d{0,2})(( |-|\(|\)){0,2}\d){6,11}'
ip_pattern = r'(\d{1,3}\.){3}\d{1,3}$'
ip_bytes_pattern = r'(\d{1,3})(?:$|\.)'
number_pattern = r'^-?((0(?!0))|[1-9])\d*(\.\d+)?$'
integer_pattern = r'^-?((0(?!0))|[1-9])\d*$'
date_pattern = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|3[0-1])'
time_pattern = r'([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
datetime_split_pattern = r'(.*)( |T)(.*)'
class PrivacyFilter:
def __init__(self, text, preserve_phone_country_code=False,
preserve_email_hostname=False,
partially_preserve_email_username=False):
self.result_text = text
self.preserve_phone_country_code = preserve_phone_country_code
self.preserve_email_hostname = preserve_email_hostname
self.partially_preserve_email_username = (
partially_preserve_email_username)
def filtered(self):
if(self.partially_preserve_email_username
and self.preserve_email_hostname):
email_split = re.findall(email_split_pattern, self.result_text)
for split in email_split:
self.result_text = re.sub(email_pattern,
email_replacement, split[0])
elif(self.partially_preserve_email_username
and not self.preserve_email_hostname):
for split in email_split:
self.result_text = re.sub(email_pattern,
email_replacement, split[0])
self.result_text = re.sub(hostname_pattern,
filtered_replacement, split[2])
elif(not self.partially_preserve_email_username
and self.preserve_email_hostname):
for split in email_split:
self.result_text = re.sub(email_pattern,
filtered_replacement, split[0])
else:
self.result_text = re.sub(email_pattern + '@' + hostname_pattern,
email_replacement, self.result_text)
if(self.preserve_phone_country_code):
self.result_text = re.sub(global_phone_pattern,
filtered_replacement, self.result_text)
else:
self.result_text = re.sub(phone_pattern,
phone_replacement, self.result_text)
return self.result_text
class Validations():
def is_hostname(value):
return re.match(hostname_pattern, value) != None
def is_email(value):
email_parts = re.findall(email_split_pattern, value)
return (re.match(email_pattern, email_parts[0][0]) and
Validations.is_hostname(email_parts[0][1]))
def is_phone(value):
return re.match(phone_pattern, value) != None
def is_ip_address(value):
if re.match(ip_pattern, value) == None:
return False
bytes_list = re.findall(ip_bytes_pattern, value)
if bytes_list == []:
return False
return all(map(lambda byte: 0 <= int(byte) <= 255, bytes_list))
def is_number(value):
return re.match(number_pattern, value) != None
def is_integer(value):
return re.match(integer_pattern, value) != None
def is_date(value):
return re.match(date_pattern, value) != None
def is_time(value):
return re.match(time_pattern, value) != None
def is_datetime(value):
datetime_parts = re.findall(datetime_split_pattern, value)
if len(datetime_parts[0]) != 3:
return False
return (Validations.is_date(datetime_parts[0][0]) and
Validations.is_time(datetime_parts[0][2]))

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

EEEEFFEFF.F....FF...FFFF..FF...EF......
======================================================================
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-mago8r/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-mago8r/test.py", line 11, in filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-mago8r/solution.py", line 49, in filtered
    for split in email_split:
UnboundLocalError: local variable 'email_split' referenced before assignment

======================================================================
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-mago8r/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-mago8r/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-mago8r/solution.py", line 41, in filtered
    for split in email_split:
UnboundLocalError: local variable 'email_split' referenced before assignment

======================================================================
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-mago8r/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-mago8r/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-mago8r/solution.py", line 41, in filtered
    for split in email_split:
UnboundLocalError: local variable 'email_split' referenced before assignment

======================================================================
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-mago8r/test.py", line 50, in test_does_not_filter_invalid_emails
    self.assertEqual(text, self.filter_email_usernames(text))
  File "/tmp/d20140513-11348-mago8r/test.py", line 11, in filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-mago8r/solution.py", line 49, in filtered
    for split in email_split:
UnboundLocalError: local variable 'email_split' referenced before assignment

======================================================================
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-mago8r/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-mago8r/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-mago8r/solution.py", line 41, in filtered
    for split in email_split:
UnboundLocalError: local variable 'email_split' referenced before assignment

======================================================================
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-mago8r/test.py", line 279, in test_validates_datetime_values
    self.assertFalse(solution.Validations.is_datetime('foo'))
  File "/tmp/d20140513-11348-mago8r/solution.py", line 100, in is_datetime
    if len(datetime_parts[0]) != 3:
IndexError: list index out of range

======================================================================
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-mago8r/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_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-mago8r/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '0[PHONE]'
- [PHONE]
+ 0[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-mago8r/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact: [EMAIL],[EMAIL]' != 'Contact: 1someone@example.com,someone.new@sub.example123.co.uk'
- Contact: [EMAIL],[EMAIL]
+ Contact: 1someone@example.com,someone.new@sub.example123.co.uk


======================================================================
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-mago8r/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_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-mago8r/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: [FILTERED]'
- Phone: +25 [FILTERED]
?       ----
+ Phone: [FILTERED]


======================================================================
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-mago8r/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-mago8r/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_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-mago8r/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-mago8r/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-mago8r/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-mago8r/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-mago8r/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-mago8r/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-mago8r/test.py", line 176, in test_validates_hostnames
    self.assertFalse(solution.Validations.is_hostname('not-a-hostname.com.12'))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 39 tests in 0.052s

FAILED (failures=14, errors=6)

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

Петър обнови решението на 23.04.2014 16:46 (преди над 10 години)

+import re
+
+email_replacement = '[EMAIL]'
+phone_replacement = '[PHONE]'
+filtered_replacement = '[FILTERED]'
+#И аз съм тъжен за дължината на следващия ред
+hostname_pattern = r'([a-zA-Z0-9]([a-zA-Z0-9-](?!-\.)){,62}\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3)?'
+email_split_pattern = r'(.*)@(.*)'
+email_pattern = r'^[a-z][a-zA-Z0-9_\+\.-]{,200}'
+phone_pattern = r'(0(?!0)|(\s|\+)[1-9]\d{0,2})(( |-|\(|\)){0,2}\d){6,11}'
+global_phone_pattern = r'((\s|\+)[1-9]\d{0,2})(( |-|\(|\)){0,2}\d){6,11}'
+ip_pattern = r'(\d{1,3}\.){3}\d{1,3}$'
+ip_bytes_pattern = r'(\d{1,3})(?:$|\.)'
+number_pattern = r'^-?((0(?!0))|[1-9])\d*(\.\d+)?$'
+integer_pattern = r'^-?((0(?!0))|[1-9])\d*$'
+date_pattern = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|3[0-1])'
+time_pattern = r'([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+datetime_split_pattern = r'(.*)( |T)(.*)'
+
+
+class PrivacyFilter:
+ def __init__(self, text, preserve_phone_country_code=False,
+ preserve_email_hostname=False,
+ partially_preserve_email_username=False):
+ self.result_text = text
+ self.preserve_phone_country_code = preserve_phone_country_code
+ self.preserve_email_hostname = preserve_email_hostname
+ self.partially_preserve_email_username = (
+ partially_preserve_email_username)
+
+ def filtered(self):
+ if(self.partially_preserve_email_username
+ and self.preserve_email_hostname):
+ email_split = re.findall(email_split_pattern, self.result_text)
+ for split in email_split:
+ self.result_text = re.sub(email_pattern,
+ email_replacement, split[0])
+
+ elif(self.partially_preserve_email_username
+ and not self.preserve_email_hostname):
+ for split in email_split:
+ self.result_text = re.sub(email_pattern,
+ email_replacement, split[0])
+ self.result_text = re.sub(hostname_pattern,
+ filtered_replacement, split[2])
+
+ elif(not self.partially_preserve_email_username
+ and self.preserve_email_hostname):
+ for split in email_split:
+ self.result_text = re.sub(email_pattern,
+ filtered_replacement, split[0])
+
+ else:
+ self.result_text = re.sub(email_pattern + '@' + hostname_pattern,
+ email_replacement, self.result_text)
+
+ if(self.preserve_phone_country_code):
+ self.result_text = re.sub(global_phone_pattern,
+ filtered_replacement, self.result_text)
+ else:
+ self.result_text = re.sub(phone_pattern,
+ phone_replacement, self.result_text)
+ return self.result_text
+
+
+class Validations():
+ def is_hostname(value):
+ return re.match(hostname_pattern, value) != None
+
+ def is_email(value):
+ email_parts = re.findall(email_split_pattern, value)
+ return (re.match(email_pattern, email_parts[0][0]) and
+ Validations.is_hostname(email_parts[0][1]))
+
+ def is_phone(value):
+ return re.match(phone_pattern, value) != None
+
+ def is_ip_address(value):
+ if re.match(ip_pattern, value) == None:
+ return False
+ bytes_list = re.findall(ip_bytes_pattern, value)
+ if bytes_list == []:
+ return False
+ return all(map(lambda byte: 0 <= int(byte) <= 255, bytes_list))
+
+ def is_number(value):
+ return re.match(number_pattern, value) != None
+
+ def is_integer(value):
+ return re.match(integer_pattern, value) != None
+
+ def is_date(value):
+ return re.match(date_pattern, value) != None
+
+ def is_time(value):
+ return re.match(time_pattern, value) != None
+
+ def is_datetime(value):
+ datetime_parts = re.findall(datetime_split_pattern, value)
+ if len(datetime_parts[0]) != 3:
+ return False
+ return (Validations.is_date(datetime_parts[0][0]) and
+ Validations.is_time(datetime_parts[0][2]))