Skip to content

Instantly share code, notes, and snippets.

@glenfant
glenfant / a_G_for_FastAPI.md
Last active April 10, 2024 20:17
A simple POC that mimics in FastAPI the "g" request lifecycle pseudo global

Like Flask "g". Good news you can do the same with FastAPI

"g" is a threadlocal magic object that lets developer add / change / remove attributes during the request lifecycle. Learn more about this "g" here.

There is no OTB equivalent in FastAPI, but thanks to the new contextvars Python 3.7+ module, I made this simple demo.

Any comment or help to improve yhis recipe is welcome.

@glenfant
glenfant / pipetestserver.py
Last active November 11, 2021 14:08
Use a stub HTTP server in unit tests for HTTP client testings (REST, ...)
# -*- coding: utf-8 -*-
"""\
==============
pipetestserver
==============
This recipe describes how you can create / activate and kill a temporary HTTP
server with a WSGI app to provide unittest resources to a client software,
that's the target of your application.
@glenfant
glenfant / mock_datetime_now.py
Last active November 11, 2021 14:08
A context manager that enables go quickly in the future or in the past with datetime.datetime.now(). This may be useful in lots of unit tests.
# -*- coding: utf-8 -*-
"""Mock datetime.datetime.now()"""
import contextlib
import datetime
@contextlib.contextmanager
def mock_datetime_now(*args, **kwargs):
"""Context manager for mocking out datetime.datetime.now() in unit tests.
@glenfant
glenfant / enumeration.py
Last active November 11, 2021 14:08
There is no enumeration type in Python a in some other languages as pascal or RubyThis is a poor man's enumeration that works as expected.
# -*- coding: utf-8 -*-
"""Python enumeration"""
import itertools
def enumeration(name, *auto, **named):
"""Pythonic enumeration type
>>> Colors = enumeration('Colors', 'GREEN', 'RED', 'YELLOW')
@glenfant
glenfant / mock_time.py
Last active November 11, 2021 14:08
A simple context manager that enabes to change time.time() to whatever you need (future or past) in your unit tests.
import contextlib
import time
@contextlib.contextmanager
def mock_time(timestamp):
"""A simple context manager for mocking time.time() useful for traveling
immediately in the future or in the past in unit tests.
>>> t0 = time.time()
>>> with mock_time(t0 + 5.0):
@glenfant
glenfant / logging_yaml_config.py
Last active November 11, 2021 14:08
A example of logging medium complex configuration using YAML on Python 2.7 or 3.2
# -*- coding: utf-8
"""\
A simple demo of logging configuration with YAML (Python 2.7)
=============================================================
Requires PyYAML -> "easy_install PyYAML"
See the recipes for configuring logging with dicts and YAML
- http://docs.python.org/2.7/howto/logging-cookbook.html
- http://stackoverflow.com/questions/10519392/python2-7-logging-configuration-with-yaml
@glenfant
glenfant / testwsgimock.py
Created February 3, 2017 09:19
How to create a temporary WSGI app suitable to unit tests mocking
# -*- coding: utf-8 -*-
# If you need to test a REST client, this is a Python 2 recipe that runs a
# simple WSGI app for your tests. Any improvement suggestion is welcome.
# Run this with "python -m unittest testingwsgi"
# Put this in a testing resources module, say tests/resources.py
import os
@glenfant
glenfant / call_blocking.py
Last active January 29, 2021 10:33
[asyncio] Simple recipe that shows how to wrap a blocking callable in an awaitable coroutine. Requires Python 3.7+
# Running a blocking callable inside a coroutine
# ==============================================
import asyncio
import concurrent.futures
import functools
from typing import Callable, Tuple, Dict, Any
# Use the better suited pool (see doc of ``concurrent.future```)
@glenfant
glenfant / conf.py
Last active August 1, 2020 22:16
How to provide a default configuration which individual points may be overriden in a custom configuration file (to improve and explain)
"""
================
euscans.settings
================
Provides settings customization and public configuration object
[DEPRECATED] I released the ``pyflexconfig`` package on PyPI that rationalizes and improves this boilerplate.
https://pypi.org/project/pyflexconfig/
@glenfant
glenfant / Vagrantfile
Last active May 26, 2020 17:14
A Vagrantfile for MacOS or Windows host, making an Ububtu 16.04 playground for the latest Docker (17.03-ce) and Docker-compose 1.12.0
# -*- mode: ruby -*-
# vi: set ft=ruby :
#
# Docker playground for MacOS and Windows
# =======================================
#
# - Installs the latest Docker + Docker-compose
# - Uses faster NFS files sync on Mac OS
# - Uses faster SMB files share on Windows
#