Решение на Регулярни изрази от Александър Чешмеджиев

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

Към профила на Александър Чешмеджиев

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 9 успешни тест(а)
  • 30 неуспешни тест(а)

Код

import re
class Validations:
@classmethod
def is_email(cls, value):
pattern = r'^\w[\w_+.-]{1,200}@ (\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
return bool(re.match(pattern, value))
@classmethod
def is_phone(cls, value):
international_pattern = r'^(00|\+)\d{1,3}\s*\d[\s()-]{,2}'
local_pattern = r'^0(?!0)\d{6,11}'
return bool(re.match(local_pattern, value)) or bool(re.match(international_pattern, value))
@classmethod
def is_hostname(cls, value):
pattern = r'(\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
return bool(re.match(pattern, value))
@classmethod
def is_ip_address(cls, value):
pattern = r'((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){4}'
return bool(re.match(pattern, value))
@classmethod
def is_number(cls, value):
pattern = r'^-?((0?\.|[1-9]+\.|[1-9])\d+|0+)'
return bool(re.match(pattern, value))
@classmethod
def is_integer(cls, value):
pattern = r'^-?([1-9]\d+|0+)'
return bool(re.match(pattern, value))
@classmethod
def is_date(cls, value):
pattern = r'\d{4}-(0\d|1[012])-(\d|1\d|2\d|3[01])'
return bool(re.match(pattern, value))
@classmethod
def is_time(cls, value):
pattern = r'(0\d|1\d|2[0123]):[0-5]\d:[0-5]\d '
return bool(re.match(pattern, value))
@classmethod
def is_datetime(cls, value):
pattern = r'(\d{4}-(0\d|1[012])-(\d|1\d|2\d|3[01]))[\sT]((0\d|1\d|2[0123]):[0-5]\d:[0-5]\d)'
return bool(re.match(pattern, value))
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):
filtered_text = self.text
email_pattern = r'^\w[\w_+.-]{1,200}@ (\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
phone_international_pattern = r'^(00|\+)\d{1,3}\s*\d[\s()-]{,2}'
phone_local_pattern = r'^0(?!0)\d{6,11}'
if self.preserve_phone_country_code == True and bool(re.search(phone_international_pattern, self.text)) == True:
preserved_international_pattern = r'\d[\s()-]{,2}(?<=(00|\+)\d{1,3}\s*'
filtered_text = re.sub(preserved_international_pattern, '[FILTERED]', self.text)
elif bool(re.search(phone_international_pattern, self.text)) == True:
filtered_text = re.sub(phone_international_pattern, '[PHONE]', self.text)
elif bool(re.search(phone_local_pattern, self.text)) == True:
filtered_text = re.sub(phone_local_pattern, '[PHONE]', self.text)
preserved_hostname_pattern = r'^\w[\w_+.-]{1,200}(?=@)'
partially_preserved_hostname_pattern = r'\w[\w_+.-]{1,200}(?<=^\w[\w_+.-]{2})(?=@)'
if self.preserve_email_hostname == True and bool(re.search(preserved_hostname_pattern, self.text)):
filtered_text = re.sub(preserverd_hostname_pattern, '[FILTERED]', self.text)
elif self.partially_preserve_email_username == True and bool(re.search(partially_preserved_hostname_pattern, self.text)) == True:
filtered_text = re.sub(partially_preserverd_hostname_pattern, '[FILTERED]', self.text)
elif bool(re.search(email_pattern, self.text)) == True:
filtered_text = re.sub(email_pattern, '[EMAIL]', self.text)
return filtered_text

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

EFFEFFFFFFF..F.FF.FF.F.FFFFFFF.FF.FFF.F
======================================================================
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-1iag4b4/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-1iag4b4/test.py", line 11, in filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1iag4b4/solution.py", line 76, in filtered
    filtered_text = re.sub(preserverd_hostname_pattern, '[FILTERED]', self.text)
NameError: global name 'preserverd_hostname_pattern' is not defined

======================================================================
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-1iag4b4/test.py", line 50, in test_does_not_filter_invalid_emails
    self.assertEqual(text, self.filter_email_usernames(text))
  File "/tmp/d20140513-11348-1iag4b4/test.py", line 11, in filter_email_usernames
    return filter.filtered()
  File "/tmp/d20140513-11348-1iag4b4/solution.py", line 76, in filtered
    filtered_text = re.sub(preserverd_hostname_pattern, '[FILTERED]', self.text)
NameError: global name 'preserverd_hostname_pattern' is not defined

======================================================================
FAIL: 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-1iag4b4/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'))
AssertionError: 'som[FILTERED]@example.com' != 'someone@example.com'
- som[FILTERED]@example.com
+ someone@example.com


======================================================================
FAIL: 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-1iag4b4/test.py", line 64, in test_does_not_brake_with_unicode
    self.assertEqual('За връзка: [FILTERED]@example.com', self.partially_filter_email_usernames('За връзка: me@example.com'))
AssertionError: 'За връзка: [FILTERED]@example.com' != 'За връзка: me@example.com'
- За връзка: [FILTERED]@example.com
?            ^^^^^^^^^^
+ За връзка: me@example.com
?            ^^


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


======================================================================
FAIL: 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-1iag4b4/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '[PHONE]21 25 543'
- [PHONE]
+ [PHONE]21 25 543


======================================================================
FAIL: 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-1iag4b4/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'))
AssertionError: '[FILTERED]@example.com' != 'me@example.com'
- [FILTERED]@example.com
+ me@example.com


======================================================================
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-1iag4b4/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[EMAIL]' != 'some.user+and-more-here@lawn.co.uk'
- [EMAIL]
+ some.user+and-more-here@lawn.co.uk


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


======================================================================
FAIL: 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-1iag4b4/test.py", line 89, in test_preserves_whitespace_around_phones
    self.assertEqual(' [PHONE] or...', solution.PrivacyFilter(' +359881212-12-1 2 or...').filtered())
AssertionError: ' [PHONE] or...' != ' +359881212-12-1 2 or...'
-  [PHONE] or...
+  +359881212-12-1 2 or...


======================================================================
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-1iag4b4/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
AssertionError: 'Phone: +25 [FILTERED]' != 'Phone: +25( 55 )12 12255'
- Phone: +25 [FILTERED]
+ Phone: +25( 55 )12 12255


======================================================================
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-1iag4b4/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_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-1iag4b4/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-1iag4b4/test.py", line 160, in test_can_validate_more_complex_phone_numbers
    self.assertIs(solution.Validations.is_phone(phone), valid)
AssertionError: False is not True

======================================================================
FAIL: test_does_not_allow_invalid_months_or_days_in_dates (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-1iag4b4/test.py", line 255, in test_does_not_allow_invalid_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('2012-06-32'))
AssertionError: True is not false

======================================================================
FAIL: test_does_not_allow_zero_months_or_days_in_dates (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-1iag4b4/test.py", line 249, in test_does_not_allow_zero_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('1000-00-01'))
AssertionError: True is not false

======================================================================
FAIL: 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-1iag4b4/test.py", line 163, in test_does_not_break_on_phones_in_multiline_strings
    self.assertFalse(solution.Validations.is_phone("0885123123\nwat?"))
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-1iag4b4/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_multiline_strings_in_integer_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-1iag4b4/test.py", line 233, in test_handles_multiline_strings_in_integer_validation_properly
    self.assertFalse(solution.Validations.is_number("42\n24"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_multiline_strings_in_numbers_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-1iag4b4/test.py", line 215, in test_handles_multiline_strings_in_numbers_validation_properly
    self.assertFalse(solution.Validations.is_number("42\n24"))
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-1iag4b4/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-1iag4b4/test.py", line 290, in test_handles_newlines_in_time_and_datetime_validation
    self.assertFalse(solution.Validations.is_datetime("2012-11-19 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-1iag4b4/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_IP_addresses (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-1iag4b4/test.py", line 183, in test_validates_IP_addresses
    self.assertTrue(solution.Validations.is_ip_address('1.2.3.4'))
AssertionError: False is not true

======================================================================
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-1iag4b4/test.py", line 281, in test_validates_datetime_values
    self.assertFalse(solution.Validations.is_datetime('2012-00-19T23:59:00'))
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-1iag4b4/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-1iag4b4/test.py", line 224, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('-42 '))
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-1iag4b4/test.py", line 205, in test_validates_more_complex_numbers
    self.assertFalse(solution.Validations.is_number('00'))
AssertionError: True is not false

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

======================================================================
FAIL: test_validates_times (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-1iag4b4/test.py", line 263, in test_validates_times
    self.assertTrue(solution.Validations.is_time('12:00:00'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 39 tests in 0.046s

FAILED (failures=28, errors=2)

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

Александър обнови решението на 23.04.2014 10:01 (преди над 10 години)

+import re
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ pattern = r'^\w[\w_+.-]{1,200}@ (\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
+ return bool(re.match(pattern, value))
+
+ @classmethod
+ def is_phone(cls, value):
+ international_pattern = r'^(00|\+)\d{1,3}\s*\d[\s()-]{,2}'
+ local_pattern = r'^0(?!0)\d{6,11}'
+ return bool(re.match(local_pattern, value)) or bool(re.match(international_pattern, value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ pattern = r'(\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
+ return bool(re.match(pattern, value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ pattern = r'((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){4}'
+ return bool(re.match(pattern, value))
+
+ @classmethod
+ def is_number(cls, value):
+ pattern = r'^-?((0?\.|[1-9]+\.|[1-9])\d+|0+)'
+ return bool(re.match(pattern, value))
+
+ @classmethod
+ def is_integer(cls, value):
+ pattern = r'^-?([1-9]\d+|0+)'
+ return bool(re.match(pattern, value))
+
+ @classmethod
+ def is_date(cls, value):
+ pattern = r'\d{4}-(0\d|1[012])-(\d|1\d|2\d|3[01])'
+ return bool(re.match(pattern, value))
+
+ @classmethod
+ def is_time(cls, value):
+ pattern = r'(0\d|1\d|2[0123]):[0-5]\d:[0-5]\d '
+ return bool(re.match(pattern, value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ pattern = r'(\d{4}-(0\d|1[012])-(\d|1\d|2\d|3[01]))[\sT]((0\d|1\d|2[0123]):[0-5]\d:[0-5]\d)'
+ return bool(re.match(pattern, value))
+
+
+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_pattern = r'^\w[\w_+.-]{1,200}@ (\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
+ phone_international_pattern = r'^(00|\+)\d{1,3}\s*\d[\s()-]{,2}'
+ phone_local_pattern = r'^0(?!0)\d{6,11}'
+
+ if self.preserve_phone_country_code == True and bool(re.match(phone_international_pattern, self.text)) == True:
+ preserved_international_pattern = r'\d[\s()-]{,2}(?<=(00|\+)\d{1,3}\s*'
+ self.text = re.sub(preserved_international_pattern, '[FILTERED]', self.text)
+ elif bool(re.match(phone_international_pattern, self.text)) == True:
+ self.text = re.sub(phone_international_pattern, '[PHONE]', self.text)
+ elif bool(re.match(phone_local_pattern, self.text)) == True:
+ self.text = re.sub(phone_local_pattern, '[PHONE]', self.text)
+ preserved_hostname_pattern = r'^\w[\w_+.-]{1,200}(?=@)'
+ partially_preserved_hostname_pattern = r'\w[\w_+.-]{1,200}(?<=^\w[\w_+.-]{2})(?=@)'
+ if self.preserve_email_hostname == True and bool(re.match(preserved_hostname_pattern, self.text)):
+ self.text = re.sub(preserverd_hostname_pattern, '[FILTERED]', self.text)
+ elif self.partially_preserve_email_username == True and bool(re.match(partially_preserved_hostname_pattern, self.text)) == True:
+ self.text = re.sub(partially_preserverd_hostname_pattern, '[FILTERED]', self.text)
+ elif bool(re.match(email_pattern, self.text)) == True:
+ self.text = re.sub(email_pattern, '[EMAIL]', self.text)
+ return self.text

Александър обнови решението на 23.04.2014 10:17 (преди над 10 години)

import re
class Validations:
@classmethod
def is_email(cls, value):
pattern = r'^\w[\w_+.-]{1,200}@ (\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
return bool(re.match(pattern, value))
@classmethod
def is_phone(cls, value):
international_pattern = r'^(00|\+)\d{1,3}\s*\d[\s()-]{,2}'
local_pattern = r'^0(?!0)\d{6,11}'
return bool(re.match(local_pattern, value)) or bool(re.match(international_pattern, value))
@classmethod
def is_hostname(cls, value):
pattern = r'(\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
return bool(re.match(pattern, value))
@classmethod
def is_ip_address(cls, value):
pattern = r'((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){4}'
return bool(re.match(pattern, value))
@classmethod
def is_number(cls, value):
pattern = r'^-?((0?\.|[1-9]+\.|[1-9])\d+|0+)'
return bool(re.match(pattern, value))
@classmethod
def is_integer(cls, value):
pattern = r'^-?([1-9]\d+|0+)'
return bool(re.match(pattern, value))
@classmethod
def is_date(cls, value):
pattern = r'\d{4}-(0\d|1[012])-(\d|1\d|2\d|3[01])'
return bool(re.match(pattern, value))
@classmethod
def is_time(cls, value):
pattern = r'(0\d|1\d|2[0123]):[0-5]\d:[0-5]\d '
return bool(re.match(pattern, value))
@classmethod
def is_datetime(cls, value):
pattern = r'(\d{4}-(0\d|1[012])-(\d|1\d|2\d|3[01]))[\sT]((0\d|1\d|2[0123]):[0-5]\d:[0-5]\d)'
return bool(re.match(pattern, value))
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_pattern = r'^\w[\w_+.-]{1,200}@ (\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
phone_international_pattern = r'^(00|\+)\d{1,3}\s*\d[\s()-]{,2}'
phone_local_pattern = r'^0(?!0)\d{6,11}'
- if self.preserve_phone_country_code == True and bool(re.match(phone_international_pattern, self.text)) == True:
+ if self.preserve_phone_country_code == True and bool(re.search(phone_international_pattern, self.text)) == True:
preserved_international_pattern = r'\d[\s()-]{,2}(?<=(00|\+)\d{1,3}\s*'
self.text = re.sub(preserved_international_pattern, '[FILTERED]', self.text)
- elif bool(re.match(phone_international_pattern, self.text)) == True:
+ elif bool(re.search(phone_international_pattern, self.text)) == True:
self.text = re.sub(phone_international_pattern, '[PHONE]', self.text)
- elif bool(re.match(phone_local_pattern, self.text)) == True:
+ elif bool(re.search(phone_local_pattern, self.text)) == True:
self.text = re.sub(phone_local_pattern, '[PHONE]', self.text)
preserved_hostname_pattern = r'^\w[\w_+.-]{1,200}(?=@)'
partially_preserved_hostname_pattern = r'\w[\w_+.-]{1,200}(?<=^\w[\w_+.-]{2})(?=@)'
- if self.preserve_email_hostname == True and bool(re.match(preserved_hostname_pattern, self.text)):
+ if self.preserve_email_hostname == True and bool(re.search(preserved_hostname_pattern, self.text)):
self.text = re.sub(preserverd_hostname_pattern, '[FILTERED]', self.text)
- elif self.partially_preserve_email_username == True and bool(re.match(partially_preserved_hostname_pattern, self.text)) == True:
+ elif self.partially_preserve_email_username == True and bool(re.search(partially_preserved_hostname_pattern, self.text)) == True:
self.text = re.sub(partially_preserverd_hostname_pattern, '[FILTERED]', self.text)
- elif bool(re.match(email_pattern, self.text)) == True:
+ elif bool(re.search(email_pattern, self.text)) == True:
self.text = re.sub(email_pattern, '[EMAIL]', self.text)
return self.text

Александър обнови решението на 23.04.2014 16:14 (преди над 10 години)

import re
class Validations:
@classmethod
def is_email(cls, value):
pattern = r'^\w[\w_+.-]{1,200}@ (\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
return bool(re.match(pattern, value))
@classmethod
def is_phone(cls, value):
international_pattern = r'^(00|\+)\d{1,3}\s*\d[\s()-]{,2}'
local_pattern = r'^0(?!0)\d{6,11}'
return bool(re.match(local_pattern, value)) or bool(re.match(international_pattern, value))
@classmethod
def is_hostname(cls, value):
pattern = r'(\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
return bool(re.match(pattern, value))
@classmethod
def is_ip_address(cls, value):
pattern = r'((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){4}'
return bool(re.match(pattern, value))
@classmethod
def is_number(cls, value):
pattern = r'^-?((0?\.|[1-9]+\.|[1-9])\d+|0+)'
return bool(re.match(pattern, value))
@classmethod
def is_integer(cls, value):
pattern = r'^-?([1-9]\d+|0+)'
return bool(re.match(pattern, value))
@classmethod
def is_date(cls, value):
pattern = r'\d{4}-(0\d|1[012])-(\d|1\d|2\d|3[01])'
return bool(re.match(pattern, value))
@classmethod
def is_time(cls, value):
pattern = r'(0\d|1\d|2[0123]):[0-5]\d:[0-5]\d '
return bool(re.match(pattern, value))
@classmethod
def is_datetime(cls, value):
pattern = r'(\d{4}-(0\d|1[012])-(\d|1\d|2\d|3[01]))[\sT]((0\d|1\d|2[0123]):[0-5]\d:[0-5]\d)'
return bool(re.match(pattern, value))
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):
+ filtered_text = self.text
email_pattern = r'^\w[\w_+.-]{1,200}@ (\w[\w-]{1,61}\w\.)(\1)*([a-zA-Z]{2,3})'
phone_international_pattern = r'^(00|\+)\d{1,3}\s*\d[\s()-]{,2}'
phone_local_pattern = r'^0(?!0)\d{6,11}'
if self.preserve_phone_country_code == True and bool(re.search(phone_international_pattern, self.text)) == True:
preserved_international_pattern = r'\d[\s()-]{,2}(?<=(00|\+)\d{1,3}\s*'
- self.text = re.sub(preserved_international_pattern, '[FILTERED]', self.text)
+ filtered_text = re.sub(preserved_international_pattern, '[FILTERED]', self.text)
elif bool(re.search(phone_international_pattern, self.text)) == True:
- self.text = re.sub(phone_international_pattern, '[PHONE]', self.text)
+ filtered_text = re.sub(phone_international_pattern, '[PHONE]', self.text)
elif bool(re.search(phone_local_pattern, self.text)) == True:
- self.text = re.sub(phone_local_pattern, '[PHONE]', self.text)
+ filtered_text = re.sub(phone_local_pattern, '[PHONE]', self.text)
preserved_hostname_pattern = r'^\w[\w_+.-]{1,200}(?=@)'
partially_preserved_hostname_pattern = r'\w[\w_+.-]{1,200}(?<=^\w[\w_+.-]{2})(?=@)'
if self.preserve_email_hostname == True and bool(re.search(preserved_hostname_pattern, self.text)):
- self.text = re.sub(preserverd_hostname_pattern, '[FILTERED]', self.text)
+ filtered_text = re.sub(preserverd_hostname_pattern, '[FILTERED]', self.text)
elif self.partially_preserve_email_username == True and bool(re.search(partially_preserved_hostname_pattern, self.text)) == True:
- self.text = re.sub(partially_preserverd_hostname_pattern, '[FILTERED]', self.text)
+ filtered_text = re.sub(partially_preserverd_hostname_pattern, '[FILTERED]', self.text)
elif bool(re.search(email_pattern, self.text)) == True:
- self.text = re.sub(email_pattern, '[EMAIL]', self.text)
- return self.text
+ filtered_text = re.sub(email_pattern, '[EMAIL]', self.text)
+ return filtered_text