App toggles library
This library has been created taking into account the work done by Pete Hodgson published in https://martinfowler.com/.
How to use it
The example below uses a sample feature to include a cancellation link in a email so the user can cancel the sales order.
- Create the /tmp/test_app_toggles.json file with the following content:
{
"isOrderCancellationEnabled": true,
"isAutoRefundEnable": false
}
- Run the app
import app_toggles
provider = app_toggles.JsonToggleProvider("/tmp/test_app_toggles.json")
toggles = app_toggles.Toggles(provider=provider)
@toggles.toggle_decision
def usage_of_order_cancellation_email(get_toggles, when_on, when_off):
order_cancellation_enabled, auto_refund_enabled = get_toggles(
["isOrderCancellationEnabled", "isAutoRefundEnable"]
)
if order_cancellation_enabled and auto_refund_enabled:
return when_on
return when_off
def create_email_body(client_name: str, sales_order_number: str) -> str:
header = f"""
Dear {client_name},
Your order number {sales_order_number} has bee approved.
"""
footer = """
Cheers,
Your company team
"""
cancellation_text = usage_of_order_cancellation_email(
when_on=f"To cancel your order follow this link: http://cancel/{sales_order_number}",
when_off="",
)
return f"""
{header}
{cancellation_text}
{footer}
"""
body = create_email_body("Gustavo", "2342937")
print(body)
- Feel free to change the value of the toggles in the JSON file and see how the email body changes
How to extend this library
The library exposes a "Provider" interface. You can create your own interface to interact with external services that manage feature flags. Below you' ll find a sample code used to retrieve toggles stored in memory.
DON'T FORGET TO CREATE A PR SO WE CAN ADD THE IMPLEMENTATION YOU'VE CREATED!
import typing
import app_toggles
class InMemoryProvider(app_toggles.Provider):
toggles = {
"isOrderCancellationEnabled": True,
"isAutoRefundEnable": True
}
def get_toggles(self, toggle_names: typing.List[str]) -> typing.Tuple[bool]:
missing_toogles = [toggle for toggle in toggle_names if toggle not in InMemoryProvider.toggles]
if missing_toogles:
raise app_toggles.ToggleNotFoundError(f"The follwing toggles where not found: {', '.join(missing_toogles)}")
return [
toggle_value
for toggle_name, toggle_value in InMemoryProvider.toggles.items()
if toggle_name in toggle_names
]
Metadata
Release files for app-toggles 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| app_toggles-0.1.2.tar.gz | 3.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| app_toggles-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.5 kB
Release files / app_toggles-0.1.2.tar.gz
| Download URL | app_toggles-0.1.2.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a610ec31c78f6f75b9f9933998615bc0364e18e0c7d00fc64a34cb763eb0d4a9
|
|
BLAKE2b-256 checksum How to use checksums |
91edd6e400c334234e898c94e3c6b43a40ee1ba86857d63a9c5234575b831851
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.10.4 Darwin/22.5.0
|
Release files / app_toggles-0.1.2-py3-none-any.whl
| Download URL | app_toggles-0.1.2-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e159c7aa26c7198ff920240f154190b1c4d3fc46da0e95c4c113bd3a45718b86
|
|
BLAKE2b-256 checksum How to use checksums |
6d098c4d8f6c6ceb4cc2c1787bc6627c04f05b15c90d6dd372d0733ee3b2b73d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.1 CPython/3.10.4 Darwin/22.5.0
|
Security for app-toggles 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.