Решение на Регулярни изрази от Кирил Киров

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

Към профила на Кирил Киров

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 26 успешни тест(а)
  • 13 неуспешни тест(а)

Код

def text_to_email_filtered(email, hostname, username):
if not bool(re.search(r'[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', email)):
return email
email_to_list = email.split(' ')
i = 0
value = email_to_list
for val in value:
match = re.search( r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)$', val)
if match is not None and hostname == False and username == False:
value[i] = '[EMAIL] '
elif match is not None and hostname == True and username == False:
v = value[i].split('@')
value[i] = '[FILTERED]' + '@' + v[1] + ' '
elif match is not None and username == True:
v = value[i].split('@')
if len(v[0]) < 7:
value[i] = '[FILTERED]' + '@' + v[1] + ' '
else:
value[i] = str(value[i][0]) + str(value[i][1]) + \
str(value[i][2]) + '[FILTERED]' + \
'@' + v[1] + ' '
else:
value[i] = val + ' '
i = i + 1
new_text = ''.join(value)
new_text = new_text[:-1]
return new_text
def text_to_tel(text, code):
text1 = text
if code == False:
p = next_phone(text1)
while p != []:
text1 = text1[:p[0]] + '[PHONE]' + text1[p[1]:]
p = next_phone(text1)
if code == True:
p = next_phone(text1)
while p != []:
kod = re.search('^(\+[1-9]\d{,2}|00[1-9]\d{,2})', text1[p[0]:])
if kod is not None:
text1 = text1[:p[0]] + kod.group(0) + ' [FILTERED]' + text1[p[1]:]
p = next_phone(text1)
else:
text1 = text1[:p[0]] + '[PHONE]' + text1[p[1]:]
p = next_phone(text1)
return text1
def next_phone(r):
phone = re.compile(
r'(\+[1-9]\d{,2}|00[1-9]\d{,2}|0[1-9]{,3})[ -()]{,2}([ -()]{,2}[0-9][ -()]{,2}){6,10}[0-9]')
chislo= re.compile(r'\d+')
iterator=phone.finditer(r)
list_match=[]
for match in iterator:
start, end=match.span()
list_match.append(start)
list_match.append(end)
break
return list_match
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):
text = text_to_email_filtered(
self._text, self.preserve_email_hostname, self.partially_preserve_email_username)
text = text_to_tel(text, self.preserve_phone_country_code)
return text
import calendar
import re
class Validations:
@classmethod
def is_phone(cls, value):
if bool(re.search(r'^(\+[1-9]\d{1,2}|00[1-9]\d{1,2}|0[1-9]{1,3})([ ()-]{,2}\d[ ()-]{,2}){6,11}[0-9]$', value)):
return True
return False
@classmethod
def is_email(cls, value):
if value.count('@') != 1:
return False
domain_host = value.split('@')
# da go proverq dali stana
# and and len(domain_host[0])<65 :
if bool(re.search(r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
if len(domain_host[0]) < 64 and cls.is_hostname(domain_host[1]):
return True
return False
@classmethod
def is_hostname(cls, value):
if bool(re.match(r'([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
new_hosts = value.split('.')
for host in new_hosts:
if len(host) > 64:
return False
return True
return False
@classmethod
def is_ip_address(cls, value):
value_for_expr = '.' + value
if re.match(r'(\.\d{1,4}){4,4}$', value_for_expr) != type(None):
list_of_bits = value.split('.')
if int(list_of_bits[0]) > 0 and int(list_of_bits[0]) < 256:
for i in list_of_bits:
if int(i) >= 0 and int(i) < 256:
continue
else:
return False
else:
return False
return True
return False
@classmethod
def is_number(cls, value):
if bool(re.match(r'(-?0\.\d|-?[1-9]|-?0$|-?[1-9]\d*\.\d)\d*$', value)):
return True
return False
@classmethod
def is_integer(cls, value):
if bool(re.search(r'(^-?0$|^-?[1-9]\d*$)', value)):
return True
return False
@classmethod
def is_date(cls, value):
date_dash = value + '-'
if bool(re.search(r'^\d{4,4}-(\d{2,2}-){2,2}$', date_dash)):
new_value = value.split('-')
if int(new_value[1]) > 0 and int(new_value[1]) < 13:
if int(new_value[2]) in range(1, 32) and int(new_value[1]) in [1, 3, 5, 7, 10, 12]:
return True
if int(new_value[2]) in range(1, 31) and int(new_value[1]) not in [1, 2, 3, 5, 7, 10, 12]:
return True
if int(new_value[1]) == 2 and calendar.isleap(int(new_value[0])) \
and int(new_value[2]) in range(1, 30):
return True
if int(new_value[1]) == 2 and not calendar.isleap(int(new_value[0])) \
and int(new_value[2]) in range(1, 29):
return False
return False
@classmethod
def is_time(cls, value):
# def is_hour(value):
hour_value = value + ':'
if bool(re.search(r'^(\d{1,2}:){3,3}$', hour_value)):
hour_parts = hour_value.split(':')
if int(hour_parts[0]) >= 0 and int(hour_parts[0]) < 24:
if int(hour_parts[1]) >= 0 and int(hour_parts[2]) >= 0:
if int(hour_parts[1]) < 60 and int(hour_parts[2]) < 60:
return True
return False
@classmethod
def is_datetime(cls, value):
if not bool(re.search(r'^\d{4,4}-\d{2,2}-\d{2,2} \d{2,2}:\d{2,2}:\d{2,2}$', value)):
return False
new_value = value.split(' ')
if cls.is_date(new_value[0]) and cls.is_time(new_value[1]):
return True
return False

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

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


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


======================================================================
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-7s0vpj/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).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-7s0vpj/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]-12-1 2 or...'
-  [PHONE] or...
+  [PHONE]-12-1 2 or...
?         +++++++


======================================================================
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-7s0vpj/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_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-7s0vpj/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-7s0vpj/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-7s0vpj/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_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-7s0vpj/test.py", line 185, in test_validates_IP_addresses
    self.assertTrue(solution.Validations.is_ip_address('0.0.0.0'))
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-7s0vpj/test.py", line 278, in test_validates_datetime_values
    self.assertTrue(solution.Validations.is_datetime('2012-11-19T19:00:00'))
AssertionError: False is not true

======================================================================
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-7s0vpj/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_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-7s0vpj/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.065s

FAILED (failures=13)

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

Кирил обнови решението на 21.04.2014 17:14 (преди над 10 години)

+def text_to_email_filtered(email, hostname, username):
+
+ if not bool(re.search(r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)$', email)):
+ return email
+
+ email_to_list = email.split(' ')
+ i = 0
+ value = email_to_list
+ for val in value:
+ match = re.search(
+ r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)$', val)
+
+ if match is not None and hostname == False and username == False:
+ value[i] = '[EMAIL] '
+
+ elif match is not None and hostname == True and username == False:
+ v = value[i].split('@')
+ value[i] = '[FILTERED]' + '@' + v[1] + ' '
+
+ elif match is not None and username == True:
+ v = value[i].split('@')
+ if len(v[0]) < 7:
+ value[i] = '[FILTERED]' + '@' + v[1] + ' '
+ else:
+ value[i] = str(value[i][0]) + str(value[i][1]) + \
+ str(value[i][2]) + '[FILTERED]' + \
+ '@' + v[1] + ' '
+
+ else:
+ value[i] = val + ' '
+ i = i + 1
+ new_text = ''.join(value)
+ new_text = new_text[:-1]
+ return new_text
+
+
+def text_to_tel(text, code):
+ phone = re.compile(
+ r'(\+[1-9]\d{,2}|00[1-9]\d{,2}|0[1-9]{,3})[ -()]{,2}([ -()]{,2}[0-9][ -()]{,2}){6,10}[0-9]')
+
+ if code == False:
+ iterator = phone.finditer(text)
+ for match in iterator:
+ # print(match.group(0))
+ start, end = match.span()
+ text = text[:start] + '[PHONE]' + text[end:]
+
+ if code == True:
+ iterator = phone.finditer(text)
+ for match in iterator:
+ start, end = match.span()
+
+ p = re.search('^(\+[1-9]\d{,2}|00[1-9]\d{,2})', match.group(0))
+
+ if p is not None:
+ text = text[:start] + p.group(0) + '[FILTERED]' + text[end:]
+ else:
+ text = text[:start] + '[FILTERED]' + text[end:]
+ return text
+
+
+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):
+ text = text_to_email_filtered(
+ self._text, self.preserve_email_hostname, self.partially_preserve_email_username)
+ text = text_to_tel(text, self.preserve_phone_country_code)
+ return text
+
+
+import calendar
+import re
+
+
+class Validations:
+
+ @classmethod
+ def is_phone(cls, value):
+ if bool(re.search(r'^(\+[1-9]\d{1,2}|00[1-9]\d{1,2}|0[1-9]{1,3})([ ()-]{,2}\d[ ()-]{,2}){6,11}[0-9]$', value)):
+ return True
+ return False
+
+ @classmethod
+ def is_email(cls, value):
+ if value.count('@') != 1:
+ return False
+ domain_host = value.split('@')
+ # da go proverq dali stana
+ # and and len(domain_host[0])<65 :
+ if bool(re.search(r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
+ if len(domain_host[0]) < 64 and cls.is_hostname(domain_host[1]):
+ return True
+ return False
+
+ @classmethod
+ def is_hostname(cls, value):
+ if bool(re.match(r'([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
+ new_hosts = value.split('.')
+ for host in new_hosts:
+ if len(host) > 64:
+ return False
+ return True
+ return False
+
+ @classmethod
+ def is_ip_address(cls, value):
+ value_for_expr = '.' + value
+ if re.match(r'(\.\d{1,4}){4,4}$', value_for_expr) != type(None):
+ list_of_bits = value.split('.')
+ if int(list_of_bits[0]) > 0 and int(list_of_bits[0]) < 256:
+ for i in list_of_bits:
+ if int(i) >= 0 and int(i) < 256:
+ continue
+ else:
+ return False
+ else:
+ return False
+ return True
+ return False
+
+ @classmethod
+ def is_number(cls, value):
+ if bool(re.match(r'(-?0\.\d|-?[1-9]|-?0$|-?[1-9]\d*\.\d)\d*$', value)):
+ return True
+ return False
+
+ @classmethod
+ def is_integer(cls, value):
+ if bool(re.search(r'(^-?0$|^-?[1-9]\d*$)', value)):
+ return True
+ return False
+
+ @classmethod
+ def is_date(cls, value):
+ date_dash = value + '-'
+ if bool(re.search(r'^\d{4,4}-(\d{2,2}-){2,2}$', date_dash)):
+ new_value = value.split('-')
+ if int(new_value[1]) > 0 and int(new_value[1]) < 13:
+ if int(new_value[2]) in range(1, 32) and int(new_value[1]) in [1, 3, 5, 7, 10, 12]:
+ return True
+ if int(new_value[2]) in range(1, 31) and int(new_value[1]) not in [1, 2, 3, 5, 7, 10, 12]:
+ return True
+ if int(new_value[1]) == 2 and calendar.isleap(int(new_value[0])) \
+ and int(new_value[2]) in range(1, 30):
+ return True
+
+ if int(new_value[1]) == 2 and not calendar.isleap(int(new_value[0])) \
+ and int(new_value[2]) in range(1, 29):
+ return False
+ return False
+
+ @classmethod
+ def is_time(cls, value):
+ # def is_hour(value):
+ hour_value = value + ':'
+ if bool(re.search(r'^(\d{1,2}:){3,3}$', hour_value)):
+ hour_parts = hour_value.split(':')
+ if int(hour_parts[0]) >= 0 and int(hour_parts[0]) < 24:
+ if int(hour_parts[1]) >= 0 and int(hour_parts[2]) >= 0:
+ if int(hour_parts[1]) < 60 and int(hour_parts[2]) < 60:
+ return True
+ return False
+
+ @classmethod
+ def is_datetime(cls, value):
+ if not bool(re.search(r'^\d{4,4}-\d{2,2}-\d{2,2} \d{2,2}:\d{2,2}:\d{2,2}$', value)):
+ return False
+ new_value = value.split(' ')
+ if cls.is_date(new_value[0]) and cls.is_time(new_value[1]):
+ return True
+ return False

Кирил обнови решението на 22.04.2014 02:56 (преди над 10 години)

def text_to_email_filtered(email, hostname, username):
- if not bool(re.search(r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)$', email)):
+ if not bool(re.search(r'[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', email)):
return email
email_to_list = email.split(' ')
i = 0
value = email_to_list
for val in value:
- match = re.search(
- r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)$', val)
+ match = re.search( r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)$', val)
if match is not None and hostname == False and username == False:
value[i] = '[EMAIL] '
elif match is not None and hostname == True and username == False:
v = value[i].split('@')
value[i] = '[FILTERED]' + '@' + v[1] + ' '
elif match is not None and username == True:
v = value[i].split('@')
if len(v[0]) < 7:
value[i] = '[FILTERED]' + '@' + v[1] + ' '
else:
value[i] = str(value[i][0]) + str(value[i][1]) + \
str(value[i][2]) + '[FILTERED]' + \
'@' + v[1] + ' '
else:
value[i] = val + ' '
i = i + 1
new_text = ''.join(value)
new_text = new_text[:-1]
return new_text
def text_to_tel(text, code):
phone = re.compile(
r'(\+[1-9]\d{,2}|00[1-9]\d{,2}|0[1-9]{,3})[ -()]{,2}([ -()]{,2}[0-9][ -()]{,2}){6,10}[0-9]')
if code == False:
iterator = phone.finditer(text)
for match in iterator:
# print(match.group(0))
start, end = match.span()
text = text[:start] + '[PHONE]' + text[end:]
if code == True:
iterator = phone.finditer(text)
for match in iterator:
start, end = match.span()
p = re.search('^(\+[1-9]\d{,2}|00[1-9]\d{,2})', match.group(0))
if p is not None:
- text = text[:start] + p.group(0) + '[FILTERED]' + text[end:]
+ text = text[:start] + p.group(0) + ' [FILTERED]' + text[end:]
+ iterator = phone.finditer(text)
else:
text = text[:start] + '[FILTERED]' + text[end:]
+ iterator = phone.finditer(text)
return text
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):
text = text_to_email_filtered(
self._text, self.preserve_email_hostname, self.partially_preserve_email_username)
text = text_to_tel(text, self.preserve_phone_country_code)
return text
import calendar
import re
class Validations:
@classmethod
def is_phone(cls, value):
if bool(re.search(r'^(\+[1-9]\d{1,2}|00[1-9]\d{1,2}|0[1-9]{1,3})([ ()-]{,2}\d[ ()-]{,2}){6,11}[0-9]$', value)):
return True
return False
@classmethod
def is_email(cls, value):
if value.count('@') != 1:
return False
domain_host = value.split('@')
# da go proverq dali stana
# and and len(domain_host[0])<65 :
if bool(re.search(r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
if len(domain_host[0]) < 64 and cls.is_hostname(domain_host[1]):
return True
return False
@classmethod
def is_hostname(cls, value):
if bool(re.match(r'([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
new_hosts = value.split('.')
for host in new_hosts:
if len(host) > 64:
return False
return True
return False
@classmethod
def is_ip_address(cls, value):
value_for_expr = '.' + value
if re.match(r'(\.\d{1,4}){4,4}$', value_for_expr) != type(None):
list_of_bits = value.split('.')
if int(list_of_bits[0]) > 0 and int(list_of_bits[0]) < 256:
for i in list_of_bits:
if int(i) >= 0 and int(i) < 256:
continue
else:
return False
else:
return False
return True
return False
@classmethod
def is_number(cls, value):
if bool(re.match(r'(-?0\.\d|-?[1-9]|-?0$|-?[1-9]\d*\.\d)\d*$', value)):
return True
return False
@classmethod
def is_integer(cls, value):
if bool(re.search(r'(^-?0$|^-?[1-9]\d*$)', value)):
return True
return False
@classmethod
def is_date(cls, value):
date_dash = value + '-'
if bool(re.search(r'^\d{4,4}-(\d{2,2}-){2,2}$', date_dash)):
new_value = value.split('-')
if int(new_value[1]) > 0 and int(new_value[1]) < 13:
if int(new_value[2]) in range(1, 32) and int(new_value[1]) in [1, 3, 5, 7, 10, 12]:
return True
if int(new_value[2]) in range(1, 31) and int(new_value[1]) not in [1, 2, 3, 5, 7, 10, 12]:
return True
if int(new_value[1]) == 2 and calendar.isleap(int(new_value[0])) \
and int(new_value[2]) in range(1, 30):
return True
if int(new_value[1]) == 2 and not calendar.isleap(int(new_value[0])) \
and int(new_value[2]) in range(1, 29):
return False
return False
@classmethod
def is_time(cls, value):
# def is_hour(value):
hour_value = value + ':'
if bool(re.search(r'^(\d{1,2}:){3,3}$', hour_value)):
hour_parts = hour_value.split(':')
if int(hour_parts[0]) >= 0 and int(hour_parts[0]) < 24:
if int(hour_parts[1]) >= 0 and int(hour_parts[2]) >= 0:
if int(hour_parts[1]) < 60 and int(hour_parts[2]) < 60:
return True
return False
@classmethod
def is_datetime(cls, value):
if not bool(re.search(r'^\d{4,4}-\d{2,2}-\d{2,2} \d{2,2}:\d{2,2}:\d{2,2}$', value)):
return False
new_value = value.split(' ')
if cls.is_date(new_value[0]) and cls.is_time(new_value[1]):
return True
return False

Кирил обнови решението на 23.04.2014 00:48 (преди над 10 години)

def text_to_email_filtered(email, hostname, username):
if not bool(re.search(r'[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', email)):
return email
email_to_list = email.split(' ')
i = 0
value = email_to_list
for val in value:
match = re.search( r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)$', val)
if match is not None and hostname == False and username == False:
value[i] = '[EMAIL] '
elif match is not None and hostname == True and username == False:
v = value[i].split('@')
value[i] = '[FILTERED]' + '@' + v[1] + ' '
elif match is not None and username == True:
v = value[i].split('@')
if len(v[0]) < 7:
value[i] = '[FILTERED]' + '@' + v[1] + ' '
else:
value[i] = str(value[i][0]) + str(value[i][1]) + \
str(value[i][2]) + '[FILTERED]' + \
'@' + v[1] + ' '
else:
value[i] = val + ' '
i = i + 1
new_text = ''.join(value)
new_text = new_text[:-1]
return new_text
-
def text_to_tel(text, code):
- phone = re.compile(
- r'(\+[1-9]\d{,2}|00[1-9]\d{,2}|0[1-9]{,3})[ -()]{,2}([ -()]{,2}[0-9][ -()]{,2}){6,10}[0-9]')
-
+ text1 = text
if code == False:
- iterator = phone.finditer(text)
- for match in iterator:
- # print(match.group(0))
- start, end = match.span()
- text = text[:start] + '[PHONE]' + text[end:]
+ p = next_phone(text1)
+ while p != []:
+ text1 = text1[:p[0]] + '[PHONE]' + text1[p[1]:]
+ p = next_phone(text1)
if code == True:
- iterator = phone.finditer(text)
- for match in iterator:
- start, end = match.span()
+ p = next_phone(text1)
+ while p != []:
+ kod = re.search('^(\+[1-9]\d{,2}|00[1-9]\d{,2})', text1[p[0]:])
+ if kod is not None:
+ text1 = text1[:p[0]] + kod.group(0) + ' [FILTERED]' + text1[p[1]:]
+ p = next_phone(text1)
+ else:
+ text1 = text1[:p[0]] + '[PHONE]' + text1[p[1]:]
+ p = next_phone(text1)
+ return text1
- p = re.search('^(\+[1-9]\d{,2}|00[1-9]\d{,2})', match.group(0))
- if p is not None:
- text = text[:start] + p.group(0) + ' [FILTERED]' + text[end:]
- iterator = phone.finditer(text)
- else:
- text = text[:start] + '[FILTERED]' + text[end:]
- iterator = phone.finditer(text)
- return text
+def next_phone(r):
+ phone = re.compile(
+ r'(\+[1-9]\d{,2}|00[1-9]\d{,2}|0[1-9]{,3})[ -()]{,2}([ -()]{,2}[0-9][ -()]{,2}){6,10}[0-9]')
+
+ chislo= re.compile(r'\d+')
+ iterator=phone.finditer(r)
+
+ list_match=[]
+ for match in iterator:
+ start, end=match.span()
+ list_match.append(start)
+ list_match.append(end)
+ break
+ return list_match
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):
text = text_to_email_filtered(
self._text, self.preserve_email_hostname, self.partially_preserve_email_username)
text = text_to_tel(text, self.preserve_phone_country_code)
return text
import calendar
import re
class Validations:
@classmethod
def is_phone(cls, value):
if bool(re.search(r'^(\+[1-9]\d{1,2}|00[1-9]\d{1,2}|0[1-9]{1,3})([ ()-]{,2}\d[ ()-]{,2}){6,11}[0-9]$', value)):
return True
return False
@classmethod
def is_email(cls, value):
if value.count('@') != 1:
return False
domain_host = value.split('@')
# da go proverq dali stana
# and and len(domain_host[0])<65 :
if bool(re.search(r'^[a-zA-Z0-9][a-zA-Z0-9-_\.+]{,200}@([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
if len(domain_host[0]) < 64 and cls.is_hostname(domain_host[1]):
return True
return False
@classmethod
def is_hostname(cls, value):
if bool(re.match(r'([a-zA-Z0-9]([a-zA-Z0-9-]{,61})[a-zA-z0-9])(((\.[a-zA-Z]{2,3})+)$)', value)):
new_hosts = value.split('.')
for host in new_hosts:
if len(host) > 64:
return False
return True
return False
@classmethod
def is_ip_address(cls, value):
value_for_expr = '.' + value
if re.match(r'(\.\d{1,4}){4,4}$', value_for_expr) != type(None):
list_of_bits = value.split('.')
if int(list_of_bits[0]) > 0 and int(list_of_bits[0]) < 256:
for i in list_of_bits:
if int(i) >= 0 and int(i) < 256:
continue
else:
return False
else:
return False
return True
return False
@classmethod
def is_number(cls, value):
if bool(re.match(r'(-?0\.\d|-?[1-9]|-?0$|-?[1-9]\d*\.\d)\d*$', value)):
return True
return False
@classmethod
def is_integer(cls, value):
if bool(re.search(r'(^-?0$|^-?[1-9]\d*$)', value)):
return True
return False
@classmethod
def is_date(cls, value):
date_dash = value + '-'
if bool(re.search(r'^\d{4,4}-(\d{2,2}-){2,2}$', date_dash)):
new_value = value.split('-')
if int(new_value[1]) > 0 and int(new_value[1]) < 13:
if int(new_value[2]) in range(1, 32) and int(new_value[1]) in [1, 3, 5, 7, 10, 12]:
return True
if int(new_value[2]) in range(1, 31) and int(new_value[1]) not in [1, 2, 3, 5, 7, 10, 12]:
return True
if int(new_value[1]) == 2 and calendar.isleap(int(new_value[0])) \
and int(new_value[2]) in range(1, 30):
return True
if int(new_value[1]) == 2 and not calendar.isleap(int(new_value[0])) \
and int(new_value[2]) in range(1, 29):
return False
return False
@classmethod
def is_time(cls, value):
# def is_hour(value):
hour_value = value + ':'
if bool(re.search(r'^(\d{1,2}:){3,3}$', hour_value)):
hour_parts = hour_value.split(':')
if int(hour_parts[0]) >= 0 and int(hour_parts[0]) < 24:
if int(hour_parts[1]) >= 0 and int(hour_parts[2]) >= 0:
if int(hour_parts[1]) < 60 and int(hour_parts[2]) < 60:
return True
return False
@classmethod
def is_datetime(cls, value):
if not bool(re.search(r'^\d{4,4}-\d{2,2}-\d{2,2} \d{2,2}:\d{2,2}:\d{2,2}$', value)):
return False
new_value = value.split(' ')
if cls.is_date(new_value[0]) and cls.is_time(new_value[1]):
return True
return False