Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gcascio/arg-guard
ArgGuard is a simple abstract utility class to remove some boilerplate code when asserting class arguments.
https://github.com/gcascio/arg-guard
arguments assert python utility
Last synced: about 4 hours ago
JSON representation
ArgGuard is a simple abstract utility class to remove some boilerplate code when asserting class arguments.
- Host: GitHub
- URL: https://github.com/gcascio/arg-guard
- Owner: gcascio
- License: mit
- Created: 2021-09-17T02:23:27.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2021-10-03T20:12:43.000Z (about 3 years ago)
- Last Synced: 2024-10-13T18:57:50.456Z (26 days ago)
- Topics: arguments, assert, python, utility
- Language: Python
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ArgGuard [![Tests Actions Status](https://github.com/gcascio/arg-guard/workflows/Tests/badge.svg)](https://github.com/gcascio/arg-guard/actions)
ArgGuard is a simple abstract utility class to remove some boilerplate code when asserting class arguments.
## Installation
ArgGuard can be installed by running `pip install argguard`
## Usage
The abstract `ArgGuard` class provides the abstract `_argGuard` method. This is the method where all assertions, checks and validations of the class arguments should take place. Every class that extends the `ArgGuard` class needs to implement this method.
A second central method is `_assert_args` which stores the provided arguments and calls `_argGuard`. This method should be called as soon as possible in the constructor of the class to be guarded.
## API
| Method | Args | Description |
|----------------------|----------------------------|------------------------------------------------------------------|
| `_arg_guard` | `self` | Abstract method where all assertions should take pace |
| `_assert_args` | `self`, `args` | Stores the provided arguments and calls `_argGuard` |
| `_get_args` | `self`, `*requested_args` | Retrieves all requested arguments |
| `_get_required_args` | `self`, `*requested_args` | Retrieves all requested arguments and fails if they are missing |## Example
```python
# Without ArgGuard
class A:
def __init__(self, a, b, c=0):
assert (
'a' in self.__args
), 'Required argument "a" is missing'assert (
'b' in self.__args
), 'Required argument "b" is missing'assert (
a % b == 0
), 'a ({}) must be divisible by b ({})'.format(a, b)self.state = a // b + c
# With ArgGuard
class A(ArgGuard):
def __init__(self, a, b, c=0):
self._assert_args(locals())
self.state = a // b + cdef _arg_guard(self):
a, b = self._get_required_args('a', 'b')assert (
a % b == 0
), 'a ({}) must be divisible by b ({})'.format(a, b)
```