I personally find it hard to fault your code. Actually I'm quite surprised about the absence of code.
Other than a few PEP8 errors there is one changeare three changes that I would recommend.
You remove allboth \\
and \"
from your quotes, but you do it in an overly verbose way:
stripped = quote.replace('\\\\', '').replace('\\"', '"').strip('".')
Instead you can use re.sub
:
stripped = re.sub(r'\\[\\"]', '', quote).strip('".')
This way you read that it can replace eitherboth \\
orand \"
at the same time for slightly better readability.
I don't know if there is much of a performance difference, however.
I would also add another function. Currently, as currently you will use the validate function as follows:
try:
validate('')
except:
# Handle non-valid email
else:
# Handle valid email
Personally this is a lot of boilerplate if all you wish to know is if it is valid.
For these cases I would recommend that you make aan is_valid
function.
This would change the above to:
if is_valid(''):
# Handle valid email
else:
# Handle non-valid email
It can help readability in cases where you don't want to know the error. Which probably isn't how you want it to be used, with all the helpful errors. But is a way I know I would want to use it.
All your functions are public which encourages me to do:
import email
email.valid_quotes('joe@domain')
This isshould be a private function, that
that I probably shouldn't be using, and
and so you should name it _valid_quotes
.
Whilst it can still be used the same way,
it's now it's a 'Python private'.
And follows how re.py defines itits functions.
And as @Mathias said you should also add __all__
too.
Other than the above three points, you have a few PEP8 errors you may not have picked up on. But therethey're quite petty:
-
Surround top-level function and class definitions with two blank lines.
You have totoo much whitespace around your imports, two blank lines would be enough (Which would still go against PEP8).
You didn't indent enough on two of the errors in
validate
.You have one line that goes above 79 chars.