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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

Advanced_Descriptors-3.0.5-cp37-cp37m-win_amd64.whl (126.1 kB view details)

Uploaded CPython 3.7m Windows x86-64

Advanced_Descriptors-3.0.5-cp37-cp37m-win32.whl (111.6 kB view details)

Uploaded CPython 3.7m Windows x86

Advanced_Descriptors-3.0.5-cp36-cp36m-win_amd64.whl (125.9 kB view details)

Uploaded CPython 3.6m Windows x86-64

Advanced_Descriptors-3.0.5-cp36-cp36m-win32.whl (111.5 kB view details)

Uploaded CPython 3.6m Windows x86

File details

Details for the file Advanced_Descriptors-3.0.5-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: Advanced_Descriptors-3.0.5-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 126.1 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for Advanced_Descriptors-3.0.5-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 831d2c10dd6eb57e68ff96180f54b4aed0ad252fc735ebcb958ac31773b0c048
MD5 19237e4371bf90df0da96b45cae09266
BLAKE2b-256 97ccd5cd0bfc114966353277be45bf1a8ec93e183e8af19764c9e594b38d3123

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-3.0.5-cp37-cp37m-win32.whl.

File metadata

  • Download URL: Advanced_Descriptors-3.0.5-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 111.6 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for Advanced_Descriptors-3.0.5-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 5a973548db0cf12e7c8352c9b89fc65221fcfe909772f4860adda61a7a4e3e72
MD5 edc213a4f9fce417ec3434d5089b4946
BLAKE2b-256 177595d025f9cd19253b3c9e3ff9d8aaaeecd6bd14fde1fcd9db1e49481ef375

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-3.0.5-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: Advanced_Descriptors-3.0.5-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 125.9 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for Advanced_Descriptors-3.0.5-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 7d37935562baf376ba00448bd7c591ba24afed5ef7f56860e9676cff7e889a74
MD5 49d9a7bb5866e1c904d3ab5f30b841f1
BLAKE2b-256 66be1917c9f71b96eaa1e317c43210499e3498dc36164b05c49b79da52f5bc92

See more details on using hashes here.

File details

Details for the file Advanced_Descriptors-3.0.5-cp36-cp36m-win32.whl.

File metadata

  • Download URL: Advanced_Descriptors-3.0.5-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 111.5 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for Advanced_Descriptors-3.0.5-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 16c540f0140391f62d848e4d9dd8abe4b808f87a209abd59004cd7a5c4ddd011
MD5 93843394eb7cf80d03460e5e5a7731d3
BLAKE2b-256 2de0beea76d33f414e18c38d158fe55e27ad01d5a0a6d4da2bb3275e03058c49

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