From f3fa1f0720007de35edc189713c00c68bcd6d0f5 Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Tue, 2 Aug 2022 21:22:22 +0200 Subject: [PATCH] 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* | '' | --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5cbe1f4..672abca 100644 --- a/README.md +++ b/README.md @@ -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: