Гергана обнови решението на 23.04.2014 00:57 (преди над 10 години)
+import re
+
+USER_PATTERN = r'\w[\w\+\.-]{,200}'
+HOSTNAME_PATTERN = r'([a-zA-Z\d][\w-]{,61}[^-]\.)+([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)'
+NUMBER_PATTERN = r'-?(0|[1-9]\d*)(\.\d+)?'
+INTEGER_PATTERN = r'-?(0|[1-9]\d*)'
+DATE_PATTERN = r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[1-2]\d|3[01])'
+TIME_PATTERN = r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d'
+INTERNATIONAL_PHONE_PATTERN = r'((00|\+)[1-9]{1,3})([ \(\)-]{,2}\d)+\d'
+LOCAL_PHONE_PATTERN = r'0[ \(\)-]{,2}[1-9]([ \(\)-]{,2}\d){5,10}\d'
+
+
+class PrivacyFilter:
+
+ def __init__(self, text):
+ self.text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ return self.phone_filtered(self.email_filtered(self.text))
+
+ def phone_filtered(self, text):
+ return self.local_phone_filtered(self.inter_phone_filtered(text))
+
+ def local_phone_filtered(self, text):
+ return re.sub(LOCAL_PHONE_PATTERN, '[PHONE]', text)
+
+ def inter_phone_filtered(self, text):
+ if self.preserve_phone_country_code:
+ return re.sub(INTERNATIONAL_PHONE_PATTERN, r'\1 [FILTERED]', text)
+ else:
+ return re.sub(INTERNATIONAL_PHONE_PATTERN, '[FILTERED]', text)
+
+ def filter_email_match(self, match):
+ if self.partially_preserve_email_username:
+ if len(match.group(1)) < 6:
+ return '[FILTERED]@' + match.group(2)
+ else:
+ return match.group(1)[:3] + '[FILTERED]@' + match.group(2)
+ elif self.preserve_email_hostname:
+ return '[FILTERED]@' + match.group(2)
+ else:
+ return '[EMAIL]'
+
+ def email_filtered(self, text):
+ pattern = '(' + USER_PATTERN + ')' + '@' + '(' + HOSTNAME_PATTERN + ')'
+ return re.sub(pattern, self.filter_email_match, text)
+
+
+class Validations:
+ @staticmethod
+ def full_match(pattern, text):
+ return re.match(pattern, text)
+
+ @classmethod
+ def is_email(cls, value):
+ user, hostname = value.split('@')
+ if cls.full_match(USER_PATTERN, user) and cls.is_hostname(hostname):
+ return True
+ return False
+
+ @classmethod
+ def is_phone(cls, value):
+ numbers = re.sub(r'^(00|\+)', '', re.sub(r'[ \(\)-]', '', value))
+ if re.match(r'00|\+', value) and len(numbers) in range(8, 13):
+ if cls.full_match(INTERNATIONAL_PHONE_PATTERN, value):
+ return True
+ elif re.match(r'0', value) and len(numbers) in range(7, 12):
+ if cls.full_match(LOCAL_PHONE_PATTERN, value):
+ return True
+ else:
+ return False
+
+ @classmethod
+ def is_hostname(cls, value):
+ if cls.full_match(HOSTNAME_PATTERN, value):
+ return True
+ return False
+
+ @classmethod
+ def is_ip_address(cls, value):
+ ip_address = value.split('.')
+ return len(ip_address) == 4 and \
+ all(int(byte) in range(0, 256) for byte in ip_address)
+
+ @classmethod
+ def is_number(cls, value):
+ if cls.full_match(NUMBER_PATTERN, value):
+ return True
+ return False
+
+ @classmethod
+ def is_integer(cls, value):
+ if cls.full_match(INTEGER_PATTERN, value):
+ return True
+ return False
+
+ @classmethod
+ def is_date(cls, value):
+ if cls.full_match(DATE_PATTERN, value):
+ return True
+ return False
+
+ @classmethod
+ def is_time(cls, value):
+ if cls.full_match(TIME_PATTERN, value):
+ return True
+ return False
+
+ @classmethod
+ def is_datetime(cls, value):
+ try:
+ date, time = re.split(r'[ T]', value)
+ return cls.is_date(date) and cls.is_time(time)
+ except ValueError:
+ return False