Skip to content

Instantly share code, notes, and snippets.

@fcoclavero
Last active August 4, 2023 20:11
Show Gist options
  • Save fcoclavero/703677bedfb1b72581a4c80ffc03e91c to your computer and use it in GitHub Desktop.
Save fcoclavero/703677bedfb1b72581a4c80ffc03e91c to your computer and use it in GitHub Desktop.
Django Cheatsheet
class SuppressExceptionsDecorator:
"""Decorator for temporarily suppressing specified exceptions.
Exception suppression is limited to the function execution.
Example:
```python
import warnings
@SuppressErrorsDecorator(MyException)
def foo():
raise MyException("bar")
foo() # this function call should not raise `MyException`.
```
"""
def __init__(self, *exceptions: Exception) -> None:
"""Decorator constructor that saves the exceptions to be suppressed.
Arguments:
exceptions: the exceptions to be suppressed during the
execution of the decorated Callable.
"""
self.exceptions = exceptions
def __call__(self, func: Callable) -> Callable:
"""Decorator for temporarily suppressing specific exceptions during the
execution of the decorated Callable.
Implementing the `__call__` method makes the class callable.
See:
https://docs.python.org/3/reference/datamodel.html#emulating-callable-objects
Arguments:
func:
The Callable to be decorated.
Returns:
The decorated Callable, which suppresses the specified exceptions
during its execution time.
"""
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Wrapped function to be returned by the decorator.
Arguments:
args:
Original Callable arguments.
kwargs:
Original Callable keyword arguments.
Returns:
Original Callable evaluation return values.
"""
try: # exception suppression limited to this code block
return func(*args, **kwargs) # evaluate Callable
except self.exceptions: # register exceptions to be suppressed
pass
return wrapper # return decorated Callable
class SuppressWarningsDecorator:
"""Decorator for temporarily suppressing specified warnings.
Warning suppression is limited to the function execution.
See:
https://docs.python.org/3/library/warnings.html#temporarily-suppressing-warnings
Example:
```python
import warnings
@SuppressWarningDecorator(MyWarning)
def foo():
warnings.warn("bar", MyWarning)
foo() # this function call should not raise `MyWarning`.
```
"""
def __init__(self, *warnings: Warning) -> None:
"""Decorator constructor that saves the warnings to be suppressed.
Arguments:
warnings:
The warnings to be suppressed during the
execution of the decorated Callable.
"""
self.warnings = warnings
def __call__(self, func: Callable) -> Callable:
"""Decorator for temporarily suppressing specific warnings during the
execution of the decorated Callable.
Implementing the `__call__` method makes the class callable.
See:
https://docs.python.org/3/reference/datamodel.html#emulating-callable-objects
Arguments:
func: the Callable to be decorated.
Returns:
Callable: the decorated Callable, which suppresses the specified warnings
during its execution time.
"""
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Wrapped function to be returned by the decorator.
Arguments:
args:
Original Callable arguments.
kwargs:
Original Callable keyword arguments.
Returns:
Any: original Callable evaluation return values.
"""
with catch_warnings(): # warning suppression limited to this code block
for warning in self.warnings: # register warnings to be suppressed
simplefilter("ignore", category=warning)
return func(*args, **kwargs) # evaluate Callable
return wrapper # return decorated Callable
# Add users to Group
group.user_set.add(*django_users) # where `django_users` is a list of `django.contrib.auth.models.User`s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment