A Python package for automating the creation of common class properties
Project description
proper_tea
A Python package for automating the creation of common class properties.
Usage
Properties are a useful feature of modern Python, allowing users to cleanly add 'getter' and 'setter' functions in an unobtrusive manner. For example, a common use case is to ensure a class attribute cannot be set to a negative number:
class Item:
def __init__(self,weight):
self.weight = weight # weight in kg
@property
def weight(self):
# A simple getter
return self._weight
@weight.setter
def weight(self, value):
# Cannot set negative weight!
if value < 0:
raise ValueError("Weight cannot be negative")
self._weight = value
item = Item(2.4)
# The 'getter' function is called when accessing the 'weight' attribute
assert item.weight == 2.4
# The 'setter' function is called when assigning to the 'weight' attribute
item.weight = 4.6
assert item.weight == 4.6
# The following raises an exception
# item.weight = -1.0
A downside of properties is that they require a significant amount of boilerplate code. Each property will usually require the implementation of two functions, and often this code is repeated. For example, if the Item class above were also given a 'price' attribute, it too cannot be negative, so the user must implement near-identical getter and setter functions.
proper_tea
makes use of 'property factories', inspired by the book Fluent Python (Luciano Ramalho, O'Reilly, 2015), to automate the creation of common properties. Using proper_tea
, the above class may instead be written:
import proper_tea as pt
class Item:
weight = pt.positive() # weight in kg
def __init__(self,weight):
self.weight = weight
Extending it to include other variables couldn't be easier:
import proper_tea as pt
class Item:
weight = pt.positive() # weight in kg
price = pt.positive_int() # price in pennies
temperature = pt.float_greater_than(-273.15) # temperature in Celsius
rating = pt.int_in_range((1,10)) # 1-10 star rating
format = pt.in_set({ "jpeg", "png"}) # output format, must match strings exactly
def __init__(self, weight, price, temperature, rating, format):
self.weight = weight
self.price = price
self.temperature = temperature
self.rating = rating
self.format = format
Installation
The latest version of proper_tea
may be installed using pip:
python3 -m pip install proper_tea
To install from the GitHub repo, first clone it, and then install via:
python3 -m pip install .
To run the tests:
python3 -m pip install .[tests]
pytest tests
Additional Features
NumPy support
proper_tea
provides property factories for NumPy arrays:
import numpy as np
import proper_tea as pt
import proper_tea.numpy
class MyClass:
x = pt.numpy.numpy_array()
def __init__(self, x):
self.x = x
# Automatically convert iterables to NumPy array
my_class = MyClass([1,2,3])
assert isinstance( my_class.x, np.ndarray)
# Assigning to my_class.x will also automatically convert
my_class.x = [[4,5,6],[7,8,9]]
assert isinstance( my_class.x, np.ndarray)
assert my_class.x.ndim == 2
# Existing NumPy arrays are not copied
y = np.linspace( 0.0, 100.0, 1001)
my_class.x = y
assert x is y
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 proper_tea-0.3.4.tar.gz
.
File metadata
- Download URL: proper_tea-0.3.4.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4637497169e95c6ff70871bba3096842e17ce4688ab81dca478bb480e5f4c2df |
|
MD5 | a8d84997bab01873036835731c404848 |
|
BLAKE2b-256 | 05eef7b0dd16cc1a8c6327c188d5b8ab126b094403b43788fbd709bef7c3ef89 |
File details
Details for the file proper_tea-0.3.4-py3-none-any.whl
.
File metadata
- Download URL: proper_tea-0.3.4-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abcad8e7ed43fcbb98334e659859ba6d87db7415100d113fe761e2bbcd224a4c |
|
MD5 | 97e8a49ba07bb47cb7e21e4570a5fb76 |
|
BLAKE2b-256 | bee2b5c40e5499c118235819f2000a4aacd1f7279d847c745587c7ccc2f4934e |