Skip to content

Commit 8574e16

Browse files
committed
feat: Add flag dependencies example to example.py
Add comprehensive flag dependencies example demonstrating: - Local evaluation of flags with dependency chains - Clear setup instructions for creating required flags - Testing both positive and negative dependency scenarios - Integration with the grouped example menu system Follows the same pattern as posthog-ruby example with real flag testing rather than mocked flags.
1 parent d19fa8f commit 8574e16

File tree

1 file changed

+81
-7
lines changed

1 file changed

+81
-7
lines changed

โ€Žexample.pyโ€Ž

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ def load_env_file():
8282
print("1. Identify and capture examples")
8383
print("2. Feature flag local evaluation examples")
8484
print("3. Feature flag payload examples")
85-
print("4. Context management and tagging examples")
86-
print("5. Run all examples")
87-
print("6. Exit")
88-
choice = input("\nEnter your choice (1-6): ").strip()
85+
print("4. Flag dependencies examples")
86+
print("5. Context management and tagging examples")
87+
print("6. Run all examples")
88+
print("7. Exit")
89+
choice = input("\nEnter your choice (1-7): ").strip()
8990

9091
if choice == "1":
9192
print("\n" + "=" * 60)
@@ -214,6 +215,62 @@ def load_env_file():
214215
print(f"Value (variant or enabled): {result.get_value()}")
215216

216217
elif choice == "4":
218+
print("\n" + "=" * 60)
219+
print("FLAG DEPENDENCIES EXAMPLES")
220+
print("=" * 60)
221+
print("๐Ÿ”— Testing flag dependencies with local evaluation...")
222+
print(" Flag structure: 'test-flag-dependency' depends on 'beta-feature' being enabled")
223+
print("")
224+
print("๐Ÿ“‹ Required setup (if 'test-flag-dependency' doesn't exist):")
225+
print(" 1. Create feature flag 'beta-feature':")
226+
print(" - Condition: email contains '@example.com'")
227+
print(" - Rollout: 100%")
228+
print(" 2. Create feature flag 'test-flag-dependency':")
229+
print(" - Condition: flag 'beta-feature' is enabled")
230+
print(" - Rollout: 100%")
231+
print("")
232+
233+
posthog.debug = True
234+
235+
# Test @example.com user (should satisfy dependency if flags exist)
236+
result1 = posthog.feature_enabled(
237+
"test-flag-dependency",
238+
"example_user",
239+
person_properties={"email": "[email protected]"},
240+
only_evaluate_locally=True,
241+
)
242+
print(f"โœ… @example.com user (test-flag-dependency): {result1}")
243+
244+
# Test non-example.com user (dependency should not be satisfied)
245+
result2 = posthog.feature_enabled(
246+
"test-flag-dependency",
247+
"regular_user",
248+
person_properties={"email": "[email protected]"},
249+
only_evaluate_locally=True,
250+
)
251+
print(f"โŒ Regular user (test-flag-dependency): {result2}")
252+
253+
# Test beta-feature directly for comparison
254+
beta1 = posthog.feature_enabled(
255+
"beta-feature",
256+
"example_user",
257+
person_properties={"email": "[email protected]"},
258+
only_evaluate_locally=True,
259+
)
260+
beta2 = posthog.feature_enabled(
261+
"beta-feature",
262+
"regular_user",
263+
person_properties={"email": "[email protected]"},
264+
only_evaluate_locally=True,
265+
)
266+
print(f"๐Ÿ“Š Beta feature comparison - @example.com: {beta1}, regular: {beta2}")
267+
268+
print("\n๐ŸŽฏ Results Summary:")
269+
print(f" - Flag dependencies evaluated locally: {'โœ… YES' if result1 != result2 else 'โŒ NO'}")
270+
print(" - Zero API calls needed: โœ… YES (all evaluated locally)")
271+
print(" - Python SDK supports flag dependencies: โœ… YES")
272+
273+
elif choice == "5":
217274
print("\n" + "=" * 60)
218275
print("CONTEXT MANAGEMENT AND TAGGING EXAMPLES")
219276
print("=" * 60)
@@ -274,7 +331,7 @@ def process_payment(payment_id):
274331
process_order("12345")
275332
process_payment("67890")
276333

277-
elif choice == "5":
334+
elif choice == "6":
278335
print("\n๐Ÿ”„ Running all examples...")
279336

280337
# Run example 1
@@ -308,20 +365,37 @@ def process_payment(payment_id):
308365
print(f"Payload: {posthog.get_feature_flag_payload('beta-feature', 'distinct_id')}")
309366

310367
# Run example 4
368+
print(f"\n{'๐Ÿ”ธ' * 20} FLAG DEPENDENCIES {'๐Ÿ”ธ' * 20}")
369+
print("๐Ÿ”— Testing flag dependencies...")
370+
result1 = posthog.feature_enabled(
371+
"test-flag-dependency",
372+
"demo_user",
373+
person_properties={"email": "[email protected]"},
374+
only_evaluate_locally=True,
375+
)
376+
result2 = posthog.feature_enabled(
377+
"test-flag-dependency",
378+
"demo_user2",
379+
person_properties={"email": "[email protected]"},
380+
only_evaluate_locally=True,
381+
)
382+
print(f"โœ… @example.com user: {result1}, regular user: {result2}")
383+
384+
# Run example 5
311385
print(f"\n{'๐Ÿ”ธ' * 20} CONTEXT MANAGEMENT {'๐Ÿ”ธ' * 20}")
312386
print("๐Ÿท๏ธ Testing context management...")
313387
with posthog.new_context():
314388
posthog.tag("demo_run", "all_examples")
315389
posthog.capture("demo_completed")
316390
print("โœ… Demo completed with context tags")
317391

318-
elif choice == "6":
392+
elif choice == "7":
319393
print("๐Ÿ‘‹ Goodbye!")
320394
posthog.shutdown()
321395
exit()
322396

323397
else:
324-
print("โŒ Invalid choice. Please run again and select 1-6.")
398+
print("โŒ Invalid choice. Please run again and select 1-7.")
325399
posthog.shutdown()
326400
exit()
327401

0 commit comments

Comments
ย (0)