Skip to content

Instantly share code, notes, and snippets.

# -*- coding: utf-8 -*-
# $Id: offline_bootstrap.py 73407 2011-08-21 20:35:04Z glenfant $
"""Offline buildout bootstraping for use on installation targets with no
Internet access.
See http://glenfant.wordpress.com/2011/07/31/bootstrap-and-install-a-buildout-based-project-without-internet-connection/
We assume that the directory that contains this file has the following
structure:
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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_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.