Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gcascio committed Sep 21, 2021
0 parents commit 24f70f7
Show file tree
Hide file tree
Showing 12 changed files with 454 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Tests
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Python 3
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install pipenv
run: pip install pipenv
- name: Install dependencies
run: pipenv install --dev
- name: Run tests
run: pipenv run pytest
138 changes: 138 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Giovanni Cascio

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]
pytest = "*"
flake8 = "*"

[requires]
python_version = "3.8"
115 changes: 115 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 + c

def _arg_guard(self):
a, b = self._get_required_args('a', 'b')

assert (
a % b == 0
), 'a ({}) must be divisible by b ({})'.format(a, b)
```
3 changes: 3 additions & 0 deletions argguard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .argguard import ArgGuard

__all__ = ['ArgGuard']
Loading

0 comments on commit 24f70f7

Please sign in to comment.