Skip to main content

Utility class to assert class arguments

Project description

ArgGuard Tests Actions Status

ArgGuard is a simple abstract utility class to remove some boilerplate code when asserting class arguments.

Usage

The ArgGuard class provides the protected 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

# 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):
        # If a required argument is missing, an error is thrown
        a, b = self._get_required_args('a', 'b')

        assert (
            a % b == 0
        ), 'a ({}) must be divisible by b ({})'.format(a, b)

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

argguard-0.0.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argguard-0.0.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file argguard-0.0.1.tar.gz.

File metadata

  • Download URL: argguard-0.0.1.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for argguard-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9056ed8a0af3284280706c73244577b3b5d15561c6d7d3ce1dfb35b029f42eee
MD5 8b5ec2c3b0f7b59e4a6f31d01942950a
BLAKE2b-256 1112bcdc9da5e4513b918c1c00f9e7b8889c555cfd9ce4d22e2be3984efcb4a0

See more details on using hashes here.

File details

Details for the file argguard-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: argguard-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.10

File hashes

Hashes for argguard-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2b11b6cba136e920e8f3a7cee8ef03d32495ba2b87fb9b40041408077e16c228
MD5 892a2b6c31873ee2388fdbe07617cedf
BLAKE2b-256 6d0cbee68ab76e625602dca44b5b488157468c0bc6eb1de0ace9c5aeb22d719d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page