Теодор обнови решението на 19.04.2014 04:23 (преди над 10 години)
+import re
+
+
+class PrivacyFilter():
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+ self.phone_pattern = Validations.phone_pattern[1:-1]
+ self.email_pattern = Validations.email_pattern[1:-1]
+
+ def filtered(self):
+ filtered_text = self.text
+
+ # Phone Filter
+ if self.preserve_phone_country_code:
+ phone_match_obj = re.search(self.phone_pattern, filtered_text)
+ country_code = phone_match_obj.group('code')
+ if country_code != '0':
+ filtered_text = re.sub(self.phone_pattern,
+ country_code+' [FILTERED]',
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text)
+
+ if self.partially_preserve_email_username:
+ self.preserve_email_hostname = True
+
+ # Email Filter
+ if self.preserve_email_hostname:
+ email_match_obj = re.search(self.email_pattern, filtered_text)
+ email_hostname = email_match_obj.group('hostname')
+ prefix_to_add = ''
+ if self.partially_preserve_email_username and \
+ len(email_match_obj.group('prefix')) >= 6:
+ prefix_to_add = email_match_obj.group('prefix')[:3]
+
+ filtered_text = re.sub(self.email_pattern,
+ prefix_to_add+'[FILTERED]@'+email_hostname,
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.email_pattern,
+ '[EMAIL]',
+ filtered_text)
+
+ return filtered_text
+
+
+class Validations():
+ hostname_pattern = \
+ (r'^(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
+ r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*)$')
+
+ email_pattern = \
+ (r'^(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
+ r'(?P<hostname>(([a-zA-Z0-9]{,63}|'
+ r'[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
+ r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*))$')
+
+ phone_pattern = \
+ (r'^(?P<code>0|(00|\+)[1-9][0-9]{,2})'
+ r'([\ \-\(\)]{,2}[0-9]){6,11}$')
+
+ ip_address_pattern = \
+ (r'^((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
+
+ number_pattern = \
+ (r'^\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
+
+ integer_pattern = \
+ (r'^\-*(0|[1-9][0-9]*)$')
+
+ date_pattern = \
+ (r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
+
+ time_pattern = \
+ (r'^([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
+
+ datetime_pattern = \
+ (r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
+ r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
+
+ def __init__():
+ pass
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.search(cls.hostname_pattern, value))
+
+ @classmethod
+ def is_email(cls, value):
+ return bool(re.search(cls.email_pattern, value))
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(re.search(cls.phone_pattern, value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.search(cls.ip_address_pattern, value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(re.search(cls.number_pattern, value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.search(cls.integer_pattern, value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.search(cls.date_pattern, value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.search(cls.time_pattern, value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return bool(re.search(cls.datetime_pattern, value))