Решение на Регулярни изрази от Божидар Михайлов

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

Към профила на Божидар Михайлов

Резултати

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

Код

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):
result = ""
def email_replacer(match):
if len(match.group()) - len(match.group("tail")) < 6:
return "[FILTERED]" + match.group("tail")
else:
return match.group()[:3] + "[FILTERED]" + match.group("tail")
if self.partially_preserve_email_username:
result = re.sub(self._email, email_replacer, self.__text)
elif self.preserve_email_hostname:
result = re.sub(self._email, "[FILTERED]\g<tail>", self.__text)
else:
result = re.sub(self._email, "[EMAIL]", self.__text)
def phone_replacer(match):
if (self.preserve_phone_country_code and
re.match(self._inter_phone_prefix, match.group("prefix"))):
return match.group("prefix") + " [FILTERED]"
else:
return "[PHONE]"
result = re.sub(self._phone, phone_replacer, result)
return result
_domain_name = r"[\da-zA-Z][\da-zA-Z-]{,62}(?!-)"
_TLD = r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?"
_hostname = "(?:" + _domain_name + r"\.)+" + _TLD
_email = r"\b[a-zA-Z\d][\w+.-]{,200}(?P<tail>@" + _hostname + r")\b"
_local_phone_prefix = r"(?<![0-9a-zA-Z+])0(?!0)"
_inter_phone_prefix = r"(?:\b00|\+)[1-9]\d{,2}"
_phone_body = r"([ \(\)-]{0,2}\d){6,11}\b"
_phone = r"(?P<prefix>" + _local_phone_prefix +\
"|" + _inter_phone_prefix + ")" + _phone_body
_byte = r"(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])"
_ip = r"\b(?:" + _byte + r"\.){3}" + _byte
_integer = r"-?(?:0|[1-9][0-9]*)"
_number = _integer + r"(?:\.[0-9]+)?"
_date = r"[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])"
_time = r"(?:0\d|1\d|2[0-3]):(?:0\d|[1-5]\d):(?:0\d|[1-5]\d)"
_datetime = _date + "( |T)" + _time
class Validations:
@classmethod
def is_email(cls, text):
return re.match(PrivacyFilter._email + "$", text) is not None
@classmethod
def is_phone(cls, text):
return re.match(PrivacyFilter._phone + "$", text) is not None
@classmethod
def is_hostname(cls, text):
return re.match(PrivacyFilter._hostname + "$", text) is not None
@classmethod
def is_ip_address(cls, text):
return re.match(PrivacyFilter._ip + "$", text) is not None
@classmethod
def is_number(cls, text):
return re.match(PrivacyFilter._number + "$", text) is not None
@classmethod
def is_integer(cls, text):
return re.match(PrivacyFilter._integer + "$", text) is not None
@classmethod
def is_date(cls, text):
return re.match(PrivacyFilter._date + "$", text) is not None
@classmethod
def is_time(cls, text):
return re.match(PrivacyFilter._time + "$", text) is not None
@classmethod
def is_datetime(cls, text):
return re.match(PrivacyFilter._datetime + "$", text) is not None

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

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

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

FAILED (failures=7)

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

Божидар обнови решението на 22.04.2014 15:06 (преди над 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):
+ result = ""
+
+ def email_replacer(match):
+ if len(match.group()) - len(match.group("tail")) < 6:
+ return "[FILTERED]" + match.group("tail")
+ else:
+ return match.group()[:3] + "[FILTERED]" + match.group("tail")
+ if self.partially_preserve_email_username:
+ result = re.sub(self._email, email_replacer, self.__text)
+ elif self.preserve_email_hostname:
+ result = re.sub(self._email, "[FILTERED]\g<tail>", self.__text)
+ else:
+ result = re.sub(self._email, "[EMAIL]", self.__text)
+
+ def phone_replacer(match):
+ if (self.preserve_phone_country_code and
+ re.match(self._inter_phone_prefix, match.group("prefix"))):
+ return match.group("prefix") + " [FILTERED]"
+ else:
+ return "[PHONE]"
+ result = re.sub(self._phone, phone_replacer, result)
+ return result
+
+ _domain_name = r"[\da-zA-Z][\da-zA-Z-]{,62}[\da-zA-Z]"
+ _TLD = r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?"
+ _hostname = "(?:" + _domain_name + r"\.)+" + _TLD
+ _email = r"\b[a-zA-Z\d][\w+.-]{,200}(?P<tail>@" + _hostname + r")\b"
+ _local_phone_prefix = r"(?<![0-9a-zA-Z+])0(?!0)"
+ _inter_phone_prefix = r"(?:\b00|\+)[1-9]\d{,2}"
+ _phone_body = r"([ \(\)-]{0,2}\d){6,11}\b"
+ _phone = r"(?P<prefix>" + _local_phone_prefix +\
+ "|" + _inter_phone_prefix + ")" + _phone_body
+ _byte = r"(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])"
+ _ip = r"\b(?:" + _byte + r"\.){3}" + _byte
+
+ _integer = r"-?(?:0|[1-9][0-9]*)"
+ _number = _integer + r"(?:\.[0-9]+)?"
+
+ _date = r"[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])"
+ _time = r"(?:0\d|1\d|2[0-3]):(?:0\d|[1-5]\d):(?:0\d|[1-5]\d)"
+ _datetime = _date + "( |T)" + _time
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, text):
+ return re.match(PrivacyFilter._email + "$", text) is not None
+
+ @classmethod
+ def is_phone(cls, text):
+ return re.match(PrivacyFilter._phone + "$", text) is not None
+
+ @classmethod
+ def is_hostname(cls, text):
+ return re.match(PrivacyFilter._hostname + "$", text) is not None
+
+ @classmethod
+ def is_ip_address(cls, text):
+ return re.match(PrivacyFilter._ip + "$", text) is not None
+
+ @classmethod
+ def is_number(cls, text):
+ return re.match(PrivacyFilter._number + "$", text) is not None
+
+ @classmethod
+ def is_integer(cls, text):
+ return re.match(PrivacyFilter._integer + "$", text) is not None
+
+ @classmethod
+ def is_date(cls, text):
+ return re.match(PrivacyFilter._date + "$", text) is not None
+
+ @classmethod
+ def is_time(cls, text):
+ return re.match(PrivacyFilter._time + "$", text) is not None
+
+ @classmethod
+ def is_datetime(cls, text):
+ return re.match(PrivacyFilter._datetime + "$", text) is not None

Божидар обнови решението на 22.04.2014 17:53 (преди над 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):
result = ""
def email_replacer(match):
if len(match.group()) - len(match.group("tail")) < 6:
return "[FILTERED]" + match.group("tail")
else:
return match.group()[:3] + "[FILTERED]" + match.group("tail")
if self.partially_preserve_email_username:
result = re.sub(self._email, email_replacer, self.__text)
elif self.preserve_email_hostname:
result = re.sub(self._email, "[FILTERED]\g<tail>", self.__text)
else:
result = re.sub(self._email, "[EMAIL]", self.__text)
def phone_replacer(match):
if (self.preserve_phone_country_code and
re.match(self._inter_phone_prefix, match.group("prefix"))):
return match.group("prefix") + " [FILTERED]"
else:
return "[PHONE]"
result = re.sub(self._phone, phone_replacer, result)
return result
- _domain_name = r"[\da-zA-Z][\da-zA-Z-]{,62}[\da-zA-Z]"
+ _domain_name = r"[\da-zA-Z][\da-zA-Z-]{,62}(?!-)"
_TLD = r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?"
_hostname = "(?:" + _domain_name + r"\.)+" + _TLD
_email = r"\b[a-zA-Z\d][\w+.-]{,200}(?P<tail>@" + _hostname + r")\b"
_local_phone_prefix = r"(?<![0-9a-zA-Z+])0(?!0)"
_inter_phone_prefix = r"(?:\b00|\+)[1-9]\d{,2}"
_phone_body = r"([ \(\)-]{0,2}\d){6,11}\b"
_phone = r"(?P<prefix>" + _local_phone_prefix +\
"|" + _inter_phone_prefix + ")" + _phone_body
_byte = r"(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])"
_ip = r"\b(?:" + _byte + r"\.){3}" + _byte
_integer = r"-?(?:0|[1-9][0-9]*)"
_number = _integer + r"(?:\.[0-9]+)?"
_date = r"[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])"
_time = r"(?:0\d|1\d|2[0-3]):(?:0\d|[1-5]\d):(?:0\d|[1-5]\d)"
_datetime = _date + "( |T)" + _time
class Validations:
@classmethod
def is_email(cls, text):
return re.match(PrivacyFilter._email + "$", text) is not None
@classmethod
def is_phone(cls, text):
return re.match(PrivacyFilter._phone + "$", text) is not None
@classmethod
def is_hostname(cls, text):
return re.match(PrivacyFilter._hostname + "$", text) is not None
@classmethod
def is_ip_address(cls, text):
return re.match(PrivacyFilter._ip + "$", text) is not None
@classmethod
def is_number(cls, text):
return re.match(PrivacyFilter._number + "$", text) is not None
@classmethod
def is_integer(cls, text):
return re.match(PrivacyFilter._integer + "$", text) is not None
@classmethod
def is_date(cls, text):
return re.match(PrivacyFilter._date + "$", text) is not None
@classmethod
def is_time(cls, text):
return re.match(PrivacyFilter._time + "$", text) is not None
@classmethod
def is_datetime(cls, text):
return re.match(PrivacyFilter._datetime + "$", text) is not None