Personal helpers package
Project description
...
SafeExecutor
The SafeExecutor decorator allows you to quickly execute a function or method within a try/except block.
Parameter default allows you to override the return value in case of an error.
Parameter logger takes a logger
object and executes the logger.error(error)
method if an error occurs
Examples:
from aghelper.utils import SafeExecutor
from logging import getLogger, StreamHandler, Formatter
logger = getLogger(__name__)
formatter = Formatter("%(asctime)s - %(levelname)s - %(message)s")
sh = StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)
@SafeExecutor
def foo():
return 1/0
class FooClass:
@SafeExecutor
def foo_method(self):
return 1/0
@classmethod
@SafeExecutor
def foo_classmethod(cls):
return 1/0
@staticmethod
@SafeExecutor
def foo_staticmethod():
return 1/0
@SafeExecutor(default=0)
def change_return(self):
return 1/0
@SafeExecutor(logger=logger)
def write_to_log(self):
return 1/0
print(f"1. Func result: {foo()}")
print(f"2. Method foo result: {FooClass().foo_method()}")
print(f"3. Class method foo result: {FooClass.foo_classmethod()}")
print(f"5. Static method foo result: {FooClass.foo_staticmethod()}")
print(f"6. Set default return: {FooClass().change_return()}")
print(f"7. Write error to log: {FooClass().write_to_log()}")
>>> 1. Func result: None
>>> 2. Method foo result: None
>>> 3. Class method foo result: None
>>> 5. Static method foo result: None
>>> 6. Set default return: 0
>>> 7. Write error to log: None
>>> 2023-01-13 17:06:55,437 - ERROR - Execute error: division by zero
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
ag_helper-0.0.4.tar.gz
(4.1 kB
view details)
Built Distribution
File details
Details for the file ag_helper-0.0.4.tar.gz
.
File metadata
- Download URL: ag_helper-0.0.4.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce9c5daf2d2893e52c7600c0550bcd1a956e1958daed168b2b56e59adada9520 |
|
MD5 | adf06489b194f4541930136aa049bfda |
|
BLAKE2b-256 | 3fae7fa28f8c8097ecc116d18d5d0dfb7b3e8548f6a3a320d5812abfa064fac0 |
File details
Details for the file ag_helper-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: ag_helper-0.0.4-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73723869aac12b091904b747ac1c77150b20b8aaf45c3a94f566fd6451e308dc |
|
MD5 | 8042eada8f3a7681971f2af5d4486772 |
|
BLAKE2b-256 | b90356e77510a9edf76a1b4263d091624cdb538b8830472450b35f515f947ef6 |