Skip to main content

Sistema simples de eventos e propriedades reativas em Python

Project description

PySimpleEvents

Stars Python Version

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


Download files

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

Source Distribution

pysimpleevents-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

pysimpleevents-0.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pysimpleevents-0.1.0.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

Hashes for pysimpleevents-0.1.0.tar.gz
Algorithm Hash digest
SHA256 91a0ea61f39917f5f70112ab4c4e605c800e274a67026746801b6d3973f7c51a
MD5 738bf801f2c63293b1994251612e0e1d
BLAKE2b-256 119b8e8b9e30b7a2ea5a5716f17c09b2d4c42191e5880f495bb0b90a6bbf4eca

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pysimpleevents-0.1.0-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

Hashes for pysimpleevents-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 036b11ca058cf4e0674e96a1152aff3550a2bbd8aafdd142d3370f706d659758
MD5 8280c7489d3f007a7fcd0b4c7efd7004
BLAKE2b-256 484c076706a08dc833ddf174f2ae2f1fcf7f685fb04fb6bf3c34e991976b14db

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