# Horatio :sunglasses:
Time your python scripts easily and with style. This tool uses [`fslog`](https://github.com/fsossai/fslog) to format its output.
The same classes can be used either through `with` or as a `@decorator`.
```
pip install horatio
```
## Example
```python
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:
```python
with horatio.step("Inverting the matrix"):
B = np.linalg.inv(A)
```
As a decorator:
```python
@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:
```python
@horatio.section():
def inv(A):
return np.linalg.inv(A)
```
As a context:
```python
@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:
```python
@horatio.flat():
def inv(A):
return np.linalg.inv(A)
```
As a context:
```python
with horatio.flat("inv"):
B = np.linalg.inv(A)
```
Will produce something like
```
[*] inv
[*] inv: 0.123 s
```
Raw data
{
"_id": null,
"home_page": "http://github.com/fsossai/horatio",
"name": "horatio",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Federico Sossai",
"author_email": "federico.sossai@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/f0/4c/834dfc486c4deca9d0df15b661e35788d307e6710cc8c2aeff5357f342f0/horatio-0.1.6.tar.gz",
"platform": null,
"description": "# Horatio :sunglasses:\nTime your python scripts easily and with style. This tool uses [`fslog`](https://github.com/fsossai/fslog) to format its output.\n\nThe same classes can be used either through `with` or as a `@decorator`.\n\n```\npip install horatio\n```\n\n## Example\n\n```python\nimport horatio\nimport fslog\nimport time\n\n@horatio.section(\"Factorial computation\", tail=\"Took {}\")\ndef fact(n):\n if n == 1:\n fslog.log(\"Reached base case\")\n return 1\n fslog.log(\"This is not the base case\")\n with horatio.step(\"Sleeping for a second\"):\n time.sleep(1)\n res = n * fact(n-1)\n return res\n\nhoratio.unit = \"s\" # or \"ms\", \"us\", \"m\", \"h\"\nfact(4)\n```\nWill produce the following **output**:\n```\n\u250c\u2500 Factorial computation\n\u2502 This is not the base case\n\u2502 Sleeping for a second ... done in 1.001 s\n\u2502 \u250c\u2500 Factorial computation\n\u2502 \u2502 This is not the base case\n\u2502 \u2502 Sleeping for a second ... done in 1.003 s\n\u2502 \u2502 \u250c\u2500 Factorial computation\n\u2502 \u2502 \u2502 This is not the base case\n\u2502 \u2502 \u2502 Sleeping for a second ... done in 1.002 s\n\u2502 \u2502 \u2502 \u250c\u2500 Factorial computation\n\u2502 \u2502 \u2502 \u2502 Reached base case\n\u2502 \u2502 \u2502 \u2514\u2500 Took 0.000 s\n\u2502 \u2502 \u2514\u2500 Took 1.003 s\n\u2502 \u2514\u2500 Took 2.006 s\n\u2514\u2500 Took 3.007 s\n```\n\n## Features\n\n### `horatio.step()` \nPrints the description and the elapsed time in the same line. It is suggested for code sections that don't print any output.\n\n As a context:\n```python\nwith horatio.step(\"Inverting the matrix\"):\n B = np.linalg.inv(A)\n```\nAs a decorator:\n```python\n@horatio.step(\"Inverting the matrix\"):\ndef inv(A):\n return np.linalg.inv(A)\n```\nWill produce something like `Invering the matrix ... took 0.123 s`.\n\n### `horatio.section()`\nIt's useful when timing complex code with nested calls to other timed functions.\n\nAs a decorator:\n```python\n@horatio.section():\ndef inv(A):\n return np.linalg.inv(A)\n```\nAs a context:\n```python\n@horatio.section()\ndef parse(file_name):\n fslog.log(\"File name:\", file_name)\n return None\n\n@horatio.section()\ndef count_words(d):\n return 0\n\n@horatio.section()\ndef main():\n d = parse(\"words.txt\")\n n = count_words(d)\n fslog.log(n)\n```\nWill produce something like\n```\n\u250c\u2500 main\n\u2502 \u250c\u2500 parse\n\u2502 \u2502 File name: words.txt\n\u2502 \u2514\u2500 parse: 0.123 s\n\u2502 \u250c\u2500 count_words\n\u2502 \u2514\u2500 count_words: 4.567 s\n\u2514\u2500 main: 4.701 s\n```\n\n### `horatio.flat()`\nIt's useful when timing code that prints text and we want the output to be flat (no indentation).\n\nAs a decorator:\n```python\n@horatio.flat():\ndef inv(A):\n return np.linalg.inv(A)\n```\n As a context:\n```python\nwith horatio.flat(\"inv\"):\n B = np.linalg.inv(A)\n```\nWill produce something like\n```\n[*] inv\n[*] inv: 0.123 s\n```\n\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Time your python scripts easily and with style",
"version": "0.1.6",
"project_urls": {
"Homepage": "http://github.com/fsossai/horatio"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f04c834dfc486c4deca9d0df15b661e35788d307e6710cc8c2aeff5357f342f0",
"md5": "53d9c3616b93e9a752011eec4ab2e1b4",
"sha256": "0ac4aaa6741d3cec606aed67952419b30448b0f289f63d68340f02e097ada302"
},
"downloads": -1,
"filename": "horatio-0.1.6.tar.gz",
"has_sig": false,
"md5_digest": "53d9c3616b93e9a752011eec4ab2e1b4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 2839,
"upload_time": "2023-12-07T05:57:57",
"upload_time_iso_8601": "2023-12-07T05:57:57.838114Z",
"url": "https://files.pythonhosted.org/packages/f0/4c/834dfc486c4deca9d0df15b661e35788d307e6710cc8c2aeff5357f342f0/horatio-0.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-07 05:57:57",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "fsossai",
"github_project": "horatio",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "fslog",
"specs": []
}
],
"lcname": "horatio"
}