Иван обнови решението на 21.04.2014 23:03 (преди над 10 години)
+import re
+
+
+class Validations:
+ @classmethod
+ def is_number(cls, value):
+ return bool(re.search(r'^-?0$|^-?[1-9]\d*$|^-?[1-9]\d*\.\d+$|^-?0\.\d+$', value))
+
+ @classmethod
+ def is_email(cls, value):
+ return bool(re.search(r'^[^\W_][a-zA-Z0-9\+\.\-_]{,200}@(([^\W_]\.)|([^\W_][0-9a-zA-Z\-]{,61}[^\W_])\.)+[a-zA-Z]{2,3}(\.[A-Za-z]{2})?$', value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.search(r'^(([^\W_]\.)|([^\W_][0-9a-zA-Z\-]{,61}[^\W_])\.)+[a-zA-Z]{2,3}(\.[A-Za-z]{2})?$', value))
+
+ @classmethod
+ def is_phone(cls, value):
+ local = r'^0([\s()\-]{,2}\d){6,11}$'
+ international = r'^(00|\+)[1-9]\d{0,2}([\s()\-]{,2}\d){6,11}$'
+ return bool(re.search(local, value)) or bool(re.search(international, value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.search(r'^(\d\.|[1-9]\d\.|1\d\d\.|2[0-5][0-5]\.){3}(\d|[1-9]\d|1\d\d|2[0-5][0-5])$', value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.search(r'^-?0$|^-?[1-9]\d*$', value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.search(r'^\d{4}-(0[1-9]|1[0-2])-([0-2][1-9]|3[0-1])$', value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.search(r'^([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$', value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ match = re.search(r'^(\S*)[T ](\S*)$', value)
+ if not match:
+ return False
+ return Validations.is_date(match.group(1)) and Validations.is_time(match.group(2))
+
+
+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):
+ if self.preserve_phone_country_code:
+ return self.local_phone_filtered(self.partially_inter_phone(text))
+ else:
+ return self.local_phone_filtered(self.inter_phone_filtered(text))
+
+ def partially_inter_phone(self, text):
+ pattern = r'((00|\+)[1-9]\d{0,2})([\s()\-]{,2}\d){6,11}\b'
+ return re.sub(pattern, r'\1 [FILTERED]', text)
+
+ def inter_phone_filtered(self, text):
+ pattern = r'(00|\+)[1-9]\d{0,2}([\s()\-]{,2}\d){6,11}\b'
+ return re.sub(pattern, r'[PHONE]', text)
+
+ def local_phone_filtered(self, text):
+ pattern = r'\b0([\s()\-]{,2}\d){6,11}\b'
+ return re.sub(pattern, r'[PHONE]', text)
+
+ def email_filtered(self, text):
+ if self.partially_preserve_email_username:
+ return self.filtered_hostname(self.partially_filtered_hostname(text))
+ elif self.preserve_email_hostname:
+ return self.filtered_hostname(text)
+ else:
+ return self.normal_filtered_email(text)
+
+ def partially_filtered_hostname(self, text):
+ pattern = r'\b([^\W_][\w\+\.\-]{2})[\w\+\.\-]{3,198}(@((([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b))'
+ return re.sub(pattern, r'\1[FILTERED]\2', text)
+
+ def filtered_hostname(self, text):
+ pattern = r'\b[^\W_][\w\+\.\-]{,200}@((([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b)'
+ return re.sub(pattern, r'[FILTERED]@\1', text)
+
+ def normal_filtered_email(self, text):
+ pattern = r'\b[^\W_][\w\+\.\-]{,200}@(([^\W_]\.)|([^\W_][\-\dA-Za-z]{,60}[^\W_])\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
+ return re.sub(pattern, r'[EMAIL]', text)