A minimalistic feature flag implementation with type hints.
Project description
Fiicha is minimalistic mypy-friendly feature flag implementation with no external dependencies.
Installation
pip install fiicha
For Development
Clone the repo, change current working dir to the root of the repo and execute:
python -m venv env
. env/bin/activate
pip install -e .[test,lint]
Usage
Basic
Basic usage is simple:
Sublcass from FeatureFlags
Add FeatureFlag attributes representing individual feature flags.
from fiicha import FeatureFlag, FeatureFlags
class MyProjectFeatureFlags(FeatureFlags):
release_x = FeatureFlag("Enable features from upcoming release X")
use_new_algorithm = FeatureFlag("Use new algorithm for ...")
ff = MyProjectFeatureFlags({"release_x": True})
print(ff.release_x) # True
print(ff.use_new_algorithm) # False
In addition, you kan specify a key defining default state of the feature flags:
ff = MyProjectFeatureFlags({"all": True, "release_x": False}, default_key="all")
print(ff.release_x) # False
print(ff.use_new_algorithm) # True
Classes subclassed from FeatureFlags has full type hint support, so when you are accessing removed or otherwise non-existent feature flags, you can detect it before running tests:
ff.xxx # will trigger mypy's attr-defined error
Utils
The package comes with few util functions to avoid building your own framework for simple use cases.
Single-line Configuration
from fiicha import parse_feature_flags_string
flags_string = "release_x !use_new_algorithm" # stored in config
ff = MyProjectFeatureFlags(parse_feature_flags_string(flags_string))
print(ff.release_x) # True
print(ff.use_new_algorithm) # False
Load Flags From Environment Variables
import os
from fiicha import feature_flags_from_environ
os.environ["MYPROJ_FEATURE_RELEASE_X"] = "True" # can also be 1, t, yes, on
os.environ["MYPROJ_FEATURE_USE_NEW_ALGORITHM"] = "False" # can also be 0, f, no, off or empty string
ff = MyProjectFeatureFlags(feature_flags_from_environ("MYPROJ_FEATURE_"))
print(ff.release_x) # True
print(ff.use_new_algorithm) # False
Automatically Document Your Feature Flags
from fiicha import make_napoleon_doc, make_sphinx_doc
class GoogleStyleDocFeatureFlags(FeatureFlags, make_doc=make_napoleon_doc):
a = FeatureFlag("Enable feature A")
b = FeatureFlag("Enable feature B")
class SphinxStyleDocFeatureFlags(FeatureFlags, make_doc=make_sphinx_doc):
x = FeatureFlag("Enable feature X")
y = FeatureFlag("Enable feature Y")
print(GoogleStyleDocFeatureFlags.__doc__)
print(SphinxStyleDocFeatureFlags.__doc__)
Context Variable Support
You might want to do A/B testing or otherwise toggle feature flags for individual users without affecting the rest of the system. For such cases, there is FeatureFlagsContext - a wrapper around ContextVar. It copies current feature flags and sets them as new context variable on enter and resets them back on exit. This way you will be able to achieve global feature flags protection from changes made within context of a request or a task.
from contextvars import ContextVar
from fiicha import FeatureFlag, FeatureFlags, FeatureFlagsContext
class MyFeatureFlags(FeatureFlags):
a = FeatureFlag("Enable feature A")
b = FeatureFlag("Enable feature B")
root = MyFeatureFlags()
var: ContextVar[MyFeatureFlags] = ContextVar("test", default=root)
ff_ctx = FeatureFlagsContext(var)
with ff_ctx:
ff = var.get()
ff.a = True
assert ff is not root # is a copy
assert not root.a # not changed
Advanced
See examples subfolder within the repo.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fiicha-0.2.0.tar.gz
.
File metadata
- Download URL: fiicha-0.2.0.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1ddad744fdbf4ee8b003136964381b4722a8c072ef41781da10bd1a6728ae8b |
|
MD5 | 6e672c8d1af5cf241a1e01da23495c2b |
|
BLAKE2b-256 | 783b17d02248d9e53fd37a4efb151af17e413fdc44b682fd52d9006abaf72e14 |
File details
Details for the file fiicha-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: fiicha-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1781ec9170a0e84df3cf5a3526ed273396db29cbf3a19c2b6794c93a08b82e3 |
|
MD5 | 0c030ab94833fa51ca29d936797398fd |
|
BLAKE2b-256 | 97c6a0408aeda414110faafd970224a34decf06ac38fcf86db849fb9df321971 |