Skip to main content

Simple feature flags with percentage rollout and user targeting.

Project description

philiprehberger-feature-flag

Tests PyPI version GitHub release Last updated License Bug Reports Feature Requests Sponsor

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 package useful, consider starring the repository.

LinkedIn More packages

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.0.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.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_feature_flag-0.2.0.tar.gz
Algorithm Hash digest
SHA256 82ad84285f94d97cdf0ff123984a907cd84d48b6166b489c01ee354bb817b32d
MD5 8277a3ef6019f33fa9633b444a9ff1da
BLAKE2b-256 e500b5ded2f1e3a0a5ce1286f058899b7e4b6f4427b7b1ee457c566242eed69a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_feature_flag-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 33ff30a51837cd897bb5e2bd849ecd0440c3c6dc2fa1b41fc30a13de16e8f93d
MD5 a61405432536d1815ec494a48034216b
BLAKE2b-256 fe7002c6906d89fbf8c3b6074f80edfd84710fe769b5225eac11fd3b2a6f2ee7

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