Решение на Регулярни изрази от Милица Борисова

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

Към профила на Милица Борисова

Резултати

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

Код

import re
HOST_NAME_PATTERN = '([\da-zA-Z][\da-zA-Z-]{,61}[\da-zA-Z]\.|[\da-zA-Z]\.)+'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
NAME_PATTERN = '[\da-zA-z][\da-zA-z-_\+\.]{,200}'
EMAIL_PATTERN = NAME_PATTERN + '@' + HOST_NAME_PATTERN
I_CODE_PATTERN = '(0{2}|\+)[1-9][0-9]{,2}'
PHONE_NUMBER_PATTERN = '(([\s\-\(\)]){,2}[0-9]){6,11}'
LOCAL_PHONE_PATTERN = '0([\s\-\(\)])?[1-9]' + PHONE_NUMBER_PATTERN
INTERNATIONAL_PHONE_PATTERN = I_CODE_PATTERN + PHONE_NUMBER_PATTERN
PHONE_PATTERN = '(' + LOCAL_PHONE_PATTERN + '|' + \
INTERNATIONAL_PHONE_PATTERN + ')'
IP_PATTERN = '((1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}'\
'(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])'
NUMBER_PATTERN = '-?(0|[1-9][0-9]*)((.|,)[0-9]+)?'
INTEGER_PATTERN = '-?(0|[1-9][0-9]*)'
DATE_PATTERN = '[0-9]{4}-(0[1-9]|1[1-2])-([1-2][0-9]|3[0-1]|0[1-9])'
TIME_PATTERN = '([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]'
DATE_TIME_PATTERN = DATE_PATTERN + '(\s|T)' + TIME_PATTERN
class PrivacyFilter:
def __init__(self, text):
self.data = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
if bool(re.search(re.compile('(\s|^)' + EMAIL_PATTERN + '(\s|$)'), self.data)):
if self.preserve_email_hostname or \
self.partially_preserve_email_username:
if self.partially_preserve_email_username and\
len(re.search(re.compile(NAME_PATTERN + '@'), self.data)\
.group()) > 7:
text = re.split(re.compile(EMAIL_PATTERN), self.data)
email = re.search(re.compile(EMAIL_PATTERN),\
self.data).group()
return text[0] + email[:3] + re.sub(r'[\da-zA-z-_+.]{,200}@', \
'[FILTERED]@', email[3:])
return re.sub(re.compile(NAME_PATTERN + '@'), '[FILTERED]@', \
self.data)
return re.sub(re.compile(EMAIL_PATTERN), '[EMAIL]', self.data)
if bool(re.search(re.compile('(\s|^)' + PHONE_PATTERN + '(\s|$)'), self.data)):
if self.preserve_phone_country_code and\
bool(re.search(re.compile(INTERNATIONAL_PHONE_PATTERN), self.data)):
code = re.search(re.compile(I_CODE_PATTERN ), self.data).group()
phone = re.search(re.compile(PHONE_NUMBER_PATTERN), self.data).group()
text = re.split(re.compile(PHONE_PATTERN), self.data)
return text[0] + code + re.sub(re.compile(PHONE_NUMBER_PATTERN), \
' [FILTERED]', phone)
return re.sub(re.compile(PHONE_PATTERN), '[PHONE]', self.data)
return self.data
class Validations:
@classmethod
def is_email(cls, value):
return bool(re.match(re.compile(EMAIL_PATTERN + '$'), value))
@classmethod
def is_hostname(cls, value):
return bool(re.match(re.compile(HOST_NAME_PATTERN + '$'), value))
@classmethod
def is_phone(cls, value):
return bool(re.match(re.compile(PHONE_PATTERN + '$'), value))
@classmethod
def is_ip_address(cls, value):
return bool(re.match(re.compile(IP_PATTERN + '$'), value))
@classmethod
def is_number(cls, value):
return bool(re.match(re.compile(NUMBER_PATTERN + '$'), value))
@classmethod
def is_integer(cls, value):
return bool(re.match(re.compile(INTEGER_PATTERN + '$'), value))
@classmethod
def is_date(cls, value):
return bool(re.match(re.compile(DATE_PATTERN + '$'), value))
@classmethod
def is_time(cls, value):
return bool(re.match(re.compile(TIME_PATTERN + '$'), value))
@classmethod
def is_datetime(cls, value):
return bool(re.match(re.compile(DATE_TIME_PATTERN + '$'), value))

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

...F...F.......F......FF..FF...........
======================================================================
FAIL: test_does_not_filter_invalid_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-1cet4zn/test.py", line 48, in test_does_not_filter_invalid_emails
    self.assertEqual(text, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact me here: _invalid@email.com' != 'Contact me here: [EMAIL]'
- Contact me here: _invalid@email.com
+ Contact me here: [EMAIL]


======================================================================
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-1cet4zn/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact:[EMAIL]' != 'Contact:someone@example.com'
- Contact:[EMAIL]
+ Contact:someone@example.com


======================================================================
FAIL: test_can_validate_more_complex_emails (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-1cet4zn/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), 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-1cet4zn/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-1cet4zn/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-1cet4zn/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-1cet4zn/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.054s

FAILED (failures=7)

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

Милица обнови решението на 22.04.2014 16:46 (преди над 10 години)

+import re
+
+
+HOST_NAME_PATTERN = '([\da-zA-Z][\da-zA-Z-]{,61}[\da-zA-Z]\.|[\da-zA-Z]\.)+'\
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+
+NAME_PATTERN = '[\da-zA-z][\da-zA-z-_\+\.]{,200}'
+
+EMAIL_PATTERN = NAME_PATTERN + '@' + HOST_NAME_PATTERN
+
+I_CODE_PATTERN = '(0{2}|\+)[1-9][0-9]{,2}'
+
+PHONE_NUMBER_PATTERN = '(([\s\-\(\)]){,2}[0-9]){5,10}'
+
+LOCAL_PHONE_PATTERN = '0([\s\-\(\)])?[1-9]' + PHONE_NUMBER_PATTERN
+
+INTERNATIONAL_PHONE_PATTERN = I_CODE_PATTERN + PHONE_NUMBER_PATTERN
+
+PHONE_PATTERN = '(' + LOCAL_PHONE_PATTERN + '|' + \
+ INTERNATIONAL_PHONE_PATTERN + ')'
+
+IP_PATTERN = '((1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}'\
+ '(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])'
+
+NUMBER_PATTERN = '-?(0|[1-9][0-9]*)((.|,)[0-9]+)?'
+
+INTEGER_PATTERN = '-?(0|[1-9][0-9]*)'
+
+DATE_PATTERN = '[0-9]{4}-(0[1-9]|1[1-2])-([1-2][0-9]|3[0-1]|0[1-9])'
+
+TIME_PATTERN = '([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]'
+
+DATE_TIME_PATTERN = DATE_PATTERN + '(\s|T)' + TIME_PATTERN
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.data = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ if bool(re.search(re.compile(EMAIL_PATTERN), self.data)):
+ if self.preserve_email_hostname or \
+ self.partially_preserve_email_username:
+ if self.partially_preserve_email_username and\
+ len(re.search(re.compile(NAME_PATTERN + '@'), self.data)\
+ .group()) > 7:
+ text = re.split(re.compile(EMAIL_PATTERN), self.data)
+ email = re.search(re.compile(EMAIL_PATTERN),\
+ self.data).group()
+ return text[0] + email[:3] + re.sub(r'[\da-zA-z-_+.]{,200}@', \
+ '[FILTERED]@', email[3:])
+ return re.sub(re.compile(NAME_PATTERN + '@'), '[FILTERED]@', \
+ self.data)
+ return re.sub(re.compile(EMAIL_PATTERN), '[EMAIL]', self.data)
+ if bool(re.search(re.compile(PHONE_PATTERN), self.data)):
+ if self.preserve_phone_country_code and\
+ bool(re.search(re.compile(INTERNATIONAL_PHONE_PATTERN), self.data)):
+ code = re.search(re.compile(I_CODE_PATTERN), self.data).group()
+ phone = re.search(re.compile(PHONE_NUMBER_PATTERN), self.data).group()
+ text = re.split(re.compile(PHONE_PATTERN), self.data)
+ return text[0] + code + re.sub(re.compile(PHONE_NUMBER_PATTERN), \
+ ' [FILTERED]', phone)
+ return re.sub(re.compile(PHONE_PATTERN), '[PHONE]', self.data)
+ return self.data
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, value):
+ return bool(re.match(re.compile(EMAIL_PATTERN + '$'), value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.match(re.compile(HOST_NAME_PATTERN + '$'), value))
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(re.match(re.compile(PHONE_PATTERN + '$'), value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.match(re.compile(IP_PATTERN + '$'), value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(re.match(re.compile(NUMBER_PATTERN + '$'), value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.match(re.compile(INTEGER_PATTERN + '$'), value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.match(re.compile(DATE_PATTERN + '$'), value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.match(re.compile(TIME_PATTERN + '$'), value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return bool(re.match(re.compile(DATE_TIME_PATTERN + '$'), value))

Милица обнови решението на 22.04.2014 20:20 (преди над 10 години)

import re
HOST_NAME_PATTERN = '([\da-zA-Z][\da-zA-Z-]{,61}[\da-zA-Z]\.|[\da-zA-Z]\.)+'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
NAME_PATTERN = '[\da-zA-z][\da-zA-z-_\+\.]{,200}'
EMAIL_PATTERN = NAME_PATTERN + '@' + HOST_NAME_PATTERN
I_CODE_PATTERN = '(0{2}|\+)[1-9][0-9]{,2}'
-PHONE_NUMBER_PATTERN = '(([\s\-\(\)]){,2}[0-9]){5,10}'
+PHONE_NUMBER_PATTERN = '(([\s\-\(\)]){,2}[0-9]){6,11}'
LOCAL_PHONE_PATTERN = '0([\s\-\(\)])?[1-9]' + PHONE_NUMBER_PATTERN
INTERNATIONAL_PHONE_PATTERN = I_CODE_PATTERN + PHONE_NUMBER_PATTERN
PHONE_PATTERN = '(' + LOCAL_PHONE_PATTERN + '|' + \
INTERNATIONAL_PHONE_PATTERN + ')'
IP_PATTERN = '((1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}'\
'(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])'
NUMBER_PATTERN = '-?(0|[1-9][0-9]*)((.|,)[0-9]+)?'
INTEGER_PATTERN = '-?(0|[1-9][0-9]*)'
DATE_PATTERN = '[0-9]{4}-(0[1-9]|1[1-2])-([1-2][0-9]|3[0-1]|0[1-9])'
TIME_PATTERN = '([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]'
DATE_TIME_PATTERN = DATE_PATTERN + '(\s|T)' + TIME_PATTERN
class PrivacyFilter:
def __init__(self, text):
self.data = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
- if bool(re.search(re.compile(EMAIL_PATTERN), self.data)):
+ if bool(re.search(re.compile('(\s|^)' + EMAIL_PATTERN + '(\s|$)'), self.data)):
if self.preserve_email_hostname or \
self.partially_preserve_email_username:
if self.partially_preserve_email_username and\
len(re.search(re.compile(NAME_PATTERN + '@'), self.data)\
.group()) > 7:
text = re.split(re.compile(EMAIL_PATTERN), self.data)
email = re.search(re.compile(EMAIL_PATTERN),\
self.data).group()
return text[0] + email[:3] + re.sub(r'[\da-zA-z-_+.]{,200}@', \
'[FILTERED]@', email[3:])
return re.sub(re.compile(NAME_PATTERN + '@'), '[FILTERED]@', \
self.data)
return re.sub(re.compile(EMAIL_PATTERN), '[EMAIL]', self.data)
- if bool(re.search(re.compile(PHONE_PATTERN), self.data)):
+ if bool(re.search(re.compile('(\s|^)' + PHONE_PATTERN + '(\s|$)'), self.data)):
if self.preserve_phone_country_code and\
bool(re.search(re.compile(INTERNATIONAL_PHONE_PATTERN), self.data)):
- code = re.search(re.compile(I_CODE_PATTERN), self.data).group()
+ code = re.search(re.compile(I_CODE_PATTERN ), self.data).group()
phone = re.search(re.compile(PHONE_NUMBER_PATTERN), self.data).group()
text = re.split(re.compile(PHONE_PATTERN), self.data)
return text[0] + code + re.sub(re.compile(PHONE_NUMBER_PATTERN), \
' [FILTERED]', phone)
return re.sub(re.compile(PHONE_PATTERN), '[PHONE]', self.data)
return self.data
class Validations:
@classmethod
def is_email(cls, value):
return bool(re.match(re.compile(EMAIL_PATTERN + '$'), value))
@classmethod
def is_hostname(cls, value):
return bool(re.match(re.compile(HOST_NAME_PATTERN + '$'), value))
@classmethod
def is_phone(cls, value):
return bool(re.match(re.compile(PHONE_PATTERN + '$'), value))
@classmethod
def is_ip_address(cls, value):
return bool(re.match(re.compile(IP_PATTERN + '$'), value))
@classmethod
def is_number(cls, value):
return bool(re.match(re.compile(NUMBER_PATTERN + '$'), value))
@classmethod
def is_integer(cls, value):
return bool(re.match(re.compile(INTEGER_PATTERN + '$'), value))
@classmethod
def is_date(cls, value):
return bool(re.match(re.compile(DATE_PATTERN + '$'), value))
@classmethod
def is_time(cls, value):
return bool(re.match(re.compile(TIME_PATTERN + '$'), value))
@classmethod
def is_datetime(cls, value):
return bool(re.match(re.compile(DATE_TIME_PATTERN + '$'), value))