Skip to content

Commit 1480a53

Browse files
committed
initial commit
0 parents  commit 1480a53

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

.github/workflows/checks.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Checks
2+
3+
on:
4+
push:
5+
branches: ["**"]
6+
7+
jobs:
8+
checks:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- uses: actions/setup-python@v5
15+
with:
16+
python-version: "3.13"
17+
18+
- name: Install dependencies
19+
run: make install
20+
21+
- name: Run app
22+
run: make build

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.venv/
2+
__pycache__/
3+
*.py[cod]
4+
.pytest_cache/
5+
.mypy_cache/
6+
.coverage
7+
htmlcov/
8+
.DS_Store

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
PYTHON := .venv/bin/python
2+
PIP := .venv/bin/pip
3+
4+
.PHONY: install build
5+
6+
install:
7+
python3 -m venv .venv
8+
$(PIP) install -r requirements.txt
9+
10+
build:
11+
$(PYTHON) main.py

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# featurevisor-example-python
2+
3+
Tiny example application using the Featurevisor Python SDK.
4+
5+
It mirrors the Go example by:
6+
7+
- fetching a remote Featurevisor datafile,
8+
- creating a Featurevisor instance,
9+
- setting evaluation context inside the script,
10+
- printing whether a feature is enabled.
11+
12+
The example uses this datafile:
13+
14+
[https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-tag-all.json](https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-tag-all.json)
15+
16+
## Requirements
17+
18+
- Python 3.10+
19+
20+
## Installation
21+
22+
```bash
23+
python3 -m venv .venv
24+
source .venv/bin/activate
25+
pip install -r requirements.txt
26+
```
27+
28+
## Usage
29+
30+
```bash
31+
python main.py
32+
```
33+
34+
## What It Does
35+
36+
The script fetches the remote datafile, builds a Featurevisor instance, sets this default context, and evaluates a feature:
37+
38+
```json
39+
{
40+
"userId": "123",
41+
"deviceId": "device-23456",
42+
"country": "nl"
43+
}
44+
```
45+
46+
It evaluates `my_feature`, which exists in the sample datafile.

main.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
3+
import json
4+
import sys
5+
from urllib.error import HTTPError, URLError
6+
from urllib.request import Request, urlopen
7+
8+
from featurevisor import create_instance
9+
10+
DATAFILE_URL = "https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-tag-all.json"
11+
FEATURE_KEY = "my_feature"
12+
CONTEXT = {
13+
"userId": "123",
14+
"deviceId": "device-23456",
15+
"country": "nl",
16+
}
17+
18+
19+
def fetch_datafile(url: str) -> dict:
20+
try:
21+
request = Request(
22+
url,
23+
headers={
24+
"User-Agent": "featurevisor-example-python/1.0",
25+
"Accept": "application/json",
26+
},
27+
)
28+
with urlopen(request) as response:
29+
return json.load(response)
30+
except HTTPError as exc:
31+
raise RuntimeError(f"failed to fetch datafile: HTTP {exc.code}") from exc
32+
except URLError as exc:
33+
raise RuntimeError(f"failed to fetch datafile: {exc.reason}") from exc
34+
35+
36+
def main() -> int:
37+
try:
38+
datafile = fetch_datafile(DATAFILE_URL)
39+
f = create_instance({"datafile": datafile, "logLevel": "error"})
40+
f.set_context(CONTEXT)
41+
enabled = f.is_enabled(FEATURE_KEY)
42+
except Exception as exc:
43+
print(f"Error: {exc}", file=sys.stderr)
44+
return 1
45+
46+
print(f"Datafile revision: {f.get_revision()}")
47+
print(f"Context: {json.dumps(CONTEXT)}")
48+
print(f"Feature '{FEATURE_KEY}' enabled: {enabled}")
49+
print(f"All evaluations: {json.dumps(f.get_all_evaluations(), sort_keys=True)}")
50+
return 0
51+
52+
53+
if __name__ == "__main__":
54+
raise SystemExit(main())

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
featurevisor==0.2.0

0 commit comments

Comments
 (0)