Horatio 😎
Time your python scripts easily and with style. This tool uses fslog to format its output.
The same classes can be used either through with or as a @decorator.
pip install horatio
Example
import horatio
import fslog
import time
@horatio.section("Factorial computation", tail="Took {}")
def fact(n):
if n == 1:
fslog.log("Reached base case")
return 1
fslog.log("This is not the base case")
with horatio.step("Sleeping for a second"):
time.sleep(1)
res = n * fact(n-1)
return res
horatio.unit = "s" # or "ms", "us", "m", "h"
fact(4)
Will produce the following output:
┌─ Factorial computation
│ This is not the base case
│ Sleeping for a second ... done in 1.001 s
│ ┌─ Factorial computation
│ │ This is not the base case
│ │ Sleeping for a second ... done in 1.003 s
│ │ ┌─ Factorial computation
│ │ │ This is not the base case
│ │ │ Sleeping for a second ... done in 1.002 s
│ │ │ ┌─ Factorial computation
│ │ │ │ Reached base case
│ │ │ └─ Took 0.000 s
│ │ └─ Took 1.003 s
│ └─ Took 2.006 s
└─ Took 3.007 s
Features
horatio.step()
Prints the description and the elapsed time in the same line. It is suggested for code sections that don't print any output.
As a context:
with horatio.step("Inverting the matrix"):
B = np.linalg.inv(A)
As a decorator:
@horatio.step("Inverting the matrix"):
def inv(A):
return np.linalg.inv(A)
Will produce something like Invering the matrix ... took 0.123 s.
horatio.section()
It's useful when timing complex code with nested calls to other timed functions.
As a decorator:
@horatio.section():
def inv(A):
return np.linalg.inv(A)
As a context:
@horatio.section()
def parse(file_name):
fslog.log("File name:", file_name)
return None
@horatio.section()
def count_words(d):
return 0
@horatio.section()
def main():
d = parse("words.txt")
n = count_words(d)
fslog.log(n)
Will produce something like
┌─ main
│ ┌─ parse
│ │ File name: words.txt
│ └─ parse: 0.123 s
│ ┌─ count_words
│ └─ count_words: 4.567 s
└─ main: 4.701 s
horatio.flat()
It's useful when timing code that prints text and we want the output to be flat (no indentation).
As a decorator:
@horatio.flat():
def inv(A):
return np.linalg.inv(A)
As a context:
with horatio.flat("inv"):
B = np.linalg.inv(A)
Will produce something like
[*] inv
[*] inv: 0.123 s
Metadata
Release files for horatio 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| horatio-0.1.6.tar.gz | 2.8 kB | Details |
Release files / horatio-0.1.6.tar.gz
| Download URL | horatio-0.1.6.tar.gz |
|---|---|
| Size | 2.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0ac4aaa6741d3cec606aed67952419b30448b0f289f63d68340f02e097ada302
|
|
BLAKE2b-256 checksum How to use checksums |
f04c834dfc486c4deca9d0df15b661e35788d307e6710cc8c2aeff5357f342f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|