Skip to main content

Time your python scripts easily and with style

Project description

Horatio :sunglasses:

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

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

horatio-0.1.6.tar.gz (2.8 kB view details)

Uploaded Source

File details

Details for the file horatio-0.1.6.tar.gz.

File metadata

  • Download URL: horatio-0.1.6.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for horatio-0.1.6.tar.gz
Algorithm Hash digest
SHA256 0ac4aaa6741d3cec606aed67952419b30448b0f289f63d68340f02e097ada302
MD5 53d9c3616b93e9a752011eec4ab2e1b4
BLAKE2b-256 f04c834dfc486c4deca9d0df15b661e35788d307e6710cc8c2aeff5357f342f0

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