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
ip_timestamps = collections.defaultdict(list) | |
# ...other code that handles the web service request... | |
ip_timestamps[req.ip_address].append(time.time()) |
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
# 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()] |
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) |
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
# 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 |
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
>>> 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! |
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
>>> d = {'food': 'spam'} | |
>>> 'food' in d | |
True | |
>>> 'missing' in d | |
False | |
>>> 'missing' not in d | |
True |
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
>>> d['missing'] | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
KeyError: 'missing' |
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
>>> # 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'] |
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
package main | |
import ( | |
"net/http" | |
"io" | |
"strconv" | |
) | |
func HelloServer(c http.ResponseWriter, req *http.Request) { | |
body := "Hello World\n" |
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
angular.module 'app' | |
.directive 'apiErrorMessage', -> | |
dir = {} | |
dir.restrict = 'A' | |
dir.scope = | |
field: '=apiErrorMessage' | |
dir.priority = 100 | |
dir.transclude = true |
NewerOlder