Решение на Регулярни изрази от Атанас Димитров

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

Към профила на Атанас Димитров

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 29 успешни тест(а)
  • 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):
email_name = r'\b\w[\w\+\-_\.]{,200}\@'
hostname = r'(\w[\w\-]{0,61}\w\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
email_pattern = email_name + hostname
filtered_text = self.text
while re.search(email_pattern, filtered_text):
email = re.search(email_pattern, filtered_text).group()
pivot = email.index('@')
if self.partially_preserve_email_username:
if pivot >= 6:
change = email[:3] + '[FILTERED]' + email[pivot:]
else:
change = '[FILTERED]' + email[pivot:]
elif self.preserve_email_hostname:
change = '[FILTERED]' + email[pivot:]
else:
change = '[EMAIL]'
filtered_text = filtered_text.replace(email, change, 1)
local = r'\b(0[ \-()]{,2}[1-9]|0[ \-()]{1,2}\d)([ \-()]{,2}\d){5,10}\b'
international = r'((00|\+)[1-9]\d{0,2})([ \-()]{,2}\d){6,11}\b'
phone_pattern = local + r'|' + international
while re.search(phone_pattern, filtered_text):
phone = re.search(phone_pattern, filtered_text).group()
if Validations.is_local_phone(phone):
change = '[PHONE]'
elif self.preserve_phone_country_code:
change = re.search(international, phone).group(1) + \
' [FILTERED]'
else:
change = '[PHONE]'
filtered_text = filtered_text.replace(phone, change, 1)
return filtered_text
class Validations():
def is_email(value):
pattern = r'\w[\w\+\-_\.]{,200}\@'
start_of_hostname = value.index('@') + 1
hostname = value[start_of_hostname:]
return bool(re.match(pattern, value)) and \
Validations.is_hostname(hostname)
def is_phone(value):
return Validations.is_local_phone(value) or \
Validations.is_international_phone(value)
def is_local_phone(value):
local = r'(0[ \-()]{,2}[1-9]|0[ \-()]{1,2}\d)([ \-()]{,2}\d){5,10}$'
return bool(re.match(local, value))
def is_international_phone(value):
international = r'(00|\+)[1-9]\d{0,2}([ \-()]{,2}\d){6,11}$'
return bool(re.match(international, value))
def is_hostname(value):
pattern = r'(\w[\w\-]{0,61}\w\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
return bool(re.match(pattern, value))
def is_ip_address(value):
if re.match(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', value):
for number in value.split('.'):
if int(number) >= 256:
return False
return True
return False
def is_number(value):
pattern = r'0$|-0$|0\.\d+$|-0\.\d+$|[1-9]\d*\.\d+$|-[1-9]\d*\.\d+$|[1-9]\d*$'
return bool(re.match(pattern, value))
def is_integer(value):
return bool(re.match(r'0$|-0$|[1-9]\d*$|-[1-9]\d*$', value))
def is_date(value):
date = value.split('-')
return bool(re.match(r'\d{4}-\d{2}-\d{2}$', value)) and \
int(date[1]) >= 1 and int(date[1]) <= 12 and \
int(date[2]) >= 1 and int(date[2]) <= 31
def is_time(value):
time = value.split(':')
return bool(re.match(r'\d{2}:\d{2}:\d{2}$', value)) and \
int(time[0]) >= 0 and int(time[0]) <= 23 and \
int(time[1]) >= 0 and int(time[1]) <= 59 and \
int(time[2]) >= 0 and int(time[2]) <= 59
def is_datetime(value):
pattern = r'\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}$'
return bool(re.match(pattern, value)) and \
Validations.is_date(value[:10]) and \
Validations.is_time(value[11:])

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

...FF..F.......FF.....FF..FF....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-12rww43/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-12rww43/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-12rww43/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[EMAIL]' != 'larodi@x.com'
- [EMAIL]
+ larodi@x.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-12rww43/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_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-12rww43/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-12rww43/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-12rww43/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-12rww43/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-12rww43/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_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-12rww43/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

----------------------------------------------------------------------
Ran 39 tests in 0.059s

FAILED (failures=10)

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

Атанас обнови решението на 19.04.2014 15:07 (преди около 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):
+ email_name = r'\b\w[\w\+\-_\.]{,200}@'
+ hostname = r'(\w[\w\-]{0,61}\w\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
+ email_pattern = email_name + hostname
+ filtered_text = self.text
+ while re.search(email_pattern, filtered_text):
+ email = re.search(email_pattern, filtered_text).group()
+ pivot = email.index('@')
+ if self.partially_preserve_email_username:
+ if pivot >= 6:
+ change = email[:3] + '[FILTERED]' + email[pivot:]
+ else:
+ change = '[FILTERED]' + email[pivot:]
+ elif self.preserve_email_hostname:
+ change = '[FILTERED]' + email[pivot:]
+ else:
+ change = '[EMAIL]'
+ filtered_text = filtered_text.replace(email, change, 1)
+
+ local = r'\b(0[ \-()]{,2}[1-9]|0[ \-()]{1,2}\d)([ \-()]{,2}\d){5,10}\b'
+ international = r'((00|\+)[1-9]\d{0,2})([ \-()]{,2}\d){6,11}\b'
+ phone_pattern = local + r'|' + international
+ while re.search(phone_pattern, filtered_text):
+ phone = re.search(phone_pattern, filtered_text).group()
+ if Validations.is_local_phone(phone):
+ change = '[PHONE]'
+ elif self.preserve_phone_country_code:
+ change = re.search(international, phone).group(1) + \
+ ' [FILTERED]'
+ else:
+ change = '[PHONE]'
+ filtered_text = filtered_text.replace(phone, change, 1)
+ return filtered_text
+
+
+class Validations():
+ def is_email(value):
+ pattern = r'\w[\w\+\-_\.]{,200}@'
+ start_of_hostname = value.index('@') + 1
+ hostname = value[start_of_hostname:]
+ return bool(re.match(pattern, value)) and \
+ Validations.is_hostname(hostname)
+
+ def is_phone(value):
+ return Validations.is_local_phone(value) or \
+ Validations.is_international_phone(value)
+
+ def is_local_phone(value):
+ local = r'(0[ \-()]{,2}[1-9]|0[ \-()]{1,2}\d)([ \-()]{,2}\d){5,10}$'
+ return bool(re.match(local, value))
+
+ def is_international_phone(value):
+ international = r'(00|\+)[1-9]\d{0,2}([ \-()]{,2}\d){6,11}$'
+ return bool(re.match(international, value))
+
+ def is_hostname(value):
+ pattern = r'(\w[\w\-]{0,61}\w\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
+ return bool(re.match(pattern, value))
+
+ def is_ip_address(value):
+ if re.match(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', value):
+ for number in value.split('.'):
+ if int(number) >= 256:
+ return False
+ return True
+ return False
+
+ def is_number(value):
+ pattern = r'0$|-0$|0\.\d+$|-0\.\d+$|[1-9]\d*\.\d+$|-[1-9]\d*\.\d+$|[1-9]\d*$'
+ return bool(re.match(pattern, value))
+
+ def is_integer(value):
+ return bool(re.match(r'0$|-0$|[1-9]\d*$|-[1-9]\d*$', value))
+
+ def is_date(value):
+ date = value.split('-')
+ return bool(re.match(r'\d{4}-\d{2}-\d{2}$', value)) and \
+ int(date[1]) >= 1 and int(date[1]) <= 12 and \
+ int(date[2]) >= 1 and int(date[2]) <= 31
+
+ def is_time(value):
+ time = value.split(':')
+ return bool(re.match(r'\d{2}:\d{2}:\d{2}$', value)) and \
+ int(time[0]) >= 0 and int(time[0]) <= 23 and \
+ int(time[1]) >= 0 and int(time[1]) <= 59 and \
+ int(time[2]) >= 0 and int(time[2]) <= 59
+
+ def is_datetime(value):
+ pattern = r'\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}$'
+ return bool(re.match(pattern, value))

Атанас обнови решението на 22.04.2014 22:24 (преди около 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):
- email_name = r'\b\w[\w\+\-_\.]{,200}@'
+ email_name = r'\b\w[\w\+\-_\.]{,200}\@'
hostname = r'(\w[\w\-]{0,61}\w\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
email_pattern = email_name + hostname
filtered_text = self.text
while re.search(email_pattern, filtered_text):
email = re.search(email_pattern, filtered_text).group()
pivot = email.index('@')
if self.partially_preserve_email_username:
if pivot >= 6:
change = email[:3] + '[FILTERED]' + email[pivot:]
else:
change = '[FILTERED]' + email[pivot:]
elif self.preserve_email_hostname:
change = '[FILTERED]' + email[pivot:]
else:
change = '[EMAIL]'
filtered_text = filtered_text.replace(email, change, 1)
local = r'\b(0[ \-()]{,2}[1-9]|0[ \-()]{1,2}\d)([ \-()]{,2}\d){5,10}\b'
international = r'((00|\+)[1-9]\d{0,2})([ \-()]{,2}\d){6,11}\b'
phone_pattern = local + r'|' + international
while re.search(phone_pattern, filtered_text):
phone = re.search(phone_pattern, filtered_text).group()
if Validations.is_local_phone(phone):
change = '[PHONE]'
elif self.preserve_phone_country_code:
change = re.search(international, phone).group(1) + \
' [FILTERED]'
else:
change = '[PHONE]'
filtered_text = filtered_text.replace(phone, change, 1)
return filtered_text
class Validations():
def is_email(value):
- pattern = r'\w[\w\+\-_\.]{,200}@'
+ pattern = r'\w[\w\+\-_\.]{,200}\@'
start_of_hostname = value.index('@') + 1
hostname = value[start_of_hostname:]
return bool(re.match(pattern, value)) and \
Validations.is_hostname(hostname)
def is_phone(value):
return Validations.is_local_phone(value) or \
Validations.is_international_phone(value)
def is_local_phone(value):
local = r'(0[ \-()]{,2}[1-9]|0[ \-()]{1,2}\d)([ \-()]{,2}\d){5,10}$'
return bool(re.match(local, value))
def is_international_phone(value):
international = r'(00|\+)[1-9]\d{0,2}([ \-()]{,2}\d){6,11}$'
return bool(re.match(international, value))
def is_hostname(value):
pattern = r'(\w[\w\-]{0,61}\w\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
return bool(re.match(pattern, value))
def is_ip_address(value):
if re.match(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', value):
for number in value.split('.'):
if int(number) >= 256:
return False
return True
return False
def is_number(value):
pattern = r'0$|-0$|0\.\d+$|-0\.\d+$|[1-9]\d*\.\d+$|-[1-9]\d*\.\d+$|[1-9]\d*$'
return bool(re.match(pattern, value))
def is_integer(value):
return bool(re.match(r'0$|-0$|[1-9]\d*$|-[1-9]\d*$', value))
def is_date(value):
date = value.split('-')
return bool(re.match(r'\d{4}-\d{2}-\d{2}$', value)) and \
int(date[1]) >= 1 and int(date[1]) <= 12 and \
int(date[2]) >= 1 and int(date[2]) <= 31
def is_time(value):
time = value.split(':')
return bool(re.match(r'\d{2}:\d{2}:\d{2}$', value)) and \
int(time[0]) >= 0 and int(time[0]) <= 23 and \
int(time[1]) >= 0 and int(time[1]) <= 59 and \
int(time[2]) >= 0 and int(time[2]) <= 59
def is_datetime(value):
pattern = r'\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}$'
- return bool(re.match(pattern, value))
+ return bool(re.match(pattern, value)) and \
+ Validations.is_date(value[:10]) and \
+ Validations.is_time(value[11:])