Skip to content

Instantly share code, notes, and snippets.

@mauler
mauler / structlog_render_level_in_uppercase.py
Created January 20, 2020 15:47
Renders log entry level in uppercase when using structlog library
def _add_log_level_upper(logger, method_name, event_dict):
if method_name == "warn":
method_name = "warning"
event_dict["level"] = method_name.upper()
return event_dict
level_styles = structlog.dev.ConsoleRenderer.get_default_level_styles()
for k, v in list(level_styles.items()):
level_styles[k.upper()] = v
@mauler
mauler / http_server_auth.py
Last active August 9, 2024 13:54 — forked from fxsjy/SimpleAuthServer.py
Python3 http.server supporting basic HTTP Auth (username/password)
# Extended python -m http.serve with --username and --password parameters for
# basic auth, based on https://gist.github.com/fxsjy/5465353
from functools import partial
from http.server import SimpleHTTPRequestHandler, test
import base64
import os
class AuthHTTPRequestHandler(SimpleHTTPRequestHandler):
@mauler
mauler / lru_cache_timeout.py
Last active September 18, 2019 10:26
functool lru_cache with timeout allowing to set expiration time on cached values
import datetime
from functools import lru_cache, wraps
def lru_cache_timeout(timeout_seconds: int):
def decorator(f):
@lru_cache(maxsize=None)
def f_cache_id(f_cache_id, *args, **kwds):
# http://codekata.com/kata/kata09-back-to-the-checkout/ implemenation using python3 with type-hints and mypy
from collections import defaultdict
from typing import NamedTuple, Dict, List
import unittest
class Product(NamedTuple):
price: int
(function(){
var Model = {}
Model.make = function(model_name) {
var model = {}
model.getByID = function(id, fn) {
JSONP.getByID(model_name, id, fn);
}
model.list = function(params, fn){
// NOTE: brasiltotal
params.exclude__codigonoticia__exact = '';
@mauler
mauler / gist:4019439
Created November 5, 2012 18:27
Hotel, preços
class HotelResource(ModelResource):
fields = ("pk", "name", "precos", )
model = Hotel
def precos(self, instance):
return Preco.objects.filter(quarto__hotel=instance)
@mauler
mauler / gist:4019438
Created November 5, 2012 18:27
Hotel, preços
class HotelResource(ModelResource):
fields = ("pk", "name", "precos", )
model = Hotel
def precos(self, instance):
return Preco.objects.filter(quarto__hotel=instance)