Skip to main content

Tools for reducing repetition in Python code

Project description

Documentation status unknown Build status unknown

DRYtools

version number: 0.1.2

author: Dan Elias

This library provides tools to make Python code shorter, less repetitive and more readable.

Features

  • function annotations to coerce and/or validate parameters, and

  • decorators and mixins to add groups of Python standard methods to classes

Example

This example provides implementation with and without drytools for a class representing a person’s name and age. The required behaviour is:

  • The constructor should accept a name and an age

    • age should be coerced to an integer

    • if name is not of type str, raise a TypeError

    • if age is negative or exceeds 200, raise a ValueError

  • The full set of comparison methods (ie: __lt__, __eq__ etc) should be implemented reflecting an ordering by age (with alphabetical ordering by name for people with the same age)

Without drytools

from functools import total_ordering

@total_ordering
class person:
    def __init__(self, name, age):
        if not isinstance(name, str):
            raise TypeError(name)
        age = int(age)
        if (age < 0) or (age > 200):
            raise ValueError(age)
        self.name = name
        self.age = age
    def _comp(self):
        return (self.age, self.name)
    def __eq__(self, other):
        return self._comp() == other._comp()
    def __gt__(self, other):
        return self._comp() > other._comp()

With drytools

from operator import ge, le
from drytools import args2attrs, check, compose_annotations, ordered_by

@ordered_by('age', 'name')
class person:
    @compose_annotations
    @args2attrs
    def __init__(self, name: check(isinstance, str, raises=TypeError),
                       age:(int, check(ge, 0), check(le, 200))):
        pass

Getting started

$ pip install drytools

Documentation

Documentation status unknown

https://drytools.readthedocs.io/en/latest/

Contributing

Continuous integration

Build status unknown

https://travis-ci.org/dan-elias/drytools

License

The project is licensed under the GPL-3 license.

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

drytools-0.1.2.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

drytools-0.1.2-py2.py3-none-any.whl (9.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file drytools-0.1.2.tar.gz.

File metadata

  • Download URL: drytools-0.1.2.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for drytools-0.1.2.tar.gz
Algorithm Hash digest
SHA256 589a942d3c1bddaddfd627367e1fae6c0de97d51b5f51a01daf1e8ed133c72fa
MD5 8a10dc01396470c161624ef9ce5798b2
BLAKE2b-256 3177ee13c9e408ec785f5c47ca00ef3711c4f30d47a5611c6c0c82f1719c2249

See more details on using hashes here.

File details

Details for the file drytools-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for drytools-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2d8dbc6da77bbbcb3d8a34fb0bd99e84b66c3afb66191f8799572e3a093bfcf5
MD5 d8086cae83edeb399f776d363f99bbe3
BLAKE2b-256 131e86d3a2a8f1763444db7a349452bc36cec9ebb5912e9d7ad42de9f93aa688

See more details on using hashes here.

Supported by

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