Last active
June 14, 2017 17:22
-
-
Save seedifferently/c2ef1c068f20e32eebeec726b910caf1 to your computer and use it in GitHub Desktop.
Python ip counter dict __missing__ example
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
# define our custom counter dict | |
class IPCounter(dict): | |
def __missing__(self, key): | |
return 0 | |
ip_counts = IPCounter() | |
# ...other code that handles the request... | |
# increment the value (if the key is missing its initial value will be 0) | |
ip_counts[req.ip_address] += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment