Last active
March 12, 2016 02:47
-
-
Save woozyking/aca171bc3f363a78bbb4 to your computer and use it in GitHub Desktop.
techies examples
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 techies import CountQueue | |
q = CountQueue(key='demo_q', host='localhost', port=6379, db=0) | |
# put, or enqueue | |
q.put('lol') | |
q.put('dota') | |
q.put('skyrim') | |
q.put('dota') # increment the count of the existing 'dota' | |
# Check size of the unique queue, two ways | |
print(q.qsize()) # 3 | |
print(len(q)) # 3 | |
# get, or dequeue | |
print(q.get()) # ('dota', 2) # the one with the most count is returned first | |
print(q.get()) # ('lol', 1) | |
print(q.get()) # ('skyrim', 1) | |
print(q.get()) # () # only 3 unique items still | |
# clear the queue | |
q.clear() |
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 logging | |
from techies import ( | |
Queue, UniQueue, CountQueue, QueueHandler, REF_LOG_FORMAT | |
) | |
from techies.compat import xrange | |
if __name__ == '__main__': | |
key = 'test_q' | |
q = Queue(key=key, host='localhost', port=6379, db=0) | |
uq = UniQueue(key=key, host='localhost', port=6379, db=1) | |
cq = CountQueue(key=key, host='localhost', port=6379, db=2) | |
logger = logging.getLogger(__name__) | |
for i in [q, uq, cq]: | |
handler = QueueHandler(i) | |
handler.setFormatter(logging.Formatter(REF_LOG_FORMAT)) | |
logger.addHandler(handler) | |
# Enqueue multiple times of the same error | |
for i in xrange(3): | |
try: | |
1 / 0 | |
except ZeroDivisionError as e: | |
logger.error(e) | |
# simple queue, should print error log 3 times | |
while len(q): | |
print(q.get()) | |
# unique queue, should just have 1 item in this case | |
print(len(uq) == 1) | |
print(uq.get()) | |
# count queue, should just have 1 item as unique queue | |
print(len(cq) == 1) | |
print(cq.get()[1]) # 3, the count of the same item | |
for i in [q, uq, cq]: | |
i.clear() |
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 techies import MultiCounter | |
counter = MultiCounter(key='demo_counter') | |
counter.incr('event_1') | |
counter.incr('event_1') | |
counter.incr('event_2') | |
counter.incr('event_3') | |
counter.incr('event_2') | |
print(counter.get_count('event_1')) # 2 | |
print(counter.get_count('event_2')) # 2 | |
print(counter.get_count('event_3')) # 1 | |
print(counter.get_count('event_null')) # 0 | |
print(counter.json()) # {u'event_2': u'4', u'event_3': u'2', u'event_1': u'4'} | |
print(unicode(counter)) # {"event_2": "2", "event_3": "1", "event_1": "2"} | |
print(str(counter)) # same as above | |
# clears the counts | |
counter.clear() |
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 techies import Queue | |
q = Queue(key='demo_q', host='localhost', port=6379, db=0) | |
# put, or enqueue | |
q.put('lol') | |
q.put('dota') | |
q.put('skyrim') | |
q.put('dota') | |
# Check size of the queue, two ways | |
print(q.qsize()) # 4 | |
print(len(q)) # 4 | |
# get, or dequeue | |
print(q.get()) # 'lol' | |
print(q.get()) # 'dota' | |
print(q.get()) # 'skyrim' | |
print(q.get()) # 'dota' | |
print(q.get()) # '' | |
# clear the queue | |
q.clear() |
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 techies import TsCounter | |
import time | |
# initialize with chunk_size and ttl defined | |
counter = TsCounter( | |
key='demo_event', | |
chunk_size=24 * 60 * 60, | |
ttl=48 * 60 * 60 | |
) | |
# or call initialize() method later to customize chunk_size and/or ttl | |
counter.initialize(chunk_size=24 * 60 * 60, ttl=48 * 60 * 60) | |
t = time.time() | |
counter.incr(timestamp=t - 86400) | |
counter.incr(timestamp=t - 86400) | |
counter.incr(timestamp=t) | |
counter.incr(timestamp=t - 86400) | |
counter.incr(timestamp=t + 86400) | |
counter.incr(timestamp=t + 86400) | |
print(counter.get_count(timestamp=t - 86400)) # 3 | |
print(counter.get_count(timestamp=t)) # 1 | |
print(counter.get_count(timestamp=t + 86400)) # 2 | |
print(counter.json()) # {u'demo_event:1429142400': {u'1429162301': u'3'}, u'demo_event:1429228800': {u'1429248701': u'1'}, u'demo_event:1429315200': {u'1429335101': u'2'}} | |
print(unicode(counter)) # {"demo_event:1429142400": {"1429162301": "3"}, "demo_event:1429228800": {"1429248701": "1"}, "demo_event:1429315200": {"1429335101": "2"}} | |
print(str(counter)) # same as above | |
# clears the counts | |
counter.clear() |
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 techies import UniQueue | |
q = UniQueue(key='demo_q', host='localhost', port=6379, db=0) | |
# put, or enqueue | |
q.put('lol') | |
q.put('dota') | |
q.put('skyrim') | |
q.put('dota') # this one is ignored | |
# Check size of the unique queue, two ways | |
print(q.qsize()) # 3 | |
print(len(q)) # 3 | |
# get, or dequeue | |
print(q.get()) # 'lol' | |
print(q.get()) # 'dota' | |
print(q.get()) # 'skyrim' | |
print(q.get()) # '' # only 3 unique items | |
# clear the queue | |
q.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment