Skip to main content

Simple feature flags with percentage rollout and user targeting.

Project description

philiprehberger-feature-flag

Tests PyPI version Last updated

Simple feature flags with percentage rollout and user targeting.

Installation

pip install philiprehberger-feature-flag

Usage

Basic flags

from philiprehberger_feature_flag import flags

flags.load({"dark_mode": True, "beta_ui": False})

if flags.is_enabled("dark_mode"):
    enable_dark_mode()

Percentage rollout

flags.load({
    "new_checkout": {
        "enabled": True,
        "rollout": 25,  # 25% of users
    }
})

if flags.is_enabled("new_checkout", user_id="user-42"):
    show_new_checkout()

User targeting

flags.load({
    "admin_panel": {
        "enabled": True,
        "users": ["alice", "bob"],
    }
})

if flags.is_enabled("admin_panel", user_id="alice"):
    show_admin_panel()

Load from JSON file

flags.load("flags.json")

Load from environment variables

# Set FF_DARK_MODE=true, FF_BETA=0, etc.
flags.load()  # reads FF_* env vars

Runtime overrides

flags.override("beta_ui", True)   # force-enable for testing
flags.reset()                     # clear all overrides

Change callbacks

def on_flag_change(name, old, new):
    print(f"Flag {name} changed from {old} to {new}")

flags.on_change(on_flag_change)
flags.load({"dark_mode": True})
# prints: Flag dark_mode changed from None to True

Flag groups

flags.load({
    "ui_dark_mode": True,
    "ui_sidebar": False,
    "api_rate_limit": 100,
})

ui_flags = flags.group("ui_")
# {"ui_dark_mode": True, "ui_sidebar": False}

API

Function / Method Description
FlagStore() Create a new flag store
store.load(config) Load flags from dict, JSON file path, or env vars (None)
store.is_enabled(name, **context) Check if a flag is enabled
store.all() Return all loaded flags as a dict
store.override(name, value) Set a runtime override
store.reset() Clear all runtime overrides
store.on_change(callback) Register a callback fired as callback(flag_name, old_value, new_value) on changes
store.group(prefix) Return dict of flags whose name starts with prefix with resolved values
flags Module-level FlagStore instance

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_feature_flag-0.2.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_feature_flag-0.2.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_feature_flag-0.2.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_feature_flag-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7711b5e8393066b8ea26a1ca28e8b49a6456719bdba526b8212890a63eeaf03c
MD5 c6a0dfec378591bd77ccd9a3798b9c83
BLAKE2b-256 fe69298e73d571f32153c23cf3ffc78a4db27bcf17c42aefb4cde461fd1a8e99

See more details on using hashes here.

File details

Details for the file philiprehberger_feature_flag-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_feature_flag-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d75681dd97b4bd19186c90d5eae45bd553572756034a2f006f53a65870508aa7
MD5 09e37e6ff2142b88ce864a76bb7cdea0
BLAKE2b-256 8b9f7e27c5804b2cc04072585e620069c444f0d4feb72954ca32a2dfc0f2b96c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page