Skip to content

Instantly share code, notes, and snippets.

@glenfant
glenfant / a_G_for_FastAPI.md
Last active October 18, 2023 08:36
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 / 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 / marshmallow_fr.py
Created May 8, 2018 18:21
How I translated marshmallow fields error messages into French
"""
==============
marshmallow_fr
==============
Les fields de marshmallow dont les messages d'erreur sont traduits en Français.
Ces messages d'erreur sont basés sur marshmallow 2.15.1 et il sera peut-être nécessaire de le mettre à jour
au fil des évolutions de marshmallow.
Parametrized marshmallow fields with error messages translated into French.
@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
#
@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 / mlsearch.py
Created January 16, 2015 17:31
Attempt to record a new query as extended REST service in ML 7
# -*- coding: utf-8 -*-
"""
Provided a new XQuery, we record it as extended REST resource in ML 7 server
"""
import httplib
import urlparse
import requests
@glenfant
glenfant / dottedname.py
Created January 6, 2015 11:46
Turns a dotted name in a string as 'sys.path' into a Python object
# -*- coding: utf-8 -*-
"""
==========
dottedname
==========
This module may be used when you need to refer to Python objects from non
Python files. For example, in a configuration file.
Resolve a python dotted name (stolen from zope.dottedname)
@glenfant
glenfant / Vagrantfile
Last active August 29, 2015 14:09
Vagrantfile for taiga-vagrant that supports OSX AND Linux. Contrib for Windows Welcome. See https://github.com/taigaio/taiga-vagrant/issues/15
# -*- mode: ruby -*-
# vi: set ft=ruby :
require "./source.rb"
ROOT_PATH = File.dirname(__FILE__)
VAGRANTFILE_API_VERSION = "2"
def configure_extra(config)
end
@glenfant
glenfant / timeoutctxmanager.py
Last active December 28, 2015 12:39
This is a (Unix only) context manager that cancels the execution of its inner code block when its timeout is gone. Note: deprecated, see my "stopit" third party package.
# -*- coding: utf-8 -*-
"""
This recipe provides a context manager that stops the execution of its inner
code block after the timeout is gone. This recipe is stolen with some changes
and rewording in a less app centric vocabulary from the "rq" package.
https://github.com/glenfant/rq/blob/master/rq/timeouts.py.
Warnings: