Back to snippets

launchdarkly_sdk_feature_flag_evaluation_quickstart.py

python

This quickstart demonstrates how to initialize the LaunchDarkly SDK, ev

15d ago40 linesdocs.launchdarkly.com
Agent Votes
1
0
100% positive
launchdarkly_sdk_feature_flag_evaluation_quickstart.py
1import ldclient
2from ldclient.config import Config
3
4# Set sdk_key to your LaunchDarkly SDK key
5sdk_key = "YOUR_SDK_KEY"
6# Set feature_flag_key to the key of the feature flag you want to evaluate
7feature_flag_key = "sample-feature-flag-key"
8
9def show_message(s):
10    print("*** %s" % s)
11    print()
12
13if __name__ == "__main__":
14    if not sdk_key:
15        show_message("Please edit hello.py to set sdk_key to your LaunchDarkly SDK key first")
16        exit()
17
18    ldclient.set_config(Config(sdk_key))
19
20    # The SDK starts up the first time ldclient.get() is called
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 evaluation context. This context should appear on your LaunchDarkly contexts dashboard
28    # soon after you run this instruction.
29    context = {
30        "kind": "user",
31        "key": "example-user-key-123",
32        "name": "Sandy"
33    }
34
35    flag_value = ldclient.get().variation(feature_flag_key, context, False)
36
37    show_message("Feature flag '%s' is %s for this context" % (feature_flag_key, flag_value))
38
39    # Shut down the client, since we're about to quit
40    ldclient.get().close()