Таня обнови решението на 23.04.2014 15:07 (преди над 10 години)
+import re
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.unfiltered_text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ self.phone_pattern = r"(0|(00|\+)[1-9]\d{,2})((\b[)( -]){,2}\d){6,11}\b"
+ self.phone_country_code_pattern = r"^(0|(00|\+)[1-9]\d{,2})"
+ self.email_pattern = r"\b[a-zA-Z0-9]((\w|[+.-])){0,200}@[1-9a-zA-Z](([0-9a-zA-Z]|-){,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b"
+
+ def filtered(self):
+ filtered_text = self.unfiltered_text
+
+ email = re.search(self.email_pattern, filtered_text)
+ while email:
+ start, end = email.span()
+ filtered = self.filter_email(filtered_text[start:end])
+ filtered_text = filtered_text[:start] + filtered + filtered_text[end:]
+ email = re.search(self.email_pattern, filtered_text)
+
+ phone = re.search(self.phone_pattern, filtered_text)
+ while phone:
+ start, end = phone.span()
+ filtered = self.filter_phone(filtered_text[start:end])
+ filtered_text = filtered_text[:start] + filtered + filtered_text[end:]
+ phone = re.search(self.phone_pattern, filtered_text)
+
+ return filtered_text
+
+ def filter_phone(self, phone):
+ if self.preserve_phone_country_code:
+ country_code = re.search(self.phone_country_code_pattern, phone)
+ length_country_code = country_code.span()[1]
+ filtered_phone = phone[:length_country_code] + " " + "[FILTERED]"
+ else:
+ filtered_phone = "[PHONE]"
+ return filtered_phone
+
+ def filter_email(self, email):
+ if not self.preserve_email_hostname and not self.partially_preserve_email_username:
+ filtered_email = "[EMAIL]"
+ elif self.preserve_email_hostname and not self.partially_preserve_email_username:
+ hostname_start = email.find('@')
+ filtered_email = "[FILTERED]" + email[hostname_start:]
+ elif self.partially_preserve_email_username:
+ hostname_start = email.find('@')
+ filtered_email = email[:3] + "[FILTERED]" + email[hostname_start:]
+ return filtered_email
+
+
+class Validations:
+
+ def is_email(email):
+ if not '@' in email:
+ return False
+
+ hostname = re.split('@', email)[1]
+ if not Validations.is_hostname(hostname):
+ return False
+
+ username = r"^[a-zA-Z0-9]((\w|[+.-])){0,200}@"
+ return bool(re.match(username, email))
+
+ def is_phone(phone):
+ phone_pattern = r"^(0|(00|\+)[1-9]\d{,2})((\b[)( -]){,2}\d){6,11}$"
+ return bool(re.match(phone_pattern, phone))
+
+ def is_hostname(hostname):
+ hostname_pattern = r"^[1-9a-zA-Z](([0-9a-zA-Z]|-){,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$"
+ return bool(re.match(hostname_pattern, hostname))
+
+ def is_ip_address(ip_address):
+ ip_pattern = r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
+ return bool(re.match(ip_pattern, ip_address))
+
+ def is_number(number):
+ number_pattern = r"^-?(0|[1-9]\d*)(\.\d+)?$"
+ return bool(re.match(number_pattern, number))
+
+ def is_integer(integer):
+ integer_pattern = r"^-?(0|[1-9]\d*)$"
+ return bool(re.match(integer_pattern, integer))
+
+ def is_date(date):
+ date_pattern = r"^([0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))$"
+ return bool(re.match(date_pattern, date))
+
+ def is_time(time):
+ time_pattern = r"^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"
+ return bool(re.match(time_pattern, time))
+
+ def is_datetime(datetime):
+ parsed_datetime = re.split(r"[ T]", datetime)
+ if len(parsed_datetime) != 2:
+ return False
+ date = parsed_datetime[0]
+ time = parsed_datetime[1]
+ return Validations.is_date(date) and Validations.is_time(time)