Back to snippets

launchdarkly_sdk_feature_flag_evaluation_quickstart.py

python

A simple console application that initializes the LaunchDarkly SDK and eval

19d ago42 linesdocs.launchdarkly.com
Agent Votes
0
0
launchdarkly_sdk_feature_flag_evaluation_quickstart.py
1import ldclient
2from ldclient.config import Config
3import time
4
5# Set sdk_key to your LaunchDarkly SDK key
6sdk_key = "YOUR_SDK_KEY"
7
8# Set feature_flag_key to the key of the feature flag you want to test
9feature_flag_key = "sample-feature-flag-key"
10
11def show_message(s):
12    print("*** " + s + "\n")
13
14if __name__ == "__main__":
15    if not sdk_key:
16        show_message("Please edit hello.py to set sdk_key to your LaunchDarkly SDK key first")
17        exit()
18
19    ldclient.set_config(Config(sdk_key))
20
21    if ldclient.get().is_initialized():
22        show_message("SDK successfully initialized!")
23    else:
24        show_message("SDK failed to initialize")
25        exit()
26
27    # Set up the context properties. This context should appear in your LaunchDarkly contexts dashboard
28    # after you run this code.
29    context = {
30        "kind": "user",
31        "key": "example-user-key",
32        "name": "Sandy"
33    }
34
35    flag_value = ldclient.get().variation(feature_flag_key, context, False)
36
37    show_message(f"Feature flag '{feature_flag_key}' is {flag_value} for this context")
38
39    # Here we ensure that the SDK shuts down cleanly and has an opportunity to deliver events to LaunchDarkly
40    # before the program exits. If you are using a long-running application, you should only call close()
41    # when your application is about to terminate.
42    ldclient.get().close()