Sistema simples de eventos e propriedades reativas em Python
Project description
PySimpleEvents
PySimpleEvents é uma biblioteca Python que fornece um sistema simples e flexível de eventos e propriedades reativas, permitindo que desenvolvedores conectem funções a eventos e rastreiem alterações de variáveis de forma elegante e intuitiva.
Índice
Instalação
Em breve disponível via PyPI:
pip install PySimpleEvents
Instalação a partir do repositório (exemplo):
git clone https://github.com/oDaniel728/PySimpleEvents.git
cd PySimpleEvents
pip install .
Eventos (Event)
A classe Event permite criar eventos que chamam funções conectadas sempre que o evento é emitido.
Conectar listeners
from PySimpleEvents import Event
event = Event[[], None]() # Event sem argumentos
@event.connect
def listener():
print("Hello World")
Emitir eventos
event.emit() # Output: Hello World
Listeners de uma vez (once)
Executa o listener apenas na primeira emissão:
def temp_listener():
print("Executed once!")
event.once(temp_listener)
event.emit() # Output: Executed once!
event.emit() # Não executa
Aguardando eventos (wait)
Permite pausar até que o evento seja emitido:
import threading
event = Event[[], None]()
def trigger_event():
import time
time.sleep(1)
event.emit()
threading.Thread(target=trigger_event).start()
event.wait() # Pausa até que o evento seja emitido
print("Event triggered!")
Propriedades Reativas (Property)
A classe Property permite criar variáveis rastreáveis com eventos acionados ao acessar (on_get) ou alterar (on_set) o valor.
Eventos on_set e on_get
from PySimpleEvents import Property
health = Property
@health.on_set
def _health_set(old: int, new: int) -> None:
print(f"Damage: {old - new}")
@health.on_get
def _health_get(current: int) -> None:
print(f"Health: {current}")
Exemplos de uso
# Alterando valor
health.set(80) # Output: Damage: 20
# Multiplicação (usa __imul__)
health *= 50 # Output: Damage: 30
# Acessando valor
print(~health) # Output: Health: 50 \n 50
print(health.get()) # Output: 50
# Operadores de comparação
print(health > 20) # True
print(health == 50) # True
Veja mais em ~/examples/
Licença
Este projeto está licenciado sob a Apache License 2.0. Consulte o arquivo LICENSE para mais detalhes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysimpleevents-0.1.1.tar.gz.
File metadata
- Download URL: pysimpleevents-0.1.1.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03ac87e0ef54a3e86eb56dffabcdc60c5a921c03f6fc447b2e6d23191ef325ea
|
|
| MD5 |
5a129ceddc635b395bd67dd7bb23b4bf
|
|
| BLAKE2b-256 |
7cc79246e9b8e2ee1887b82b45c7a80a8d7358d14e2536feb758a467c571129e
|
File details
Details for the file pysimpleevents-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pysimpleevents-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb5f78a0ce7735b7abb34aa533b18fcc07f93ccb9936f38860e4f8f8ecc954ce
|
|
| MD5 |
96b00d42e07ad7a52d919171642e4a17
|
|
| BLAKE2b-256 |
f42810d9cf572d7ab73afa0034371cc24d5a76d4fc5f112caf13de475b5704b3
|