Решение на Регулярни изрази от Антония Чекръкчиева

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

Към профила на Антония Чекръкчиева

Резултати

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

Код

import re
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
def filtered(self):
if self.preserve_phone_country_code:
if Validations.is_phone(self.text):
PrivacyFilter.preserve_phone_country_code = False
return self.text[:self.text.index(':')+1] + ' +359 [FILTERED]'
PrivacyFilter.preserve_phone_country_code = False
return self.text
if self.preserve_email_hostname:
if Validations.is_email(self.text):
PrivacyFilter.preserve_email_hostname = False
return '[FILTERED]' + self.text[self.text.index('@'):]
PrivacyFilter.preserve_email_hostname = False
return self.text
if self.partially_preserve_email_username:
if Validations.is_email(self.text):
PrivacyFilter.partially_preserve_email_username = False
if self.text.index('@') <7:
return '[FILTERED]' + self.text[self.text.index('@'):]
return self.text[:3] + '[FILTERED]' + self.text[self.text.index('@'):]
PrivacyFilter.partially_preserve_email_username = False
return self.text
else:
if ' ' in self.text and ':' not in self.text:
string = ''
for items in self.text.split(' '):
if Validations.is_email(items):
print(items)
string += '[EMAIL] '
elif Validations.is_phone(items):
print(items)
string += '[PHONE] '
return string
if ':' in self.text:
text_for_validate = self.text[self.text.index(':')+1:]
if Validations.is_phone(self.text[self.text.index(':')+1:]):
return self.text[:self.text.index(':')+1] + ' [PHONE]'
if Validations.is_email(self.text[self.text.index(':')+1:]):
return self.text[:self.text.index(':')+1] + ' [EMAIL]'
else:
if Validations.is_phone(self.text):
return '[PHONE]'
if Validations.is_email(text_for_validate):
return '[EMAIL]'
class Validations:
@classmethod
def is_email(cls, value):
if '@' not in value:
return False
return bool(re.search(r'[A-Za-z0-9][A-Za-z0-9_+-.]{,200}@$',value[:value.index('@')+1])) & Validations.is_hostname(value[value.index('@')+1:])
@classmethod
def is_phone(cls, value):
return bool(re.search(r'(0[1-9]\d{6,11}|((00|\+)[1-9]\d{1,2}\d{6,11}))$',value))
@classmethod
def is_hostname(cls, value):
return bool(re.search(r'^([A-Za-z0-9]\w{,61}[A-Za-z0-9]\.)+[A-Za-z0-9]{2,3}$',value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(r'^([0-1]?[\d]?[\d]|[2][0-4][\d]|25[0-5]).([0-1]?[\d]?[\d]|[2][0-4][\d]|25[0-5]).([0-1]?[\d]?[\d]|[2][0-4][\d]|25[0-5]).([0-1]?[\d]?[\d]|[2][0-4][\d]|25[0-5])$',value))
@classmethod
def is_number(cls, value):
return bool(re.search(r'^((-[1-9]|[1-9])(\d+)?(.\d+)?|0(.\d+)?|-0.\d+)$',value))
@classmethod
def is_integer(cls, value):
return bool(re.search(r'^((-[1-9]|[1-9])(\d+)?|0)$',value))
@classmethod
def is_date(cls, value):
return bool(re.search(r'^[\d][\d][\d][\d]-(0[1-9]|1[0-2])-(3[0-1]|[1-2][\d]|0[1-9])$',value))
@classmethod
def is_time(cls, value):
return bool(re.search(r'^(2[0-3]|1[\d]|0[\d]|[^\d][\d]):([0-5][\d]|[\d]):([0-5][\d]|[\d])$',value))
@classmethod
def is_datetime(cls, value):
return Validations.is_date(value[:10]) & Validations.is_time(value[11:])

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

..FFFF.E.FFE...FFF....FF..FF....F.F....
======================================================================
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-851o2/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
  File "/tmp/d20140513-11348-851o2/solution.py", line 57, in filtered
    if Validations.is_email(text_for_validate):
UnboundLocalError: local variable 'text_for_validate' referenced before assignment

======================================================================
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-851o2/test.py", line 19, in test_works_with_blank_or_whitespace_strings_and_preserves_whitespace
    self.assertEqual('', solution.PrivacyFilter('').filtered())
  File "/tmp/d20140513-11348-851o2/solution.py", line 57, in filtered
    if Validations.is_email(text_for_validate):
UnboundLocalError: local variable 'text_for_validate' referenced before assignment

======================================================================
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-851o2/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' != 'За [FILTERED]@example.com'
- За връзка: [FILTERED]@example.com
?   --------
+ За [FILTERED]@example.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-851o2/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-851o2/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '+1555 123, 55555' != ''
- +1555 123, 55555
+ 


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


======================================================================
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-851o2/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...' != ''
-  [PHONE] 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-851o2/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: 0025(55) 12 12255'
- Phone: 0025 [FILTERED]
+ Phone: 0025(55) 12 12255


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

======================================================================
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-851o2/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-851o2/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-851o2/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-851o2/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-851o2/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-851o2/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_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-851o2/test.py", line 167, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('some.long-subdomain.domain.co.ul'))
AssertionError: False is not true

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

----------------------------------------------------------------------
Ran 39 tests in 0.040s

FAILED (failures=15, errors=2)

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

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

+import re
+
+class PrivacyFilter:
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+
+ def filtered(self):
+ if self.preserve_phone_country_code:
+ if Validations.is_phone(self.text):
+ PrivacyFilter.preserve_phone_country_code = False
+ return self.text[:self.text.index(':')+1] + ' +359 [FILTERED]'
+ PrivacyFilter.preserve_phone_country_code = False
+ return self.text
+
+ if self.preserve_email_hostname:
+ if Validations.is_email(self.text):
+ PrivacyFilter.preserve_email_hostname = False
+ return '[FILTERED]' + self.text[self.text.index('@'):]
+ PrivacyFilter.preserve_email_hostname = False
+ return self.text
+
+ if self.partially_preserve_email_username:
+ if Validations.is_email(self.text):
+ PrivacyFilter.partially_preserve_email_username = False
+ if self.text.index('@') <7:
+ return '[FILTERED]' + self.text[self.text.index('@'):]
+ return self.text[:3] + '[FILTERED]' + self.text[self.text.index('@'):]
+ PrivacyFilter.partially_preserve_email_username = False
+ return self.text
+
+ else:
+ if ' ' in self.text and ':' not in self.text:
+ string = ''
+ for items in self.text.split(' '):
+ if Validations.is_email(items):
+ print(items)
+ string += '[EMAIL] '
+ elif Validations.is_phone(items):
+ print(items)
+ string += '[PHONE] '
+
+ return string
+ if ':' in self.text:
+ text_for_validate = self.text[self.text.index(':')+1:]
+ if Validations.is_phone(self.text[self.text.index(':')+1:]):
+ return self.text[:self.text.index(':')+1] + ' [PHONE]'
+ if Validations.is_email(self.text[self.text.index(':')+1:]):
+ return self.text[:self.text.index(':')+1] + ' [EMAIL]'
+ else:
+ if Validations.is_phone(self.text):
+ return '[PHONE]'
+ if Validations.is_email(text_for_validate):
+ return '[EMAIL]'
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ if '@' not in value:
+ return False
+ return bool(re.search(r'[A-Za-z0-9][A-Za-z0-9_+-.]{,200}@$',value[:value.index('@')+1])) & Validations.is_hostname(value[value.index('@')+1:])
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(re.search(r'(0[1-9]\d{6,11}|((00|\+)[1-9]\d{1,2}\d{6,11}))$',value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.search(r'^([A-Za-z0-9]\w{,61}[A-Za-z0-9]\.)+[A-Za-z0-9]{2,3}$',value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.search(r'^([0-1]?[\d]?[\d]|[2][0-4][\d]|25[0-5]).([0-1]?[\d]?[\d]|[2][0-4][\d]|25[0-5]).([0-1]?[\d]?[\d]|[2][0-4][\d]|25[0-5]).([0-1]?[\d]?[\d]|[2][0-4][\d]|25[0-5])$',value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(re.search(r'^((-[1-9]|[1-9])(\d+)?(.\d+)?|0(.\d+)?|-0.\d+)$',value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.search(r'^((-[1-9]|[1-9])(\d+)?|0)$',value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.search(r'^[\d][\d][\d][\d]-(0[1-9]|1[0-2])-(3[0-1]|[1-2][\d]|0[1-9])$',value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.search(r'^(2[0-3]|1[\d]|0[\d]|[^\d][\d]):([0-5][\d]|[\d]):([0-5][\d]|[\d])$',value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return Validations.is_date(value[:10]) & Validations.is_time(value[11:])