Skip to content

Commit

Permalink
Merge pull request #35 from hslatman/hs_python_mailchecker
Browse files Browse the repository at this point in the history
Add a simple Python template
  • Loading branch information
FGRibreau committed Sep 29, 2015
2 parents 702ba0a + 31e6639 commit ca0adb2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
npm-*.log
compare.js
*.pyc
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ if(!MailChecker('myemail.com')){
}
```


### Python
```python
# no package yet; just drop in MailChecker.py where you want to use it.
import MailChecker
m = MailChecker.MailChecker()

if not m.is_valid('bla@example.com'):
print "O RLY !"
```

--------------------


Expand Down
23 changes: 23 additions & 0 deletions platform/python/MailChecker.py

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions platform/python/MailChecker.tmpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import re

class MailChecker(object):

def __init__(self):
self.list_string = """{{list}}"""

# valid email format regex source: https://gist.github.com/dideler/5219706
self.email_regex = """([a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`"
"{|}~-]+)*(@|\sat\s)(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(\.|"
"\sdot\s))+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)"""
self.fake_matcher = re.compile('|'.join(map(lambda x: '\\b' + x.lower() + '$', self.list_string.split(","))))
self.valid_matcher = re.compile(self.email_regex)

def is_valid(self, email):
email = email.lower()
return self.is_valid_email_format(email) and self.fake_matcher.search(email) == None

def is_valid_email_format(self, email):
if email and email != '':
return self.valid_matcher.search(email) != None
else:
return False
18 changes: 18 additions & 0 deletions test/platform.python.test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# compatibility with both Python 2.x and 3.x
from __future__ import print_function

# for testing only; add the python MailChecker to the system path
import os, sys
sys.path.insert(0, '../platform/python/')

# import MailChecker, create instance, check emails
import MailChecker

m = MailChecker.MailChecker()

print( m.is_valid('bla@example.com'))
print( m.is_valid('bla@yourlms.biz'))
print( m.is_valid('example@you.it'))
print( m.is_valid('bla@hotmail.com'))
print( m.is_valid('bla@yui.it'))
print( m.is_valid(''))

0 comments on commit ca0adb2

Please sign in to comment.