Skip to main content

Advanced descriptors for special cases.

Project description

Advanced descriptors

https://travis-ci.com/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.8.tar.gz (26.1 kB view details)

Uploaded Source

Built Distributions

Advanced_Descriptors-2.2.8-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

Advanced_Descriptors-2.2.8-cp35-cp35m-win_amd64.whl (115.6 kB view details)

Uploaded CPython 3.5m Windows x86-64

Advanced_Descriptors-2.2.8-cp35-cp35m-win32.whl (101.1 kB view details)

Uploaded CPython 3.5m Windows x86

File details

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

File metadata

  • Download URL: Advanced-Descriptors-2.2.8.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for Advanced-Descriptors-2.2.8.tar.gz
Algorithm Hash digest
SHA256 176c790c4f88dae8475706972d957b674a665409c16db6560c85b8153c0387c5
MD5 ccb3af5972ed11663c55cbef1b9e90be
BLAKE2b-256 6e98be453984042d66d6bcc1d1371c7dd7b2f02173b9a0f0f6eef2f524fcc314

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Advanced_Descriptors-2.2.8-py3-none-any.whl
  • Upload date:
  • Size: 11.8 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/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.4.4

File hashes

Hashes for Advanced_Descriptors-2.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 88b10ac0eded5928525400b8a204dcc6d76296289ae127a5f12d30ed431e9925
MD5 13a3291406e5d89e326f64eafc73b9ab
BLAKE2b-256 9933dc3fa55e7b9a6a6fc92a0354b117772093f32d16059b7258e56416cc50f2

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.8-cp35-cp35m-win_amd64.whl.

File metadata

  • Download URL: Advanced_Descriptors-2.2.8-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 115.6 kB
  • Tags: CPython 3.5m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for Advanced_Descriptors-2.2.8-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 be694f7f4b72684623b7354ea0313f48de33905679874974905add804ddef4ae
MD5 10b9d25dcf581afabd3e91b8fbbccfb0
BLAKE2b-256 5bcad668bbf8777223359db6873ddc082222c0ba893881c7e27d25a8a3d2523b

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.8-cp35-cp35m-win32.whl.

File metadata

  • Download URL: Advanced_Descriptors-2.2.8-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 101.1 kB
  • Tags: CPython 3.5m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for Advanced_Descriptors-2.2.8-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 12976a15804e09612026a8b78d14edac6316ce8458c3f8a8d86da91da8b544d3
MD5 145fc9e9703fbdd3ee9777b04c4c9022
BLAKE2b-256 b9d831361aa90a5d3c792afa8801ef840889de625aabcf632040c81c0b405f94

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.8-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for Advanced_Descriptors-2.2.8-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ac373c3f3c55086aaf2ba02016f1b11540e3bc2abb768d7b7c011c4049823663
MD5 ac0081c83174314fabf6b578723d7ba9
BLAKE2b-256 e15906c97069856105e916955b5861afc750bdd262d0b076cade8063a8789659

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.8-cp35-cp35m-manylinux1_i686.whl.

File metadata

  • Download URL: Advanced_Descriptors-2.2.8-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 419.0 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for Advanced_Descriptors-2.2.8-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 5af6205d3ab180af35dd6794cec9a1ce32ea08f2c25e18c8832e35f2eb42b665
MD5 028b29e9e70c740c9a6d2b2cd7943ac8
BLAKE2b-256 82711ed6d6a187a1c69bda587298cd305617e54ab98c7cd1f644f4ce54eaa7f5

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.8-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for Advanced_Descriptors-2.2.8-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a29163012ee0b8d79000272cf872fd34dc38ce712231e62a0816a464c6cab3d1
MD5 059922011bde663e79807121d1d28fea
BLAKE2b-256 03e3bd1f4dcbc263751cad25b07c62a4ffe535827d7609f46ea217a55d8cf702

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-2.2.8-cp34-cp34m-manylinux1_i686.whl.

File metadata

  • Download URL: Advanced_Descriptors-2.2.8-cp34-cp34m-manylinux1_i686.whl
  • Upload date:
  • Size: 416.8 kB
  • Tags: CPython 3.4m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for Advanced_Descriptors-2.2.8-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 98d41c0ee7e00725efdb84be78a40a1133fbbda75a7bbc2f44a35aa7c69263c3
MD5 6bdd3af85012f1540a7a8516f6a5ec3f
BLAKE2b-256 b066c6b80a50a62995df8fc20e0558154c61d45e1965d70a80ff8061be891f35

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