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

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

Към профила на Снежана Спасова

Резултати

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

Код

import re
class PrivacyFilter():
def __init__(self, text):
self.original = text
self.text = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
self.text = self.original
mail = re.search('([a-zA-Z\d])[\w\+\.-]{,200}@([a-zA-Z\d]' +
'([a-zA-Z\d-]{,61}[a-zA-Z\d])?\.)+[a-zA-Z]' +
'{2,3}(\.[a-zA-Z]{2})?', self.text)
if self.preserve_phone_country_code is True:
phone = re.search('(00|\+)[1-9]\d{,2}([\s()-]{,2}\d)' +
'{6,11}', self.text)
if phone is not None:
prefix = re.search('(00|\+)[1-9]\d{,2}', self.text)
self.text = re.sub('(00|\+)[1-9]\d{,2}([\s()-]{,2}\d){6,11}',
prefix.group() + ' [FILTERED]', self.text)
if self.preserve_email_hostname is True:
if mail is not None:
email = mail.group().split('@')
self.text = re.sub(email[0], '[FILTERED]', self.text)
if self.partially_preserve_email_username is True:
if mail is not None:
email = mail.group().split('@')
if(len(email[0])) <= 6:
self.text = re.sub(email[0], '[FILTERED]', self.text)
else:
self.text = re.sub(email[0][3:], '[FILTERED]', self.text)
if not (self.preserve_email_hostname or
self.partially_preserve_email_username):
if mail is not None:
self.text = re.sub(mail.group(), '[EMAIL]', self.text)
if not self.preserve_phone_country_code:
phone = re.search('(0([\s()-]([\s()-]{,2}\d){6,11}|[1-9]([\s()-]' +
'{,2}\d){5,10}))|((00|\+)[1-9]\d{,2}([\s()-]' +
'{,2}\d){6,11})', self.text)
if phone is not None:
self.text = re.sub(phone.group(), '[PHONE]', self.text)
return self.text
class Validations():
def __init__(self):
pass
@classmethod
def is_email(cls, value):
username, hostname = value.split('@')
mail = re.search(r'\A([a-zA-Z\d])[\w\+\.-]{,200}@?', username)
return (mail.group() == username and
cls.is_hostname(hostname) if mail is not None else False)
@classmethod
def is_hostname(cls, value):
hostname = re.search(r'\A([a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?\.)' +
'+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?', value)
return hostname.group() == value if hostname is not None else False
@classmethod
def is_phone(cls, value):
local_format = re.search(r'\A0([\s()-]([\s()-]{,2}\d){6,11}|[1-9](' +
'[\s()-]{,2}\d){5,10})', value)
international_format = re.search(r'\A(00|\+)[1-9]\d{,2}([\s()-]{,2}' +
'\d){6,11}', value)
local = (local_format.group() == value if local_format
is not None else False)
international = (international_format.group() == value if
international_format is not None else False)
return local or international
@classmethod
def is_ip_address(cls, value):
bytes = value.split('.')
if len(bytes) != 4:
return False
return len(list(filter(lambda x: x in range(0, 256),
list(map(int, bytes))))) == 4
@classmethod
def is_number(cls, value):
number = re.search(r'\A-?((0(\.\d+[1-9])?)|[1-9]\d*(\.\d+)?)', value)
return number.group() == value if number is not None else False
@classmethod
def is_integer(cls, value):
return cls.is_number(value) and '.' not in value
@classmethod
def is_date(cls, value):
date = value.split('-')
if len(date) != 3:
return False
return (int(date[0]) in range(0, 10000) and int(date[1])
in range(1, 13) and int(date[2]) in range(1, 32))
@classmethod
def is_time(cls, value):
time = value.split(':')
if len(time) != 3:
return False
return (int(time[0]) in range(0, 24) and
int(time[1]) in range(0, 60) and int(time[2]) in range(0, 60))
@classmethod
def is_datetime(cls, value):
date = value.split("T")
if len(date) == 2:
return cls.is_date(date[0]) and cls.is_time(date[1])
date = value.split("-")
if len(date) == 4:
return cls.is_date('-'.join(date[:3])) and cls.is_time(date[3])
date = value.split()
if len(date) == 2:
return cls.is_date(date[0]) and cls.is_time(date[1])
else:
return False

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

F..FFE.F.E.....EFF....F...FF.......F..F
======================================================================
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-1hv0kp3/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
  File "/tmp/d20140513-11348-1hv0kp3/solution.py", line 48, in filtered
    self.text = re.sub(phone.group(), '[PHONE]', self.text)
  File "/opt/python3.3/lib/python3.3/re.py", line 170, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  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_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-1hv0kp3/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-1hv0kp3/solution.py", line 48, in filtered
    self.text = re.sub(phone.group(), '[PHONE]', self.text)
  File "/opt/python3.3/lib/python3.3/re.py", line 170, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  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_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-1hv0kp3/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), valid)
  File "/tmp/d20140513-11348-1hv0kp3/solution.py", line 58, in is_email
    username, hostname = value.split('@')
ValueError: too many values to unpack (expected 2)

======================================================================
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-1hv0kp3/test.py", line 55, in test_allows_email_hostname_to_be_preserved
    self.assertEqual('[FILTERED]@exa.mple.com', self.filter_email_usernames('some12-+3@exa.mple.com'))
AssertionError: '[FILTERED]@exa.mple.com' != 'some12-+3@exa.mple.com'
- [FILTERED]@exa.mple.com
+ some12-+3@exa.mple.com


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


======================================================================
FAIL: test_obfuscates_more_complicated_emails (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1hv0kp3/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_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-1hv0kp3/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_does_not_allow_invalid_hours_minutes_or_seconds (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-1hv0kp3/test.py", line 273, in test_does_not_allow_invalid_hours_minutes_or_seconds
    self.assertFalse(solution.Validations.is_time('12:1:9'))
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-1hv0kp3/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_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-1hv0kp3/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-1hv0kp3/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_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-1hv0kp3/test.py", line 207, in test_validates_more_complex_numbers
    self.assertTrue(solution.Validations.is_number('0.0'))
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-1hv0kp3/test.py", line 267, in test_validates_times
    self.assertFalse(solution.Validations.is_time('3:59:59'))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 39 tests in 0.045s

FAILED (failures=11, errors=3)

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

Снежана обнови решението на 23.04.2014 04:24 (преди над 10 години)

+import re
+
+class PrivacyFilter():
+ def __init__(self, text):
+ self.original = text
+ self.text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ self.text = self.original
+ mail = re.search('([a-zA-Z\d])[\w\+\.-]{,200}@([a-zA-Z\d]([a-zA-Z\d-]' +
+ '{,61}[a-zA-Z\d])?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?', self.text)
+
+ if self.preserve_phone_country_code == True:
+ phone = re.search('(00|\+)[1-9]\d{,2}([\s()-]{,2}\d)' +
+ '{6,11}', self.text)
+ if phone != None:
+ prefix = re.search('(00|\+)[1-9]\d{,2}', self.text)
+ self.text = re.sub('(00|\+)[1-9]\d{,2}([\s()-]{,2}\d){6,11}',
+ prefix.group() + ' [FILTERED]', self.text)
+
+ if self.preserve_email_hostname == True:
+ if mail != None:
+ email = mail.group().split('@')
+ self.text = re.sub(email[0], '[FILTERED]', self.text)
+
+ if self.partially_preserve_email_username == True:
+ if mail != None:
+ email = mail.group().split('@')
+ if(len(email[0])) <= 6:
+ self.text = re.sub(email[0], '[FILTERED]', self.text)
+ else:
+ self.text = re.sub(email[0][3:], '[FILTERED]', self.text)
+
+ if not (self.preserve_email_hostname or
+ self.partially_preserve_email_username):
+ if mail != None:
+ self.text = re.sub(mail.group(), '[EMAIL]', self.text)
+
+ if not self.preserve_phone_country_code:
+ phone = re.search('(0([\s()-]([\s()-]{,2}\d){6,11}|[1-9]([\s()-]' +
+ '{,2}\d){5,10}))|((00|\+)[1-9]\d{,2}([\s()-]{,2}\d){6,11})', self.text)
+ if phone != None:
+ self.text = re.sub(phone.group(), '[PHONE]', self.text)
+ return self.text
+
+
+class Validations():
+ def __init__(self):
+ pass
+
+ @classmethod
+ def is_email(cls, value):
+ username, hostname = value.split('@')
+ mail = re.search(r'\A([a-zA-Z\d])[\w\+\.-]{,200}@?', username)
+ return mail.group() == username and cls.is_hostname(hostname) if mail != None else False
+
+ @classmethod
+ def is_hostname(cls, value):
+ hostname = re.search(r'\A([a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?\.)'+
+ '+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?', value)
+ return hostname.group() == value if hostname != None else False
+
+ @classmethod
+ def is_phone(cls, value):
+ local_format = re.search(r'\A0([\s()-]([\s()-]{,2}\d){6,11}|[1-9](' +
+ '[\s()-]{,2}\d){5,10})', value)
+ international_format = re.search(r'\A(00|\+)[1-9]\d{,2}([\s()-]{,2}' +
+ '\d){6,11}', value)
+ local = local_format.group() == value if local_format != None else False
+ international = international_format.group() == value if international_format != None else False
+ return local or international
+
+ @classmethod
+ def is_ip_address(cls, value):
+ bytes = value.split('.')
+ if len(bytes) != 4:
+ return False
+ return len(list(filter(lambda x: x in range(0,256),list(map(int, bytes))))) == 4
+
+ @classmethod
+ def is_number(cls, value):
+ number = re.search(r'\A-?((0(\.\d+[1-9])?)|[1-9]\d*(\.\d+)?)', value)
+ return number.group() == value if number != None else False
+
+ @classmethod
+ def is_integer(cls, value):
+ return cls.is_number(value) and '.' not in value
+
+ @classmethod
+ def is_date(cls, value):
+ date = value.split('-')
+ if len(date) != 3:
+ return False
+ return int(date[0]) in range(0,10000) and int(date[1]) in range(1,13) and int(date[2]) in range(1,32)
+
+ @classmethod
+ def is_time(cls, value):
+ time = value.split(':')
+ if len(time) != 3:
+ return False
+ return int(time[0]) in range(0,24) and int(time[1]) in range(0,60) and int(time[2]) in range(0,60)
+
+ @classmethod
+ def is_datetime(cls, value):
+ datetime_separated_by_space = value.split()
+ datetime_separated_by_separator = value.split("-")
+ datetime_separated_by_t = value.split("T")
+ if len(datetime_separated_by_t) == 2:
+ return cls.is_date(datetime_separated_by_t[0]) and cls.is_time(datetime_separated_by_t[1])
+ elif len(datetime_separated_by_separator) == 4:
+ return cls.is_date('-'.join(datetime_separated_by_separator[:3])) and cls.is_time(datetime_separated_by_separator[3])
+ elif len(datetime_separated_by_space) == 2:
+ return cls.is_date(datetime_separated_by_space[0]) and cls.is_time(datetime_separated_by_space[1])
+ else:
+ return False

Снежана обнови решението на 23.04.2014 05:08 (преди над 10 години)

import re
+
class PrivacyFilter():
def __init__(self, text):
self.original = text
self.text = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
self.text = self.original
- mail = re.search('([a-zA-Z\d])[\w\+\.-]{,200}@([a-zA-Z\d]([a-zA-Z\d-]' +
- '{,61}[a-zA-Z\d])?\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?', self.text)
-
- if self.preserve_phone_country_code == True:
+ mail = re.search('([a-zA-Z\d])[\w\+\.-]{,200}@([a-zA-Z\d]' +
+ '([a-zA-Z\d-]{,61}[a-zA-Z\d])?\.)+[a-zA-Z]' +
+ '{2,3}(\.[a-zA-Z]{2})?', self.text)
+ if self.preserve_phone_country_code is True:
phone = re.search('(00|\+)[1-9]\d{,2}([\s()-]{,2}\d)' +
- '{6,11}', self.text)
- if phone != None:
+ '{6,11}', self.text)
+ if phone is not None:
prefix = re.search('(00|\+)[1-9]\d{,2}', self.text)
self.text = re.sub('(00|\+)[1-9]\d{,2}([\s()-]{,2}\d){6,11}',
- prefix.group() + ' [FILTERED]', self.text)
+ prefix.group() + ' [FILTERED]', self.text)
- if self.preserve_email_hostname == True:
- if mail != None:
+ if self.preserve_email_hostname is True:
+ if mail is not None:
email = mail.group().split('@')
self.text = re.sub(email[0], '[FILTERED]', self.text)
- if self.partially_preserve_email_username == True:
- if mail != None:
+ if self.partially_preserve_email_username is True:
+ if mail is not None:
email = mail.group().split('@')
if(len(email[0])) <= 6:
self.text = re.sub(email[0], '[FILTERED]', self.text)
else:
self.text = re.sub(email[0][3:], '[FILTERED]', self.text)
if not (self.preserve_email_hostname or
- self.partially_preserve_email_username):
- if mail != None:
+ self.partially_preserve_email_username):
+ if mail is not None:
self.text = re.sub(mail.group(), '[EMAIL]', self.text)
if not self.preserve_phone_country_code:
phone = re.search('(0([\s()-]([\s()-]{,2}\d){6,11}|[1-9]([\s()-]' +
- '{,2}\d){5,10}))|((00|\+)[1-9]\d{,2}([\s()-]{,2}\d){6,11})', self.text)
- if phone != None:
+ '{,2}\d){5,10}))|((00|\+)[1-9]\d{,2}([\s()-]' +
+ '{,2}\d){6,11})', self.text)
+ if phone is not None:
self.text = re.sub(phone.group(), '[PHONE]', self.text)
return self.text
class Validations():
def __init__(self):
pass
@classmethod
def is_email(cls, value):
username, hostname = value.split('@')
mail = re.search(r'\A([a-zA-Z\d])[\w\+\.-]{,200}@?', username)
- return mail.group() == username and cls.is_hostname(hostname) if mail != None else False
+ return (mail.group() == username and
+ cls.is_hostname(hostname) if mail is not None else False)
@classmethod
def is_hostname(cls, value):
- hostname = re.search(r'\A([a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?\.)'+
- '+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?', value)
- return hostname.group() == value if hostname != None else False
+ hostname = re.search(r'\A([a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?\.)' +
+ '+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?', value)
+ return hostname.group() == value if hostname is not None else False
@classmethod
def is_phone(cls, value):
local_format = re.search(r'\A0([\s()-]([\s()-]{,2}\d){6,11}|[1-9](' +
- '[\s()-]{,2}\d){5,10})', value)
+ '[\s()-]{,2}\d){5,10})', value)
international_format = re.search(r'\A(00|\+)[1-9]\d{,2}([\s()-]{,2}' +
- '\d){6,11}', value)
- local = local_format.group() == value if local_format != None else False
- international = international_format.group() == value if international_format != None else False
+ '\d){6,11}', value)
+ local = (local_format.group() == value if local_format
+ is not None else False)
+ international = (international_format.group() == value if
+ international_format is not None else False)
return local or international
@classmethod
def is_ip_address(cls, value):
bytes = value.split('.')
if len(bytes) != 4:
return False
- return len(list(filter(lambda x: x in range(0,256),list(map(int, bytes))))) == 4
+ return len(list(filter(lambda x: x in range(0, 256),
+ list(map(int, bytes))))) == 4
@classmethod
def is_number(cls, value):
number = re.search(r'\A-?((0(\.\d+[1-9])?)|[1-9]\d*(\.\d+)?)', value)
- return number.group() == value if number != None else False
+ return number.group() == value if number is not None else False
@classmethod
def is_integer(cls, value):
return cls.is_number(value) and '.' not in value
@classmethod
def is_date(cls, value):
date = value.split('-')
if len(date) != 3:
return False
- return int(date[0]) in range(0,10000) and int(date[1]) in range(1,13) and int(date[2]) in range(1,32)
+ return (int(date[0]) in range(0, 10000) and int(date[1])
+ in range(1, 13) and int(date[2]) in range(1, 32))
@classmethod
def is_time(cls, value):
time = value.split(':')
if len(time) != 3:
return False
- return int(time[0]) in range(0,24) and int(time[1]) in range(0,60) and int(time[2]) in range(0,60)
+ return (int(time[0]) in range(0, 24) and
+ int(time[1]) in range(0, 60) and int(time[2]) in range(0, 60))
@classmethod
def is_datetime(cls, value):
- datetime_separated_by_space = value.split()
- datetime_separated_by_separator = value.split("-")
- datetime_separated_by_t = value.split("T")
- if len(datetime_separated_by_t) == 2:
- return cls.is_date(datetime_separated_by_t[0]) and cls.is_time(datetime_separated_by_t[1])
- elif len(datetime_separated_by_separator) == 4:
- return cls.is_date('-'.join(datetime_separated_by_separator[:3])) and cls.is_time(datetime_separated_by_separator[3])
- elif len(datetime_separated_by_space) == 2:
- return cls.is_date(datetime_separated_by_space[0]) and cls.is_time(datetime_separated_by_space[1])
+ date = value.split("T")
+ if len(date) == 2:
+ return cls.is_date(date[0]) and cls.is_time(date[1])
+ date = value.split("-")
+ if len(date) == 4:
+ return cls.is_date('-'.join(date[:3])) and cls.is_time(date[3])
+ date = value.split()
+ if len(date) == 2:
+ return cls.is_date(date[0]) and cls.is_time(date[1])
else:
- return False
+ return False