This file contains 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
pipeline = [ | |
{ | |
"$match": { | |
"$and": query | |
} | |
}, | |
{"$group": {"_id": "$user_id", "user_id": {"$sum": 1}}} | |
] |
This file contains 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
import socket | |
import threading | |
from StringIO import StringIO | |
import paramiko | |
HOST, PORT = 'localhost', 2222 | |
HOSTKEY = """ | |
-----BEGIN EC PRIVATE KEY----- | |
MHcCAQEEINnMNKzog0Cbnun+EKuFu4kcpc0iKJALw5LkC1E1x+9EoAoGCCqGSM49 | |
AwEHoUQDQgAEt11fabDR77CPL/1OUfayQ/m3C1uRI/039pBmYagk4zQ4Mxwow6L6 |
This file contains 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
import signal | |
import Queue | |
def handler(signum, frame): | |
q.put(None) | |
signal.signal(signal.SIGTERM, handler) | |
signal.signal(signal.SIGINT, handler) | |
q = Queue.Queue() |
This file contains 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
import signal | |
import Queue | |
def handler(signum, frame): | |
global shutdown | |
# Don't log here, not reentrant | |
shutdown = True | |
signal.signal(signal.SIGTERM, handler) | |
signal.signal(signal.SIGINT, handler) |
This file contains 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
import threading | |
import socket | |
import Queue | |
def accept(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind(("", 65021)) | |
s.listen(5) | |
s.accept() |
This file contains 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
bounds_w = 100 | |
bounds_h = 100 | |
im_w, im_h = im.size | |
scales = (bounds_w / im_w, bounds_h / im_h) | |
if scale_type == "meet": | |
scale = min(scales) | |
elif scale_type == "fill": | |
scale = max(scales) |
This file contains 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
class Enum(object): | |
def __init__(self, **kwargs): | |
self._values = set() | |
for k, v in kwargs.items(): | |
setattr(self, k, v) | |
self._values.add(v) | |
def validate(self, values): | |
return set(values).issubset(self._values) |
This file contains 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
from abc import ABCMeta | |
class ListLike: | |
__metaclass__ = ABCMeta | |
ListLike.register(list) | |
ListLike.register(tuple) | |
ListLike.register(_FieldSequence) | |
>>> isinstance([], ListLike) |
This file contains 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
struct tm time; | |
strptime("1934-11-02T10:45:11", "%FT%T%z", &time); | |
long ts = timegm(&time); | |
NSDate *d = [NSDate dateWithTimeIntervalSince1970:ts]; | |
NSLog(@"%@", d); |
This file contains 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
[aw@jello tmp] $ python sort-slice.py | |
45920256 | |
[aw@jello tmp] $ python sort-heapq.py | |
5808128 |