Skip to content

chore: Improvements to example.py#310

Merged
haacked merged 7 commits intomasterfrom
haacked/improved-example
Aug 19, 2025
Merged

chore: Improvements to example.py#310
haacked merged 7 commits intomasterfrom
haacked/improved-example

Conversation

@haacked
Copy link
Collaborator

@haacked haacked commented Aug 19, 2025

This PR makes some improvements to example.py to make it easier to test changes.

  1. Loads values (personal api key, project api key, host) from .env.
    a. Includes an .env.example with sample configuration values. cp .env.example .env and then update values in .env
    b. Include fallback to hardcoded values for backward compatibility
  2. Group examples in example.py and allow running specific groups or all examples:

Sample run:

> python example.py
🔑 Testing PostHog authentication...
[FEATURE FLAGS] Error loading feature flags: [PostHog]  (502)
✅ Authentication successful!
   Project API Key: phc_8oSYh...
   Personal API Key: phx_EKsBX...
   Host: http://localhost:8010


🚀 PostHog Python SDK Demo - Choose an example to run:

1. Identify and capture examples
2. Feature flag local evaluation examples
3. Feature flag payload examples
4. Context management and tagging examples
5. Run all examples
6. Exit

Enter your choice (1-6): 6
👋 Goodbye!

- Add .env.example with sample configuration values
- Update example.py to read API keys and host from environment variables
- Include fallback to hardcoded values for backward compatibility
- Add simple .env file loader without external dependencies
- Document usage in example.py header comments

This makes it easier for users to configure the example script
without modifying the source code directly.
The PostHog module-level functions require api_key to be set, while
project_api_key is used for local evaluation and other features.
Setting both ensures all functionality works correctly.
@haacked haacked requested a review from a team August 19, 2025 02:16
@dmarticus dmarticus moved this to In Review in Feature Flags Aug 19, 2025
@haacked haacked force-pushed the haacked/improved-example branch from 9ae9e19 to c03e16a Compare August 19, 2025 02:16
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR significantly improves the developer experience for the PostHog Python SDK's example script by transforming it from a basic monolithic demonstration into an interactive, configurable demo tool. The changes introduce environment variable configuration loading from a .env file with fallback to hardcoded values for backward compatibility, ensuring existing workflows remain functional.

Key improvements include:

  1. Environment Configuration: Added support for loading credentials (project API key, personal API key, and host) from a .env file, with an .env.example template provided. The implementation includes a custom environment file parser that respects existing environment variables and provides clear guidance when credentials are missing.

  2. Authentication Testing: Implemented upfront credential validation that tests PostHog authentication before running examples, providing immediate feedback on configuration issues with clear error messages and setup guidance.

  3. Interactive Menu System: Restructured the examples into four categorized groups (identify/capture, feature flags local evaluation, feature flag payloads, and context management) with an interactive menu that allows developers to run specific examples or all examples at once.

  4. Enhanced User Experience: Added emoji indicators, better error handling, and professional formatting to make the tool more approachable and easier to debug.

The changes maintain all original PostHog SDK functionality while making the example script much more practical for developers who want to test specific features, use their own credentials securely, or evaluate the SDK's capabilities in a structured way. This fits well within the broader PostHog Python SDK ecosystem as a developer-friendly tool for learning and testing the library's features.

Confidence score: 4/5

  • This PR is safe to merge with low risk of breaking existing functionality
  • Score reflects well-structured code with good error handling, though the custom .env parsing could use minor improvements
  • Pay attention to the custom environment file parsing logic for potential edge cases with malformed .env files

2 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

haacked and others added 4 commits August 18, 2025 21:14
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@github-project-automation github-project-automation bot moved this from In Review to Approved in Feature Flags Aug 19, 2025
@haacked haacked merged commit 6a3e7ef into master Aug 19, 2025
10 checks passed
@haacked haacked deleted the haacked/improved-example branch August 19, 2025 04:46
@github-project-automation github-project-automation bot moved this from Approved to Done in Feature Flags Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants