Skip to content

Instantly share code, notes, and snippets.

@seedifferently
Last active June 14, 2017 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seedifferently/c2ef1c068f20e32eebeec726b910caf1 to your computer and use it in GitHub Desktop.
Save seedifferently/c2ef1c068f20e32eebeec726b910caf1 to your computer and use it in GitHub Desktop.
Python ip counter dict __missing__ example
# 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