Featurevisor Python SDK
Project description
featurevisor-python
This repository ports the latest Featurevisor JavaScript SDK to Python.
The package name is featurevisor, and it targets Python 3.10+.
This SDK is compatible with Featurevisor v2.0 projects and above.
Installation
pip install featurevisor
Initialization
Initialize the SDK with Featurevisor datafile content:
from urllib.request import urlopen
import json
from featurevisor import create_instance
datafile_url = "https://cdn.yoursite.com/datafile.json"
with urlopen(datafile_url) as response:
datafile_content = json.load(response)
f = create_instance({
"datafile": datafile_content,
})
Evaluation Types
We can evaluate 3 types of values against a particular feature:
- Flag (
bool): whether the feature is enabled or not - Variation (
string): the variation of the feature (if any) - Variables: variable values of the feature (if any)
Context
Context is a plain dictionary of attribute values used during evaluation:
context = {
"userId": "123",
"country": "nl",
}
You can provide context at initialization:
f = create_instance({
"context": {
"deviceId": "123",
"country": "nl",
},
})
You can merge more context later:
f.set_context({
"userId": "234",
})
Or replace the existing context:
f.set_context(
{
"deviceId": "123",
"userId": "234",
"country": "nl",
"browser": "chrome",
},
True,
)
You can also pass additional per-evaluation context:
is_enabled = f.is_enabled("my_feature", {"country": "nl"})
variation = f.get_variation("my_feature", {"country": "nl"})
variable_value = f.get_variable("my_feature", "my_variable", {"country": "nl"})
Check If Enabled
if f.is_enabled("my_feature"):
pass
Getting Variation
variation = f.get_variation("my_feature")
if variation == "treatment":
pass
Getting Variables
bg_color = f.get_variable("my_feature", "bgColor")
Typed convenience methods are also available:
f.get_variable_boolean(feature_key, variable_key, context={})
f.get_variable_string(feature_key, variable_key, context={})
f.get_variable_integer(feature_key, variable_key, context={})
f.get_variable_double(feature_key, variable_key, context={})
f.get_variable_array(feature_key, variable_key, context={})
f.get_variable_object(feature_key, variable_key, context={})
f.get_variable_json(feature_key, variable_key, context={})
Getting All Evaluations
all_evaluations = f.get_all_evaluations()
Sticky
You can pin feature evaluations with sticky values:
f = create_instance({
"sticky": {
"myFeatureKey": {
"enabled": True,
"variation": "treatment",
"variables": {
"myVariableKey": "myVariableValue",
},
}
}
})
Or update them later:
f.set_sticky({
"myFeatureKey": {
"enabled": False,
}
})
Setting Datafile
The SDK accepts either parsed JSON content or a JSON string:
f.set_datafile(datafile_content)
f.set_datafile(json.dumps(datafile_content))
Logging
Supported log levels:
fatalerrorwarninfodebug
from featurevisor import create_instance
f = create_instance({
"datafile": datafile_content,
"logLevel": "debug",
})
You can also provide a custom logger handler via create_logger.
Events
The SDK emits:
datafile_setcontext_setsticky_set
unsubscribe = f.on("datafile_set", lambda details: print(details))
unsubscribe()
Hooks
Hooks support:
beforebucketKeybucketValueafter
They can be passed during initialization or added later with add_hook.
Child Instance
child = f.spawn({"country": "de"})
child.is_enabled("my_feature")
Close
f.close()
CLI Usage
The Python package also exposes a CLI:
python -m featurevisor test
python -m featurevisor benchmark
python -m featurevisor assess-distribution
These commands are intended for use from inside a Featurevisor project and rely on npx featurevisor being available locally.
Test
Run Featurevisor test specs using the Python SDK:
python -m featurevisor test \
--projectDirectoryPath=/path/to/featurevisor-project
Useful options:
python -m featurevisor test --keyPattern=foo
python -m featurevisor test --assertionPattern=variation
python -m featurevisor test --onlyFailures
python -m featurevisor test --showDatafile
python -m featurevisor test --verbose
python -m featurevisor test --with-tags
python -m featurevisor test --with-scopes
Benchmark
Benchmark repeated Python SDK evaluations against a built datafile:
python -m featurevisor benchmark \
--projectDirectoryPath=/path/to/featurevisor-project \
--environment=production \
--feature=my_feature \
--context='{"userId":"123"}' \
--n=1000
For variation benchmarks:
python -m featurevisor benchmark \
--projectDirectoryPath=/path/to/featurevisor-project \
--environment=production \
--feature=my_feature \
--variation \
--context='{"userId":"123"}'
For variable benchmarks:
python -m featurevisor benchmark \
--projectDirectoryPath=/path/to/featurevisor-project \
--environment=production \
--feature=my_feature \
--variable=my_variable_key \
--context='{"userId":"123"}'
Assess Distribution
Inspect enabled/disabled and variation distribution over repeated evaluations:
python -m featurevisor assess-distribution \
--projectDirectoryPath=/path/to/featurevisor-project \
--environment=production \
--feature=my_feature \
--context='{"country":"nl"}' \
--n=1000
You can also populate UUID-based context keys per iteration:
python -m featurevisor assess-distribution \
--projectDirectoryPath=/path/to/featurevisor-project \
--environment=production \
--feature=my_feature \
--populateUuid=userId \
--populateUuid=deviceId
Development
This repository assumes:
- Python 3.10+
- Node.js with
npx - Access to a Featurevisor project for CLI and tester integration
Run the local test suite:
make test
Run the example project integration directly:
PYTHONPATH=src python3 -m featurevisor test \
--projectDirectoryPath=/path/to/featurevisor-project
Releasing
- Update version in pyproject.toml
- Push commit to main branch
- Wait for CI to complete
- Tag the release with the version number
vX.X.X - This will trigger a new release to PyPI
License
MIT © Fahad Heylaal
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file featurevisor-0.2.0.tar.gz.
File metadata
- Download URL: featurevisor-0.2.0.tar.gz
- Upload date:
- Size: 31.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
695109c973674bfd8ee36f9b0a649a7b497ae32396899e873610340e98cfbd6a
|
|
| MD5 |
e30bffd70411729b676f50cdd588b003
|
|
| BLAKE2b-256 |
f8f8c6ae1c566eca83a32380d5d73b8c1d5208fb04538870c96528a37ae83435
|
Provenance
The following attestation bundles were made for featurevisor-0.2.0.tar.gz:
Publisher:
publish.yml on featurevisor/featurevisor-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
featurevisor-0.2.0.tar.gz -
Subject digest:
695109c973674bfd8ee36f9b0a649a7b497ae32396899e873610340e98cfbd6a - Sigstore transparency entry: 1077031662
- Sigstore integration time:
-
Permalink:
featurevisor/featurevisor-python@f68d43cbbb713784b373c16d5d6032a4f2e06355 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/featurevisor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f68d43cbbb713784b373c16d5d6032a4f2e06355 -
Trigger Event:
push
-
Statement type:
File details
Details for the file featurevisor-0.2.0-py3-none-any.whl.
File metadata
- Download URL: featurevisor-0.2.0-py3-none-any.whl
- Upload date:
- Size: 27.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff335471a82dc691f18ed8f80ee31a671deb018dbe3d09d5959f4cd62a4162e8
|
|
| MD5 |
f61a5a659191d697f2167ddd05b730b1
|
|
| BLAKE2b-256 |
331cd81a49cdc9e8d8592a439db3b772e6c9ee413b452f6a35946dbac2d09095
|
Provenance
The following attestation bundles were made for featurevisor-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on featurevisor/featurevisor-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
featurevisor-0.2.0-py3-none-any.whl -
Subject digest:
ff335471a82dc691f18ed8f80ee31a671deb018dbe3d09d5959f4cd62a4162e8 - Sigstore transparency entry: 1077031674
- Sigstore integration time:
-
Permalink:
featurevisor/featurevisor-python@f68d43cbbb713784b373c16d5d6032a4f2e06355 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/featurevisor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f68d43cbbb713784b373c16d5d6032a4f2e06355 -
Trigger Event:
push
-
Statement type: