Skip to main content

A collection of property variants

Project description

more_properties

A collection of property variants.

Basic Usage

Variants behave mostly as the built-in property, except where noted.

Given the following class,

from more_properties import property, class_property, static_property


class Parrot:
    @property
    def name(self):
        return "Fred"

    @class_property
    def order(cls):
        return Psittaciformes

    @static_property
    def planet():
        return Earth

the properties may be accessed like so:

>>> Parrot().name
'Fred'
>>> Parrot.order
<class 'Psittaciformes'>
>>> Parrot.planet
<class 'Earth'>

Setters/Deleters

Setters and deleters are defined in the same way as the built-in property. Either with the decorator method

from more_properties import class_property


class Foo:
    name = "Foo"

    @class_property
    def identifier(cls):
        """Object identifier"""
        return cls.name.lower()

    @identifier.setter
    def identifier(cls, value):
        cls.name = value.title()

    @identifier.deleter
    def identifier(cls):
        cls.name = None

or the inline method

from more_properties import class_property


class Foo:
    name = "Foo"

    @classmethod
    def get_identifier(cls):
        return cls.name.lower()

    @classmethod
    def set_identifier(cls, value):
        cls.name = value.title()

    @classmethod
    def del_identifier(cls):
        cls.name = None

    identifier = class_property(
        get_identifier,
        set_identifier,
        del_identifier,
        "Object identifier"
    )

Reference

property

A modified version of the built-in property.

Always behaves as a data descriptor, regardless of which (if any) of getter, setter, and deleter are set.

Behaviour when accessed on a class, is undefined.

class_property

A property for classes. Both cls.x and instance.x call the getter with the class. Setting instance.x calls the setter with the class and value. Deleting instance.x call the deleter with the class only.

from more_properties import class_property


class Foo:
    @class_property
    def identifier(cls):
        """Class identifier"""
        return cls.__name__.lower()


class Bar(Foo):
    pass
>>> Foo.identifier
'foo'
>>> Foo().identifier
'foo'
>>> Bar.identifier
'bar'
>>> Bar().identifier
'bar'

classproperty provided as a synonym, for consistency with classmethod.

static_property

A property independent of its accessor. Both cls.x and instance.x call the getter with no parameters. Setting instance.x calls the setter with the value only. Deleting instance.x call the deleter with no parameters.

from more_properties import static_property


x = "bar"

class Foo:
    @static_property
    def val():
        return x
>>> Foo.val
'bar'
>>> Foo().val
'bar'

staticproperty provided as a synonym, for consistency with staticmethod.

cached_property

cached_class_property

cached_static_property

Variants of property, class_property, and static_property, respectively.

They are each used in the same way as the originals, but cache the value of the getters.

from dataclasses import dataclass

from more_properties import cached_property


@dataclass
class Foo:
    x: int

    @cached_property
    def y(self):
        print("Doing work")
        return self.x + 1
>>> bar = Foo(1)
>>> bar.y
Doing work
2
>>> bar.y
2

If the setters/deleters are defined, then the cache is cleared before they are called.

Further, the cache may be explicitly cleared through the clear_cache method, exposed only during class creation.

@dataclass
class Foo:
    x: int

    @cached_property
    def y(self):
        print("Doing work")
        return self.x + 1

    y_clear_cache = y.clear_cache
>>> bar = Foo(1)
>>> bar.y
Doing work
2
>>> bar.y
2
>>> bar.y_clear_cache()
>>> bar.y
Doing work
2

Installation

Install and update using the standard Python package manager pip:

pip install more_properties

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

more_properties-1.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

more_properties-1.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file more_properties-1.1.0.tar.gz.

File metadata

  • Download URL: more_properties-1.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for more_properties-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a64852f32311e95750e2c2cd46355d2aca65327b093abb558767492cc67a6b57
MD5 22f408b9f6e5c80a2068366957022fde
BLAKE2b-256 5f05931d0ec0dee24459256745d9337b979920c36e1c8eba686a194149cde33c

See more details on using hashes here.

File details

Details for the file more_properties-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: more_properties-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for more_properties-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71fa5e339783d64f7ae422c44f536b52290f92c96fcc8f5623ba3dd3ddde29de
MD5 e81e85342b484cbfc083ad50efcf614d
BLAKE2b-256 de2b97035358ce2632b507862a606afa4c78128021147a004e3136efc1a57832

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