Skip to main content

Package for aiding writing classes with lots of similar simple properties without the boilerplate

Project description

Package for aiding writing classes with lots of similar simple properties without the boilerplate.

Status

Latest Release

PyPI

Travis CI

https://travis-ci.com/brocksam/pyproprop.svg?branch=master

Docs

Documentation Status

Appveyor

https://ci.appveyor.com/api/projects/status/github/brocksam/pyproprop?svg=true

PyPI

PyPI - Downloads

Coverage

Codecov

Anaconda

Conda

License

https://img.shields.io/badge/license-MIT-brightgreen.svg

What is pyproprop?

Do you often find yourself writing classes with properties such as:

from some_other_module import DefaultObject, some_type

class ExampleClass:

    def __init__(self,
                 type_checked_value,
                 bounded_numeric_value,
                 specific_length_sequence_value,
                 obj_with_method_applied_value,
                 ):
        self.type_check_attr = type_checked_value
        self.bounded_numeric_attr = bounded_numeric_value
        self.specific_length_sequence_attr = specific_length_sequence_value
        self.obj_with_method_applied_attr = obj_with_method_applied_value
        self.instantiate_default_if_none_attr = None

    @property
    def type_checked_attr(self):
        return self._type_checked_attr

    @type_checked_attr.setter
    def type_checked_attr(self, val):
        if not isinstance(val, some_type):
            msg = "`type_checked_attr` must be of `some_type`"
            raise TypeError(msg)
        self._type_checked_attr = val

    @property
    def bounded_numeric_attr(self):
        return self._bounded_numeric_attr

    @bounded_numeric_attr.setter
    def bounded_numeric_attr(self, val):
        val = float(val)
        lower_bound = -1.0
        upper_bound = 2.5
        if val < lower_bound:
            msg = f"`bounded_numeric_attr` must be greater than {lower_bound}"
            raise ValueError(msg)
        if val >= upper_bound:
            msg = (f"`bounded_numeric_attr` must be less than or equal to "
                   f"{upper_bound}.")
            raise ValueError(msg)
        self._type_checked_attr = val

    @property
    def specific_length_sequence_attr(self):
        return self._specific_length_sequence_attr

    @specific_length_sequence_attr.setter
    def specific_length_sequence_attr(self, val):
        if len(val) != 2:
            msg = "`specific_length_sequence` must be an iterable of length 2."
            raise ValueError(msg)
        self._specific_length_sequence_attr = val

    @property
    def obj_with_method_applied_value(self):
        return self._obj_with_method_applied_value

    @obj_with_method_applied_value.setter
    def obj_with_method_applied_value(self, val):
        val = str(val)
        self._obj_with_method_applied_value = val.title()

    @property
    def instantiate_default_if_none_attr(self):
        return self._instantiate_default_if_none_attr

    @instantiate_default_if_none_attr.setter
    def instantiate_default_if_none_attr(self, val):
        if val is None:
            val = DefaultObject()
        self._instantiate_default_if_none_attr = val

With pyproprop all of this boilerplate can be removed and instead the exact same class can be rewritten as:

from pyproprop import processed_property
from some_other_module import DefaultObject, some_type

class ExampleClass:

    type_checked_attr = processed_property(
        "type_checked_attr",
        description="property with enforced type of `some_type`",
        type=some_type,
    )
    bounded_numeric_attr = processed_property(
        "bounded_numeric_attr",
        description="numerical attribute with upper and lower bounds"
        type=float,
        cast=True,
        min=-1.0,
        max=2.5,
    )
    specific_length_sequence_attr = processed_property(
        "specific_length_sequence_attr",
        description="sequence of length exactly 2",
        len=2,
    )
    obj_with_method_applied_attr = processed_property(
        "obj_with_method_applied_attr",
        description="sting formatted to use title case"
        type=str,
        cast=True,
        method="title",
    )
    instantiate_default_if_none_attr = processed_property(
        "instantiate_default_if_none_attr",
        default=DefaultObject,
    )

    def __init__(self,
                 type_checked_value,
                 bounded_numeric_value,
                 specific_length_sequence_value,
                 obj_with_method_applied_value,
                 ):
        self.type_check_attr = type_checked_value
        self.bounded_numeric_attr = bounded_numeric_value
        self.specific_length_sequence_attr = specific_length_sequence_value
        self.obj_with_method_applied_attr = obj_with_method_applied_value
        self.instantiate_default_if_none_attr = None

Installation

The easiest way to install pyproprop is using the Anaconda Python distribution and its included Conda package management system. To install pyproprop and its required dependencies, enter the following command at a command prompt:

conda install pyproprop

To install using pip, enter the following command at a command prompt:

pip install pyproprop

For more information, refer to the installation documentation.

Contribute

License

This project is licensed under the terms of the MIT 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

pyproprop-0.4.2.tar.gz (36.4 kB view details)

Uploaded Source

Built Distribution

pyproprop-0.4.2-py3-none-any.whl (35.5 kB view details)

Uploaded Python 3

File details

Details for the file pyproprop-0.4.2.tar.gz.

File metadata

  • Download URL: pyproprop-0.4.2.tar.gz
  • Upload date:
  • Size: 36.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for pyproprop-0.4.2.tar.gz
Algorithm Hash digest
SHA256 11fb608e1248b9517330efaa22f0fa842158cee494a6f996a454b98aa20bf312
MD5 eeb5eda8e70b12a3bb8cf4788193bb98
BLAKE2b-256 863db6518ff1e9e4edd9fa985ff38809b8c3936f67d9d65efc28c0d9496d68fa

See more details on using hashes here.

File details

Details for the file pyproprop-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: pyproprop-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 35.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for pyproprop-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ad6b1bbbc360c6dacdb035d50d36d0836ddd77f83e15833468dd5dcb2ee4da62
MD5 6b774bde6b018eb3828923736de9ca4a
BLAKE2b-256 4adbfdaa1ea993d0dbc255b5c297d37e16df453f414f90976a264ab6667ada0c

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