DEV Community

Cover image for Switch Values and Logic—No Ifs, Just a Python Library
tsuruko
tsuruko

Posted on • Edited on

Switch Values and Logic—No Ifs, Just a Python Library

I recently released my first Python library as a beta version, and it's already close to 200 installs on PyPI 🎉
In this post, I’ll give a quick overview of what the library does.

About the Library

Library name: triggon

Install with:

pip install triggon
Enter fullscreen mode Exit fullscreen mode

This library lets you automatically switch behavior—such as changing variable values, performing early returns, or calling functions—based on pre-defined labels, without writing any if-statements.

Key features:

  • Auto-switching literal or variable values
  • Early return handling based on flags
  • Dynamically execute functions

Sample Code Overview

Here are 3 simple examples that demonstrate how it works:

➀ Auto-switching a single value

Switch the greeting only if a specific function is called:

from triggon import Triggon

tg = Triggon("msg", new="Good night")  # Set the label and its new value

def switch_msg():
    tg.set_trigger("msg")  # Activate the trigger for 'msg'
    greet()

def greet():
    # Set the original value for the label
    x = tg.alter_literal("msg", org="Good morning")

    print(x)

# First call
greet()

# Second call
switch_msg()
Enter fullscreen mode Exit fullscreen mode
# First output
Good morning

# Second output
Good night
Enter fullscreen mode Exit fullscreen mode

➁ Switching multiple values assigned to a single label

You can assign multiple values to a single label, using index-based control.
You can also bind multiple values to the same index.

(Excerpt from the README)

import random
from triggon import Triggon

# Set two values (index 0 and 1) for each label
tg = Triggon({
    "level_1": ["Normal", 80],
    "level_2": ["Rare", 100],
    "level_3": ["Legendary", 150],
})

level = None
attack = None

def spin_gacha():
    items = ["level_1", "level_2", "level_3"]
    result = random.choice(items)

    tg.set_trigger(result) # Activate the trigger based on result

    tg.alter_var(result, level)            # Index 0
    tg.alter_var(result, attack, index=1)  # Index 1

    print(f"You got a {level} sword!")
    print(f"Attack power: {attack}")

spin_gacha()
Enter fullscreen mode Exit fullscreen mode
# Output (if result is 'level_2')
You got a Rare sword!
Attack power: 100
Enter fullscreen mode Exit fullscreen mode

You can also use a * prefix in labels to indicate the index position
(e.g., '**label' for index 2).
When passing labels as a dictionary, use * instead of keyword arguments, since keywords aren't supported in that case.

➂ Early Return + Function Execution

Here’s an example combining early return and function execution:

from triggon import Triggon, TrigFunc

# Early return returns a predefined value
# Function execution returns the result of the function
tg = Triggon({
    "skip": "Process stopped early!",
    "call": None,
})

F = TrigFunc() # Create an instance for function wrapping

def example():
    print("If the 'call' flag is active, jump to example_2().")
    tg.trigger_func("call", F.example_2())
    return "Finished normally!"

def example_2():
    print("example_2() reached!")
    tg.trigger_return("skip")

# example() works on its own if the trigger isn't activated
result = tg.exit_point("skip", F.example())
print(result)

# Activate the triggers to set off an early return and function call
tg.set_trigger(["skip", "call"])

result = tg.exit_point("skip", F.example())
print(result)
Enter fullscreen mode Exit fullscreen mode
# First output
If the 'call' flag is active, jump to example_2().
Finished normally!

# Second output
If the 'call' flag is active, jump to example_2().
example_2() reached!
Process stopped early!
Enter fullscreen mode Exit fullscreen mode

That’s a quick overview of how triggon works!
It’s especially helpful for temporary overrides or complex conditional flows.

This is still a beta release, so I’m actively fixing bugs and updating features.
The source code is available on GitHub—feel free to check it out!

Bug reports and feature suggestions are always welcome 🙌

📘 For detailed docs and more examples:
README

🔗 GitHub: tsuruko12/triggon
🔗 X: @tool_tsuruko12

Top comments (0)