Решение на Регулярни изрази от Стоян Христов

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

Към профила на Стоян Христов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 20 успешни тест(а)
  • 19 неуспешни тест(а)

Код

import re
def hostname():
TLD = r'[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?$'
hostname = r'(?:[a-zA-Z0-9][a-zA-Z0-9-]{,62}(?<!-)\.)+' + TLD
return hostname
def email():
email = r'[a-zA-Z0-9][\w+.-]{,200}@' + hostname()
return email
def number():
number = r'^-?(0|([1-9]\d+))(\.\d+)?$'
return number
def integer():
integer = r'^-?(0|([1-9]\d+))$'
return integer
def date():
date = r'[0-9]{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([12][0-9])|(3[01]))$'
return date
def time():
time = r'(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]$'
return time
def ip():
ip = r'((0|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5])))'
return ip + r'\.' + ip + r'\.' + ip + r'\.' + ip + r'$'
def phone():
prefix = r'(0(?!0)|((00|(\+))[1-9]\d{,2}))'
real = r'([() -]{,2}\d){6,11}$'
return prefix + real
class Validations:
@classmethod
def is_email(cls, value):
match = re.match(email(), value)
return match is not None
@classmethod
def is_phone(cls, value):
match = re.match(phone(), value)
return match is not None
@classmethod
def is_hostname(cls, value):
match = re.match(hostname(), value)
return match is not None
@classmethod
def is_ip_address(cls, value):
match = re.match(ip(), value)
return match is not None
@classmethod
def is_number(cls, value):
match = re.match(number(), value)
return match is not None
@classmethod
def is_integer(cls, value):
match = re.match(integer(), value)
return match is not None
@classmethod
def is_date(cls, value):
match = re.match(date(), value)
return match is not None
@classmethod
def is_time(cls, value):
match = re.match(time(), value)
return match is not None
@classmethod
def is_datetime(cls, value):
datetime = date()[:-1] + r'[T ]' + time()
match = re.match(datetime, value)
return match is not None

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

EEEEEEEEEEEE....F.....FF..FF......F.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-1tt9she/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-1tt9she/test.py", line 9, in filter_email_usernames
    filter = solution.PrivacyFilter(text)
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/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-1tt9she/test.py", line 14, in partially_filter_email_usernames
    filter = solution.PrivacyFilter(text)
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/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-1tt9she/test.py", line 14, in partially_filter_email_usernames
    filter = solution.PrivacyFilter(text)
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/test.py", line 48, in test_does_not_filter_invalid_emails
    self.assertEqual(text, solution.PrivacyFilter(text).filtered())
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/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-1tt9she/test.py", line 14, in partially_filter_email_usernames
    filter = solution.PrivacyFilter(text)
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/test.py", line 24, in test_obfuscates_simple_emails
    self.assertEqual('Contact: [EMAIL]', solution.PrivacyFilter('Contact: someone@example.com').filtered())
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/test.py", line 89, in test_preserves_whitespace_around_phones
    self.assertEqual(' [PHONE] or...', solution.PrivacyFilter(' +359881212-12-1 2 or...').filtered())
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/test.py", line 98, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    filter = solution.PrivacyFilter(text)
AttributeError: 'module' object has no attribute 'PrivacyFilter'

======================================================================
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-1tt9she/test.py", line 19, in test_works_with_blank_or_whitespace_strings_and_preserves_whitespace
    self.assertEqual('', solution.PrivacyFilter('').filtered())
AttributeError: 'module' object has no attribute 'PrivacyFilter'

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

======================================================================
FAIL: test_validates_numbers (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1tt9she/test.py", line 197, in test_validates_numbers
    self.assertTrue(solution.Validations.is_number('9'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 39 tests in 0.054s

FAILED (failures=7, errors=12)

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

Стоян обнови решението на 23.04.2014 16:41 (преди над 10 години)

+import re
+
+
+def hostname():
+ TLD = r'[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?$'
+ hostname = r'(?:[a-zA-Z0-9][a-zA-Z0-9-]{,62}(?<!-)\.)+' + TLD
+ return hostname
+
+
+def email():
+ email = r'[a-zA-Z0-9][\w-\+\.]{,200}@' + hostname()
+ return email
+
+
+def number():
+ number = r'^-?(0|([1-9]\d+))(\.\d+)?$'
+ return number
+
+
+def integer():
+ integer = r'^-?(0|([1-9]\d+))$'
+ return integer
+
+
+def date():
+ date = r'[0-9]{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([12][0-9])|(3[01]))$'
+ return date
+
+
+def time():
+ time = r'[0-3][0-3]:[0-5][0-9]:[0-5][0-9]$'
+ return time
+
+
+def ip():
+ ip = r'((0|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5])))'
+ return ip + r'\.' + ip + r'\.' + ip + r'\.' + ip + r'$'
+
+
+def phone():
+ prefix = r'(0(?!0)|((00|(\+))[1-9]\d{,2}))'
+ real = r'([() -]{,2}\d){6,11}$'
+ return prefix + real
+
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ match = re.match(email(), value)
+ return match is not None
+
+ @classmethod
+ def is_phone(cls, value):
+ match = re.match(phone(), value)
+ return match is not None
+
+ @classmethod
+ def is_hostname(cls, value):
+ match = re.match(hostname(), value)
+ return match is not None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ match = re.match(ip(), value)
+ return match is not None
+
+ @classmethod
+ def is_number(cls, value):
+ match = re.match(number(), value)
+ return match is not None
+
+ @classmethod
+ def is_integer(cls, value):
+ match = re.match(integer(), value)
+ return match is not None
+
+ @classmethod
+ def is_date(cls, value):
+ match = re.match(date(), value)
+ return match is not None
+
+ @classmethod
+ def is_time(cls, value):
+ match = re.match(time(), value)
+ return match is not None
+
+ @classmethod
+ def is_datetime(cls, value):
+ datetime = date()[:-1] + r'T|[]' + time()
+ match = re.match(datetime, value)
+ return match is not None

Стоян обнови решението на 23.04.2014 16:48 (преди над 10 години)

import re
def hostname():
TLD = r'[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?$'
hostname = r'(?:[a-zA-Z0-9][a-zA-Z0-9-]{,62}(?<!-)\.)+' + TLD
return hostname
def email():
- email = r'[a-zA-Z0-9][\w-\+\.]{,200}@' + hostname()
+ email = r'[a-zA-Z0-9][\w+.-]{,200}@' + hostname()
return email
def number():
number = r'^-?(0|([1-9]\d+))(\.\d+)?$'
return number
def integer():
integer = r'^-?(0|([1-9]\d+))$'
return integer
def date():
date = r'[0-9]{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([12][0-9])|(3[01]))$'
return date
def time():
- time = r'[0-3][0-3]:[0-5][0-9]:[0-5][0-9]$'
+ time = r'(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]$'
return time
def ip():
ip = r'((0|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5])))'
return ip + r'\.' + ip + r'\.' + ip + r'\.' + ip + r'$'
def phone():
prefix = r'(0(?!0)|((00|(\+))[1-9]\d{,2}))'
real = r'([() -]{,2}\d){6,11}$'
return prefix + real
class Validations:
@classmethod
def is_email(cls, value):
match = re.match(email(), value)
return match is not None
@classmethod
def is_phone(cls, value):
match = re.match(phone(), value)
return match is not None
@classmethod
def is_hostname(cls, value):
match = re.match(hostname(), value)
return match is not None
@classmethod
def is_ip_address(cls, value):
match = re.match(ip(), value)
return match is not None
@classmethod
def is_number(cls, value):
match = re.match(number(), value)
return match is not None
@classmethod
def is_integer(cls, value):
match = re.match(integer(), value)
return match is not None
@classmethod
def is_date(cls, value):
match = re.match(date(), value)
return match is not None
@classmethod
def is_time(cls, value):
match = re.match(time(), value)
return match is not None
@classmethod
def is_datetime(cls, value):
- datetime = date()[:-1] + r'T|[]' + time()
+ datetime = date()[:-1] + r'[T ]' + time()
match = re.match(datetime, value)
return match is not None