Skip to content

Instantly share code, notes, and snippets.

View seedifferently's full-sized avatar

Seth Davis seedifferently

View GitHub Profile
@seedifferently
seedifferently / ip_logger_defaultdict_ex.py
Last active June 14, 2017 17:26
Python ip logger defaultdict example
ip_timestamps = collections.defaultdict(list)
# ...other code that handles the web service request...
ip_timestamps[req.ip_address].append(time.time())
@seedifferently
seedifferently / ip_logger_dict_ex.py
Created June 14, 2017 14:46
Python ip logger dict example
# assuming "ip_timestamps" is our dict and "req.ip_address" is the requestor's IP address
if req.ip_address in ip_timestamps:
# append the timestamp
ip_timestamps[req.ip_address].append(time.time())
else:
# log the initial timestamp
ip_timestamps[req.ip_address] = [time.time()]
@seedifferently
seedifferently / ip_counter_dict_missing_ex.py
Last active June 14, 2017 17:22
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)
@seedifferently
seedifferently / ip_counter_dict_ex.py
Created June 14, 2017 14:44
Python ip counter dict example
# assuming "ip_counts" is our dict and "req.ip_address" is the requestor's IP address
if req.ip_address in ip_counts:
# increment the count
ip_counts[req.ip_address] += 1
else:
# set an initial count
ip_counts[req.ip_address] = 1
@seedifferently
seedifferently / dict_get_ex.py
Last active June 14, 2017 14:53
Python dict get method
>>> print(d.get('food'))
spam
>>> print(d.get('missing'))
None
>>> print(d.get('missing', 'I did not find a key by that name!'))
I did not find a key by that name!
@seedifferently
seedifferently / check_dict_key_ex.py
Last active June 14, 2017 17:13
Python dict check key
>>> d = {'food': 'spam'}
>>> 'food' in d
True
>>> 'missing' in d
False
>>> 'missing' not in d
True
@seedifferently
seedifferently / dict_keyerror_ex.py
Last active June 15, 2017 13:13
Python dict keyerror
>>> d['missing']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'missing'
@seedifferently
seedifferently / dict_ex.py
Last active June 15, 2017 13:13
Python dict examples
>>> # create an empty dict
>>> d = {}
>>> # set a key/value pair
>>> d['food'] = 'spam'
>>> # retrieve a value for the key
>>> d['food']
'spam'
>>> # remove a key/value pair
>>> del d['food']
@seedifferently
seedifferently / gowebserver.go
Created November 7, 2014 02:33
Example Go webserver with combined http and https support
package main
import (
"net/http"
"io"
"strconv"
)
func HelloServer(c http.ResponseWriter, req *http.Request) {
body := "Hello World\n"
@seedifferently
seedifferently / api_error_message_directive.coffee
Created October 27, 2014 15:57
AngularJS API Error Message directive
angular.module 'app'
.directive 'apiErrorMessage', ->
dir = {}
dir.restrict = 'A'
dir.scope =
field: '=apiErrorMessage'
dir.priority = 100
dir.transclude = true