Tools for reducing repetition in Python code
Project description
DRYtools
version number: 0.1.0
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 special attributes 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
Contributing
Issue Tracker: https://github.com/dan-elias/drytools/issues
Source Code: https://github.com/dan-elias/drytools.git
Documentation
Continuous integration
License
The project is licensed under the GPL-3 license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file drytools-0.1.0.tar.gz
.
File metadata
- Download URL: drytools-0.1.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 066e173eb35bc8cb28120f91f0ac18fe130473fe6b5d4631305db1c5e056ff6a |
|
MD5 | 011f33a6a04a308efe6cb1f4fb30cb46 |
|
BLAKE2b-256 | 784aaa936b1fdd62c63612bb70156fa5a3a60d5471e43a4d2e5dce25f04feccb |
File details
Details for the file drytools-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: drytools-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28d31714f4e7c473d23cc11f9aa40152b81f381c8e9cbfde436f89a36ce2c918 |
|
MD5 | ce34c07d8529acfb8c9d438f14bf0e59 |
|
BLAKE2b-256 | 8fd465e98444dc0e72483223824f4de11e4689a77594dc20c0ee32af0070fdaa |