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

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

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

Резултати

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

Код

import re
class Validations:
@classmethod
def is_number(cls, value):
return bool(re.search(r'^-?0$|^-?[1-9]\d*$|^-?[1-9]\d*\.\d+$|^-?0\.\d+$', value))
@classmethod
def is_email(cls, value):
return bool(re.search(r'^[^\W_][a-zA-Z0-9\+\.\-_]{,200}@(([^\W_]\.)|([^\W_][0-9a-zA-Z\-]{,61}[^\W_])\.)+[a-zA-Z]{2,3}(\.[A-Za-z]{2})?$', value))
@classmethod
def is_hostname(cls, value):
return bool(re.search(r'^(([^\W_]\.)|([^\W_][0-9a-zA-Z\-]{,61}[^\W_])\.)+[a-zA-Z]{2,3}(\.[A-Za-z]{2})?$', value))
@classmethod
def is_phone(cls, value):
local = r'^0([\s()\-]{,2}\d){6,11}$'
international = r'^(00|\+)[1-9]\d{0,2}([\s()\-]{,2}\d){6,11}$'
return bool(re.search(local, value)) or bool(re.search(international, value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(r'^(\d\.|[1-9]\d\.|1\d\d\.|2[0-5][0-5]\.){3}(\d|[1-9]\d|1\d\d|2[0-5][0-5])$', value))
@classmethod
def is_integer(cls, value):
return bool(re.search(r'^-?0$|^-?[1-9]\d*$', value))
@classmethod
def is_date(cls, value):
return bool(re.search(r'^\d{4}-(0[1-9]|1[0-2])-([0-2][1-9]|3[0-1])$', value))
@classmethod
def is_time(cls, value):
return bool(re.search(r'^([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$', value))
@classmethod
def is_datetime(cls, value):
match = re.search(r'^(\S*)[T ](\S*)$', value)
if not match:
return False
return Validations.is_date(match.group(1)) and Validations.is_time(match.group(2))
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):
return self.phone_filtered(self.email_filtered(self.text))
def phone_filtered(self, text):
if self.preserve_phone_country_code:
return self.local_phone_filtered(self.partially_inter_phone(text))
else:
return self.local_phone_filtered(self.inter_phone_filtered(text))
def partially_inter_phone(self, text):
pattern = r'((00|\+)[1-9]\d{0,2})([\s()\-]{,2}\d){6,11}\b'
return re.sub(pattern, r'\1 [FILTERED]', text)
def inter_phone_filtered(self, text):
pattern = r'(00|\+)[1-9]\d{0,2}([\s()\-]{,2}\d){6,11}\b'
return re.sub(pattern, r'[PHONE]', text)
def local_phone_filtered(self, text):
pattern = r'\b0([\s()\-]{,2}\d){6,11}\b'
return re.sub(pattern, r'[PHONE]', text)
def email_filtered(self, text):
if self.partially_preserve_email_username:
return self.filtered_hostname(self.partially_filtered_hostname(text))
elif self.preserve_email_hostname:
return self.filtered_hostname(text)
else:
return self.normal_filtered_email(text)
def partially_filtered_hostname(self, text):
pattern = r'\b([^\W_][\w\+\.\-]{2})[\w\+\.\-]{3,198}(@((([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b))'
return re.sub(pattern, r'\1[FILTERED]\2', text)
def filtered_hostname(self, text):
pattern = r'\b[^\W_][\w\+\.\-]{,200}@((([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b)'
return re.sub(pattern, r'[FILTERED]@\1', text)
def normal_filtered_email(self, text):
pattern = r'\b[^\W_][\w\+\.\-]{,200}@(([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
return re.sub(pattern, r'[EMAIL]', text)

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

....F...........F.....FF..FF...........
======================================================================
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-1l1s22l/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '+1555 123, 55555' != '[PHONE], 55555'
- +1555 123, 55555
+ [PHONE], 55555


======================================================================
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-1l1s22l/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-1l1s22l/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-1l1s22l/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-1l1s22l/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-1l1s22l/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

----------------------------------------------------------------------
Ran 39 tests in 0.060s

FAILED (failures=6)

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

Иван обнови решението на 21.04.2014 23:03 (преди над 10 години)

+import re
+
+
+class Validations:
+ @classmethod
+ def is_number(cls, value):
+ return bool(re.search(r'^-?0$|^-?[1-9]\d*$|^-?[1-9]\d*\.\d+$|^-?0\.\d+$', value))
+
+ @classmethod
+ def is_email(cls, value):
+ return bool(re.search(r'^[^\W_][a-zA-Z0-9\+\.\-_]{,200}@(([^\W_]\.)|([^\W_][0-9a-zA-Z\-]{,61}[^\W_])\.)+[a-zA-Z]{2,3}(\.[A-Za-z]{2})?$', value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.search(r'^(([^\W_]\.)|([^\W_][0-9a-zA-Z\-]{,61}[^\W_])\.)+[a-zA-Z]{2,3}(\.[A-Za-z]{2})?$', value))
+
+ @classmethod
+ def is_phone(cls, value):
+ local = r'^0([\s()\-]{,2}\d){6,11}$'
+ international = r'^(00|\+)[1-9]\d{0,2}([\s()\-]{,2}\d){6,11}$'
+ return bool(re.search(local, value)) or bool(re.search(international, value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.search(r'^(\d\.|[1-9]\d\.|1\d\d\.|2[0-5][0-5]\.){3}(\d|[1-9]\d|1\d\d|2[0-5][0-5])$', value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.search(r'^-?0$|^-?[1-9]\d*$', value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.search(r'^\d{4}-(0[1-9]|1[0-2])-([0-2][1-9]|3[0-1])$', value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.search(r'^([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$', value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ match = re.search(r'^(\S*)[T ](\S*)$', value)
+ if not match:
+ return False
+ return Validations.is_date(match.group(1)) and Validations.is_time(match.group(2))
+
+
+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):
+ return self.phone_filtered(self.email_filtered(self.text))
+
+ def phone_filtered(self, text):
+ if self.preserve_phone_country_code:
+ return self.local_phone_filtered(self.partially_inter_phone(text))
+ else:
+ return self.local_phone_filtered(self.inter_phone_filtered(text))
+
+ def partially_inter_phone(self, text):
+ pattern = r'((00|\+)[1-9]\d{0,2})([\s()\-]{,2}\d){6,11}\b'
+ return re.sub(pattern, r'\1 [FILTERED]', text)
+
+ def inter_phone_filtered(self, text):
+ pattern = r'(00|\+)[1-9]\d{0,2}([\s()\-]{,2}\d){6,11}\b'
+ return re.sub(pattern, r'[PHONE]', text)
+
+ def local_phone_filtered(self, text):
+ pattern = r'\b0([\s()\-]{,2}\d){6,11}\b'
+ return re.sub(pattern, r'[PHONE]', text)
+
+ def email_filtered(self, text):
+ if self.partially_preserve_email_username:
+ return self.filtered_hostname(self.partially_filtered_hostname(text))
+ elif self.preserve_email_hostname:
+ return self.filtered_hostname(text)
+ else:
+ return self.normal_filtered_email(text)
+
+ def partially_filtered_hostname(self, text):
+ pattern = r'\b([^\W_][\w\+\.\-]{2})[\w\+\.\-]{3,198}(@((([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b))'
+ return re.sub(pattern, r'\1[FILTERED]\2', text)
+
+ def filtered_hostname(self, text):
+ pattern = r'\b[^\W_][\w\+\.\-]{,200}@((([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b)'
+ return re.sub(pattern, r'[FILTERED]@\1', text)
+
+ def normal_filtered_email(self, text):
+ pattern = r'\b[^\W_][\w\+\.\-]{,200}@(([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
+ return re.sub(pattern, r'[EMAIL]', text)