Решение на Регулярни изрази от Васил Бунарджиев

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

Към профила на Васил Бунарджиев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 25 успешни тест(а)
  • 14 неуспешни тест(а)

Код

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):
filtered_text = self.text
email_pattern = \
(r"([^\W_]{1}|[^\W_](\w|\.|\-|_|\+){1,200})@"
r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))")
phone_pattern = \
(r'((0)[\ \-\(\)]{,2}[1-9]([\ \-\(\)]{,2}\d){5,10})|'
r'(\b00|\+)[1-9]\d{2}([\ \-\(\)]{,2}\d){6,11}')
filtered_text = re.sub(email_pattern,'[EMAIL]',filtered_text)
filtered_text = re.sub(phone_pattern,'[PHONE]',filtered_text)
return filtered_text
class Validations:
@classmethod
def is_phone(cls, value):
pattern = \
(r'((0)[\ \-\(\)]{,2}[1-9]([\ \-\(\)]{,2}\d){5,10})|'
r'(\b00|\+)[1-9]\d{2}([\ \-\(\)]{,2}\d){6,11}$')
return bool(re.match(pattern, value))
@classmethod
def is_hostname(cls, value):
pattern = \
(r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))$")
return bool(re.match(pattern, value))
@classmethod
def is_email(cls, value):
pattern = \
(r"([^\W_]{1}|[^\W_](\w|\.|\-|_|\+){1,200})@"
r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))$")
return bool(re.match(pattern, value))
@classmethod
def is_ip_address(cls, value):
pattern = \
(r"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}"
r"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
return bool(re.match(pattern, value))
@classmethod
def is_integer(cls, value):
pattern = r'(-){,1}([1-9]\d*|0)$'
return bool(re.match(pattern, value))
@classmethod
def is_number(cls, value):
pattern = r'(-){,1}((0|[1-9]\d*)|(0|[1-9]\d*)(\.)\d+)$'
return bool(re.match(pattern, value))
@classmethod
def is_date(cls, value):
pattern = r'[0-9]{4,4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|30|31)$'
return bool(re.match(pattern, value))
@classmethod
def is_time(cls, value):
pattern = r'(0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9])){2}$'
return bool(re.match(pattern, value))
@classmethod
def is_datetime(cls, value):
pattern = \
(r"[0-9]{4,4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|30|31)(T|\ )"
r"(0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9])){2}$")
return bool(re.match(pattern, value))

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

FFFFFFF...F.....F....FFF..FF...........
======================================================================
FAIL: 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-fsziph/test.py", line 54, in test_allows_email_hostname_to_be_preserved
    self.assertEqual('[FILTERED]@example.com', self.filter_email_usernames('someone@example.com'))
AssertionError: '[FILTERED]@example.com' != '[EMAIL]'
- [FILTERED]@example.com
+ [EMAIL]


======================================================================
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-fsziph/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' != '[EMAIL]'
- som[FILTERED]@example.com
+ [EMAIL]


======================================================================
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-fsziph/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' != 'За връзка: [EMAIL]'
- За връзка: [FILTERED]@example.com
+ За връзка: [EMAIL]


======================================================================
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-fsziph/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-fsziph/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_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-fsziph/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '0[PHONE]'
- [PHONE]
+ 0[PHONE]
? +


======================================================================
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-fsziph/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' != '[EMAIL]'
- [FILTERED]@example.com
+ [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-fsziph/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[PHONE]'
- Phone: 0025 [FILTERED]
+ Phone: 0[PHONE]


======================================================================
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-fsziph/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_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-fsziph/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_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-fsziph/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-fsziph/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-fsziph/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-fsziph/test.py", line 288, in test_handles_newlines_in_time_and_datetime_validation
    self.assertFalse(solution.Validations.is_time("12:01:01\n"))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 39 tests in 0.048s

FAILED (failures=14)

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

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

+import re
+
+class Validations:
+ def is_phone(value):
+ pattern = \
+ (r'((0)[\ \-\(\)]{,2}[1-9]([\ \-\(\)]{,2}\d){5,10})|'
+ r'(\b00|\+)[1-9]\d{2}([\ \-\(\)]{,2}\d){6,11}$')
+ return bool(re.match(pattern, value))
+
+ def is_hostname(value):
+ pattern = \
+ (r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
+ r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))$")
+ return bool(re.match(pattern, value))
+
+ def is_email(value):
+ pattern = \
+ (r"([^\W_]{1}|[^\W_](\w|\.|\-|_|\+){1,200})@"
+ r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
+ r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))$")
+ return bool(re.match(pattern, value))
+
+ def is_ip_address(value):
+ pattern = \
+ (r"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}"
+ r"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
+ return bool(re.match(pattern, value))
+
+ def is_integer(value):
+ pattern = r'(-){,1}([1-9]\d*|0)$'
+ return bool(re.match(pattern, value))
+
+ def is_number(value):
+ pattern = r'(-){,1}((0|[1-9]\d*)|(0|[1-9]\d*)(\.)\d+)$'
+ return bool(re.match(pattern, value))
+
+ def is_date(value):
+ pattern = r'[0-9]{4,4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|30|31)$'
+ return bool(re.match(pattern, value))
+
+ def is_time(value):
+ pattern = r'(0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9])){2}$'
+ return bool(re.match(pattern, value))
+
+ def is_datetime(value):
+ pattern = \
+ (r"[0-9]{4,4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|30|31)(T|\ )"
+ r"(0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9])){2}$")
+ return bool(re.match(pattern, value))

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

import re
class Validations:
- def is_phone(value):
+ @classmethod
+ def is_phone(cls, value):
pattern = \
(r'((0)[\ \-\(\)]{,2}[1-9]([\ \-\(\)]{,2}\d){5,10})|'
r'(\b00|\+)[1-9]\d{2}([\ \-\(\)]{,2}\d){6,11}$')
return bool(re.match(pattern, value))
- def is_hostname(value):
+ @classmethod
+ def is_hostname(cls, value):
pattern = \
(r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))$")
return bool(re.match(pattern, value))
- def is_email(value):
+ @classmethod
+ def is_email(cls, value):
pattern = \
(r"([^\W_]{1}|[^\W_](\w|\.|\-|_|\+){1,200})@"
r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))$")
return bool(re.match(pattern, value))
- def is_ip_address(value):
+ @classmethod
+ def is_ip_address(cls, value):
pattern = \
(r"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}"
r"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
return bool(re.match(pattern, value))
- def is_integer(value):
+ @classmethod
+ def is_integer(cls, value):
pattern = r'(-){,1}([1-9]\d*|0)$'
return bool(re.match(pattern, value))
- def is_number(value):
+ @classmethod
+ def is_number(cls, value):
pattern = r'(-){,1}((0|[1-9]\d*)|(0|[1-9]\d*)(\.)\d+)$'
return bool(re.match(pattern, value))
- def is_date(value):
+ @classmethod
+ def is_date(cls, value):
pattern = r'[0-9]{4,4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|30|31)$'
return bool(re.match(pattern, value))
- def is_time(value):
+ @classmethod
+ def is_time(cls, value):
pattern = r'(0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9])){2}$'
return bool(re.match(pattern, value))
- def is_datetime(value):
+ @classmethod
+ def is_datetime(cls, value):
pattern = \
(r"[0-9]{4,4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|30|31)(T|\ )"
r"(0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9])){2}$")
return bool(re.match(pattern, value))

Васил обнови решението на 23.04.2014 16:55 (преди над 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):
+ filtered_text = self.text
+ email_pattern = \
+ (r"([^\W_]{1}|[^\W_](\w|\.|\-|_|\+){1,200})@"
+ r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
+ r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))")
+ phone_pattern = \
+ (r'((0)[\ \-\(\)]{,2}[1-9]([\ \-\(\)]{,2}\d){5,10})|'
+ r'(\b00|\+)[1-9]\d{2}([\ \-\(\)]{,2}\d){6,11}')
+ filtered_text = re.sub(email_pattern,'[EMAIL]',filtered_text)
+ filtered_text = re.sub(phone_pattern,'[PHONE]',filtered_text)
+ return filtered_text
+
+
class Validations:
@classmethod
def is_phone(cls, value):
pattern = \
(r'((0)[\ \-\(\)]{,2}[1-9]([\ \-\(\)]{,2}\d){5,10})|'
r'(\b00|\+)[1-9]\d{2}([\ \-\(\)]{,2}\d){6,11}$')
return bool(re.match(pattern, value))
@classmethod
def is_hostname(cls, value):
pattern = \
(r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))$")
return bool(re.match(pattern, value))
@classmethod
def is_email(cls, value):
pattern = \
(r"([^\W_]{1}|[^\W_](\w|\.|\-|_|\+){1,200})@"
r"(([^\W_]|[^\W_]([^\W_]|-){0,61}[^\W_])\.){1,}"
r"([a-zA-Z]{2,3}|([a-zA-Z]{2,3}\.[a-zA-Z]{2}))$")
return bool(re.match(pattern, value))
@classmethod
def is_ip_address(cls, value):
pattern = \
(r"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}"
r"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
return bool(re.match(pattern, value))
@classmethod
def is_integer(cls, value):
pattern = r'(-){,1}([1-9]\d*|0)$'
return bool(re.match(pattern, value))
@classmethod
def is_number(cls, value):
pattern = r'(-){,1}((0|[1-9]\d*)|(0|[1-9]\d*)(\.)\d+)$'
return bool(re.match(pattern, value))
@classmethod
def is_date(cls, value):
pattern = r'[0-9]{4,4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|30|31)$'
return bool(re.match(pattern, value))
@classmethod
def is_time(cls, value):
pattern = r'(0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9])){2}$'
return bool(re.match(pattern, value))
@classmethod
def is_datetime(cls, value):
pattern = \
(r"[0-9]{4,4}-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|30|31)(T|\ )"
r"(0[0-9]|1[0-9]|2[0-3])(:([0-5][0-9])){2}$")
return bool(re.match(pattern, value))