# TimeGuardian
TimeGuardian is a Python package designed for measuring and logging the execution time and memory usage of functions. This package is particularly useful for performance monitoring and optimization in Python applications.
## Features
- Decorators for timing functions and logging memory usage.
- Customizable logging for execution time and memory usage with rich formatting.
- Conditional logging based on execution time and memory usage thresholds.
## Installation
Install TimeGuardian directly from pip:
```bash
pip install timeguardian
```
Or, install from the source code:
```bash
cd path/to/TimeGuardian
pip install .
```
## Usage
Import `TimeGuardian` from the `timeguardian` package and use it as a decorator on your functions to measure their execution time and memory usage.
### Basic Usage
```python
from timeguardian import TimeGuardian
# To measure execution time and/or memory usage
@TimeGuardian.measure
def my_function():
# function implementation
# Custom name logging
@TimeGuardian.measure(name="CustomName")
def another_function():
# function implementation
# Custom logging with name, time and memory
@TimeGuardian.measure(name="CustomName", elapsed=True, memory=True) #(elapsed in ms, memory in bytes)
def another_function():
# function implementation
```
### Advanced Usage
Monitor performance with conditional logging:
```python
from timeguardian import TimeGuardian
# Monitor and log only if execution time or memory usage exceeds the specified limits (time in ms, memory in bytes)
@TimeGuardian.monitor(elapsed=200, memory=1024)
def monitored_function():
# function implementation
```
## Setting Global Units for Time and Memory Measurement
The TimeGuardian package allows you to set global units for time and memory measurements. This feature enables you to customize how the execution time and memory usage are reported.
### Usage
1. **Set the Units**: Use `TimeGuardian.set_time_unit(unit)` and `TimeGuardian.set_memory_unit(unit)` to set the global units for time and memory measurement. The `unit` parameter should be a string indicating the desired unit (e.g., 'ms' for milliseconds, 's' for seconds, 'bytes' for bytes, 'KB' for kilobytes, 'MB' for megabytes).
2. **Decorate Functions**: Apply the `@TimeGuardian.measure` decorator to your functions. This decorator will use the globally set units to measure and log the execution time and memory usage.
### Example
```python
from decorators import TimeGuardian
# Set the units for time and memory measurements
TimeGuardian.set_time_unit('ms') # Set time unit to milliseconds
TimeGuardian.set_memory_unit('MB') # Set memory unit to megabytes
@TimeGuardian.measure(elapsed=True, memory=True, name="Sample Function")
def sample_function():
# Function implementation
...
# Call the decorated function
sample_function()
```
In this example, `sample_function` is measured and logged in milliseconds for time and megabytes for memory usage.
## Contributing
Contributions to TimeGuardian are welcome!
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": "https://github.com/ceboaldin/TimeGuardian",
"name": "timeguardian",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Aldin Cebo",
"author_email": "ceboaldin@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c4/f1/a93becdabc8a2f6aa3c6e8d72d53d793c79bc193f223fac2e76b726a595c/timeguardian-0.0.7.tar.gz",
"platform": null,
"description": "\n# TimeGuardian\n\nTimeGuardian is a Python package designed for measuring and logging the execution time and memory usage of functions. This package is particularly useful for performance monitoring and optimization in Python applications.\n\n## Features\n\n- Decorators for timing functions and logging memory usage.\n- Customizable logging for execution time and memory usage with rich formatting.\n- Conditional logging based on execution time and memory usage thresholds.\n\n## Installation\n\nInstall TimeGuardian directly from pip:\n\n```bash\npip install timeguardian\n```\n\nOr, install from the source code:\n\n```bash\ncd path/to/TimeGuardian\npip install .\n```\n\n## Usage\n\nImport `TimeGuardian` from the `timeguardian` package and use it as a decorator on your functions to measure their execution time and memory usage.\n\n### Basic Usage\n\n```python\nfrom timeguardian import TimeGuardian\n\n# To measure execution time and/or memory usage\n@TimeGuardian.measure\ndef my_function():\n # function implementation\n\n# Custom name logging\n@TimeGuardian.measure(name=\"CustomName\")\ndef another_function():\n # function implementation\n\n\n# Custom logging with name, time and memory\n@TimeGuardian.measure(name=\"CustomName\", elapsed=True, memory=True) #(elapsed in ms, memory in bytes)\ndef another_function():\n # function implementation\n```\n\n### Advanced Usage\n\nMonitor performance with conditional logging:\n\n```python\nfrom timeguardian import TimeGuardian\n\n# Monitor and log only if execution time or memory usage exceeds the specified limits (time in ms, memory in bytes)\n@TimeGuardian.monitor(elapsed=200, memory=1024)\ndef monitored_function():\n # function implementation\n```\n\n## Setting Global Units for Time and Memory Measurement\n\nThe TimeGuardian package allows you to set global units for time and memory measurements. This feature enables you to customize how the execution time and memory usage are reported.\n\n### Usage\n\n1. **Set the Units**: Use `TimeGuardian.set_time_unit(unit)` and `TimeGuardian.set_memory_unit(unit)` to set the global units for time and memory measurement. The `unit` parameter should be a string indicating the desired unit (e.g., 'ms' for milliseconds, 's' for seconds, 'bytes' for bytes, 'KB' for kilobytes, 'MB' for megabytes).\n\n2. **Decorate Functions**: Apply the `@TimeGuardian.measure` decorator to your functions. This decorator will use the globally set units to measure and log the execution time and memory usage.\n\n### Example\n\n```python\nfrom decorators import TimeGuardian\n\n# Set the units for time and memory measurements\nTimeGuardian.set_time_unit('ms') # Set time unit to milliseconds\nTimeGuardian.set_memory_unit('MB') # Set memory unit to megabytes\n\n@TimeGuardian.measure(elapsed=True, memory=True, name=\"Sample Function\")\ndef sample_function():\n # Function implementation\n ...\n\n# Call the decorated function\nsample_function()\n```\n\nIn this example, `sample_function` is measured and logged in milliseconds for time and megabytes for memory usage.\n\n## Contributing\n\nContributions to TimeGuardian are welcome!\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n",
"bugtrack_url": null,
"license": "",
"summary": "TimeGuardian is a package for execution time measurement",
"version": "0.0.7",
"project_urls": {
"Homepage": "https://github.com/ceboaldin/TimeGuardian"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "05afa002add71454529d98c01ea29c41d283d624de75f7409b4cf94ede822b7c",
"md5": "5dd2c18d05731b680d276213a10dbc29",
"sha256": "e63f5963f961117a1469b4b55b40f04cd7f18ee78512ecfa8088a319c82d223f"
},
"downloads": -1,
"filename": "timeguardian-0.0.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5dd2c18d05731b680d276213a10dbc29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6971,
"upload_time": "2024-01-23T12:04:24",
"upload_time_iso_8601": "2024-01-23T12:04:24.672919Z",
"url": "https://files.pythonhosted.org/packages/05/af/a002add71454529d98c01ea29c41d283d624de75f7409b4cf94ede822b7c/timeguardian-0.0.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c4f1a93becdabc8a2f6aa3c6e8d72d53d793c79bc193f223fac2e76b726a595c",
"md5": "840cb71d29f56a1c892e0c1d8d053117",
"sha256": "09dfc439b17e63fb72b2263f57bfd040d77bdafb3f3645ee73baa94016220d8e"
},
"downloads": -1,
"filename": "timeguardian-0.0.7.tar.gz",
"has_sig": false,
"md5_digest": "840cb71d29f56a1c892e0c1d8d053117",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6795,
"upload_time": "2024-01-23T12:04:27",
"upload_time_iso_8601": "2024-01-23T12:04:27.856061Z",
"url": "https://files.pythonhosted.org/packages/c4/f1/a93becdabc8a2f6aa3c6e8d72d53d793c79bc193f223fac2e76b726a595c/timeguardian-0.0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-23 12:04:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ceboaldin",
"github_project": "TimeGuardian",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "certifi",
"specs": [
[
"==",
"2023.11.17"
]
]
},
{
"name": "charset-normalizer",
"specs": [
[
"==",
"3.3.2"
]
]
},
{
"name": "docutils",
"specs": [
[
"==",
"0.20.1"
]
]
},
{
"name": "idna",
"specs": [
[
"==",
"3.6"
]
]
},
{
"name": "importlib-metadata",
"specs": [
[
"==",
"7.0.0"
]
]
},
{
"name": "iniconfig",
"specs": [
[
"==",
"2.0.0"
]
]
},
{
"name": "jaraco.classes",
"specs": [
[
"==",
"3.3.0"
]
]
},
{
"name": "keyring",
"specs": [
[
"==",
"24.3.0"
]
]
},
{
"name": "markdown-it-py",
"specs": [
[
"==",
"3.0.0"
]
]
},
{
"name": "mdurl",
"specs": [
[
"==",
"0.1.2"
]
]
},
{
"name": "more-itertools",
"specs": [
[
"==",
"10.1.0"
]
]
},
{
"name": "nh3",
"specs": [
[
"==",
"0.2.15"
]
]
},
{
"name": "packaging",
"specs": [
[
"==",
"23.2"
]
]
},
{
"name": "pkginfo",
"specs": [
[
"==",
"1.9.6"
]
]
},
{
"name": "pluggy",
"specs": [
[
"==",
"1.3.0"
]
]
},
{
"name": "psutil",
"specs": [
[
"==",
"5.9.7"
]
]
},
{
"name": "Pygments",
"specs": [
[
"==",
"2.17.2"
]
]
},
{
"name": "pytest",
"specs": [
[
"==",
"7.4.4"
]
]
},
{
"name": "pytest-mock",
"specs": [
[
"==",
"3.12.0"
]
]
},
{
"name": "readme-renderer",
"specs": [
[
"==",
"42.0"
]
]
},
{
"name": "requests",
"specs": [
[
"==",
"2.31.0"
]
]
},
{
"name": "requests-toolbelt",
"specs": [
[
"==",
"1.0.0"
]
]
},
{
"name": "rfc3986",
"specs": [
[
"==",
"2.0.0"
]
]
},
{
"name": "rich",
"specs": [
[
"==",
"13.7.0"
]
]
},
{
"name": "tqdm",
"specs": [
[
"==",
"4.66.1"
]
]
},
{
"name": "twine",
"specs": [
[
"==",
"4.0.2"
]
]
},
{
"name": "urllib3",
"specs": [
[
"==",
"2.1.0"
]
]
},
{
"name": "zipp",
"specs": [
[
"==",
"3.17.0"
]
]
}
],
"lcname": "timeguardian"
}