Libfaketime wrapper.
Project description
Faketime
Faketime is a thin python wrapper around the amazing C library libfaketime, written by Wolfgang Hommel which you can use to lie to UNIX processes about what time and date it is.
Why does this wrapper exist?
- It provides an interface that is convenient and pythonic that you can use to lie to subprocesses run with python.
- It provides an python interface that is identical on both Mac OS X and Linux (libfaketime is used in a slightly different way on both).
- It provides a self contained library that can be installed in a virtualenv that runs consistently in any environment - installing libfaketime from package manager can instead get you older, buggy versions on different operating systems and package managers.
Install
pip install faketime
Using
>>> from commandlib import Command
>>> from faketime import Faketime
>>> from datetime import datetime
>>> faketime = Faketime("currenttime.txt")
>>> datecmd
{'LD_PRELOAD': '/full/path/to/virtualenv/site-packages/faketime/libfaketime.so.1', 'FAKETIME_TIMESTAMP_FILE': '/full/path/to/currenttime.txt'}
>>> datecmd = Command("date").with_env(**faketime.env_vars)
>>> datecmd.run()
[ should print current time ]
>>> faketime.change_time(datetime(2050, 6, 7, 10, 9, 22, 713689))
>>> datecmd.run()
Tue 7 Jun 10:09:21 BST 2050
This above example shows how to use faketime with commandlib, although the environment variables in the dict can be used with any command runner (e.g. Popen).
What's the story behind this library?
This library can be used for a number of purposes, but I mainly built it so that I could write tests with hitchstory that would lie to postgres, django and celery simultaneously about what date and time it was.
Initially I tried using freezegun to test the python code, but I realized that it only really worked on one snippet of code at a time. Moreover:
- If that code executed a python process that contained other code that other code would get the current time, not the frozen time, breaking the test.
- It was simply incapable of faking, say, postgres's time, so if an SQL query embedded a datetime query then that would break the test.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file faketime-0.9.8.tar.gz
.
File metadata
- Download URL: faketime-0.9.8.tar.gz
- Upload date:
- Size: 41.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b12e43b355489b1938ed2b1db4ad2f2c83df5b2bd8c412a026965628669a2160 |
|
MD5 | 035bec2b60063d4008751ae825ab28e4 |
|
BLAKE2b-256 | 2a4f14ae990f996f14c6b229938d1713a2ae067d3added78a2610c6baa65472e |