Ралица обнови решението на 19.04.2014 16:35 (преди над 10 години)
+import re
+
+
+PHONE = r'(((\+|00)[1-9]\d{,2})|0)[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
+INTER = r'((\+|\b00)[1-9]\d{,2})[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
+HOST = r'(([^\W_](([^\W_]|-){,61}[^\W_])?\.)+[a-zA-Z]{2,3}(\.[a-zA-z]{2})?)'
+EMAIL = r'([^\W_][\w\.\+-]{,2})[\w\.\+-]{,198}@' + HOST
+DATE = r'\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|[1-2][0-9]|3[0-1])'
+TIME = r'([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])'
+IP = r'(0|[1-9]\d?|1\d\d|2[0-5][0-5])(\.(0|[1-9]\d?|1\d\d|2[0-5][0-5])){3}'
+
+
+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):
+ return self.phone_filtered(self.email_filtered())
+
+ def phone_filtered(self, text):
+ if self.preserve_phone_country_code:
+ return self.phone_filtration(self.preserve_country_code(text))
+ return self.phone_filtration(text)
+
+ def phone_filtration(self, text):
+ return re.sub(r'(((\+|\b00)[1-9]\d{,2})|\b0)[-\s\(\)]{,2}[1-9]'
+ + r'([-\s\(\)]{,2}\d){5,10}\b',
+ '[PHONE]', text)
+
+ def email_filtered(self):
+ if self.partially_preserve_email_username:
+ return self.preserve_hostname(self.partially_preserve_username())
+ elif self.preserve_email_hostname:
+ return self.preserve_hostname(self.text)
+ return re.sub(r'\b' + EMAIL + r'\b', '[EMAIL]', self.text)
+
+ def preserve_country_code(self, text):
+ return re.sub(INTER + r'\b', r'\1 [FILTERED]', text)
+
+ def preserve_hostname(self, text):
+ return re.sub(r'\b' + EMAIL + r'\b', r'[FILTERED]@\2', text)
+
+ def partially_preserve_username(self):
+ return re.sub(r'\b' + r'([^\W_][\w\.\+-]{2})[\w\.\+-]{3,198}@' +
+ HOST + r'\b', r'\1[FILTERED]@\2', self.text)
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, value):
+ return re.match(r'^' + EMAIL + r'$', value) is not None
+
+ @classmethod
+ def is_phone(cls, value):
+ return re.match(r'^' + PHONE + r'$', value) is not None
+
+ @classmethod
+ def is_hostname(cls, value):
+ return re.match(r'^' + HOST + r'$', value) is not None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return re.match(r'^' + IP + r'$', value) is not None
+
+ @classmethod
+ def is_number(cls, value):
+ return re.match(r'^\-?(0|\d\.\d+|[1-9]\d*)$', value) is not None
+
+ @classmethod
+ def is_integer(cls, value):
+ return re.match(r'^\-?(0|[1-9]\d*)$', value) is not None
+
+ @classmethod
+ def is_date(cls, value):
+ return re.match(r'^' + DATE + r'$', value) is not None
+
+ @classmethod
+ def is_time(cls, value):
+ return re.match(r'^' + TIME + r'$', value) is not None
+
+ @classmethod
+ def is_datetime(cls, value):
+ return re.match(r'^' + DATE + r'\s' + TIME + r'$', value) is not None