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

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

Към профила на Таня Христова

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 28 успешни тест(а)
  • 11 неуспешни тест(а)

Код

import re
class PrivacyFilter:
def __init__(self, text):
self.unfiltered_text = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
self.phone_pattern = r"(0|(00|\+)[1-9]\d{,2})((\b[)( -]){,2}\d){6,11}\b"
self.phone_country_code_pattern = r"(^00|\+)[1-9]\d{,2}"
self.email_pattern = r"\b[a-zA-Z0-9]((\w|[+.-])){0,200}@[1-9a-zA-Z](([0-9a-zA-Z]|-){,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b"
def filtered(self):
filtered_text = self.unfiltered_text
email = re.search(self.email_pattern, filtered_text)
while email:
start, end = email.span()
filtered = self.filter_email(filtered_text[start:end])
filtered_text = filtered_text[:start] + filtered + filtered_text[end:]
email = re.search(self.email_pattern, filtered_text)
phone = re.search(self.phone_pattern, filtered_text)
while phone:
start, end = phone.span()
filtered = self.filter_phone(filtered_text[start:end])
filtered_text = filtered_text[:start] + filtered + filtered_text[end:]
phone = re.search(self.phone_pattern, filtered_text)
return filtered_text
def filter_phone(self, phone):
if self.preserve_phone_country_code:
country_code = re.search(self.phone_country_code_pattern, phone)
if country_code is None:
return "[PHONE]"
length_country_code = country_code.span()[1]
filtered_phone = phone[:length_country_code] + " " + "[FILTERED]"
else:
filtered_phone = "[PHONE]"
return filtered_phone
def filter_email(self, email):
if not self.preserve_email_hostname and not self.partially_preserve_email_username:
filtered_email = "[EMAIL]"
elif self.preserve_email_hostname and not self.partially_preserve_email_username:
hostname_start = email.find('@')
filtered_email = "[FILTERED]" + email[hostname_start:]
elif self.partially_preserve_email_username:
hostname_start = email.find('@')
if len(email[:hostname_start]) < 6:
return "[FILTERED]" + email[hostname_start:]
filtered_email = email[:3] + "[FILTERED]" + email[hostname_start:]
return filtered_email
class Validations:
def is_email(email):
if not '@' in email:
return False
hostname = re.split('@', email)[1]
if not Validations.is_hostname(hostname):
return False
username = r"^[a-zA-Z0-9]((\w|[+.-])){0,200}@"
return bool(re.match(username, email))
def is_phone(phone):
phone_pattern = r"^(0|(00|\+)[1-9]\d{,2})((\b[)( -]){,2}\d){6,11}$"
return bool(re.match(phone_pattern, phone))
def is_hostname(hostname):
hostname_pattern = r"^[1-9a-zA-Z](([0-9a-zA-Z]|-){,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$"
return bool(re.match(hostname_pattern, hostname))
def is_ip_address(ip_address):
ip_pattern = r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9‌​]{2}|2[0-4][0-9]|25[0-5])$"
return bool(re.match(ip_pattern, ip_address))
def is_number(number):
number_pattern = r"^-?(0|[1-9]\d*)(\.\d+)?$"
return bool(re.match(number_pattern, number))
def is_integer(integer):
integer_pattern = r"^-?(0|[1-9]\d*)$"
return bool(re.match(integer_pattern, integer))
def is_date(date):
date_pattern = r"^([0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))$"
return bool(re.match(date_pattern, date))
def is_time(time):
time_pattern = r"^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"
return bool(re.match(time_pattern, time))
def is_datetime(datetime):
parsed_datetime = re.split(r"[ T]", datetime)
if len(parsed_datetime) != 2:
return False
date, time = parsed_datetime[0], parsed_datetime[1]
return Validations.is_date(date) and Validations.is_time(time)

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

F...FF.F..F.....F.....FF..FF....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-24km0n/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' != 'some12-+3@exa.mple.com'
- [FILTERED]@exa.mple.com
+ some12-+3@exa.mple.com


======================================================================
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-24km0n/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-24km0n/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '+1 (555) 123-456-99'
- [PHONE]
+ +1 (555) 123-456-99


======================================================================
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-24km0n/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact: [EMAIL],[EMAIL]' != 'Contact: [EMAIL],someone.new@sub.example123.co.uk'
- Contact: [EMAIL],[EMAIL]
+ Contact: [EMAIL],someone.new@sub.example123.co.uk


======================================================================
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-24km0n/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: +25( 55 )12 12255'
- Phone: +25 [FILTERED]
+ Phone: +25( 55 )12 12255


======================================================================
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-24km0n/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-24km0n/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-24km0n/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-24km0n/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-24km0n/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-24km0n/test.py", line 167, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('some.long-subdomain.domain.co.ul'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 39 tests in 0.050s

FAILED (failures=11)

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

Таня обнови решението на 23.04.2014 15:07 (преди над 10 години)

+import re
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.unfiltered_text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ self.phone_pattern = r"(0|(00|\+)[1-9]\d{,2})((\b[)( -]){,2}\d){6,11}\b"
+ self.phone_country_code_pattern = r"^(0|(00|\+)[1-9]\d{,2})"
+ self.email_pattern = r"\b[a-zA-Z0-9]((\w|[+.-])){0,200}@[1-9a-zA-Z](([0-9a-zA-Z]|-){,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b"
+
+ def filtered(self):
+ filtered_text = self.unfiltered_text
+
+ email = re.search(self.email_pattern, filtered_text)
+ while email:
+ start, end = email.span()
+ filtered = self.filter_email(filtered_text[start:end])
+ filtered_text = filtered_text[:start] + filtered + filtered_text[end:]
+ email = re.search(self.email_pattern, filtered_text)
+
+ phone = re.search(self.phone_pattern, filtered_text)
+ while phone:
+ start, end = phone.span()
+ filtered = self.filter_phone(filtered_text[start:end])
+ filtered_text = filtered_text[:start] + filtered + filtered_text[end:]
+ phone = re.search(self.phone_pattern, filtered_text)
+
+ return filtered_text
+
+ def filter_phone(self, phone):
+ if self.preserve_phone_country_code:
+ country_code = re.search(self.phone_country_code_pattern, phone)
+ length_country_code = country_code.span()[1]
+ filtered_phone = phone[:length_country_code] + " " + "[FILTERED]"
+ else:
+ filtered_phone = "[PHONE]"
+ return filtered_phone
+
+ def filter_email(self, email):
+ if not self.preserve_email_hostname and not self.partially_preserve_email_username:
+ filtered_email = "[EMAIL]"
+ elif self.preserve_email_hostname and not self.partially_preserve_email_username:
+ hostname_start = email.find('@')
+ filtered_email = "[FILTERED]" + email[hostname_start:]
+ elif self.partially_preserve_email_username:
+ hostname_start = email.find('@')
+ filtered_email = email[:3] + "[FILTERED]" + email[hostname_start:]
+ return filtered_email
+
+
+class Validations:
+
+ def is_email(email):
+ if not '@' in email:
+ return False
+
+ hostname = re.split('@', email)[1]
+ if not Validations.is_hostname(hostname):
+ return False
+
+ username = r"^[a-zA-Z0-9]((\w|[+.-])){0,200}@"
+ return bool(re.match(username, email))
+
+ def is_phone(phone):
+ phone_pattern = r"^(0|(00|\+)[1-9]\d{,2})((\b[)( -]){,2}\d){6,11}$"
+ return bool(re.match(phone_pattern, phone))
+
+ def is_hostname(hostname):
+ hostname_pattern = r"^[1-9a-zA-Z](([0-9a-zA-Z]|-){,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$"
+ return bool(re.match(hostname_pattern, hostname))
+
+ def is_ip_address(ip_address):
+ ip_pattern = r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9‌​]{2}|2[0-4][0-9]|25[0-5])$"
+ return bool(re.match(ip_pattern, ip_address))
+
+ def is_number(number):
+ number_pattern = r"^-?(0|[1-9]\d*)(\.\d+)?$"
+ return bool(re.match(number_pattern, number))
+
+ def is_integer(integer):
+ integer_pattern = r"^-?(0|[1-9]\d*)$"
+ return bool(re.match(integer_pattern, integer))
+
+ def is_date(date):
+ date_pattern = r"^([0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))$"
+ return bool(re.match(date_pattern, date))
+
+ def is_time(time):
+ time_pattern = r"^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"
+ return bool(re.match(time_pattern, time))
+
+ def is_datetime(datetime):
+ parsed_datetime = re.split(r"[ T]", datetime)
+ if len(parsed_datetime) != 2:
+ return False
+ date = parsed_datetime[0]
+ time = parsed_datetime[1]
+ return Validations.is_date(date) and Validations.is_time(time)

Таня обнови решението на 23.04.2014 15:17 (преди над 10 години)

import re
class PrivacyFilter:
def __init__(self, text):
self.unfiltered_text = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
self.phone_pattern = r"(0|(00|\+)[1-9]\d{,2})((\b[)( -]){,2}\d){6,11}\b"
- self.phone_country_code_pattern = r"^(0|(00|\+)[1-9]\d{,2})"
+ self.phone_country_code_pattern = r"(^00|\+)[1-9]\d{,2}"
self.email_pattern = r"\b[a-zA-Z0-9]((\w|[+.-])){0,200}@[1-9a-zA-Z](([0-9a-zA-Z]|-){,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b"
+
def filtered(self):
filtered_text = self.unfiltered_text
email = re.search(self.email_pattern, filtered_text)
while email:
start, end = email.span()
filtered = self.filter_email(filtered_text[start:end])
filtered_text = filtered_text[:start] + filtered + filtered_text[end:]
email = re.search(self.email_pattern, filtered_text)
phone = re.search(self.phone_pattern, filtered_text)
while phone:
start, end = phone.span()
filtered = self.filter_phone(filtered_text[start:end])
filtered_text = filtered_text[:start] + filtered + filtered_text[end:]
phone = re.search(self.phone_pattern, filtered_text)
return filtered_text
def filter_phone(self, phone):
if self.preserve_phone_country_code:
country_code = re.search(self.phone_country_code_pattern, phone)
+ if country_code is None:
+ return "[PHONE]"
length_country_code = country_code.span()[1]
filtered_phone = phone[:length_country_code] + " " + "[FILTERED]"
else:
filtered_phone = "[PHONE]"
return filtered_phone
def filter_email(self, email):
if not self.preserve_email_hostname and not self.partially_preserve_email_username:
filtered_email = "[EMAIL]"
elif self.preserve_email_hostname and not self.partially_preserve_email_username:
hostname_start = email.find('@')
filtered_email = "[FILTERED]" + email[hostname_start:]
elif self.partially_preserve_email_username:
hostname_start = email.find('@')
+ if len(email[:hostname_start]) < 6:
+ return "[FILTERED]" + email[hostname_start:]
filtered_email = email[:3] + "[FILTERED]" + email[hostname_start:]
return filtered_email
class Validations:
def is_email(email):
if not '@' in email:
return False
hostname = re.split('@', email)[1]
if not Validations.is_hostname(hostname):
return False
username = r"^[a-zA-Z0-9]((\w|[+.-])){0,200}@"
return bool(re.match(username, email))
def is_phone(phone):
phone_pattern = r"^(0|(00|\+)[1-9]\d{,2})((\b[)( -]){,2}\d){6,11}$"
return bool(re.match(phone_pattern, phone))
def is_hostname(hostname):
hostname_pattern = r"^[1-9a-zA-Z](([0-9a-zA-Z]|-){,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$"
return bool(re.match(hostname_pattern, hostname))
def is_ip_address(ip_address):
ip_pattern = r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9‌​]{2}|2[0-4][0-9]|25[0-5])$"
return bool(re.match(ip_pattern, ip_address))
def is_number(number):
number_pattern = r"^-?(0|[1-9]\d*)(\.\d+)?$"
return bool(re.match(number_pattern, number))
def is_integer(integer):
integer_pattern = r"^-?(0|[1-9]\d*)$"
return bool(re.match(integer_pattern, integer))
def is_date(date):
date_pattern = r"^([0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))$"
return bool(re.match(date_pattern, date))
def is_time(time):
time_pattern = r"^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"
return bool(re.match(time_pattern, time))
def is_datetime(datetime):
parsed_datetime = re.split(r"[ T]", datetime)
if len(parsed_datetime) != 2:
return False
- date = parsed_datetime[0]
- time = parsed_datetime[1]
+ date, time = parsed_datetime[0], parsed_datetime[1]
return Validations.is_date(date) and Validations.is_time(time)