Improvements for the Python example code (#329)

* docs(readme): Fix missing `open()` in Python example code

* docs(readme): Use set instead of list in Python example code

This reduces the computational complexity of a list lookup from O(n) to
O(1).

* docs(readme): Replace `split()` with `partition()` in Python example

The will always work and never throw an exception, even if `email` is
in fact not a valid email address or doesn't contain an `@` character at
all. It also prevents unexpected behavior if `email` contains multiple
`@` signs, i.e.:

| `email`       | Lookup Before | Lookup After  |
| ------------- | ------------- | ------------- |
| 'foo@bar'     | 'bar'         | 'bar'         |
| 'foo@bar@baz' | 'bar'         | 'bar@baz'     |
| 'foo'         | *Exception*   | ''            |
This commit is contained in:
Jan Holthuis 2022-08-02 21:22:22 +02:00 committed by GitHub
parent 2258b1b6f9
commit f3fa1f0720
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,9 +15,9 @@ Example Usage
=============
**Python**
```Python
blocklist = ('disposable_email_blocklist.conf')
blocklist_content = [line.rstrip() for line in blocklist.readlines()]
if email.split('@')[1] in blocklist_content:
with open('disposable_email_blocklist.conf') as blocklist:
blocklist_content = {line.rstrip() for line in blocklist.readlines()}
if email.partition('@')[2] in blocklist_content:
message = "Please enter your permanent email address."
return (False, message)
else: