Решение на Регулярни изрази от Димитър Мутафчиев

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

Към профила на Димитър Мутафчиев

Резултати

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

Код

import re
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
def filterPhone(self, text, preserve_phone_country_code):
def replacePhone(matchobj):
if not preserve_phone_country_code:
return DELETE_PHONE
phone = matchobj.group(0)
country_code_matcher = re.match(r'^(0[1-9]*|(00|\+)([1-9][0-9]{,2}))', phone)
country_code = country_code_matcher.group(0)
return country_code + " " + FILTERED
prefix = r'(0[1-9]*|(\b00|[+])([1-9][0-9]{,2}))'
internal = r'([ ()-]{0,2}[0-9]){6,11}'
pattern = prefix + internal
return re.sub(pattern, replacePhone, text)
def filterEmail(self, text, preserve_email_hostname, partially_preserve_email_username):
def replaceEmail(matchobj):
email = matchobj.group(0)
split_email = email.rsplit('@', 1)
if len(split_email) < 2:
return False
mailbox = split_email[0]
domain = split_email[1]
if partially_preserve_email_username:
if len(mailbox) < 6:
mailbox = FILTERED
else:
mailbox = mailbox[0:3] + FILTERED
return mailbox + "@" + domain
elif preserve_email_hostname:
mailbox = FILTERED
return mailbox + "@" + domain
return DELETE_EMAIL
mailbox = r'[A-Za-z0-9](\w|[_+.-]){,200}'
tld = r'([A-Za-z]{2,3}(.[A-Za-z]{2,3})?)'
domain = r'(\w|(\w)(\w|-){,61}(\w)).'
subdomains = r'((\w|(\w)(\w|-){,61}(\w)).)*'
domain_name = subdomains + domain + tld
pattern = mailbox + r'@' + domain_name
return re.sub(pattern, replaceEmail, text)
def filtered(self):
filtered_result = self.filterEmail(self.text, self.preserve_email_hostname, self.partially_preserve_email_username)
return self.filterPhone(filtered_result, self.preserve_phone_country_code)
DELETE_PHONE = "[PHONE]"
FILTERED = "[FILTERED]"
DELETE_EMAIL = "[EMAIL]"
class Validations:
@classmethod
def is_phone(cls, value):
prefix = r'(0[1-9]*|(\b00|[+])([1-9][0-9]{,2}))'
internal = r'([ ()-]{0,2}[0-9]){6,11}'
m = re.match(r'^' + prefix + internal + r'$', value)
return m is not None
@classmethod
def is_email(cls, value):
split_value = value.rsplit('@', 1)
if len(split_value) < 2:
return False
mailbox = split_value[0]
domain = split_value[1]
m = re.match(r'^[A-Za-z0-9](\w|[_+.-]){,200}$', mailbox)
return m is not None and cls.is_hostname(domain)
@classmethod
def is_hostname(cls, value):
tld = r'([A-Za-z]{2,3}(\.[A-Za-z]{2,3})?)'
domain = r'(([0-9A-Za-z])([0-9A-Za-z]|-){,61}([0-9A-Za-z]))\.'
subdomains = r'((([0-9A-Za-z])([0-9A-Za-z]|-){,61}([0-9A-Za-z]))\.)*'
m = re.match(r'^' + subdomains + domain + tld + r'$', value)
return m is not None
@classmethod
def is_ip_address(cls, value):
m = re.match(r'^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$', value)
return m is not None and (0 <= int(m.group(1)) <= 255) and (0 <= int(m.group(2)) <= 255) and (0 <= int(m.group(3)) <= 255) and (0 <= int(m.group(4)) <= 255)
@classmethod
def is_number(cls, value):
m = re.match(r'^-?(0{1}|[1-9]\d+)(.\d+)?$', value)
return m is not None
@classmethod
def is_integer(cls, value):
return cls.is_number(value) and value.find('.') == -1
@classmethod
def is_date(cls, value):
m = re.match(r'^(\d{4})-(\d{2})-(\d{2})$', value)
return m is not None and (0 <= int(m.group(1)) <= 9999) and (1 <= int(m.group(2)) <= 12) and (1 <= int(m.group(3)) <= 31)
@classmethod
def is_time(cls, value):
m = re.match(r'^(\d{2}):(\d{2}):(\d{2})$', value)
return m is not None and (0 <= int(m.group(1)) <= 23) and (0 <= int(m.group(2)) <= 59) and (0 <= int(m.group(3)) <= 59)
@classmethod
def is_datetime(cls, value):
if len(value) != 19:
return False
date = value[:10]
time = value[11:]
return value[10] == ' ' and cls.is_date(date) and cls.is_time(time)

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

...FF..F..F.....F.....FF..FF...FF.F.F..
======================================================================
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-6ld2dz/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_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-6ld2dz/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '0005551234569' != '[PHONE]9'
- 0005551234569
+ [PHONE]9


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


======================================================================
FAIL: test_separates_preserved_country_code_from_filtered_phone_with_a_space (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-6ld2dz/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
AssertionError: 'Phone: 0025 [FILTERED]' != 'Phone: 0 [FILTERED]5'
- Phone: 0025 [FILTERED]
?         ---
+ Phone: 0 [FILTERED]5
?                    +


======================================================================
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-6ld2dz/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-6ld2dz/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-6ld2dz/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-6ld2dz/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-6ld2dz/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

======================================================================
FAIL: test_validates_datetime_values (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-6ld2dz/test.py", line 278, in test_validates_datetime_values
    self.assertTrue(solution.Validations.is_datetime('2012-11-19T19:00:00'))
AssertionError: False is not true

======================================================================
FAIL: test_validates_hostnames (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-6ld2dz/test.py", line 169, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('1.2.3.4.xip.io'))
AssertionError: False is not true

======================================================================
FAIL: test_validates_more_complex_integers (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-6ld2dz/test.py", line 227, in test_validates_more_complex_integers
    self.assertTrue(solution.Validations.is_integer('9'))
AssertionError: False is not true

======================================================================
FAIL: test_validates_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-6ld2dz/test.py", line 197, in test_validates_numbers
    self.assertTrue(solution.Validations.is_number('9'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 39 tests in 0.045s

FAILED (failures=13)

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

Димитър обнови решението на 22.04.2014 02:15 (преди над 10 години)

+import re
+
+class PrivacyFilter:
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+
+ def filterPhone(self, text, preserve_phone_country_code):
+
+ def replacePhone(matchobj):
+ if not preserve_phone_country_code:
+ return DELETE_PHONE
+
+ phone = matchobj.group(0)
+ country_code_matcher = re.match(r'^(0[1-9]*|(00|\+)([1-9][0-9]{,2}))', phone)
+ country_code = country_code_matcher.group(0)
+ return country_code + " " + FILTERED
+
+
+ prefix = r'(0[1-9]*|(\b00|[+])([1-9][0-9]{,2}))'
+ internal = r'([ ()-]{0,2}[0-9]){6,11}'
+ pattern = prefix + internal
+
+ return re.sub(pattern, replacePhone, text)
+
+
+ def filterEmail(self, text, preserve_email_hostname, partially_preserve_email_username):
+
+ def replaceEmail(matchobj):
+ email = matchobj.group(0)
+ split_email = email.rsplit('@', 1)
+ if len(split_email) < 2:
+ return False
+ mailbox = split_email[0]
+ domain = split_email[1]
+
+ if partially_preserve_email_username:
+ if len(mailbox) < 6:
+ mailbox = FILTERED
+ else:
+ mailbox = mailbox[0:3] + FILTERED
+ return mailbox + "@" + domain
+ elif preserve_email_hostname:
+ mailbox = FILTERED
+ return mailbox + "@" + domain
+
+ return DELETE_EMAIL
+
+
+ mailbox = r'[A-Za-z0-9](\w|[_+.-]){,200}'
+
+ tld = r'([A-Za-z]{2,3}(.[A-Za-z]{2,3})?)'
+ domain = r'(\w|(\w)(\w|-){,61}(\w)).'
+ subdomains = r'((\w|(\w)(\w|-){,61}(\w)).)*'
+ domain_name = subdomains + domain + tld
+
+ pattern = mailbox + r'@' + domain_name
+
+ return re.sub(pattern, replaceEmail, text)
+
+ def filtered(self):
+ filtered_result = self.filterEmail(self.text, self.preserve_email_hostname , self.partially_preserve_email_username);
+ return self.filterPhone(filtered_result, self.preserve_phone_country_code);
+
+
+DELETE_PHONE = "[PHONE]"
+FILTERED = "[FILTERED]"
+DELETE_EMAIL = "[EMAIL]"
+
+
+class Validations:
+ @classmethod
+ def is_phone(cls, value):
+ prefix = r'(0[1-9]*|(\b00|[+])([1-9][0-9]{,2}))'
+ internal = r'([ ()-]{0,2}[0-9]){6,11}'
+ m = re.match(r'^' + prefix + internal + r'$', value)
+ return m != None
+
+ @classmethod
+ def is_email(cls, value):
+ split_value = value.rsplit('@', 1)
+ if len(split_value) < 2:
+ return False
+ mailbox = split_value[0]
+ domain = split_value[1]
+ m = re.match(r'^[A-Za-z0-9](\w|[_+.-]){,200}$', mailbox)
+ return m != None and cls.is_hostname(domain)
+
+ @classmethod
+ def is_hostname(cls, value):
+ tld = r'([A-Za-z]{2,3}(\.[A-Za-z]{2,3})?)'
+ domain = r'(([0-9A-Za-z])([0-9A-Za-z]|-){,61}([0-9A-Za-z]))\.'
+ subdomains = r'((([0-9A-Za-z])([0-9A-Za-z]|-){,61}([0-9A-Za-z]))\.)*'
+ m = re.match(r'^' + subdomains + domain + tld + r'$', value)
+ return m != None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ m = re.match(r'^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$', value)
+ return m != None and (0 <= int(m.group(1)) <= 255) and (0 <= int(m.group(2)) <= 255) and (0 <= int(m.group(3)) <= 255) and (0 <= int(m.group(4)) <= 255)
+
+ @classmethod
+ def is_number(cls, value):
+ m = re.match(r'^-?(0{1}|[1-9]\d+)(.\d+)?$', value)
+ return m != None
+
+ @classmethod
+ def is_integer(cls, value):
+ return cls.is_number(value) and value.find('.') == -1
+
+ @classmethod
+ def is_date(cls, value):
+ m = re.match(r'^(\d{4})-(\d{2})-(\d{2})$', value)
+ return m != None and (0 <= int(m.group(1)) <= 9999) and (1 <= int(m.group(2)) <= 12) and (1 <= int(m.group(3)) <= 31)
+
+ @classmethod
+ def is_time(cls, value):
+ m = re.match(r'^(\d{2}):(\d{2}):(\d{2})$', value)
+ return m != None and (0 <= int(m.group(1)) <= 23) and (0 <= int(m.group(2)) <= 59) and (0 <= int(m.group(3)) <= 59)
+
+ @classmethod
+ def is_datetime(cls, value):
+ if len(value) != 19:
+ return False
+ date = value[:10]
+ time = value[11:]
+ return value[10] == ' ' and cls.is_date(date) and cls.is_time(time)

Димитър обнови решението на 22.04.2014 02:20 (преди над 10 години)

import re
+
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
def filterPhone(self, text, preserve_phone_country_code):
def replacePhone(matchobj):
if not preserve_phone_country_code:
return DELETE_PHONE
-
+
phone = matchobj.group(0)
country_code_matcher = re.match(r'^(0[1-9]*|(00|\+)([1-9][0-9]{,2}))', phone)
country_code = country_code_matcher.group(0)
return country_code + " " + FILTERED
-
prefix = r'(0[1-9]*|(\b00|[+])([1-9][0-9]{,2}))'
internal = r'([ ()-]{0,2}[0-9]){6,11}'
pattern = prefix + internal
return re.sub(pattern, replacePhone, text)
-
def filterEmail(self, text, preserve_email_hostname, partially_preserve_email_username):
def replaceEmail(matchobj):
email = matchobj.group(0)
split_email = email.rsplit('@', 1)
if len(split_email) < 2:
return False
mailbox = split_email[0]
domain = split_email[1]
if partially_preserve_email_username:
if len(mailbox) < 6:
mailbox = FILTERED
else:
mailbox = mailbox[0:3] + FILTERED
return mailbox + "@" + domain
elif preserve_email_hostname:
mailbox = FILTERED
return mailbox + "@" + domain
return DELETE_EMAIL
-
mailbox = r'[A-Za-z0-9](\w|[_+.-]){,200}'
tld = r'([A-Za-z]{2,3}(.[A-Za-z]{2,3})?)'
domain = r'(\w|(\w)(\w|-){,61}(\w)).'
subdomains = r'((\w|(\w)(\w|-){,61}(\w)).)*'
domain_name = subdomains + domain + tld
pattern = mailbox + r'@' + domain_name
return re.sub(pattern, replaceEmail, text)
def filtered(self):
- filtered_result = self.filterEmail(self.text, self.preserve_email_hostname , self.partially_preserve_email_username);
- return self.filterPhone(filtered_result, self.preserve_phone_country_code);
+ filtered_result = self.filterEmail(self.text, self.preserve_email_hostname, self.partially_preserve_email_username)
+ return self.filterPhone(filtered_result, self.preserve_phone_country_code)
DELETE_PHONE = "[PHONE]"
FILTERED = "[FILTERED]"
DELETE_EMAIL = "[EMAIL]"
class Validations:
@classmethod
def is_phone(cls, value):
prefix = r'(0[1-9]*|(\b00|[+])([1-9][0-9]{,2}))'
internal = r'([ ()-]{0,2}[0-9]){6,11}'
m = re.match(r'^' + prefix + internal + r'$', value)
- return m != None
+ return m is not None
@classmethod
def is_email(cls, value):
split_value = value.rsplit('@', 1)
if len(split_value) < 2:
return False
mailbox = split_value[0]
domain = split_value[1]
m = re.match(r'^[A-Za-z0-9](\w|[_+.-]){,200}$', mailbox)
- return m != None and cls.is_hostname(domain)
+ return m is not None and cls.is_hostname(domain)
@classmethod
def is_hostname(cls, value):
tld = r'([A-Za-z]{2,3}(\.[A-Za-z]{2,3})?)'
domain = r'(([0-9A-Za-z])([0-9A-Za-z]|-){,61}([0-9A-Za-z]))\.'
subdomains = r'((([0-9A-Za-z])([0-9A-Za-z]|-){,61}([0-9A-Za-z]))\.)*'
m = re.match(r'^' + subdomains + domain + tld + r'$', value)
- return m != None
+ return m is not None
@classmethod
def is_ip_address(cls, value):
m = re.match(r'^(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})$', value)
- return m != None and (0 <= int(m.group(1)) <= 255) and (0 <= int(m.group(2)) <= 255) and (0 <= int(m.group(3)) <= 255) and (0 <= int(m.group(4)) <= 255)
+ return m is not None and (0 <= int(m.group(1)) <= 255) and (0 <= int(m.group(2)) <= 255) and (0 <= int(m.group(3)) <= 255) and (0 <= int(m.group(4)) <= 255)
@classmethod
def is_number(cls, value):
- m = re.match(r'^-?(0{1}|[1-9]\d+)(.\d+)?$', value)
- return m != None
+ m = re.match(r'^-?(0{1}|[1-9]\d+)(.\d+)?$', value)
+ return m is not None
@classmethod
def is_integer(cls, value):
return cls.is_number(value) and value.find('.') == -1
@classmethod
def is_date(cls, value):
m = re.match(r'^(\d{4})-(\d{2})-(\d{2})$', value)
- return m != None and (0 <= int(m.group(1)) <= 9999) and (1 <= int(m.group(2)) <= 12) and (1 <= int(m.group(3)) <= 31)
+ return m is not None and (0 <= int(m.group(1)) <= 9999) and (1 <= int(m.group(2)) <= 12) and (1 <= int(m.group(3)) <= 31)
@classmethod
def is_time(cls, value):
m = re.match(r'^(\d{2}):(\d{2}):(\d{2})$', value)
- return m != None and (0 <= int(m.group(1)) <= 23) and (0 <= int(m.group(2)) <= 59) and (0 <= int(m.group(3)) <= 59)
+ return m is not None and (0 <= int(m.group(1)) <= 23) and (0 <= int(m.group(2)) <= 59) and (0 <= int(m.group(3)) <= 59)
@classmethod
def is_datetime(cls, value):
if len(value) != 19:
return False
date = value[:10]
time = value[11:]
return value[10] == ' ' and cls.is_date(date) and cls.is_time(time)