Skip to main content

Adaptive waiting and execution engine — replaces time.sleep() with system-aware, deterministic waiting.

Project description

NanoWait v7 — Adaptive Execution Engine for Python

Stop waiting blindly. Execute intelligently.

NanoWait substitui time.sleep() por um motor adaptativo que observa CPU, RAM e rede em tempo real e aprende com cada execução.


⚡ Instalação

pip install nano-wait

🚀 Uso em 30 segundos

from nano_wait import wait

# Espera adaptativa: ajusta o tempo ao hardware
wait(2)

# Modo smart: autodetecta a velocidade ideal
wait(2, smart=True)

# Polling até condição ser True (ou timeout)
wait(lambda: button.is_visible(), timeout=10)

# Condição com erro customizado
from nano_wait import wait_until
wait_until(lambda: page.is_loaded(), timeout=10, msg="Página não carregou")

🧰 API Completa

wait(t, **kwargs) — O coração da lib

Parâmetro Tipo Padrão Descrição
t float | callable | None Tempo, condição ou auto
timeout float 15.0 Timeout máximo (modo callable)
speed str | float "normal" Preset ou valor float
smart bool False Autodetecta velocidade pelo hardware
profile str None Perfil de execução
verbose bool False Logs de diagnóstico
explain bool False Retorna ExplainReport detalhado
raise_on_timeout bool False Lança WaitTimeoutError se callable expirar

Presets de velocidade: "crawl" | "slow" | "normal" | "fast" | "ultra" | "turbo"

wait(1, speed="fast")       # espera rápida
wait(1, speed=4.5)          # fator personalizado
wait(1, profile="ci")       # perfil CI/CD (agressivo)
wait(1, explain=True)       # retorna ExplainReport

wait_until(condition, *, timeout, msg, **kwargs) — Polling semântico

Igual ao wait(callable) mas lança WaitTimeoutError com mensagem customizável.

from nano_wait import wait_until, WaitTimeoutError

try:
    wait_until(lambda: driver.title == "Home", timeout=15, msg="Home page não carregou")
except WaitTimeoutError as e:
    print(f"Falhou: {e}")

timed_wait(label) — Context manager de medição

from nano_wait import timed_wait

with timed_wait("login_flow") as info:
    driver.find_element("#user").send_keys("admin")
    driver.find_element("#pass").send_keys("pass")
    driver.find_element("#submit").click()

print(f"Login levou {info['duration']:.3f}s")

execute(fn, **kwargs) — Execução com retry inteligente

from nano_wait import execute

result = execute(
    lambda: api.get_user(42),
    timeout=10,
    interval=0.3,
    max_attempts=5,
    on_error=lambda e, n: print(f"Tentativa {n}: {e}"),
)

if result.success:
    print(result.result)
else:
    result.raise_if_failed()  # relança a última exceção

wait_async — Versão assíncrona

import asyncio
from nano_wait import wait_async

async def main():
    await wait_async(1.0, smart=True)
    await wait_async(lambda: check_ready(), timeout=10)

asyncio.run(main())

wait_pool — Múltiplos waits em paralelo

from nano_wait import wait_pool

# Dispara 3 esperas simultaneamente
results = wait_pool([1.0, 2.0, 0.5], speed="fast")

🎛️ Perfis de Execução

Perfil Agr. Uso ideal
ci 0.4 GitHub Actions, GitLab CI, pipelines rápidos
testing 0.8 QA local, testes unitários
default 1.0 Uso geral
rpa 2.0 Automação de sites lentos ou legados
turbo 0.25 Velocidade máxima (quando estabilidade não importa)
safe 3.0 Estabilidade máxima (conexões frágeis, hardware fraco)
wait(2, profile="turbo")   # o mais rápido possível
wait(2, profile="safe")    # o mais estável possível

🔁 Decorators

from nano_wait import retry, timed, wait_before

# Retry automático até sucesso
@retry(timeout=10, max_attempts=5, smart=True)
def fetch_data():
    return requests.get(url).json()

# Mede tempo de execução
@timed()
def process_image(img):
    ...

# Espera adaptativa antes de cada chamada
@wait_before(0.5, smart=True)
def click_button(driver, selector):
    driver.find_element(selector).click()

🧠 Como o motor pensa

NanoWait usa a fórmula de Custo de Oportunidade de Espera:

WaitTime = (BaseTime / (SystemHealth × SpeedFactor)) × ProfileAggressiveness

Onde:

  • SystemHealth ∈ [0, 10] — derivado de CPU + RAM (e Wi-Fi se disponível)
  • SpeedFactor ∈ [0.3, 10] — controlado pelo usuário ou autodetectado
  • ProfileAggressiveness — multiplicador do perfil ativo

O motor mantém um arquivo ~/.nano_wait_learning.json que registra um bias por perfil via EMA (Exponential Moving Average), calibrando-se com cada execução.

from nano_wait import AdaptiveLearning

# Estatísticas do perfil "default"
al = AdaptiveLearning("default")
print(al.stats())
# {'profile': 'default', 'bias': 0.97, 'samples': 42, 'success_rate': 0.976}

# Todos os perfis
print(AdaptiveLearning.all_profiles_stats())

# Reset do bias
al.reset()

🌐 Utilitários de Rede

from nano_wait import has_internet

if has_internet():
    wait(2, wifi="MeuSSID", smart=True)
else:
    wait(5, profile="safe")  # rede instável → mais conservador

🐛 Exceções

from nano_wait import WaitTimeoutError, InvalidProfileError

try:
    wait(lambda: False, timeout=1, raise_on_timeout=True)
except WaitTimeoutError as e:
    print(f"Timeout: {e}")

🆚 NanoWait vs time.sleep()

time.sleep(2) wait(2, smart=True)
PC poderoso 2.000s ~0.3s
PC médio 2.000s ~1.0s
PC sobrecarregado 2.000s ~2.8s
Aprende com o tempo
Polling adaptativo
Retry inteligente

📄 Licença

MIT © NanoWait Team

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

nano_wait-6.2.0.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

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

nano_wait-6.2.0-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file nano_wait-6.2.0.tar.gz.

File metadata

  • Download URL: nano_wait-6.2.0.tar.gz
  • Upload date:
  • Size: 27.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for nano_wait-6.2.0.tar.gz
Algorithm Hash digest
SHA256 28da55d2c44f93dc584a1f53bc5af678b24638699a0fa5c650b0b00c00154c6d
MD5 d8a2537948e7eca77a10e6f3b2c2107c
BLAKE2b-256 9214c2630b5469ba250d226e39e7dea43f1a69bb8563e66f415d3c51df7e4fc7

See more details on using hashes here.

File details

Details for the file nano_wait-6.2.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for nano_wait-6.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0fb99c6eb6aaa8b66c551fc3740ba02b312c1018b59e0f4e2fbd316a7d94347b
MD5 b17e4e1c3b26833b8d62757fd1d291da
BLAKE2b-256 edc760f10d194a0daedc4b10aec09a549d0383a91839f648b3435bcff9c1481f

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