Skip to main content

A simple global state manager to help pass values through mautiple py files

Project description

State Handler

A lightweight global state manager for Python.


Features

  • Simple global key-value storage
  • Accessible across your entire Python project
  • Lightweight and dependency-free
  • Event callbacks on state changes
  • Optional persistence support

Installation

pip install state-handler

Quick Start

from state_handler import state

# Set a value
state.set("username", "bob")

# Get a value
print(state.get("username"))

API Reference

set(key, value)

Store a value in global state.

get(key, default=None)

Retrieve a value.

delete(key)

Remove a value.

exists(key)

Returns True if key exists.

all()

Returns a copy of all current state values.


Persistence

state.save("state.json")
state.load("state.json")

Saves and loads state from a file.


Callbacks (Events)

Run code whenever state changes:

def on_change(key, old, new):
    print(f"{key} changed from {old} to {new}")

state.register_callback(on_change)

state.set("mode", "debug")
state.set("mode", "release")

Example

import state_handler as sh

sh.state.set("mode", "debug")

if sh.state.get("mode") == "debug":
    print("Debug mode is ON")

Why use this?

Sometimes you need shared state across multiple files without passing variables around. This package provides a simple global state system for Python projects.


Roadmap

  • Nested state support
  • Async event listeners
  • Thread-safe mode
  • State syncing between processes

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

state_handler-0.1.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

state_handler-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file state_handler-0.1.0.tar.gz.

File metadata

  • Download URL: state_handler-0.1.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for state_handler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b4d6a902919e25ce41c2cde0a35f8745e4f4db2a45fcff08c52ae3892447d2cc
MD5 264d7a08767fb21c13d5d63de1937740
BLAKE2b-256 ecc7b15394b77de33688c92793796b296b8f4712050658b7e0651fde272c71f6

See more details on using hashes here.

File details

Details for the file state_handler-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: state_handler-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for state_handler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 601c1b3ed88a3a23569dcd52f1441b59a2754930b7cc1eb3102eb09bf3522cac
MD5 6523c6daf2537bd5a4e8a295ad6d87d2
BLAKE2b-256 05512ae04f0d712af00727eed5d188c1d73fb7bd868ceeb39ba53df564626fcc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page