Skip to content

Commit

Permalink
fix: Update to new standard format (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 authored Apr 17, 2024
1 parent 247d11d commit 5257939
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 67 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
name: Build and run
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 9 * * *'
push:
branches: [ main ]
branches: [ main, 'feat/**' ]
paths-ignore:
- '**.md' # Do not need to run CI for markdown changes.
pull_request:
branches: [ main ]
branches: [ main, 'feat/**' ]
paths-ignore:
- '**.md'

Expand All @@ -22,13 +25,13 @@ jobs:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: 3.7
python-version: 3.8

- name: Install dependencies
run: pip install --user -r requirements.txt

- uses: launchdarkly/gh-actions/actions/verify-hello-app@verify-hello-app-v1.0.0
- uses: launchdarkly/gh-actions/actions/verify-hello-app@verify-hello-app-v1.0.1
with:
use_server_key: true
role_arn: ${{ vars.AWS_ROLE_ARN }}
command: python test.py
command: python main.py
2 changes: 2 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Repository Maintainers
* @launchdarkly/team-sdk-python
22 changes: 9 additions & 13 deletions README.md
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.
88 changes: 88 additions & 0 deletions main.py
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
launchdarkly-server-sdk>=9.0.0
halo>=0.0.3
49 changes: 0 additions & 49 deletions test.py

This file was deleted.

0 comments on commit 5257939

Please sign in to comment.