ignoreexceptions


Nameignoreexceptions JSON
Version 0.11 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/ignoreexceptions
SummaryDecorators that provide graceful exception handling, customization of error handling behavior, error logging
upload_time2023-06-26 23:18:37
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords ignore exceptions decorator
VCS
bugtrack_url
requirements touchtouch
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Decorators that provide graceful exception handling, customization of error handling behavior, error logging

## pip install ignoreexceptions

### Tested against Windows 10 / Python 3.10 / Anaconda 

The decorators break_KeyboardInterrupt, ignore_all_exceptions, and ignore_Exception 
offer several advantages in exception handling and error management. Here are some of the key advantages:

### Graceful Exception Handling: 

The decorators allow for graceful handling of exceptions by catching and controlling 
their impact on the program's execution. 
Instead of allowing exceptions to propagate and potentially crash the program, 
these decorators provide the ability to handle exceptions in a controlled manner.

### Customized Exception Handling: 

The decorators provide flexibility in defining custom exception handling behavior. 
Developers can specify alternative actions, return values, or error messages when exceptions occur. 
This allows for tailored exception handling strategies based on 
the specific needs of the application or use case.

### Error Logging and Monitoring: 

The decorators can log exceptions to a 
specified logfile, enabling error tracking, monitoring, and analysis. 
This facilitates the identification and resolution of errors by providing a 
centralized record of encountered exceptions.

### Prevent Program Termination: 

By handling exceptions, these decorators prevent the abrupt termination of the program. 
This is particularly useful in long-running processes, scripts, or automation 
tasks where a program crash would interrupt the entire workflow. 
Instead, the program can continue executing or gracefully exit 
based on the specified behavior.

### Improved User Experience: 

Graceful exception handling ensures that applications do 
not crash or display cryptic error messages to end-users. 
By providing alternative actions or error messages, the decorators can enhance 
the user experience by providing meaningful feedback or fallback mechanisms.

### Debugging and Troubleshooting: 

The decorators can be valuable during 
the debugging and troubleshooting phases of development. 
By catching and logging exceptions, developers can gain insights into 
the causes of errors, track down problematic code sections, and analyze 
the stack trace for detailed debugging information.

### Robustness and Resilience: 

These decorators contribute to the overall robustness and resilience of applications or systems. 
By handling exceptions and recovering from errors, the code becomes more fault-tolerant
and can continue functioning in the presence of unexpected events or exceptional conditions.


### Importing 

```python
from ignoreexceptions import ignore_all_exceptions, ignore_Exception, break_KeyboardInterrupt
```

### Example 1: Using break_KeyboardInterrupt decorator

```python
@break_KeyboardInterrupt
def test_while_loop():
    from time import sleep

    while True:
        print("oioi")
        sleep(1)


# In this example, the break_KeyboardInterrupt decorator is applied to the test_while_loop function.
# It ensures that if a KeyboardInterrupt exception is raised during the execution of the function,
# the loop will be interrupted and the program will not be terminated.
```


### Example 2: Using ignore_all_exceptions decorator

```python
@ignore_all_exceptions(
    v="dum**ss", print_exceptions=True, active=True, logfile="c:\\logtest.txt"
)
def divide(a, b):
    return a / b


# In this example, the ignore_all_exceptions decorator is applied to the divide function.
# It handles all exceptions that may occur during the execution of the function.
# If an exception is caught, it prints the exception, logs it to a specified logfile,
# and returns the value "dum**ss" instead of raising the exception.


```

### Example 3: Using ignore_Exception decorator

```python

@ignore_Exception(
    v=list(), print_exceptions=True, active=True, logfile="c:\\logtest.txt"
)
def process_data(l=(0, 1, 2, 3, 4, 5)):
    from random import choice

    results = []
    for _ in range(100000):
        results.append(10 / choice(l))
    return results


# The decorator @ignore_Exception(v="error occurred", print_exceptions=True, active=True, logfile='c:\\logtest.txt')
# is applied to the process_data function. It means that if any exception occurs during the execution of the function,
# the exception will be caught, printed (if print_exceptions is True), and logged to the specified logfile
# ('c:\logtest.txt'). Instead of raising the exception, the function will return the value "error occurred"
# (specified by v) as the result.
#
# Therefore, this function is designed to perform a potentially risky
# computation repeatedly and handle any exceptions that may occur during the process.

# Log file output:
# 2023/06/26 19:25:16.712902
# Traceback (most recent call last):
#   File "C:\ProgramData\anaconda3\envs\dfdir\di.py", line 125, in wrapper
#     result = func(*args, **kwargs)
#   File "C:\ProgramData\anaconda3\envs\dfdir\di.py", line 193, in process_data
#     results.append(10 / choice(l))
# ZeroDivisionError: division by zero
#
# ---------------------------

```

### Combining decorators:

```python

@break_KeyboardInterrupt
@ignore_Exception(
    v=list(), print_exceptions=True, active=True, logfile="c:\\logtest.txt"
)
def process_data2(l=(0, 1, 2, 3, 4, 5)):
    from random import choice
    from time import sleep

    results = []
    for _ in range(100000):
        sleep(1)
        results.append(10 / choice(l[1:]))
    return results
    
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/ignoreexceptions",
    "name": "ignoreexceptions",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "ignore,exceptions,decorator",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/7c/1a/7a21876a4d813fa2d0c6be1b97ad214f3d759ea82534479173a109f7926c/ignoreexceptions-0.11.tar.gz",
    "platform": null,
    "description": "\r\n# Decorators that provide graceful exception handling, customization of error handling behavior, error logging\r\n\r\n## pip install ignoreexceptions\r\n\r\n### Tested against Windows 10 / Python 3.10 / Anaconda \r\n\r\nThe decorators break_KeyboardInterrupt, ignore_all_exceptions, and ignore_Exception \r\noffer several advantages in exception handling and error management. Here are some of the key advantages:\r\n\r\n### Graceful Exception Handling: \r\n\r\nThe decorators allow for graceful handling of exceptions by catching and controlling \r\ntheir impact on the program's execution. \r\nInstead of allowing exceptions to propagate and potentially crash the program, \r\nthese decorators provide the ability to handle exceptions in a controlled manner.\r\n\r\n### Customized Exception Handling: \r\n\r\nThe decorators provide flexibility in defining custom exception handling behavior. \r\nDevelopers can specify alternative actions, return values, or error messages when exceptions occur. \r\nThis allows for tailored exception handling strategies based on \r\nthe specific needs of the application or use case.\r\n\r\n### Error Logging and Monitoring: \r\n\r\nThe decorators can log exceptions to a \r\nspecified logfile, enabling error tracking, monitoring, and analysis. \r\nThis facilitates the identification and resolution of errors by providing a \r\ncentralized record of encountered exceptions.\r\n\r\n### Prevent Program Termination: \r\n\r\nBy handling exceptions, these decorators prevent the abrupt termination of the program. \r\nThis is particularly useful in long-running processes, scripts, or automation \r\ntasks where a program crash would interrupt the entire workflow. \r\nInstead, the program can continue executing or gracefully exit \r\nbased on the specified behavior.\r\n\r\n### Improved User Experience: \r\n\r\nGraceful exception handling ensures that applications do \r\nnot crash or display cryptic error messages to end-users. \r\nBy providing alternative actions or error messages, the decorators can enhance \r\nthe user experience by providing meaningful feedback or fallback mechanisms.\r\n\r\n### Debugging and Troubleshooting: \r\n\r\nThe decorators can be valuable during \r\nthe debugging and troubleshooting phases of development. \r\nBy catching and logging exceptions, developers can gain insights into \r\nthe causes of errors, track down problematic code sections, and analyze \r\nthe stack trace for detailed debugging information.\r\n\r\n### Robustness and Resilience: \r\n\r\nThese decorators contribute to the overall robustness and resilience of applications or systems. \r\nBy handling exceptions and recovering from errors, the code becomes more fault-tolerant\r\nand can continue functioning in the presence of unexpected events or exceptional conditions.\r\n\r\n\r\n### Importing \r\n\r\n```python\r\nfrom ignoreexceptions import ignore_all_exceptions, ignore_Exception, break_KeyboardInterrupt\r\n```\r\n\r\n### Example 1: Using break_KeyboardInterrupt decorator\r\n\r\n```python\r\n@break_KeyboardInterrupt\r\ndef test_while_loop():\r\n    from time import sleep\r\n\r\n    while True:\r\n        print(\"oioi\")\r\n        sleep(1)\r\n\r\n\r\n# In this example, the break_KeyboardInterrupt decorator is applied to the test_while_loop function.\r\n# It ensures that if a KeyboardInterrupt exception is raised during the execution of the function,\r\n# the loop will be interrupted and the program will not be terminated.\r\n```\r\n\r\n\r\n### Example 2: Using ignore_all_exceptions decorator\r\n\r\n```python\r\n@ignore_all_exceptions(\r\n    v=\"dum**ss\", print_exceptions=True, active=True, logfile=\"c:\\\\logtest.txt\"\r\n)\r\ndef divide(a, b):\r\n    return a / b\r\n\r\n\r\n# In this example, the ignore_all_exceptions decorator is applied to the divide function.\r\n# It handles all exceptions that may occur during the execution of the function.\r\n# If an exception is caught, it prints the exception, logs it to a specified logfile,\r\n# and returns the value \"dum**ss\" instead of raising the exception.\r\n\r\n\r\n```\r\n\r\n### Example 3: Using ignore_Exception decorator\r\n\r\n```python\r\n\r\n@ignore_Exception(\r\n    v=list(), print_exceptions=True, active=True, logfile=\"c:\\\\logtest.txt\"\r\n)\r\ndef process_data(l=(0, 1, 2, 3, 4, 5)):\r\n    from random import choice\r\n\r\n    results = []\r\n    for _ in range(100000):\r\n        results.append(10 / choice(l))\r\n    return results\r\n\r\n\r\n# The decorator @ignore_Exception(v=\"error occurred\", print_exceptions=True, active=True, logfile='c:\\\\logtest.txt')\r\n# is applied to the process_data function. It means that if any exception occurs during the execution of the function,\r\n# the exception will be caught, printed (if print_exceptions is True), and logged to the specified logfile\r\n# ('c:\\logtest.txt'). Instead of raising the exception, the function will return the value \"error occurred\"\r\n# (specified by v) as the result.\r\n#\r\n# Therefore, this function is designed to perform a potentially risky\r\n# computation repeatedly and handle any exceptions that may occur during the process.\r\n\r\n# Log file output:\r\n# 2023/06/26 19:25:16.712902\r\n# Traceback (most recent call last):\r\n#   File \"C:\\ProgramData\\anaconda3\\envs\\dfdir\\di.py\", line 125, in wrapper\r\n#     result = func(*args, **kwargs)\r\n#   File \"C:\\ProgramData\\anaconda3\\envs\\dfdir\\di.py\", line 193, in process_data\r\n#     results.append(10 / choice(l))\r\n# ZeroDivisionError: division by zero\r\n#\r\n# ---------------------------\r\n\r\n```\r\n\r\n### Combining decorators:\r\n\r\n```python\r\n\r\n@break_KeyboardInterrupt\r\n@ignore_Exception(\r\n    v=list(), print_exceptions=True, active=True, logfile=\"c:\\\\logtest.txt\"\r\n)\r\ndef process_data2(l=(0, 1, 2, 3, 4, 5)):\r\n    from random import choice\r\n    from time import sleep\r\n\r\n    results = []\r\n    for _ in range(100000):\r\n        sleep(1)\r\n        results.append(10 / choice(l[1:]))\r\n    return results\r\n    \r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Decorators that provide graceful exception handling, customization of error handling behavior, error logging",
    "version": "0.11",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/ignoreexceptions"
    },
    "split_keywords": [
        "ignore",
        "exceptions",
        "decorator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5331412f28ad16772ed909e9580b78db82acc5d9dd91fa1af13d44d43e6e126a",
                "md5": "e2463670f4f6a8076e06ea632c6f64a2",
                "sha256": "9a7fb609056920319069448f443c7bbe57a2ce08d2f2daa83340a0cbd4b1de3e"
            },
            "downloads": -1,
            "filename": "ignoreexceptions-0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e2463670f4f6a8076e06ea632c6f64a2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8645,
            "upload_time": "2023-06-26T23:18:35",
            "upload_time_iso_8601": "2023-06-26T23:18:35.842913Z",
            "url": "https://files.pythonhosted.org/packages/53/31/412f28ad16772ed909e9580b78db82acc5d9dd91fa1af13d44d43e6e126a/ignoreexceptions-0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7c1a7a21876a4d813fa2d0c6be1b97ad214f3d759ea82534479173a109f7926c",
                "md5": "db63491f6b8087bc3bbb5a8ab7ed4ff6",
                "sha256": "4190822979bfb26ea4b8d6330680d257c6f2b57ea67c37c0003f81196f63a5d2"
            },
            "downloads": -1,
            "filename": "ignoreexceptions-0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "db63491f6b8087bc3bbb5a8ab7ed4ff6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6574,
            "upload_time": "2023-06-26T23:18:37",
            "upload_time_iso_8601": "2023-06-26T23:18:37.386788Z",
            "url": "https://files.pythonhosted.org/packages/7c/1a/7a21876a4d813fa2d0c6be1b97ad214f3d759ea82534479173a109f7926c/ignoreexceptions-0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-26 23:18:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "ignoreexceptions",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "touchtouch",
            "specs": []
        }
    ],
    "lcname": "ignoreexceptions"
}
        
Elapsed time: 0.08480s