ABSmartly 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. absmartly-0.1.2/LICENSE +8 -0
  2. absmartly-0.1.2/MANIFEST.in +1 -0
  3. absmartly-0.1.2/PKG-INFO +252 -0
  4. absmartly-0.1.2/README.MD +223 -0
  5. absmartly-0.1.2/absmartly.egg-info/PKG-INFO +252 -0
  6. absmartly-0.1.2/absmartly.egg-info/SOURCES.txt +83 -0
  7. absmartly-0.1.2/absmartly.egg-info/dependency_links.txt +1 -0
  8. absmartly-0.1.2/absmartly.egg-info/requires.txt +13 -0
  9. absmartly-0.1.2/absmartly.egg-info/top_level.txt +1 -0
  10. absmartly-0.1.2/pyproject.toml +38 -0
  11. absmartly-0.1.2/sdk/__init__.py +0 -0
  12. absmartly-0.1.2/sdk/absmarly.py +67 -0
  13. absmartly-0.1.2/sdk/absmartly_config.py +17 -0
  14. absmartly-0.1.2/sdk/audience_deserializer.py +12 -0
  15. absmartly-0.1.2/sdk/audience_matcher.py +31 -0
  16. absmartly-0.1.2/sdk/client.py +56 -0
  17. absmartly-0.1.2/sdk/client_config.py +18 -0
  18. absmartly-0.1.2/sdk/context.py +780 -0
  19. absmartly-0.1.2/sdk/context_config.py +13 -0
  20. absmartly-0.1.2/sdk/context_data_deserializer.py +13 -0
  21. absmartly-0.1.2/sdk/context_data_provider.py +12 -0
  22. absmartly-0.1.2/sdk/context_event_handler.py +14 -0
  23. absmartly-0.1.2/sdk/context_event_logger.py +19 -0
  24. absmartly-0.1.2/sdk/context_event_serializer.py +10 -0
  25. absmartly-0.1.2/sdk/default_audience_deserializer.py +17 -0
  26. absmartly-0.1.2/sdk/default_context_data_deserializer.py +18 -0
  27. absmartly-0.1.2/sdk/default_context_data_provider.py +15 -0
  28. absmartly-0.1.2/sdk/default_context_event_handler.py +19 -0
  29. absmartly-0.1.2/sdk/default_context_event_serializer.py +11 -0
  30. absmartly-0.1.2/sdk/default_http_client.py +63 -0
  31. absmartly-0.1.2/sdk/default_http_client_config.py +6 -0
  32. absmartly-0.1.2/sdk/default_variable_parser.py +20 -0
  33. absmartly-0.1.2/sdk/http_client.py +29 -0
  34. absmartly-0.1.2/sdk/internal/__init__.py +0 -0
  35. absmartly-0.1.2/sdk/internal/buffers.py +28 -0
  36. absmartly-0.1.2/sdk/internal/lock/__init__.py +0 -0
  37. absmartly-0.1.2/sdk/internal/lock/atomic_bool.py +18 -0
  38. absmartly-0.1.2/sdk/internal/lock/atomic_int.py +24 -0
  39. absmartly-0.1.2/sdk/internal/lock/concurrency.py +57 -0
  40. absmartly-0.1.2/sdk/internal/lock/read_write_lock.py +30 -0
  41. absmartly-0.1.2/sdk/internal/murmur32.py +82 -0
  42. absmartly-0.1.2/sdk/internal/variant_assigner.py +36 -0
  43. absmartly-0.1.2/sdk/json/__init__.py +0 -0
  44. absmartly-0.1.2/sdk/json/attribute.py +7 -0
  45. absmartly-0.1.2/sdk/json/context_data.py +7 -0
  46. absmartly-0.1.2/sdk/json/experiement_application.py +5 -0
  47. absmartly-0.1.2/sdk/json/experiment.py +22 -0
  48. absmartly-0.1.2/sdk/json/experiment_variant.py +6 -0
  49. absmartly-0.1.2/sdk/json/exposure.py +15 -0
  50. absmartly-0.1.2/sdk/json/goal_achievement.py +7 -0
  51. absmartly-0.1.2/sdk/json/publish_event.py +15 -0
  52. absmartly-0.1.2/sdk/json/unit.py +6 -0
  53. absmartly-0.1.2/sdk/jsonexpr/evaluator.py +30 -0
  54. absmartly-0.1.2/sdk/jsonexpr/expr_evaluator.py +107 -0
  55. absmartly-0.1.2/sdk/jsonexpr/json_expr.py +42 -0
  56. absmartly-0.1.2/sdk/jsonexpr/operator.py +10 -0
  57. absmartly-0.1.2/sdk/jsonexpr/operators/and_combinator.py +11 -0
  58. absmartly-0.1.2/sdk/jsonexpr/operators/binary_operator.py +19 -0
  59. absmartly-0.1.2/sdk/jsonexpr/operators/boolean_combinator.py +17 -0
  60. absmartly-0.1.2/sdk/jsonexpr/operators/equals_operator.py +11 -0
  61. absmartly-0.1.2/sdk/jsonexpr/operators/greater_than_operator.py +8 -0
  62. absmartly-0.1.2/sdk/jsonexpr/operators/greater_than_or_equal_operator.py +8 -0
  63. absmartly-0.1.2/sdk/jsonexpr/operators/in_operator.py +16 -0
  64. absmartly-0.1.2/sdk/jsonexpr/operators/less_than_operator.py +8 -0
  65. absmartly-0.1.2/sdk/jsonexpr/operators/less_than_or_equal_operator.py +9 -0
  66. absmartly-0.1.2/sdk/jsonexpr/operators/match_operator.py +15 -0
  67. absmartly-0.1.2/sdk/jsonexpr/operators/not_operator.py +8 -0
  68. absmartly-0.1.2/sdk/jsonexpr/operators/null_operator.py +7 -0
  69. absmartly-0.1.2/sdk/jsonexpr/operators/or_combinator.py +10 -0
  70. absmartly-0.1.2/sdk/jsonexpr/operators/unary_operator.py +14 -0
  71. absmartly-0.1.2/sdk/jsonexpr/operators/value_operator.py +7 -0
  72. absmartly-0.1.2/sdk/jsonexpr/operators/var_operator.py +12 -0
  73. absmartly-0.1.2/sdk/time/clock.py +8 -0
  74. absmartly-0.1.2/sdk/time/fixed_clock.py +9 -0
  75. absmartly-0.1.2/sdk/time/system_clock_utc.py +8 -0
  76. absmartly-0.1.2/sdk/variable_parser.py +13 -0
  77. absmartly-0.1.2/setup.cfg +4 -0
  78. absmartly-0.1.2/test/test_buffers.py +62 -0
  79. absmartly-0.1.2/test/test_client.py +130 -0
  80. absmartly-0.1.2/test/test_context.py +1005 -0
  81. absmartly-0.1.2/test/test_default_audience_deserializer.py +30 -0
  82. absmartly-0.1.2/test/test_default_context_data_deserializer.py +156 -0
  83. absmartly-0.1.2/test/test_default_context_event_serializer.py +90 -0
  84. absmartly-0.1.2/test/test_murmur32.py +54 -0
  85. absmartly-0.1.2/test/test_variant_assigner.py +126 -0
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright © 2022 <copyright holders>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ include README.MD
@@ -0,0 +1,252 @@
1
+ Metadata-Version: 2.1
2
+ Name: absmartly
3
+ Version: 0.1.2
4
+ Summary: ABSmartly Python SDK lib
5
+ Author-email: ABSmartly <pram@gmail.com>
6
+ License: The MIT License (MIT)
7
+ Copyright © 2022 <copyright holders>
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
+ Project-URL: Homepage, https://github.com/absmartly/python3-sdk
15
+ Keywords: sdk,asbmartly
16
+ Classifier: Development Status :: 1 - Planning
17
+ Classifier: Intended Audience :: Science/Research
18
+ Classifier: License :: OSI Approved :: BSD License
19
+ Classifier: Operating System :: POSIX :: Linux
20
+ Classifier: Programming Language :: Python :: 2
21
+ Classifier: Programming Language :: Python :: 2.7
22
+ Classifier: Programming Language :: Python :: 3
23
+ Classifier: Programming Language :: Python :: 3.4
24
+ Classifier: Programming Language :: Python :: 3.5
25
+ Requires-Python: >=3.9
26
+ Description-Content-Type: text/markdown
27
+ Provides-Extra: dev
28
+ License-File: LICENSE
29
+
30
+ # A/B Smartly SDK
31
+
32
+ A/B Smartly - Python SDK
33
+
34
+ ## Compatibility
35
+
36
+ The A/B Smartly Python SDK is compatible with Python 3.
37
+ It provides both a blocking and an asynchronous interfaces.
38
+
39
+
40
+ ## Installation
41
+
42
+ ### Python Modules
43
+ setuptools~=60.2.0
44
+ requests~=2.28.1
45
+ urllib3~=1.26.12
46
+ jsons~=1.6.3
47
+
48
+ ### Dependencies
49
+ sh ./run.sh
50
+
51
+ ## Getting Started
52
+
53
+ Please follow the [installation](#installation) instructions before trying the following code:
54
+
55
+ #### Initialization
56
+ This example assumes an Api Key, an Application, and an Environment have been created in the A/B Smartly web console.
57
+ ```python
58
+
59
+ def main():
60
+ client_config = ClientConfig()
61
+ client_config.endpoint = "https://sandbox.test.io/v1"
62
+ client_config.api_key = "test"
63
+ client_config.application = "www"
64
+ client_config.environment = "prod"
65
+
66
+ default_client_config = DefaultHTTPClientConfig()
67
+ default_client = DefaultHTTPClient(default_client_config)
68
+ sdk_config = ABSmartlyConfig()
69
+ sdk_config.client = Client(client_config, default_client)
70
+ sdk = ABSmartly(sdk_config)
71
+
72
+ context_config = ContextConfig()
73
+ ctx = sdk.create_context(context_config)
74
+
75
+ # ...
76
+ }
77
+ ```
78
+
79
+ #### Creating a new Context synchronously
80
+ ```python
81
+ # define a new context request
82
+ context_config = ContextConfig()
83
+ context_config.publish_delay = 10
84
+ context_config.refresh_interval = 5
85
+ context_config.units = {"session_id": "bf06d8cb5d8137290c4abb64155584fbdb64d8",
86
+ "user_id": "12345"}
87
+
88
+ context_config = ContextConfig()
89
+ ctx = sdk.create_context(context_config)
90
+ ctx.wait_until_ready()
91
+ ```
92
+
93
+ #### Creating a new Context asynchronously
94
+ ```python
95
+ # define a new context request
96
+ context_config = ContextConfig()
97
+ context_config.publish_delay = 10
98
+ context_config.refresh_interval = 5
99
+ context_config.units = {"session_id": "bf06d8cb5d8137290c4abb64155584fbdb64d8",
100
+ "user_id": "12345"}
101
+
102
+ context_config = ContextConfig()
103
+ ctx = sdk.create_context(context_config)
104
+ ctx.wait_until_ready_async()
105
+ ```
106
+
107
+ #### Setting extra units for a context
108
+ You can add additional units to a context by calling the `set_unit()` or the `set_units()` method.
109
+ This method may be used for example, when a user logs in to your application, and you want to use the new unit type to the context.
110
+ Please note that **you cannot override an already set unit type** as that would be a change of identity, and will throw an exception. In this case, you must create a new context instead.
111
+ The `SetUnit()` and `SetUnits()` methods can be called before the context is ready.
112
+
113
+ ```python
114
+ context.set_unit("db_user_id", "1000013")
115
+
116
+ context.set_units({
117
+ "db_user_id": "1000013"
118
+ })
119
+ ```
120
+
121
+ #### Setting context attributes
122
+ The `set_attributes()` and `set_attributes()` methods can be called before the context is ready.
123
+ ```python
124
+ context.set_attributes("user_agent", req.get_header("User-Agent"))
125
+
126
+ context.set_attributes({
127
+ "customer_age": "new_customer"
128
+ })
129
+ ```
130
+
131
+ #### Selecting a treatment
132
+ ```python
133
+ res, _ = context.get_treatment("exp_test_experiment")
134
+ if res == 0:
135
+ # user is in control group (variant 0)
136
+ else:
137
+ # user is in treatment group
138
+ ```
139
+
140
+ #### Tracking a goal achievement
141
+ Goals are created in the A/B Smartly web console.
142
+ ```python
143
+ context.track("payment", {
144
+ "item_count": 1,
145
+ "total_amount": 1999.99
146
+ })
147
+ ```
148
+
149
+ #### Publishing pending data
150
+ Sometimes it is necessary to ensure all events have been published to the A/B Smartly collector, before proceeding.
151
+ You can explicitly call the `publish()` or `publish_async()` methods.
152
+ ```python
153
+ context.publish()
154
+ ```
155
+
156
+ #### Finalizing
157
+ The `close()` and `close_async()` methods will ensure all events have been published to the A/B Smartly collector, like `publish()`, and will also "seal" the context, throwing an error if any method that could generate an event is called.
158
+ ```python
159
+ context.close()
160
+ ```
161
+
162
+ #### Refreshing the context with fresh experiment data
163
+ For long-running contexts, the context is usually created once when the application is first started.
164
+ However, any experiments being tracked in your production code, but started after the context was created, will not be triggered.
165
+ To mitigate this, we can use the `set_refresh_interval()` method on the context config.
166
+
167
+ ```python
168
+ default_client_config = DefaultHTTPClientConfig()
169
+ default_client_config.refresh_interval = 5
170
+ ```
171
+
172
+ Alternatively, the `refresh()` method can be called manually.
173
+ The `refresh()` method pulls updated experiment data from the A/B Smartly collector and will trigger recently started experiments when `get_treatment()` is called again.
174
+ ```python
175
+ context.refresh()
176
+ ```
177
+
178
+ #### Using a custom Event Logger
179
+ The A/B Smartly SDK can be instantiated with an event logger used for all contexts.
180
+ In addition, an event logger can be specified when creating a particular context, in the `ContextConfig`.
181
+ ```python
182
+ class EventType(Enum):
183
+ ERROR = "error"
184
+ READY = "ready"
185
+ REFRESH = "refresh"
186
+ PUBLISH = "publish"
187
+ EXPOSURE = "exposure"
188
+ GOAL = "goal"
189
+ CLOSE = "close"
190
+
191
+
192
+ class ContextEventLogger:
193
+
194
+ @abstractmethod
195
+ def handle_event(self, event_type: EventType, data: object):
196
+ raise NotImplementedError
197
+ ```
198
+ The data parameter depends on the type of event.
199
+ Currently, the SDK logs the following events:
200
+
201
+ | event | when | data |
202
+ |:---: |------------------------------------------------------------|---|
203
+ | `Error` | `Context` receives an error | `Throwable` object |
204
+ | `Ready` | `Context` turns ready | `ContextData` used to initialize the context |
205
+ | `Refresh` | `Context.refresh()` method succeeds | `ContextData` used to refresh the context |
206
+ | `Publish` | `Context.publish()` method succeeds | `PublishEvent` sent to the A/B Smartly event collector |
207
+ | `Exposure` | `Context.getTreatment()` method succeeds on first exposure | `Exposure` enqueued for publishing |
208
+ | `Goal` | `Context.track()` method succeeds | `GoalAchievement` enqueued for publishing |
209
+ | `Close` | `Context.close()` method succeeds the first time | `null` |
210
+
211
+
212
+ #### Peek at treatment variants
213
+ Although generally not recommended, it is sometimes necessary to peek at a treatment or variable without triggering an exposure.
214
+ The A/B Smartly SDK provides a `peek_treament()` method for that.
215
+
216
+ ```python
217
+ res = context.peek_treamnet("exp_test_experiment")
218
+ if res == 0:
219
+ # user is in control group (variant 0)
220
+ else:
221
+ # user is in treatment group
222
+
223
+ ```
224
+
225
+ ##### Peeking at variables
226
+ ```python
227
+ variable = context.peek_variable("my_variable")
228
+ ```
229
+
230
+ #### Overriding treatment variants
231
+ During development, for example, it is useful to force a treatment for an experiment. This can be achieved with the `override()` and/or `overrides()` methods.
232
+ The `set_override()` and `set_overrides()` methods can be called before the context is ready.
233
+ ```python
234
+ context.set_override("exp_test_experiment", 1) # force variant 1 of treatment
235
+ context.set_overrides({
236
+ "exp_test_experiment": 1,
237
+ "exp_another_experiment": 0
238
+ })
239
+ ```
240
+
241
+ ## About A/B Smartly
242
+ **A/B Smartly** is the leading provider of state-of-the-art, on-premises, full-stack experimentation platforms for engineering and product teams that want to confidently deploy features as fast as they can develop them.
243
+ A/B Smartly's real-time analytics helps engineering and product teams ensure that new features will improve the customer experience without breaking or degrading performance and/or business metrics.
244
+
245
+ ### Have a look at our growing list of clients and SDKs:
246
+ - [Java SDK](https://www.github.com/absmartly/java-sdk)
247
+ - [JavaScript SDK](https://www.github.com/absmartly/javascript-sdk)
248
+ - [PHP SDK](https://www.github.com/absmartly/php-sdk)
249
+ - [Swift SDK](https://www.github.com/absmartly/swift-sdk)
250
+ - [Vue2 SDK](https://www.github.com/absmartly/vue2-sdk)
251
+ - [Go SDK](https://www.github.com/absmartly/go-sdk)
252
+ - [Ruby SDK](https://www.github.com/absmartly/ruby-sdk)
@@ -0,0 +1,223 @@
1
+ # A/B Smartly SDK
2
+
3
+ A/B Smartly - Python SDK
4
+
5
+ ## Compatibility
6
+
7
+ The A/B Smartly Python SDK is compatible with Python 3.
8
+ It provides both a blocking and an asynchronous interfaces.
9
+
10
+
11
+ ## Installation
12
+
13
+ ### Python Modules
14
+ setuptools~=60.2.0
15
+ requests~=2.28.1
16
+ urllib3~=1.26.12
17
+ jsons~=1.6.3
18
+
19
+ ### Dependencies
20
+ sh ./run.sh
21
+
22
+ ## Getting Started
23
+
24
+ Please follow the [installation](#installation) instructions before trying the following code:
25
+
26
+ #### Initialization
27
+ This example assumes an Api Key, an Application, and an Environment have been created in the A/B Smartly web console.
28
+ ```python
29
+
30
+ def main():
31
+ client_config = ClientConfig()
32
+ client_config.endpoint = "https://sandbox.test.io/v1"
33
+ client_config.api_key = "test"
34
+ client_config.application = "www"
35
+ client_config.environment = "prod"
36
+
37
+ default_client_config = DefaultHTTPClientConfig()
38
+ default_client = DefaultHTTPClient(default_client_config)
39
+ sdk_config = ABSmartlyConfig()
40
+ sdk_config.client = Client(client_config, default_client)
41
+ sdk = ABSmartly(sdk_config)
42
+
43
+ context_config = ContextConfig()
44
+ ctx = sdk.create_context(context_config)
45
+
46
+ # ...
47
+ }
48
+ ```
49
+
50
+ #### Creating a new Context synchronously
51
+ ```python
52
+ # define a new context request
53
+ context_config = ContextConfig()
54
+ context_config.publish_delay = 10
55
+ context_config.refresh_interval = 5
56
+ context_config.units = {"session_id": "bf06d8cb5d8137290c4abb64155584fbdb64d8",
57
+ "user_id": "12345"}
58
+
59
+ context_config = ContextConfig()
60
+ ctx = sdk.create_context(context_config)
61
+ ctx.wait_until_ready()
62
+ ```
63
+
64
+ #### Creating a new Context asynchronously
65
+ ```python
66
+ # define a new context request
67
+ context_config = ContextConfig()
68
+ context_config.publish_delay = 10
69
+ context_config.refresh_interval = 5
70
+ context_config.units = {"session_id": "bf06d8cb5d8137290c4abb64155584fbdb64d8",
71
+ "user_id": "12345"}
72
+
73
+ context_config = ContextConfig()
74
+ ctx = sdk.create_context(context_config)
75
+ ctx.wait_until_ready_async()
76
+ ```
77
+
78
+ #### Setting extra units for a context
79
+ You can add additional units to a context by calling the `set_unit()` or the `set_units()` method.
80
+ This method may be used for example, when a user logs in to your application, and you want to use the new unit type to the context.
81
+ Please note that **you cannot override an already set unit type** as that would be a change of identity, and will throw an exception. In this case, you must create a new context instead.
82
+ The `SetUnit()` and `SetUnits()` methods can be called before the context is ready.
83
+
84
+ ```python
85
+ context.set_unit("db_user_id", "1000013")
86
+
87
+ context.set_units({
88
+ "db_user_id": "1000013"
89
+ })
90
+ ```
91
+
92
+ #### Setting context attributes
93
+ The `set_attributes()` and `set_attributes()` methods can be called before the context is ready.
94
+ ```python
95
+ context.set_attributes("user_agent", req.get_header("User-Agent"))
96
+
97
+ context.set_attributes({
98
+ "customer_age": "new_customer"
99
+ })
100
+ ```
101
+
102
+ #### Selecting a treatment
103
+ ```python
104
+ res, _ = context.get_treatment("exp_test_experiment")
105
+ if res == 0:
106
+ # user is in control group (variant 0)
107
+ else:
108
+ # user is in treatment group
109
+ ```
110
+
111
+ #### Tracking a goal achievement
112
+ Goals are created in the A/B Smartly web console.
113
+ ```python
114
+ context.track("payment", {
115
+ "item_count": 1,
116
+ "total_amount": 1999.99
117
+ })
118
+ ```
119
+
120
+ #### Publishing pending data
121
+ Sometimes it is necessary to ensure all events have been published to the A/B Smartly collector, before proceeding.
122
+ You can explicitly call the `publish()` or `publish_async()` methods.
123
+ ```python
124
+ context.publish()
125
+ ```
126
+
127
+ #### Finalizing
128
+ The `close()` and `close_async()` methods will ensure all events have been published to the A/B Smartly collector, like `publish()`, and will also "seal" the context, throwing an error if any method that could generate an event is called.
129
+ ```python
130
+ context.close()
131
+ ```
132
+
133
+ #### Refreshing the context with fresh experiment data
134
+ For long-running contexts, the context is usually created once when the application is first started.
135
+ However, any experiments being tracked in your production code, but started after the context was created, will not be triggered.
136
+ To mitigate this, we can use the `set_refresh_interval()` method on the context config.
137
+
138
+ ```python
139
+ default_client_config = DefaultHTTPClientConfig()
140
+ default_client_config.refresh_interval = 5
141
+ ```
142
+
143
+ Alternatively, the `refresh()` method can be called manually.
144
+ The `refresh()` method pulls updated experiment data from the A/B Smartly collector and will trigger recently started experiments when `get_treatment()` is called again.
145
+ ```python
146
+ context.refresh()
147
+ ```
148
+
149
+ #### Using a custom Event Logger
150
+ The A/B Smartly SDK can be instantiated with an event logger used for all contexts.
151
+ In addition, an event logger can be specified when creating a particular context, in the `ContextConfig`.
152
+ ```python
153
+ class EventType(Enum):
154
+ ERROR = "error"
155
+ READY = "ready"
156
+ REFRESH = "refresh"
157
+ PUBLISH = "publish"
158
+ EXPOSURE = "exposure"
159
+ GOAL = "goal"
160
+ CLOSE = "close"
161
+
162
+
163
+ class ContextEventLogger:
164
+
165
+ @abstractmethod
166
+ def handle_event(self, event_type: EventType, data: object):
167
+ raise NotImplementedError
168
+ ```
169
+ The data parameter depends on the type of event.
170
+ Currently, the SDK logs the following events:
171
+
172
+ | event | when | data |
173
+ |:---: |------------------------------------------------------------|---|
174
+ | `Error` | `Context` receives an error | `Throwable` object |
175
+ | `Ready` | `Context` turns ready | `ContextData` used to initialize the context |
176
+ | `Refresh` | `Context.refresh()` method succeeds | `ContextData` used to refresh the context |
177
+ | `Publish` | `Context.publish()` method succeeds | `PublishEvent` sent to the A/B Smartly event collector |
178
+ | `Exposure` | `Context.getTreatment()` method succeeds on first exposure | `Exposure` enqueued for publishing |
179
+ | `Goal` | `Context.track()` method succeeds | `GoalAchievement` enqueued for publishing |
180
+ | `Close` | `Context.close()` method succeeds the first time | `null` |
181
+
182
+
183
+ #### Peek at treatment variants
184
+ Although generally not recommended, it is sometimes necessary to peek at a treatment or variable without triggering an exposure.
185
+ The A/B Smartly SDK provides a `peek_treament()` method for that.
186
+
187
+ ```python
188
+ res = context.peek_treamnet("exp_test_experiment")
189
+ if res == 0:
190
+ # user is in control group (variant 0)
191
+ else:
192
+ # user is in treatment group
193
+
194
+ ```
195
+
196
+ ##### Peeking at variables
197
+ ```python
198
+ variable = context.peek_variable("my_variable")
199
+ ```
200
+
201
+ #### Overriding treatment variants
202
+ During development, for example, it is useful to force a treatment for an experiment. This can be achieved with the `override()` and/or `overrides()` methods.
203
+ The `set_override()` and `set_overrides()` methods can be called before the context is ready.
204
+ ```python
205
+ context.set_override("exp_test_experiment", 1) # force variant 1 of treatment
206
+ context.set_overrides({
207
+ "exp_test_experiment": 1,
208
+ "exp_another_experiment": 0
209
+ })
210
+ ```
211
+
212
+ ## About A/B Smartly
213
+ **A/B Smartly** is the leading provider of state-of-the-art, on-premises, full-stack experimentation platforms for engineering and product teams that want to confidently deploy features as fast as they can develop them.
214
+ A/B Smartly's real-time analytics helps engineering and product teams ensure that new features will improve the customer experience without breaking or degrading performance and/or business metrics.
215
+
216
+ ### Have a look at our growing list of clients and SDKs:
217
+ - [Java SDK](https://www.github.com/absmartly/java-sdk)
218
+ - [JavaScript SDK](https://www.github.com/absmartly/javascript-sdk)
219
+ - [PHP SDK](https://www.github.com/absmartly/php-sdk)
220
+ - [Swift SDK](https://www.github.com/absmartly/swift-sdk)
221
+ - [Vue2 SDK](https://www.github.com/absmartly/vue2-sdk)
222
+ - [Go SDK](https://www.github.com/absmartly/go-sdk)
223
+ - [Ruby SDK](https://www.github.com/absmartly/ruby-sdk)