Skip to main content

Cross-platform python module to set run time limits (timer, timeout and iterative deepening) as decorators.

Project description

timework

PyPI Build Status Coverage Status Codacy Badge PyPI - Python Version platform

Cross-platform python module to set run time limits (timer, timeout, iterative) as decorators.

Install

pip install timework

Usage

import timework as tw

timework.timer

import logging

@tw.timer(logging.warning)
def timer_demo_a():
    i = 0
    while i < 2 ** 23:
        i += 1
    return i

@tw.timer(print, detail=True)
def timer_demo_b():
    i = 0
    while i < 2 ** 24:
        i += 1
    return i

@tw.timer(timeout=1)
def timer_demo_c():
    i = 0
    while i < 2 ** 25:
        i += 1
    return i
a = timer_demo_a()
b = timer_demo_b()

try:
    c = timer_demo_c()
except tw.TimeError as e:
    print('error:', e.message)
    c = e.result

print(a, b, c)
WARNING:root:timer_demo_a: 0.496672 seconds used
START:  Tue Feb 18 15:06:45 2020
FINISH: Tue Feb 18 15:06:46 2020
timer_demo_b: 0.989352 seconds used
error: timer_demo_c: 1.9817 seconds used
8388608 16777216 33554432

timework.limit

@tw.limit(3)
def limit_demo(m):
    i = 0
    while i < 2 ** m:
        i += 1
    return i
try:
    s = limit_demo(4)
except tw.TimeError as e:
    print(e)
else:
    print('result:', s)

try:
    s = limit_demo(30)
except tw.TimeError as e:
    print(e)
else:
    print('result:', s)
result: 16
limit_demo: 3 seconds exceeded

timework.iterative

@tw.iterative(3)
def iterative_demo_a(max_depth):
    i = 0
    while i < 2 ** max_depth:
        i += 1
    return max_depth, i

@tw.iterative(3, history=5, key='depth')
def iterative_demo_b(depth):
    i = 0
    while i < 2 ** depth:
        i += 1
    return depth
try:
    s = iterative_demo_a(max_depth=10)
except tw.TimeError as e:
    print(e.message)
    print(e.result, e.detail)
else:
    print('result:', s)

try:
    s = iterative_demo_a(max_depth=25)
except tw.TimeError as e:
    print(e.message)
    print(e.result, e.detail)
else:
    print('result:', s)

try:
    s = iterative_demo_b(depth=25)
except tw.TimeError as e:
    print(e.message)
    print(e.result, e.detail)
else:
    print('result:', s)
result: (10, 1024)
iterative_demo_a/iterative_deepening: 3 seconds exceeded
(20, 1048576) deque([(20, 1048576)], maxlen=1)
iterative_demo_b/iterative_deepening: 3 seconds exceeded
20 deque([16, 17, 18, 19, 20], maxlen=5)

License

MIT 漏 bugstop

Project details


Download files

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

Files for timework, version 0.2.5
Filename, size File type Python version Upload date Hashes
Filename, size timework-0.2.5-py3-none-any.whl (5.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size timework-0.2.5.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page