Skip to content

Instantly share code, notes, and snippets.

View malexer's full-sized avatar

Alex (Oleksii) Markov malexer

  • EPAM
  • USA, NC
View GitHub Profile
@malexer
malexer / configparser_env.py
Last active November 20, 2023 09:38
Python 3: ConfigParser with expanding Environment variables
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)
@malexer
malexer / filter_magic.py
Last active November 2, 2016 09:10
Trying to reimplement SQLAlchemy's query.filter magic
# 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):
@malexer
malexer / sqlalchemy_upsert.py
Last active January 26, 2024 14:09
Modelling UPSERT in SQLAlchemy (well actually it is not upsert but speed improvement is significant in comparison with simple session.merge)
# 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():
@malexer
malexer / tornado_logging_to_stdout_and_stderr.py
Last active May 20, 2017 10:52
Redirect all logging (including errors) from Tornado web framework to stdout and use stderr for error logs only.
"""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
@malexer
malexer / zmq_http_server_example.py
Created January 28, 2014 10:02
Hello World HTTP server in pyzmq (using ZeroMQ RAW socket) - a Python version of http://gist.github.com/hintjens/5480625
import zmq
DEFAULT_PAGE = '\r\n'.join([
"HTTP/1.0 200 OK",
"Content-Type: text/plain",
"",
"Hello, World!",
])