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.1.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.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

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

Hashes for pysimpleevents-0.1.1.tar.gz
Algorithm Hash digest
SHA256 03ac87e0ef54a3e86eb56dffabcdc60c5a921c03f6fc447b2e6d23191ef325ea
MD5 5a129ceddc635b395bd67dd7bb23b4bf
BLAKE2b-256 7cc79246e9b8e2ee1887b82b45c7a80a8d7358d14e2536feb758a467c571129e

See more details on using hashes here.

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

Hashes for pysimpleevents-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fb5f78a0ce7735b7abb34aa533b18fcc07f93ccb9936f38860e4f8f8ecc954ce
MD5 96b00d42e07ad7a52d919171642e4a17
BLAKE2b-256 f42810d9cf572d7ab73afa0034371cc24d5a76d4fc5f112caf13de475b5704b3

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