-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update to new standard format (#18)
- Loading branch information
Showing
6 changed files
with
108 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Repository Maintainers | ||
* @launchdarkly/team-sdk-python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
# LaunchDarkly sample Python application | ||
|
||
[![Build and run](https://github.com/launchdarkly/hello-python/actions/workflows/ci.yml/badge.svg)](https://github.com/launchdarkly/hello-python/actions/workflows/ci.yml) | ||
We've built a simple console application that demonstrates how LaunchDarkly's SDK works. | ||
|
||
We've built a console application that demonstrates how LaunchDarkly's SDK works. | ||
Below, you'll find the build procedure. For more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [Python reference guide](https://docs.launchdarkly.com/sdk/server-side/python). | ||
|
||
Below, you'll find the basic build procedure. For more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [Python SDK reference guide](https://docs.launchdarkly.com/sdk/server-side/python). | ||
|
||
This demo requires Python version 3.7 or higher. | ||
This demo requires Python 3.8 or higher. | ||
|
||
## Build instructions | ||
|
||
1. Install the LaunchDarkly Python SDK by running `pip install -r requirements.txt` | ||
1. On the command line, set the value of the environment variable `LAUNCHDARKLY_SERVER_KEY` to your LaunchDarkly SDK key. | ||
```bash | ||
export LAUNCHDARKLY_SERVER_KEY="1234567890abcdef" | ||
``` | ||
1. On the command line, set the value of the environment variable `LAUNCHDARKLY_FLAG_KEY` to an existing boolean feature flag in your LaunchDarkly project that you want to evaluate. | ||
1. Set the environment variable `LAUNCHDARKLY_SERVER_KEY` to your LaunchDarkly SDK key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, set `LAUNCHDARKLY_FLAG_KEY` to the flag key; otherwise, a boolean flag of `sample-feature` will be assumed. | ||
|
||
```bash | ||
export LAUNCHDARKLY_SERVER_KEY="1234567890abcdef" | ||
export LAUNCHDARKLY_FLAG_KEY="my-boolean-flag" | ||
``` | ||
1. Run `python test.py`. | ||
|
||
You should receive the message `"Feature flag '<flag key>' is <true/false> for this context"`. | ||
1. Install the required dependencies with `pip install -r requirements.txt`. | ||
1. On the command line, run `python main.py` | ||
|
||
You should receive the message "The <flagKey> feature flag evaluates to <flagValue>.". The application will run continuously and react to the flag changes in LaunchDarkly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import os | ||
import ldclient | ||
from ldclient import Context | ||
from ldclient.config import Config | ||
from threading import Lock, Event | ||
from datetime import datetime | ||
from halo import Halo | ||
|
||
|
||
# Set sdk_key to your LaunchDarkly SDK key. | ||
sdk_key = os.getenv('LAUNCHDARKLY_SERVER_KEY') | ||
|
||
# Set feature_flag_key to the feature flag key you want to evaluate. | ||
feature_flag_key = os.getenv('LAUNCHDARKLY_FLAG_KEY', 'sample-feature') | ||
|
||
# Set this environment variable to skip the loop process and evaluate the flag | ||
# a single time. | ||
ci = os.getenv('CI') | ||
|
||
|
||
def show_evaluation_result(key: str, value: bool): | ||
now = datetime.now().strftime('%H:%M:%S') | ||
print() | ||
print(f"*** {now}: The {key} feature flag evaluates to {value}") | ||
|
||
|
||
def show_banner(): | ||
print() | ||
print(" ██ ") | ||
print(" ██ ") | ||
print(" ████████ ") | ||
print(" ███████ ") | ||
print("██ LAUNCHDARKLY █") | ||
print(" ███████ ") | ||
print(" ████████ ") | ||
print(" ██ ") | ||
print(" ██ ") | ||
print() | ||
|
||
|
||
class FlagValueChangeListener: | ||
def __init__(self): | ||
self.__show_banner = True | ||
self.__lock = Lock() | ||
|
||
def flag_value_change_listener(self, flag_change): | ||
with self.__lock: | ||
if self.__show_banner and flag_change.new_value: | ||
show_banner() | ||
self.__show_banner = False | ||
|
||
show_evaluation_result(flag_change.key, flag_change.new_value) | ||
|
||
|
||
if __name__ == "__main__": | ||
if not sdk_key: | ||
print("*** Please set the LAUNCHDARKLY_SERVER_KEY env first") | ||
exit() | ||
if not feature_flag_key: | ||
print("*** Please set the LAUNCHDARKLY_FLAG_KEY env first") | ||
exit() | ||
|
||
ldclient.set_config(Config(sdk_key)) | ||
|
||
if not ldclient.get().is_initialized(): | ||
print("*** SDK failed to initialize. Please check your internet connection and SDK credential for any typo.") | ||
exit() | ||
|
||
print("*** SDK successfully initialized") | ||
|
||
# Set up the evaluation context. This context should appear on your | ||
# LaunchDarkly contexts dashboard soon after you run the demo. | ||
context = \ | ||
Context.builder('example-user-key').kind('user').name('Sandy').build() | ||
|
||
flag_value = ldclient.get().variation(feature_flag_key, context, False) | ||
show_evaluation_result(feature_flag_key, flag_value) | ||
|
||
if ci is None: | ||
change_listener = FlagValueChangeListener() | ||
listener = ldclient.get().flag_tracker \ | ||
.add_flag_value_change_listener(feature_flag_key, context, change_listener.flag_value_change_listener) | ||
|
||
with Halo(text='Waiting for changes', spinner='dots'): | ||
try: | ||
Event().wait() | ||
except KeyboardInterrupt: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
launchdarkly-server-sdk>=9.0.0 | ||
halo>=0.0.3 |