Skip to main content

`@cls` - Class Made Aware to Decorator

Project description

@cls - Class Made Aware to Decorator

PyPI - License PyPI PyPI - Status PyPI - Python Version

GitHub CircleCI Build Status codecov

The purpose of this library is to provide possibility to create decorators, especially in super classes, with current class cls as argument. Everything are centered with our new cls module, keyword, magic, or what every you think it would be.

Getting Started

Installation

Install by pip from PyPI:

pip install acls

Example

A typical snippet looks like:

from functools import wraps
import cls


class Base(metaclass=cls.ClsMeta):
  @cls
  def decor(cls, decor_arg):
    def wrap(func):
      @wraps(func)
      def wrapper(self):
        # do something with `func`
        retval = func(self)
        # do something with `retval`
        return retval
      return wrapper
    return wrap

class Extended(Base):
  @cls.decor('some arg')
  def func(self):
    # do something
    pass

The magic is that you can use @cls.decor in Extended class, which is inheritance from Base. What is more, with in the decorator Base.decor, argument cls will be assigned with the correct current class. In this example, it would simply be a reference to Extended. This would be helpful if you want to make use of some class property here in the decorator.

Magic

Well, there is no magic. I created a delegator in class namespace to enable both class possible to use @cls. So, it is not the module cls as we imported on the top. I use this to make it more consistent looking, and fool some interpreters like pylint. No offense, just want to make them less noisy.

Limitations

Unfortunately, This work is based on customizing python class creation. I have to make use of __prepare__, which is introduced only to python 3. That means there is no known possible backward compatibility with python 2 now. The code is tested against python 3.5+.

Please let me know if you come up with an idea how to manipulate class creation in python 2.

There are a couple of issues, with which I am talking. Contributions are welcome.

Known issue

  • relying on length of arguments and callable() to support optional arguments in decorator
  • not compatible with @classmethod, or many other decorators
  • make pylint really noisy
  • no documents :see_no_evil:!

License

MIT License, Copyright (c) 2019 guoquan

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

acls-1.0.2.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file acls-1.0.2.tar.gz.

File metadata

  • Download URL: acls-1.0.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for acls-1.0.2.tar.gz
Algorithm Hash digest
SHA256 3d448164b6275e7424407af647b283570871e68808fbe61a1cc8baeee3ac3951
MD5 90bdd0d5327a2e314154e04aba4f8ddb
BLAKE2b-256 0b031ab1f5cdffac551b7351444cccdac01d0b0f3ddf85b84a6a7d4aa858aa65

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