Python decorator function to track metadata on function calls
Project description
# Metadata for Functions
Python decorator function to track metadata on function calls
# Example
```python
@meta_func(ignore_errors=True)
def self_mult(n):
sleep(0.2)
return n*n
print(self_mult(10)) # => 100
print(self_mult.log_info()) # => {'time_started': 1422042033.971449, 'warnings': None, 'args': (10,), 'error_info': None, 'time_ended': 1422042034.171857, 'time_elapsed': 0.200408, 'return_value': 100, 'kwargs': None}
print(self_mult(15)) # => 225
print(self_mult.log_info()) # => {'time_started': 1422042034.172171, 'warnings': None, 'args': (15,), 'error_info': None, 'time_ended': 1422042034.373299, 'time_elapsed': 0.201128, 'return_value': 225, 'kwargs': None}
print(self_mult("foo")) # => None
print(self_mult.log_info()) # => {'time_started': 1422042034.373651, 'warnings': None, 'args': ('foo',), 'error_info': (<type 'exceptions.TypeError'>, TypeError("can't multiply sequence by non-int of type 'str'",), <traceback object at 0x10fcea098>), 'time_ended': 1422042034.574758, 'time_elapsed': 0.201107, 'return_value': None, 'kwargs': None}
```
# Usage
Decorate a function with `@meta_func()` passing the optional keyword argument ignore_errors = True/False. ignore_errors tells `@meta_func` whether or not to raise any exception your decorated function raises or to solely log to the error_info attribute.
# Compatibility
Tested compatible with Py2.7 and Py3, but this code is provided as is with no warranty or guarantee, implied or explicit.
# More Information
Please see my blog post here: http://garrens.com/blog/2015/01/23/metadata-for-functions-python-decorator/
Python decorator function to track metadata on function calls
# Example
```python
@meta_func(ignore_errors=True)
def self_mult(n):
sleep(0.2)
return n*n
print(self_mult(10)) # => 100
print(self_mult.log_info()) # => {'time_started': 1422042033.971449, 'warnings': None, 'args': (10,), 'error_info': None, 'time_ended': 1422042034.171857, 'time_elapsed': 0.200408, 'return_value': 100, 'kwargs': None}
print(self_mult(15)) # => 225
print(self_mult.log_info()) # => {'time_started': 1422042034.172171, 'warnings': None, 'args': (15,), 'error_info': None, 'time_ended': 1422042034.373299, 'time_elapsed': 0.201128, 'return_value': 225, 'kwargs': None}
print(self_mult("foo")) # => None
print(self_mult.log_info()) # => {'time_started': 1422042034.373651, 'warnings': None, 'args': ('foo',), 'error_info': (<type 'exceptions.TypeError'>, TypeError("can't multiply sequence by non-int of type 'str'",), <traceback object at 0x10fcea098>), 'time_ended': 1422042034.574758, 'time_elapsed': 0.201107, 'return_value': None, 'kwargs': None}
```
# Usage
Decorate a function with `@meta_func()` passing the optional keyword argument ignore_errors = True/False. ignore_errors tells `@meta_func` whether or not to raise any exception your decorated function raises or to solely log to the error_info attribute.
# Compatibility
Tested compatible with Py2.7 and Py3, but this code is provided as is with no warranty or guarantee, implied or explicit.
# More Information
Please see my blog post here: http://garrens.com/blog/2015/01/23/metadata-for-functions-python-decorator/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
meta_func-0.1.3.tar.gz
(2.4 kB
view details)
File details
Details for the file meta_func-0.1.3.tar.gz
.
File metadata
- Download URL: meta_func-0.1.3.tar.gz
- Upload date:
- Size: 2.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a8df9e0fc3797ab8942d67912163d2a410310e88029c10b0370c45a8c47cff4 |
|
MD5 | 534e7ed272ed00fb3537a82bc92bee84 |
|
BLAKE2b-256 | 9a2c6523741535354cf8e01252007bf854a38b49f0fd1a2230bd80b58f946c1a |