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 zmq | |
DEFAULT_PAGE = '\r\n'.join([ | |
"HTTP/1.0 200 OK", | |
"Content-Type: text/plain", | |
"", | |
"Hello, World!", | |
]) |
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
"""Redirect all logging (including errors) from Tornado web framework to stdout | |
and use stderr for error logs only. | |
Can be usefull when you want to store separatelly all logs and error logs. | |
""" | |
import logging | |
import sys | |
import tornado.log |
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
# Note: it is a copy of great answer by "mgoldwasser" from Stackoverflow | |
# Check the original answer here: http://stackoverflow.com/a/26018934/1032439 | |
# Imagine that post1, post5, and post1000 are posts objects with ids 1, 5 and 1000 respectively | |
# The goal is to "upsert" these posts. | |
# we initialize a dict which maps id to the post object | |
my_new_posts = {1: post1, 5: post5, 1000: post1000} | |
for each in posts.query.filter(posts.id.in_(my_new_posts.keys())).all(): |
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
# after using SQLAlchemy I've decided to try my python skills in coding it's filter function magic | |
# without looking into their source code, just for fun | |
# Note: it's not complete, just proof of concept | |
class BindProperty(type): | |
def __new__(cls, name, bases, namespace, **kwds): | |
result = type.__new__(cls, name, bases, dict(namespace)) | |
for name, value in namespace.items(): | |
if isinstance(value, Property): |
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 configparser | |
import os | |
class EnvInterpolation(configparser.BasicInterpolation): | |
"""Interpolation which expands environment variables in values.""" | |
def before_get(self, parser, section, option, value, defaults): | |
value = super().before_get(parser, section, option, value, defaults) | |
return os.path.expandvars(value) |