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

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

Към профила на Венцислав Велков

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 12 успешни тест(а)
  • 27 неуспешни тест(а)

Код

import re
class Validations:
@staticmethod
def is_email(value):
pattern = r'^\w(\w|_|\+|\.|-){0,200}@^\w(\w|-){1,62}[^-].' \
+ r'[a-zA-Z]{2,3}(.[a-zA-Z]{2,3})?$'
if re.search(pattern, value):
return True
return False
@staticmethod
def is_phone(value):
pat1 = r'^\s0( |-|\(|\)){0,2}[123456789](( |-|\(|\)){0,2}|\d){6,11}$'
pat2 = r'^\b00(( |-|\(|\)){0,2}|\d){6,11}$'
pat3 = r'^+[123456789]\d{0,2}(( |-|\(|\)){0,2}|\d){6,11}$'
if re.search(pat1, value) or \
re.search(pat2, value) or \
re.search(pat3, value):
return True
return False
@staticmethod
def is_hostname(value):
pattern = r'^\w(\w|-){1,62}[^-]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
if re.search(pattern, value):
return True
return False
@staticmethod
def is_ip_address(value):
pattern = r'^((\d|\d\d|1\d\d|2[01234]\d|25[012345])\.)' \
+ r'{3,3}(\d|\d\d|1\d\d|2[01234]\d|25[012345])$'
if re.search(pattern, value):
return True
return False
@staticmethod
def is_number(value):
pattern = r'^-?(0$|\d+$|(\d+\.\d+$))'
if re.search(pattern, value):
return True
return False
@staticmethod
def is_integer(value):
pattern = r'^-?\d+$'
if re.search(pattern, value):
return True
return False
@staticmethod
def is_date(value):
pattern = r'^\d{4,4}-([123456789]|1[12])-([123456789]|[12]\d|3[01])$'
if re.search(pattern, value):
return True
return False
@staticmethod
def is_time(value):
pattern = r'^([01]\d|2[0123]):[012345]\d:[012345]\d$'
if re.search(pattern, value):
return True
return False
@staticmethod
def is_datetime(value):
datetime = value.split()
if len(datetime) == 2:
return Validations.is_date(datetime[0]) \
and Validations.is_time(datetime[1])
elif len(value.split('T')) == 2:
return Validations.is_date(value.split('T')[0]) \
and Validations.is_time(value.split('T')[1])
else:
return False
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
def filter_emails(self):
pattern = r'^\w(\w|_|\+|\.|-){0,200}@^\w(\w|-){1,62}[^-].' \
+ r'[a-zA-Z]{2,3}(.[a-zA-Z]{2,3})?$'
self.text = re.subn(pattern, email_sub, self.text)[0]
def filter_phones():
pat1 = r'^\s0( |-|\(|\)){0,2}[123456789](( |-|\(|\)){0,2}|\d){6,11}$'
pat2 = r'^\b00(( |-|\(|\)){0,2}|\d){6,11}$'
pat3 = r'^+[123456789]\d{0,2}(( |-|\(|\)){0,2}|\d){6,11}$'
self.text = re.subn(pat1, phone_sub, self.text)[0]
self.text = re.subn(pat2, phone_sub, self.text)[0]
self.text = re.subn(pat3, phone_sub, self.text)[0]
def filtered(self):
self.filter_emails()
self.filter_phones()
return self.text
def phone_sub(matchobj):
if preserve_phone_country_code:
country_code = re.search(r'(00|+)', matchobj.group())
if country_code:
return country_code.group() + '[PHONE]'
return '[PHONE]'
def email_sub(matchobj):
if preserve_email_hostname:
hostname = re.search(r'@\.+', matchobj.group())
return '[FILTERED]' + hostname.group()
return '[EMAIL]'

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

EEEEEEEEEEEEFFFFF....EFF..FFF...F.FF.E.
======================================================================
ERROR: test_allows_email_hostname_to_be_preserved (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-1wb1umx/test.py", line 54, in test_allows_email_hostname_to_be_preserved
    self.assertEqual('[FILTERED]@example.com', self.filter_email_usernames('someone@example.com'))
  File "/tmp/d20140513-11348-1wb1umx/test.py", line 11, in filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: test_allows_email_usernames_to_be_partially_preserved (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-1wb1umx/test.py", line 58, in test_allows_email_usernames_to_be_partially_preserved
    self.assertEqual('som[FILTERED]@example.com', self.partially_filter_email_usernames('someone@example.com'))
  File "/tmp/d20140513-11348-1wb1umx/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: test_does_not_brake_with_unicode (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-1wb1umx/test.py", line 64, in test_does_not_brake_with_unicode
    self.assertEqual('За връзка: [FILTERED]@example.com', self.partially_filter_email_usernames('За връзка: me@example.com'))
  File "/tmp/d20140513-11348-1wb1umx/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: 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-1wb1umx/test.py", line 48, in test_does_not_filter_invalid_emails
    self.assertEqual(text, solution.PrivacyFilter(text).filtered())
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: 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-1wb1umx/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: 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-1wb1umx/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: test_filters_whole_email_usernames_if_too_short (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-1wb1umx/test.py", line 61, in test_filters_whole_email_usernames_if_too_short
    self.assertEqual('[FILTERED]@example.com', self.partially_filter_email_usernames('me@example.com'))
  File "/tmp/d20140513-11348-1wb1umx/test.py", line 16, in partially_filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: 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-1wb1umx/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: test_obfuscates_simple_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-1wb1umx/test.py", line 24, in test_obfuscates_simple_emails
    self.assertEqual('Contact: [EMAIL]', solution.PrivacyFilter('Contact: someone@example.com').filtered())
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: test_preserves_whitespace_around_phones (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-1wb1umx/test.py", line 89, in test_preserves_whitespace_around_phones
    self.assertEqual(' [PHONE] or...', solution.PrivacyFilter(' +359881212-12-1 2 or...').filtered())
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: 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-1wb1umx/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: test_works_with_blank_or_whitespace_strings_and_preserves_whitespace (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-1wb1umx/test.py", line 19, in test_works_with_blank_or_whitespace_strings_and_preserves_whitespace
    self.assertEqual('', solution.PrivacyFilter('').filtered())
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 103, in filtered
    self.filter_phones()
TypeError: filter_phones() takes 0 positional arguments but 1 was given

======================================================================
ERROR: test_does_not_break_on_phones_in_multiline_strings (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-1wb1umx/test.py", line 163, in test_does_not_break_on_phones_in_multiline_strings
    self.assertFalse(solution.Validations.is_phone("0885123123\nwat?"))
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 20, in is_phone
    re.search(pat3, value):
  File "/opt/python3.3/lib/python3.3/re.py", line 161, in search
    return _compile(pattern, flags).search(string)
  File "/opt/python3.3/lib/python3.3/functools.py", line 258, in wrapper
    result = user_function(*args, **kwds)
  File "/opt/python3.3/lib/python3.3/re.py", line 274, in _compile
    return sre_compile.compile(pattern, flags)
  File "/opt/python3.3/lib/python3.3/sre_compile.py", line 493, in compile
    p = sre_parse.parse(p, flags)
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 724, in parse
    p = _parse_sub(source, pattern, 0)
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 347, in _parse_sub
    itemsappend(_parse(source, state))
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 552, in _parse
    raise error("nothing to repeat")
sre_constants.error: nothing to repeat

======================================================================
ERROR: test_validates_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-1wb1umx/test.py", line 130, in test_validates_phone_numbers
    self.assertTrue(solution.Validations.is_phone('+35929555111'))
  File "/tmp/d20140513-11348-1wb1umx/solution.py", line 20, in is_phone
    re.search(pat3, value):
  File "/opt/python3.3/lib/python3.3/re.py", line 161, in search
    return _compile(pattern, flags).search(string)
  File "/opt/python3.3/lib/python3.3/functools.py", line 258, in wrapper
    result = user_function(*args, **kwds)
  File "/opt/python3.3/lib/python3.3/re.py", line 274, in _compile
    return sre_compile.compile(pattern, flags)
  File "/opt/python3.3/lib/python3.3/sre_compile.py", line 493, in compile
    p = sre_parse.parse(p, flags)
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 724, in parse
    p = _parse_sub(source, pattern, 0)
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 347, in _parse_sub
    itemsappend(_parse(source, state))
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 552, in _parse
    raise error("nothing to repeat")
sre_constants.error: nothing to repeat

======================================================================
FAIL: test_allows_huge_years_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-1wb1umx/test.py", line 246, in test_allows_huge_years_in_date_validation
    self.assertTrue(solution.Validations.is_date('9999-01-01'))
AssertionError: False is not true

======================================================================
FAIL: test_allows_validation_for_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-1wb1umx/test.py", line 105, in test_allows_validation_for_emails
    self.assertTrue(solution.Validations.is_email('foo@bar.com'))
AssertionError: False is not true

======================================================================
FAIL: test_allows_zero_years_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-1wb1umx/test.py", line 243, in test_allows_zero_years_in_date_validation
    self.assertTrue(solution.Validations.is_date('0000-01-01'))
AssertionError: False is not true

======================================================================
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-1wb1umx/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), valid)
AssertionError: False is not True

======================================================================
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-1wb1umx/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-1wb1umx/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-1wb1umx/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-1wb1umx/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-1wb1umx/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_returns_boolean_True_or_False (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-1wb1umx/test.py", line 109, in test_returns_boolean_True_or_False
    self.assertIs(solution.Validations.is_email('foo@bar.com'), True)
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-1wb1umx/test.py", line 167, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('some.long-subdomain.domain.co.ul'))
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-1wb1umx/test.py", line 225, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('00'))
AssertionError: True is not false

======================================================================
FAIL: test_validates_more_complex_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-1wb1umx/test.py", line 205, in test_validates_more_complex_numbers
    self.assertFalse(solution.Validations.is_number('00'))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 39 tests in 0.044s

FAILED (failures=13, errors=14)

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

Венцислав обнови решението на 23.04.2014 16:58 (преди над 10 години)

+import re
+
+
+class Validations:
+ @staticmethod
+ def is_email(value):
+ pattern = r'^\w(\w|_|\+|\.|-){0,200}@^\w(\w|-){1,62}[^-].' \
+ + r'[a-zA-Z]{2,3}(.[a-zA-Z]{2,3})?$'
+ if re.search(pattern, value):
+ return True
+ return False
+
+ @staticmethod
+ def is_phone(value):
+ pat1 = r'^\s0( |-|\(|\)){0,2}[123456789](( |-|\(|\)){0,2}|\d){6,11}$'
+ pat2 = r'^\b00(( |-|\(|\)){0,2}|\d){6,11}$'
+ pat3 = r'^+[123456789]\d{0,2}(( |-|\(|\)){0,2}|\d){6,11}$'
+ if re.search(pat1, value) or \
+ re.search(pat2, value) or \
+ re.search(pat3, value):
+ return True
+ return False
+
+ @staticmethod
+ def is_hostname(value):
+ pattern = r'^\w(\w|-){1,62}[^-]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
+ if re.search(pattern, value):
+ return True
+ return False
+
+ @staticmethod
+ def is_ip_address(value):
+ pattern = r'^((\d|\d\d|1\d\d|2[01234]\d|25[012345])\.)' \
+ + r'{3,3}(\d|\d\d|1\d\d|2[01234]\d|25[012345])$'
+ if re.search(pattern, value):
+ return True
+ return False
+
+ @staticmethod
+ def is_number(value):
+ pattern = r'^-?(0$|\d+$|(\d+\.\d+$))'
+ if re.search(pattern, value):
+ return True
+ return False
+
+ @staticmethod
+ def is_integer(value):
+ pattern = r'^-?\d+$'
+ if re.search(pattern, value):
+ return True
+ return False
+
+ @staticmethod
+ def is_date(value):
+ pattern = r'^\d{4,4}-([123456789]|1[12])-([123456789]|[12]\d|3[01])$'
+ if re.search(pattern, value):
+ return True
+ return False
+
+ @staticmethod
+ def is_time(value):
+ pattern = r'^([01]\d|2[0123]):[012345]\d:[012345]\d$'
+ if re.search(pattern, value):
+ return True
+ return False
+
+ @staticmethod
+ def is_datetime(value):
+ datetime = value.split()
+ if len(datetime) == 2:
+ return Validations.is_date(datetime[0]) \
+ and Validations.is_time(datetime[1])
+ elif len(value.split('T')) == 2:
+ return Validations.is_date(value.split('T')[0]) \
+ and Validations.is_time(value.split('T')[1])
+ else:
+ return False
+
+
+class PrivacyFilter:
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+
+ def filter_emails(self):
+ pattern = r'^\w(\w|_|\+|\.|-){0,200}@^\w(\w|-){1,62}[^-].' \
+ + r'[a-zA-Z]{2,3}(.[a-zA-Z]{2,3})?$'
+ self.text = re.subn(pattern, email_sub, self.text)[0]
+
+ def filter_phones():
+ pat1 = r'^\s0( |-|\(|\)){0,2}[123456789](( |-|\(|\)){0,2}|\d){6,11}$'
+ pat2 = r'^\b00(( |-|\(|\)){0,2}|\d){6,11}$'
+ pat3 = r'^+[123456789]\d{0,2}(( |-|\(|\)){0,2}|\d){6,11}$'
+ self.text = re.subn(pat1, phone_sub, self.text)[0]
+ self.text = re.subn(pat2, phone_sub, self.text)[0]
+ self.text = re.subn(pat3, phone_sub, self.text)[0]
+
+ def filtered(self):
+ self.filter_emails()
+ self.filter_phones()
+ return self.text
+
+
+def phone_sub(matchobj):
+ if preserve_phone_country_code:
+ country_code = re.search(r'(00|+)', matchobj.group())
+ if country_code:
+ return country_code.group() + '[PHONE]'
+ return '[PHONE]'
+
+
+def email_sub(matchobj):
+ if preserve_email_hostname:
+ hostname = re.search(r'@\.+', matchobj.group())
+ return '[FILTERED]' + hostname.group()
+ return '[EMAIL]'