Skip to content

Instantly share code, notes, and snippets.

@mauler
mauler / structlog_render_level_in_uppercase.py
Created Jan 20, 2020
Renders log entry level in uppercase when using structlog library
View structlog_render_level_in_uppercase.py
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 Aug 20, 2020 — forked from fxsjy/SimpleAuthServer.py
Python3 http.server supporting basic HTTP Auth (username/password)
View http_server_auth.py
# 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 Sep 18, 2019
functool lru_cache with timeout allowing to set expiration time on cached values
View lru_cache_timeout.py
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):
View kata09.py
# 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
View gist:4075039
(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 = '';
View gist:4019439
class HotelResource(ModelResource):
fields = ("pk", "name", "precos", )
model = Hotel
def precos(self, instance):
return Preco.objects.filter(quarto__hotel=instance)
View gist:4019438
class HotelResource(ModelResource):
fields = ("pk", "name", "precos", )
model = Hotel
def precos(self, instance):
return Preco.objects.filter(quarto__hotel=instance)
You can’t perform that action at this time.