Решение на Регулярни изрази от Стоян Ефтимов

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

Към профила на Стоян Ефтимов

Резултати

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

Код

import re
class Patterns:
hostname = \
r'(?P<hostname>(([a-zA-Z0-9][a-zA-Z0-9-]{,61}[a-zA-Z0-9]|' \
r'[a-zA-Z0-9])\.)+' + \
r'(?P<domain_TLD>[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))\b'
email = r'(?P<username>\b[a-zA-Z0-9][\w\+\.-]{,200})@' + hostname
phone_number = r'(?P<country_code>\b0(?!0)|((\b00|\+)[1-9]\d{,2}))' + \
r'([- \(\)]{,2}\d){6,11}\b'
ip_address = r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])'
number = r'-?((0(?!0)|[1-9][0-9]*)(\.[0-9]+)?)'
integer = r'-?(0(?!0)|[1-9][0-9]*)\b'
date = r'\b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])'
time = r'(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])'
datetime = date + r'[T ]' + time
class PrivacyFilter:
def __init__(self, text):
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
self.text = text
self._email_pattern = re.compile(Patterns.email)
self._phone_pattern = re.compile(Patterns.phone_number)
def filtered(self):
def email_filter(match_object):
if self.partially_preserve_email_username:
if len(match_object.group('username')) >= 6:
return match_object.group('username')[0:3] + \
'[FILTERED]@' +\
match_object.group('hostname')
return '[FILTERED]@' + match_object.group('hostname')
if self.preserve_email_hostname:
return '[FILTERED]@' + match_object.group('hostname')
return '[EMAIL]'
def phone_filter(match_object):
if self.preserve_phone_country_code:
return match_object.group('country_code') + ' [FILTERED]'
return '[PHONE]'
filtered_email = re.sub(self._email_pattern, email_filter, self.text)
return re.sub(self._phone_pattern, phone_filter, filtered_email)
class Validations:
@classmethod
def is_email(cls, value):
return bool(re.match(Patterns.email + '$', value))
@classmethod
def is_phone(cls, value):
return bool(re.match(Patterns.phone_number + '$', value))
@classmethod
def is_hostname(cls, value):
return bool(re.match(Patterns.hostname + '$', value))
@classmethod
def is_ip_address(cls, value):
return bool(re.match(Patterns.ip_address + '$', value))
@classmethod
def is_number(cls, value):
return bool(re.match(Patterns.number + '$', value))
@classmethod
def is_integer(cls, value):
return bool(re.match(Patterns.integer + '$', value))
@classmethod
def is_date(cls, value):
return bool(re.match(Patterns.date + '$', value))
@classmethod
def is_time(cls, value):
return bool(re.match(Patterns.time + '$', value))
@classmethod
def is_datetime(cls, value):
return bool(re.match(Patterns.datetime + '$', value))

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

....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-3ambbv/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-3ambbv/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-3ambbv/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-3ambbv/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-3ambbv/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-3ambbv/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.052s

FAILED (failures=6)

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

Стоян обнови решението на 18.04.2014 22:47 (преди над 10 години)

+import re
+
+
+class Patterns:
+ hostname = \
+ r'(?P<hostname>(([a-zA-Z0-9][a-zA-Z0-9-]{,61}[a-zA-Z0-9]|[a-zA-Z0-9])\.)+' + \
+ r'(?P<domain_TLD>[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))\b'
+ email = r'(?P<username>\b[a-zA-Z0-9][\w\+\.-]{,200})@' + hostname
+ phone_number = r'(?P<country_code>\b0(?!0)|((\b00|\+)[1-9]\d{,2}))' + \
+ r'([- \(\)]{,2}\d){6,11}\b'
+ ip_address = r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
+ r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
+ r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
+ r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])'
+ number = r'-?((0(?!0)|[1-9][0-9]*)(\.[0-9]+)?)'
+ integer = r'-?(0(?!0)|[1-9][0-9]*)\b'
+ date = r'\b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])'
+ time = r'(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])'
+ datetime = date + r'[T ]' + time
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+ self.text = text
+ self._email_pattern = re.compile(Patterns.email)
+ self._phone_pattern = re.compile(Patterns.phone_number)
+
+
+ def filtered(self):
+ def email_filter(match_object):
+ if self.partially_preserve_email_username:
+ if len(match_object.group('username')) >= 6:
+ return match_object.group('username')[0:3] + \
+ '[FILTERED]@' +\
+ match_object.group('hostname')
+ return '[FILTERED]@' + match_object.group('hostname')
+ if self.preserve_email_hostname:
+ return '[FILTERED]@' + match_object.group('hostname')
+ return '[EMAIL]'
+
+ def phone_filter(match_object):
+ if self.preserve_phone_country_code:
+ return match_object.group('country_code') + ' [FILTERED]'
+ return '[PHONE]'
+
+ filtered_email = re.sub(self._email_pattern, email_filter, self.text)
+ return re.sub(self._phone_pattern, phone_filter, filtered_email)
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, value):
+ return bool(re.match(Patterns.email + '$', value))
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(re.match(Patterns.phone_number + '$', value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.match(Patterns.hostname + '$', value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.match(Patterns.ip_address + '$', value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(re.match(Patterns.number + '$', value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.match(Patterns.integer + '$', value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.match(Patterns.date + '$', value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.match(Patterns.time + '$', value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return bool(re.match(Patterns.datetime + '$', value))

Стоян обнови решението на 18.04.2014 22:49 (преди над 10 години)

import re
class Patterns:
hostname = \
- r'(?P<hostname>(([a-zA-Z0-9][a-zA-Z0-9-]{,61}[a-zA-Z0-9]|[a-zA-Z0-9])\.)+' + \
+ r'(?P<hostname>(([a-zA-Z0-9][a-zA-Z0-9-]{,61}[a-zA-Z0-9]|' \
+ r'[a-zA-Z0-9])\.)+' + \
r'(?P<domain_TLD>[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?))\b'
email = r'(?P<username>\b[a-zA-Z0-9][\w\+\.-]{,200})@' + hostname
phone_number = r'(?P<country_code>\b0(?!0)|((\b00|\+)[1-9]\d{,2}))' + \
r'([- \(\)]{,2}\d){6,11}\b'
ip_address = r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])\.' + \
r'([2][0-5][0-5]|[1][0-9]{2}|[1-9][0-9]|[0-9])'
number = r'-?((0(?!0)|[1-9][0-9]*)(\.[0-9]+)?)'
integer = r'-?(0(?!0)|[1-9][0-9]*)\b'
date = r'\b\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])'
time = r'(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])'
datetime = date + r'[T ]' + time
class PrivacyFilter:
+
def __init__(self, text):
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
self.text = text
self._email_pattern = re.compile(Patterns.email)
self._phone_pattern = re.compile(Patterns.phone_number)
-
def filtered(self):
def email_filter(match_object):
if self.partially_preserve_email_username:
if len(match_object.group('username')) >= 6:
return match_object.group('username')[0:3] + \
'[FILTERED]@' +\
match_object.group('hostname')
return '[FILTERED]@' + match_object.group('hostname')
if self.preserve_email_hostname:
return '[FILTERED]@' + match_object.group('hostname')
return '[EMAIL]'
def phone_filter(match_object):
if self.preserve_phone_country_code:
return match_object.group('country_code') + ' [FILTERED]'
return '[PHONE]'
filtered_email = re.sub(self._email_pattern, email_filter, self.text)
return re.sub(self._phone_pattern, phone_filter, filtered_email)
class Validations:
+
@classmethod
def is_email(cls, value):
return bool(re.match(Patterns.email + '$', value))
@classmethod
def is_phone(cls, value):
return bool(re.match(Patterns.phone_number + '$', value))
@classmethod
def is_hostname(cls, value):
return bool(re.match(Patterns.hostname + '$', value))
@classmethod
def is_ip_address(cls, value):
return bool(re.match(Patterns.ip_address + '$', value))
@classmethod
def is_number(cls, value):
return bool(re.match(Patterns.number + '$', value))
@classmethod
def is_integer(cls, value):
return bool(re.match(Patterns.integer + '$', value))
@classmethod
def is_date(cls, value):
return bool(re.match(Patterns.date + '$', value))
@classmethod
def is_time(cls, value):
return bool(re.match(Patterns.time + '$', value))
@classmethod
def is_datetime(cls, value):
return bool(re.match(Patterns.datetime + '$', value))