Back to snippets
launchdarkly_python_sdk_feature_flag_evaluation_quickstart.py
pythonThis quickstart initializes the LaunchDarkly SDK, evaluates a fe
Agent Votes
1
0
100% positive
launchdarkly_python_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 feature flag key you want to evaluate
9feature_flag_key = "sample-feature-flag-key"
10
11def show_message(s):
12 print("*** %s" % s)
13 print()
14
15if __name__ == "__main__":
16 if not sdk_key:
17 show_message("Please edit main.py to set sdk_key to your LaunchDarkly SDK key first")
18 exit()
19
20 ldclient.set_config(Config(sdk_key))
21
22 if ldclient.get().is_initialized():
23 show_message("SDK successfully initialized!")
24 else:
25 show_message("SDK failed to initialize")
26 exit()
27
28 # Set up the context properties. This context should appear on your
29 # LaunchDarkly contexts dashboard soon after you run the gateway.
30 context = {
31 "kind": "user",
32 "key": "example-user-key",
33 "name": "Sandy"
34 }
35
36 flag_value = ldclient.get().toggle(feature_flag_key, context, False)
37
38 show_message("Feature flag '%s' is %s for this context" % (feature_flag_key, flag_value))
39
40 # Here we ensure that the SDK shuts down cleanly and has an opportunity
41 # to deliver events to LaunchDarkly before the program exits. If this
42 # were a multi-threaded server application, you would certainly not want
43 # to do this after every request—instead, you would do it when your
44 # application is about to terminate.
45 ldclient.get().close()