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 functools | |
import time | |
import threading | |
import logging | |
import Queue | |
import hunspell | |
import tornado.web | |
import tornado.websocket | |
import tornado.locale |
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
try: | |
import simplejson as json | |
except ImportError: | |
try: | |
import json | |
except ImportError: | |
raise ImportError | |
import datetime | |
from bson.objectid import ObjectId | |
from werkzeug import Response |
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
# pip install tornado then just launch it with python slow_server.py --port=8080 | |
# then call http://localhost:8080/wait/600 for a 600ms request | |
# Fabrice Aneche https://gist.github.com/2995678 | |
import tornado.web | |
import tornado.httpserver | |
import time | |
import logging | |
from tornado.options import define, options |
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
# 2012 Fabrice Aneche. https://gist.github.com/2996326 | |
# Gevent rewrite with requests | |
# | |
# Copyright 2011 The greplin-tornado-ses Authors. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 |
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 werkzeug.routing import NumberConverter, ValidationError | |
class NegativeFloatConverter(NumberConverter): | |
regex = r'\-?\d+\.\d+' | |
num_convert = float | |
def __init__(self, map, min=None, max=None): | |
NumberConverter.__init__(self, map, 0, min, max) | |
app.url_map.converters['float'] = NegativeFloatConverter |
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 traceback | |
import datetime | |
import socket | |
import os | |
import resource | |
... | |
def log_exception(view_func): | |
""" log exception decorator for a view, | |
""" |
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 json | |
import datetime | |
import re | |
DATE_FORMAT = '%Y-%m-%d %H:%M:%S' | |
def datetime_parser(entry): | |
for k, v in entry.items(): | |
if isinstance(v, basestring) and re.search("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$", v): | |
try: | |
entry[k] = datetime.datetime.strptime(v, DATE_FORMAT) |
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
# list comprehension + dict comprehension should be memory efficient and can remove multiple keys | |
# but code is ugly | |
clean_list = [ | |
{k: v | |
for k, v in dict_to_clean.iteritems() | |
if k not in ['tid', 'aid']} | |
for dict_to_clean in my_list] | |
# lambda can remove only one key and I hate lambdas | |
map(lambda d: d.pop('tid'), my_list) |
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 RepeatForm(Form): | |
action = TextField('Action', validators=[Required()]) | |
def validate_action(form, field): | |
if field.data == "set": | |
field.data = True | |
elif field.data == "unset": | |
field.data = False | |
else: | |
raise ValidationError("Invalid: should be set or unset.") |
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
// lookup for the users table create it otherwise | |
// var tables []string and StringSlice are equivalent | |
var tables sort.StringSlice | |
err = r.TableList().Run(session).All(&tables) | |
if err != nil { | |
log.Panic(err) | |
} | |
// I miss python if "users" in tables | |
exists := false |
OlderNewer