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

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

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

Резултати

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

Код

import re
class PrivacyFilter:
def __init__(self, text):
self.text = text
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def filtered(self):
def replacement(mail):
username_length = len(mail.group(2))
if username_length < 6:
filtered = "[FILTERED]" + str(mail.group(3))
else:
filtered = (mail.group(2)[:3] +
"[FILTERED]" + str(mail.group(3)))
return filtered
match = self.text
if (self.partially_preserve_email_username):
match = re.sub(r'(\b([a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200})(@'
'([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\\b)',
replacement, match)
elif (self.preserve_email_hostname):
match = re.sub(r'(\b[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}(@'
'([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\\b))',
r'[FILTERED]\2', match)
else:
match = re.sub(r'(\b[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}@'
'([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\\b',
'[EMAIL]', match)
if (self.preserve_phone_country_code):
match = re.sub(r'(((\b00|\+)[1-9][0-9]{1,2})[\- \(\)]{0,2}'
r'([0-9][- \(\)]{0,2}){5,10}[0-9])\b',
r"\2 [FILTERED]", match)
match = re.sub(r'((\b00|\+)[1-9][0-9]{1,2}[\- \(\)]{0,2}'
'([0-9][- \(\)]{0,2}){5,10}[0-9])\\b',
"[PHONE]", match)
match = re.sub(r'\b(0[- \(\)]{0,2}[1-9][\- \(\)]{0,2}'
'([0-9][- \(\)]{0,2}){4,9}[0-9])\\b', '[PHONE]', match)
return match
class Validations:
@classmethod
def is_email(cls, value):
pattern = r'^[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}@'\
r'([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'\
r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
match = re.search(pattern, value)
return match is not None
@classmethod
def is_phone(cls, value):
pattern_local = r'^0[- \(\)]{0,2}[1-9][\- \(\)]{0,2}'\
r'([0-9][- \(\)]{0,2}){4,9}[0-9]$'
pattern_international = r'^(00|\+)[1-9][0-9]{1,2}[\- \(\)]{0,2}'\
r'([0-9][- \(\)]{0,2}){5,10}[0-9]$'
match = re.search(pattern_local, value)
match_international = re.search(pattern_international, value)
return (match is not None or match_international is not None)
@classmethod
def is_hostname(cls, value):
pattern = (r'^([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$')
match = re.search(pattern, value)
return match is not None
@classmethod
def is_ip_address(cls, value):
pattern = (r'^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?'
'|0)\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)$')
match = re.search(pattern, value)
return match is not None
@classmethod
def is_number(cls, value):
pattern = r'^-?(([1-9][0-9]*(\.[0-9]+)?)|(0(\.[0-9]+)?))$'
match = re.search(pattern, value)
return match is not None
@classmethod
def is_integer(cls, value):
pattern = r'^-?([1-9][0-9]*|0)$'
match = re.search(pattern, value)
return match is not None
@classmethod
def is_date(cls, value):
pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$'
match = re.search(pattern, value)
return match is not None
@classmethod
def is_time(cls, value):
pattern = r'^([01][0-9]|2[0-3])(:[0-5][0-9]){2}$'
match = re.search(pattern, value)
return match is not None
@classmethod
def is_datetime(cls, value):
pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])'\
r'[ T]([01][0-9]|2[0-3])(:[0-5][0-9]){2}$'
match = re.search(pattern, value)
return match is not None

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

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


======================================================================
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-vllg3o/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-vllg3o/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-vllg3o/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-vllg3o/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-vllg3o/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=7)

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

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

+import re
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.text = text
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def filtered(self):
+ def replacement(mail):
+ username_length = len(mail.group(2))
+ if username_length < 6:
+ filtered = "[FILTERED]" + str(mail.group(3))
+ else:
+ filtered = (mail.group(2)[:3] +
+ "[FILTERED]" + str(mail.group(3)))
+ return filtered
+ match = self.text
+ if (self.partially_preserve_email_username):
+ match = re.sub(r'(\b([a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200})(@'
+ '([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\\b)',
+ replacement, match)
+ elif (self.preserve_email_hostname):
+ match = re.sub(r'(\b[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}(@'
+ '([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\\b))',
+ r'[FILTERED]\2', match)
+ else:
+ match = re.sub(r'(\b[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}@'
+ '([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\\b',
+ '[EMAIL]', match)
+ if (self.preserve_phone_country_code):
+ match = re.sub(r'(((\b00|\+)[1-9][0-9]{1,2})[\- \(\)]{0,2}'
+ r'([0-9][- \(\)]{0,2}){5,10}[0-9])\b',
+ r"\2 [FILTERED]", match)
+ match = re.sub(r'((\b00|\+)[1-9][0-9]{1,2}[\- \(\)]{0,2}'
+ '([0-9][- \(\)]{0,2}){5,10}[0-9])\\b',
+ "[PHONE]", match)
+ match = re.sub(r'\b(0[- \(\)]{0,2}[1-9][\- \(\)]{0,2}'
+ '([0-9][- \(\)]{0,2}){4,9}[0-9])\\b', '[PHONE]', match)
+ return match
+
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ pattern = r'^[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}@'\
+ r'([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'\
+ r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_phone(cls, value):
+ pattern_local = r'^0[- \(\)]{0,2}[1-9][\- \(\)]{0,2}'\
+ r'([0-9][- \(\)]{0,2}){4,9}[0-9]$'
+ pattern_international = r'^(00|\+)[1-9][0-9]{1,2}[\- \(\)]{0,2}'\
+ r'([0-9][- \(\)]{0,2}){5,10}[0-9]$'
+ match = re.search(pattern_local, value)
+ match_international = re.search(pattern_international, value)
+ return (match is not None or match_international is not None)
+
+ @classmethod
+ def is_hostname(cls, value):
+ pattern = (r'^([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
+ r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$')
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ pattern = (r'^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?'
+ '|0)\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)$')
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_number(cls, value):
+ pattern = r'^-?(([1-9][0-9]*(\.[0-9]+)?)|(0(\.[0-9]+)?))$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_integer(cls, value):
+ pattern = r'^-?([1-9][0-9]*|0)$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_date(cls, value):
+ pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_time(cls, value):
+ pattern = r'^([01][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_datetime(cls, value):
+ pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])'\
+ r'[ T]([01][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+ match = re.search(pattern, value)
+ return match is not None