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.7.tar.gz (44.6 kB view details)

Uploaded Source

Built Distributions

Advanced_Descriptors-2.2.7-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

Advanced_Descriptors-2.2.7-cp35-cp35m-win_amd64.whl (115.3 kB view details)

Uploaded CPython 3.5m Windows x86-64

Advanced_Descriptors-2.2.7-cp35-cp35m-win32.whl (100.7 kB view details)

Uploaded CPython 3.5m Windows x86

File details

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

File metadata

  • Download URL: Advanced-Descriptors-2.2.7.tar.gz
  • Upload date:
  • Size: 44.6 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 PyPy/5.10.1

File hashes

Hashes for Advanced-Descriptors-2.2.7.tar.gz
Algorithm Hash digest
SHA256 795cf575413249b7708c1856bc0498f35783fb5af5149c20116c1010fb1c9a6c
MD5 0fa49d14840ffb985d1dbae3191a1d37
BLAKE2b-256 757bacbc1474f70d4f68d7310ceb0143319a5e5e4208c98201435ab93fbadc2c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Advanced_Descriptors-2.2.7-py3-none-any.whl
  • Upload date:
  • Size: 11.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.4.4

File hashes

Hashes for Advanced_Descriptors-2.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 5235f8265c627ec57234b24c7451fa295fc7369c044a0021655c8e2287785037
MD5 9b7d65a7c25db51a6750da97f88d2552
BLAKE2b-256 9c597daef91c68f42a276c5b8428321dcfe8c910fc7ac6178a1ba54f3ac2e8a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Advanced_Descriptors-2.2.7-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 115.3 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for Advanced_Descriptors-2.2.7-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 bb9dff0ac333850e04921f06b0ec17d625a14d37faf6cf69a813a97d5095361a
MD5 29c9aeba4e5ba6a4d63e6a1209446b36
BLAKE2b-256 3383d52a92a66ec39fe2e4eca89986f48e533ef656d0432952cc6759561d1720

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Advanced_Descriptors-2.2.7-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 100.7 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for Advanced_Descriptors-2.2.7-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 06ef8d10446b34d92e54e7133886778ec6c3b8d9de0a0416a57cb588d1a3e67e
MD5 ec5d044a11c883ff3b52ccfb578ff4c4
BLAKE2b-256 5c313dda1dd33952c944149c1e92342ecebaa790df9899bc9f8ae85cf2fe4e8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for Advanced_Descriptors-2.2.7-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 30b15d9512402ba6e9e3167d330800cc16f252d830c77f5a4edfaf3254b2bbe2
MD5 09ef93206377751d4dc1e6fde36a6d8a
BLAKE2b-256 12af58bc63b5568af5a51fb4eedfc1d521e9e02e342aca884a6fa1fd4fa53aec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Advanced_Descriptors-2.2.7-cp35-cp35m-manylinux1_i686.whl
  • Upload date:
  • Size: 417.9 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for Advanced_Descriptors-2.2.7-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b1ecd227a048c614fca6a423ab5d3751d2b369afca072974c88646c13d807987
MD5 a8376cbbd79ca31e36786d74cb6f544d
BLAKE2b-256 badbf354eb88d982daef2916a19abb62dbd11e5a3a0ba12e3b12e13a7c94172f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for Advanced_Descriptors-2.2.7-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ecfa7fd46a48f7effa43d12b6f5543ac724019ada0055ea2e7414d3e69606fa4
MD5 6a604e0be80035ed2057d0e854ad71ad
BLAKE2b-256 ae648309d2fee319c17a79a30054199d5aa17c5a0aa4544be552e03f7d134beb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Advanced_Descriptors-2.2.7-cp34-cp34m-manylinux1_i686.whl
  • Upload date:
  • Size: 416.1 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 PyPy/5.10.1

File hashes

Hashes for Advanced_Descriptors-2.2.7-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 682d421eb82d5240dae90ecbabe7d3c53db70966610dc127a6de87000621e2df
MD5 c12e28c9b143d60af304530323ceeac3
BLAKE2b-256 bfee45457bebd9112aec3c645d06bc13c0dc4e0b299495ce6ef8981217551a3b

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