Created
May 6, 2015 09:27
-
-
Save zagorulkinde/29b5c1060604a83ef483 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from sys import argv | |
from time import time | |
class Login(object): | |
def __init__(self): | |
self.r = re.compile('^([a-z]|[a-z][a-z0-9-\.]{0,18}[a-z0-9])$', re.I) | |
self.charmap_min = ord('a') | |
self.charmap_max = ord('z') | |
self.charmap_min_dig = ord('0') | |
self.charmap_max_dig = ord('9') | |
self.charmap_midle = [i for i in range(ord('a'), ord('z'))] | |
self.charmap_adds = [ord('-'), ord('.')] | |
self.charmap_digits = [i for i in range(ord('0'), ord('9'))] | |
def regexp(self, data): | |
start_time = time() | |
matcher = self.r.match(data) | |
print ('Exec time', time() - start_time) | |
return matcher is not None | |
def manual(self, data): | |
start_time = time() | |
data = data.lower() | |
if len(data) <= 20: | |
f = data[0] | |
l = data[-1] | |
d = data[1:-1] | |
if self.charmap_min <= ord(f) <= self.charmap_max: | |
if ord(l) in self.charmap_digits or self.charmap_midle: | |
for element in d: | |
if not (self.charmap_min <= ord(element) <= self.charmap_max\ | |
or ord(element) in self.charmap_adds\ | |
or ord(element) in self.charmap_digits): | |
print ('Not matched', time() - start_time) | |
return False | |
print ('Matched', time() - start_time) | |
return True | |
print ('Not matched', time() - start_time) | |
return False | |
def test(mails): | |
with open(mails) as f: | |
l = Login() | |
for mail in f: | |
l.manual(mail) | |
l.regexp(mail) | |
if __name__ == '__main__': | |
l = Login() | |
data = argv[2] | |
flag = argv[1] | |
if flag == '--regexp': | |
print l.regexp(data) | |
elif flag == '--naive': | |
print l.manual(data) | |
elif flag == '--selftest': | |
test(data) | |
else: | |
raise Exception("flag not matched") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment