Решение на Регулярни изрази от Антонио Николов

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

Към профила на Антонио Николов

Резултати

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

Код

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):
prefix = r'((?<=\s)|^)((00|\+)[1-9][0-9]{0,2}|0(?!0))'
number = r'(( |-|\(|\)){0,2}?\d){6,11}?((?= \D)|(?= \D)|(?=\s)|$)'
user = r'((?<=\s)|^)[a-zA-Z0-9][\w\+\.-]{,200}@'
domain = r'(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
TLD = r'[a-zA-Z]{2,3}(?=\s|$)'
filter = '[FILTERED]'
filter_code = ' [FILTERED]'
text = self._text
if self.partially_preserve_email_username:
name1 = r'((?<=^[a-zA-Z0-9][\w\+\.-]{2})|'
name2 = r'(?<=\s[a-zA-Z0-9][\w\+\.-]{2}))[\w\+\.-]{3,198}(?=@'
text = re.sub(name1 + name2 + domain + TLD + ')', filter, text)
self.preserve_email_hostname = True
if self.preserve_email_hostname:
name = r'((?<=\s)|^)[a-zA-Z0-9][\w\+\.-]{,200}(?=@'
text = re.sub(name + domain + TLD + ')', filter, text)
text = re.sub(user + domain + TLD, '[EMAIL]', text)
if self.preserve_phone_country_code:
code_space = r'((?<=\s00[1-9]\d\d)|(?<=\s\+[1-9]\d\d)|'
code_start = r'(?<=^00[1-9]\d\d)|(?<=^\+[1-9]\d\d))'
text = re.sub(code_space + code_start + number, filter_code, text)
code_space = r'((?<=\s00[1-9]\d)|(?<=\s\+[1-9]\d)|'
code_start = r'(?<=^00[1-9]\d)|(?<=^\+[1-9]\d))'
text = re.sub(code_space + code_start + number, filter_code, text)
code_space = r'((?<=\s00[1-9])|(?<=\s\+[1-9])|'
code_start = r'(?<=^00[1-9])|(?<=^\+[1-9]))'
text = re.sub(code_space + code_start + number, filter_code, text)
text = re.sub(prefix + number, '[PHONE]', text)
return text
class Validations:
@classmethod
def is_email(cls, value):
user = r'^[a-zA-Z0-9][\w\+\.-]{,200}@'
domain = r'(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
TLD = r'[a-zA-Z]{2,3}$'
return re.search(user + domain + TLD, value) is not None
@classmethod
def is_phone(cls, value):
prefix = r'^((00|\+)[1-9][0-9]{0,2}|0(?!0))'
number = r'(( |-|\(|\)){0,2}?\d){6,11}?$'
return re.search(prefix + number, value) is not None
@classmethod
def is_hostname(cls, value):
domain = r'^(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
TLD = r'[a-zA-Z]{2,3}$'
return re.search(domain + TLD, value) is not None
@classmethod
def is_ip_address(cls, value):
ip = r'^((\d|\d\d|1\d\d|2[0-4]\d|25[0-5])\.){3}'
ip_last_number = r'(\d|\d\d|1\d\d|2[0-4]\d|25[0-5])$'
return re.search(ip + ip_last_number, value) is not None
@classmethod
def is_number(cls, value):
number = r'^-{0,1}(0|[1-9]\d*)(\.\d+){0,1}$'
return re.search(number, value) is not None
@classmethod
def is_integer(cls, value):
number = r'^-{0,1}(0|[1-9]\d*)$'
return re.search(number, value) is not None
@classmethod
def is_date(cls, value):
date = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])$'
return re.search(date, value) is not None
@classmethod
def is_time(cls, value):
time = r'^([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$'
return re.search(time, value) is not None
@classmethod
def is_datetime(cls, value):
date = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])( |T)'
time = r'([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$'
return re.search(date + time, value) is not None

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

....F..F.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-ixi03m/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_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-ixi03m/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_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-ixi03m/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] 2 or...'
-  [PHONE] or...
+  [PHONE] 2 or...
?          ++


======================================================================
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-ixi03m/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-ixi03m/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-ixi03m/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-ixi03m/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-ixi03m/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.057s

FAILED (failures=8)

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

Антонио обнови решението на 21.04.2014 20:05 (преди над 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):
+ prefix = r'((?<=\s)|^)((00|\+)[1-9][0-9]{0,2}|0(?!0))'
+ number = r'(( |-|\(|\)){0,2}?\d){6,11}?((?= \D)|(?= \D)|(?=\s)|$)'
+ user = r'((?<=\s)|^)[a-zA-Z0-9][\w\+\.-]{,200}@'
+ domain = r'(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
+ TLD = r'[a-zA-Z]{2,3}(?=\s|$)'
+ filter = '[FILTERED]'
+ filter_code = ' [FILTERED]'
+ text = self._text
+
+ if self.partially_preserve_email_username:
+ name1 = r'((?<=^[a-zA-Z0-9][\w\+\.-]{2})|'
+ name2= r'(?<=\s[a-zA-Z0-9][\w\+\.-]{2}))[\w\+\.-]{3,198}(?=@'
+ text = re.sub(name1 + name2 + domain + TLD + ')', filter, text)
+ self.preserve_email_hostname = True
+ if self.preserve_email_hostname:
+ name = r'((?<=\s)|^)[a-zA-Z0-9][\w\+\.-]{,200}(?=@'
+ text = re.sub(name + domain + TLD + ')', filter, text)
+
+ text = re.sub(user + domain + TLD, '[EMAIL]', text)
+
+ if self.preserve_phone_country_code:
+ code_space = r'((?<=\s00[1-9]\d\d)|(?<=\s\+[1-9]\d\d)|'
+ code_start = r'(?<=^00[1-9]\d\d)|(?<=^\+[1-9]\d\d))'
+ text = re.sub(code_space + code_start + number , filter_code, text)
+ code_space = r'((?<=\s00[1-9]\d)|(?<=\s\+[1-9]\d)|'
+ code_start = r'(?<=^00[1-9]\d)|(?<=^\+[1-9]\d))'
+ text = re.sub(code_space + code_start + number , filter_code, text)
+ code_space = r'((?<=\s00[1-9])|(?<=\s\+[1-9])|'
+ code_start = r'(?<=^00[1-9])|(?<=^\+[1-9]))'
+ text = re.sub(code_space + code_start + number , filter_code, text)
+
+ text = re.sub(prefix + number, '[PHONE]', text)
+
+ return text
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, value):
+ user = r'^[a-zA-Z0-9][\w\+\.-]{,200}@'
+ domain = r'(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
+ TLD = r'[a-zA-Z]{2,3}$'
+
+ return re.search(user + domain + TLD, value) != None
+
+ @classmethod
+ def is_phone(cls, value):
+ prefix = r'^((00|\+)[1-9][0-9]{0,2}|0(?!0))'
+ number = r'(( |-|\(|\)){0,2}?\d){6,11}?$'
+
+ return re.search(prefix + number, value) != None
+
+ @classmethod
+ def is_hostname(cls, value):
+ domain = r'^(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
+ TLD = r'[a-zA-Z]{2,3}$'
+
+ return re.search(domain + TLD, value) != None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ ip = r'^((\d|\d\d|1\d\d|2[0-4]\d|25[0-5])\.){3}'
+ ip_last_number = r'(\d|\d\d|1\d\d|2[0-4]\d|25[0-5])$'
+
+ return re.search(ip + ip_last_number, value) != None
+
+ @classmethod
+ def is_number(cls, value):
+ number = r'^-{0,1}(0|[1-9]\d*)(\.\d+){0,1}$'
+
+ return re.search(number, value) != None
+
+ @classmethod
+ def is_integer(cls, value):
+ number = r'^-{0,1}(0|[1-9]\d*)$'
+
+ return re.search(number, value) != None
+
+ @classmethod
+ def is_date(cls, value):
+ date = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])$'
+
+ return re.search(date, value) != None
+
+ @classmethod
+ def is_time(cls, value):
+ time = r'^([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$'
+
+ return re.search(time, value) != None
+
+ @classmethod
+ def is_datetime(cls,value):
+ date = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])( |T)'
+ time = r'([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$'
+
+ return re.search(date + time, value) != None

Антонио обнови решението на 21.04.2014 20:15 (преди над 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):
prefix = r'((?<=\s)|^)((00|\+)[1-9][0-9]{0,2}|0(?!0))'
number = r'(( |-|\(|\)){0,2}?\d){6,11}?((?= \D)|(?= \D)|(?=\s)|$)'
user = r'((?<=\s)|^)[a-zA-Z0-9][\w\+\.-]{,200}@'
domain = r'(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
TLD = r'[a-zA-Z]{2,3}(?=\s|$)'
filter = '[FILTERED]'
filter_code = ' [FILTERED]'
text = self._text
if self.partially_preserve_email_username:
name1 = r'((?<=^[a-zA-Z0-9][\w\+\.-]{2})|'
- name2= r'(?<=\s[a-zA-Z0-9][\w\+\.-]{2}))[\w\+\.-]{3,198}(?=@'
+ name2 = r'(?<=\s[a-zA-Z0-9][\w\+\.-]{2}))[\w\+\.-]{3,198}(?=@'
text = re.sub(name1 + name2 + domain + TLD + ')', filter, text)
self.preserve_email_hostname = True
if self.preserve_email_hostname:
name = r'((?<=\s)|^)[a-zA-Z0-9][\w\+\.-]{,200}(?=@'
text = re.sub(name + domain + TLD + ')', filter, text)
text = re.sub(user + domain + TLD, '[EMAIL]', text)
if self.preserve_phone_country_code:
code_space = r'((?<=\s00[1-9]\d\d)|(?<=\s\+[1-9]\d\d)|'
code_start = r'(?<=^00[1-9]\d\d)|(?<=^\+[1-9]\d\d))'
- text = re.sub(code_space + code_start + number , filter_code, text)
+ text = re.sub(code_space + code_start + number, filter_code, text)
code_space = r'((?<=\s00[1-9]\d)|(?<=\s\+[1-9]\d)|'
code_start = r'(?<=^00[1-9]\d)|(?<=^\+[1-9]\d))'
- text = re.sub(code_space + code_start + number , filter_code, text)
+ text = re.sub(code_space + code_start + number, filter_code, text)
code_space = r'((?<=\s00[1-9])|(?<=\s\+[1-9])|'
code_start = r'(?<=^00[1-9])|(?<=^\+[1-9]))'
- text = re.sub(code_space + code_start + number , filter_code, text)
+ text = re.sub(code_space + code_start + number, filter_code, text)
text = re.sub(prefix + number, '[PHONE]', text)
return text
class Validations:
@classmethod
def is_email(cls, value):
user = r'^[a-zA-Z0-9][\w\+\.-]{,200}@'
domain = r'(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
TLD = r'[a-zA-Z]{2,3}$'
- return re.search(user + domain + TLD, value) != None
+ return re.search(user + domain + TLD, value) is not None
@classmethod
def is_phone(cls, value):
prefix = r'^((00|\+)[1-9][0-9]{0,2}|0(?!0))'
number = r'(( |-|\(|\)){0,2}?\d){6,11}?$'
- return re.search(prefix + number, value) != None
+ return re.search(prefix + number, value) is not None
@classmethod
def is_hostname(cls, value):
domain = r'^(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
TLD = r'[a-zA-Z]{2,3}$'
- return re.search(domain + TLD, value) != None
+ return re.search(domain + TLD, value) is not None
@classmethod
def is_ip_address(cls, value):
ip = r'^((\d|\d\d|1\d\d|2[0-4]\d|25[0-5])\.){3}'
ip_last_number = r'(\d|\d\d|1\d\d|2[0-4]\d|25[0-5])$'
- return re.search(ip + ip_last_number, value) != None
+ return re.search(ip + ip_last_number, value) is not None
@classmethod
def is_number(cls, value):
number = r'^-{0,1}(0|[1-9]\d*)(\.\d+){0,1}$'
- return re.search(number, value) != None
+ return re.search(number, value) is not None
@classmethod
def is_integer(cls, value):
number = r'^-{0,1}(0|[1-9]\d*)$'
- return re.search(number, value) != None
+ return re.search(number, value) is not None
@classmethod
def is_date(cls, value):
date = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])$'
- return re.search(date, value) != None
+ return re.search(date, value) is not None
@classmethod
def is_time(cls, value):
time = r'^([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$'
- return re.search(time, value) != None
+ return re.search(time, value) is not None
@classmethod
- def is_datetime(cls,value):
+ def is_datetime(cls, value):
date = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])( |T)'
time = r'([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$'
- return re.search(date + time, value) != None
+ return re.search(date + time, value) is not None