Skip to main content

Advanced descriptors for special cases.

Project description

Advanced descriptors

https://travis-ci.org/python-useful-helpers/advanced-descriptors.svg?branch=master Azure DevOps builds https://coveralls.io/repos/github/python-useful-helpers/advanced-descriptors/badge.svg?branch=master Documentation Status https://img.shields.io/pypi/v/advanced-descriptors.svg https://img.shields.io/pypi/pyversions/advanced-descriptors.svg https://img.shields.io/pypi/status/advanced-descriptors.svg https://img.shields.io/github/license/python-useful-helpers/advanced-descriptors.svg https://img.shields.io/badge/code%20style-black-000000.svg

This package includes helpers for special cases:

  • SeparateClassMethod - allow to have classmethod and normal method both with the same name.

  • AdvancedProperty - property with possibility to set class wide getter.

  • LogOnAccess - property with logging on successful get/set/delete or failure.

SeparateClassMethod

This descriptor can be set using standard decorator syntax. Create instance with arguments:

def imeth(instance):
    return instance.value

def cmeth(owner):
    return owner.value

class Target(object):
    value = 1

    def __init__(self):
        self.value = 2
    getval = advanced_descriptors.SeparateClassMethod(
        imeth, cmeth
    )

Create instance wrapping as decorator:

class Target(object):
    value = 1

    def __init__(self):
        self.value = 2

    @advanced_descriptors.SeparateClassMethod
    def getval(self):
        return self.value

    @getval.class_method
    def getval(cls):
        return cls.value

Cases with method only and classmethod only is useless: method as-is and @classmethod should be used in corresponding cases.

AdvancedProperty

This descriptor should be used in cases, when in addition to normal property API, class getter is required. If class-wide setter and deleter also required - you should use standard propery in metaclass.

Usage examples:

  1. In addition to normal property API:

class Target(object):
    _value = 777

    def __init__(self):
        self._value = 42

    @advanced_descriptors.AdvancedProperty
    def val(self):
        return self._value

    @val.setter
    def val(self, value):
        self._value = value

    @val.deleter
    def val(self):
        self._value = 0

    @val.cgetter
    def val(cls):
        return cls._value
  1. Use class-wide getter for instance too:

class Target(object):
    _value = 1

    val = advanced_descriptors.AdvancedProperty()

    @val.cgetter
        def val(cls):
            return cls._value

LogOnAccess

This special case of property is useful in cases, where a lot of properties should be logged by similar way without writing a lot of code.

Basic API is conform with property, but in addition it is possible to customize logger, log levels and log conditions.

Usage examples:

  1. Simple usage. All by default, logger is re-used from instance if available with names logger or log else used internal advanced_descriptors.log_on_access logger:

import logging

class Target(object):

    def init(self, val='ok')
        self.val = val
        self.logger = logging.get_logger(self.__class__.__name__)  # Single for class, follow subclassing

    def __repr__(self):
        return "{cls}(val={self.val})".format(cls=self.__class__.__name__, self=self)

    @advanced_descriptors.LogOnAccess
    def ok(self):
        return self.val

    @ok.setter
    def ok(self, val):
        self.val = val

    @ok.deleter
    def ok(self):
        self.val = ""
  1. Use with global logger for class:

class Target(object):

  def init(self, val='ok')
      self.val = val

  def __repr__(self):
      return "{cls}(val={self.val})".format(cls=self.__class__.__name__, self=self)

  @advanced_descriptors.LogOnAccess
  def ok(self):
      return self.val

  @ok.setter
  def ok(self, val):
      self.val = val

  @ok.deleter
  def ok(self):
      self.val = ""

  ok.logger = 'test_logger'
  ok.log_level = logging.INFO
  ok.exc_level = logging.ERROR
  ok.log_object_repr = True  # As by default
  ok.log_success = True  # As by default
  ok.log_failure = True  # As by default
  ok.log_traceback = True  # As by default
  ok.override_name = None  # As by default: use original name

Testing

The main test mechanism for the package advanced-descriptors is using tox. Available environments can be collected via tox -l

CI systems

For code checking several CI systems is used in parallel:

  1. Travis CI: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it’s publishes coverage on coveralls.

  2. coveralls: is used for coverage display.

  3. Azure CI: is used for functional tests on Windows.

CD system

Travis CI: is used for package delivery on PyPI.

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

Advanced-Descriptors-2.2.6.tar.gz (37.7 kB view details)

Uploaded Source

Built Distributions

Advanced_Descriptors-2.2.6-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file Advanced-Descriptors-2.2.6.tar.gz.

File metadata

  • Download URL: Advanced-Descriptors-2.2.6.tar.gz
  • Upload date:
  • Size: 37.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for Advanced-Descriptors-2.2.6.tar.gz
Algorithm Hash digest
SHA256 188f525080811f0bf8f194d89686ce45c24123448521746733dd1adbe9c629e1
MD5 61bae5d611fb3e685ad4362309feb76c
BLAKE2b-256 86488fcae414f04c03097a1dfe98b62edded143635f0dafbc927a62f12a5c71c

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.6-py3-none-any.whl.

File metadata

  • Download URL: Advanced_Descriptors-2.2.6-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for Advanced_Descriptors-2.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 201cb1dd048b816aaef635efd3a3bc53b1704c37df5096f141117567ad00565b
MD5 9589e691099338fab337a8e8046208d5
BLAKE2b-256 4873ee2b4167105514fa308a5561ab1c5b7aa7f6324a9df9ed36fae526bc06da

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.6-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for Advanced_Descriptors-2.2.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 200357ea5f84e8f23b243aa868acf74e84e7f1093590eb21da590d49d51fe4c7
MD5 a0b09de70453aa3737c1843534a5d14e
BLAKE2b-256 529a4c41c0901570283eec4380101ed57cacf8ee7e099b2335eb76d38e7fac43

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.6-cp37-cp37m-manylinux1_i686.whl.

File metadata

  • Download URL: Advanced_Descriptors-2.2.6-cp37-cp37m-manylinux1_i686.whl
  • Upload date:
  • Size: 435.1 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for Advanced_Descriptors-2.2.6-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 1f8b96ac08dee205f10ad47e7d2fc01595db287e88ce823965a509fc10c2efc8
MD5 588131bbb55b8d11ce69f644bbb0260f
BLAKE2b-256 da735b254c30fd4cf125755816df7be252420597d9610d2edafa8aac1424959d

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.6-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for Advanced_Descriptors-2.2.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 baba4f8b1e7106dca61f9bff916a540a340b82f925faa59a1132764916f33310
MD5 d737a0bc9859406672d9827224e2c4c2
BLAKE2b-256 f0e7a468db2efa05f1a79521ed4215ba34af1f11cb8096504994096e9013e3f0

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.6-cp36-cp36m-manylinux1_i686.whl.

File metadata

  • Download URL: Advanced_Descriptors-2.2.6-cp36-cp36m-manylinux1_i686.whl
  • Upload date:
  • Size: 434.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for Advanced_Descriptors-2.2.6-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 9494b4df6c0588ae34c461a669111bf3d0d0dc295bc0aaeb8733690455e72958
MD5 121bae61c214587f6c7ce606e5bbf26e
BLAKE2b-256 6964ecc66909e6339c9e22774363f2eb2100c6fbaef43ebecf596dc8a0f74dc2

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