Python

The Hyphen Toggle Provider for Python is an OpenFeature provider implementation that enables seamless feature flag evaluation. This section covers setup, usage, and configuration specific to the Python implementation.

Installation

Install the provider and the OpenFeature Python SDK:

pip install openfeature-sdk hyphen-openfeature-provider

Setup and Initialization

To begin using the Hyphen Provider, follow these steps:

  1. Import the required modules

  2. Configure the provider with your publicKey, application name and environment.

    You can specify the environment in one of two formats:
    • Alternate ID (e.g., "production", "staging") — the environment in which your application is running. • Project Environment ID (e.g., pevr_abc123) — useful for internal references.

  3. Register the provider with OpenFeature and create an OpenFeature client for your application.

from openfeature import api
from openfeature_provider_hyphen import (
    HyphenProvider,
    HyphenProviderOptions,
    HyphenEvaluationContext
)

# Initialize provider options
options = HyphenProviderOptions(
    application="your-app-name",
    environment="production"
)

# Create and set the provider
provider = HyphenProvider(
    public_key="your-public-key",
    options=options
)
api.set_provider(provider)

# Create a client
client = api.get_client()
  1. Use HyphenEvaluationContext to configure the required context for feature targeting evaluations, incorporating user or application context.
from openfeature_provider_hyphen import HyphenUser

# Create user details
user = HyphenUser(
    id="user-123",
    email="[email protected]",
    name="John Doe",
    custom_attributes={
        "role": "admin",
        "subscription": "premium"
    }
)

# Create evaluation context
context = HyphenEvaluationContext(
    targeting_key="user-123",
    attributes={
        "user": user,
        "ip_address": "203.0.113.42",
        "custom_attributes": {
            "device": "mobile",
            "platform": "ios"
        }
    }
)

Usage

Evaluation Context Example

Use the client to evaluate different types of feature flags in your application.

try:
    # Boolean flag evaluation
    is_enabled = client.get_boolean_value(
        flag_key="your-flag-key",
        default_value=False,
        evaluation_context=context
    )
    print(f"Feature enabled: {is_enabled}")

    # String flag evaluation
    theme = client.get_string_value(
        flag_key="app-theme",
        default_value="light",
        evaluation_context=context
    )

    # Integer flag evaluation
    max_items = client.get_integer_value(
        flag_key="max-items",
        default_value=10,
        evaluation_context=context
    )

    # Object flag evaluation
    config = client.get_object_value(
        flag_key="feature-config",
        default_value={
            "enabled": True,
            "timeout": 30
        },
        evaluation_context=context
    )
except Exception as e:
    print(f"Error evaluating flag: {e}")

Configuration

Options

OptionTypeRequiredDescription
applicationstrYesThe application id or alternate ID.
environmentstrYesThe environment identifier for the Hyphen project (project environment ID or alternateId).
horizon_urlsList[str]NoCustom Hyphen server URLs.
enable_toggle_usageboolNoEnable/disable telemetry (default: true).
cache_ttl_secondsintNoCache TTL in seconds (default: 300).
generate_cache_key_fnCallableNoCustom cache key generation function.

Context

FieldTypeRequiredDescription
targeting_keystrYesThe key used for caching the evaluation response.
attributesDictNoDictionary containing context attributes.
attributes.userHyphenUserNoUser information for targeting.
attributes.ip_addressstrNoThe IP address of the user.
attributes.custom_attributesDict[str, Any]NoAdditional contextual information.

HyphenUser Fields

FieldTypeRequiredDescription
idstrYesThe unique identifier of the user
emailstrNoThe email address of the user
namestrNoThe name of the user
custom_attributesDict[str, Any]NoCustom attributes specific to the user