Милица обнови решението на 22.04.2014 16:46 (преди над 10 години)
+import re
+
+
+HOST_NAME_PATTERN = '([\da-zA-Z][\da-zA-Z-]{,61}[\da-zA-Z]\.|[\da-zA-Z]\.)+'\
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+
+NAME_PATTERN = '[\da-zA-z][\da-zA-z-_\+\.]{,200}'
+
+EMAIL_PATTERN = NAME_PATTERN + '@' + HOST_NAME_PATTERN
+
+I_CODE_PATTERN = '(0{2}|\+)[1-9][0-9]{,2}'
+
+PHONE_NUMBER_PATTERN = '(([\s\-\(\)]){,2}[0-9]){5,10}'
+
+LOCAL_PHONE_PATTERN = '0([\s\-\(\)])?[1-9]' + PHONE_NUMBER_PATTERN
+
+INTERNATIONAL_PHONE_PATTERN = I_CODE_PATTERN + PHONE_NUMBER_PATTERN
+
+PHONE_PATTERN = '(' + LOCAL_PHONE_PATTERN + '|' + \
+ INTERNATIONAL_PHONE_PATTERN + ')'
+
+IP_PATTERN = '((1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}'\
+ '(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])'
+
+NUMBER_PATTERN = '-?(0|[1-9][0-9]*)((.|,)[0-9]+)?'
+
+INTEGER_PATTERN = '-?(0|[1-9][0-9]*)'
+
+DATE_PATTERN = '[0-9]{4}-(0[1-9]|1[1-2])-([1-2][0-9]|3[0-1]|0[1-9])'
+
+TIME_PATTERN = '([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]'
+
+DATE_TIME_PATTERN = DATE_PATTERN + '(\s|T)' + TIME_PATTERN
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.data = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ if bool(re.search(re.compile(EMAIL_PATTERN), self.data)):
+ if self.preserve_email_hostname or \
+ self.partially_preserve_email_username:
+ if self.partially_preserve_email_username and\
+ len(re.search(re.compile(NAME_PATTERN + '@'), self.data)\
+ .group()) > 7:
+ text = re.split(re.compile(EMAIL_PATTERN), self.data)
+ email = re.search(re.compile(EMAIL_PATTERN),\
+ self.data).group()
+ return text[0] + email[:3] + re.sub(r'[\da-zA-z-_+.]{,200}@', \
+ '[FILTERED]@', email[3:])
+ return re.sub(re.compile(NAME_PATTERN + '@'), '[FILTERED]@', \
+ self.data)
+ return re.sub(re.compile(EMAIL_PATTERN), '[EMAIL]', self.data)
+ if bool(re.search(re.compile(PHONE_PATTERN), self.data)):
+ if self.preserve_phone_country_code and\
+ bool(re.search(re.compile(INTERNATIONAL_PHONE_PATTERN), self.data)):
+ code = re.search(re.compile(I_CODE_PATTERN), self.data).group()
+ phone = re.search(re.compile(PHONE_NUMBER_PATTERN), self.data).group()
+ text = re.split(re.compile(PHONE_PATTERN), self.data)
+ return text[0] + code + re.sub(re.compile(PHONE_NUMBER_PATTERN), \
+ ' [FILTERED]', phone)
+ return re.sub(re.compile(PHONE_PATTERN), '[PHONE]', self.data)
+ return self.data
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, value):
+ return bool(re.match(re.compile(EMAIL_PATTERN + '$'), value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.match(re.compile(HOST_NAME_PATTERN + '$'), value))
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(re.match(re.compile(PHONE_PATTERN + '$'), value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.match(re.compile(IP_PATTERN + '$'), value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(re.match(re.compile(NUMBER_PATTERN + '$'), value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.match(re.compile(INTEGER_PATTERN + '$'), value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.match(re.compile(DATE_PATTERN + '$'), value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.match(re.compile(TIME_PATTERN + '$'), value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return bool(re.match(re.compile(DATE_TIME_PATTERN + '$'), value))
Имам и едно въпросче. Ако в DATE_TIME_PATTERN
за разделител сложа \b
, а не \s
примерните тестове не минават. От къде може да идва този проблем?