Skip to main content

Class based extension/plugin library

Project description

https://travis-ci.org/katyukha/extend-me.svg?branch=master https://coveralls.io/repos/katyukha/extend-me/badge.png

Extend Me - Class based extension/plugin library

This module provides mechanism of extension of your application based on ‘extension via inheritance’. Under this words I mean ability to define new extensions of application objects simply by subclassing of extensible classes of app.

For example we have app with class ‘Worker’ which we would like to make extensible (allowing third party modules to extend or change its behavior). Thinking strait, there are a lot of work to be done, to impelement mechanism of loading, registering, end enabling extension, with lot of glue code, which must define some entry points to connect extension and main app. But why not make it simpler, supposing that any subclass of ‘Worker’ will extend it? And this module provides implementation of this in two ways:

  • Explicit (by using metaclass ExtensibleType directly)

    • When using this way You will heve seperatly Base class to be subclassed by extension classes and class getter which will construct class based on all defined extensions using multiple inhertance

  • Implicit (by using Extensible class which use metaclass magic implicitly)

    • Extensible class takes care of all metaclass magic related to generation objects of correct class

How it Works

Metaclass (ExtensibleType) tracks all subclasses of class it is applied to, and provides method to build class based on all subclasses of base class, thus using all functionality of all subclasses. Thus generation of correct class is separate process which should be used everywhere where extensible class is requred.

To simplify this class Extensible was implemented. It has redefined method __new__ which automaticaly creates instances of correct class (class that inherited from base class and all its extensions’)

Examples

ExtensibleType

At the begining we should create a metaclass that will automaticaly gether all information about all extensions, and apply this metaclass to class we would like to enable extensions for:

>>> import six  # Used for Python 2/3 compatability
>>> mc = ExtensibleType._("Object")

>>> @six.add_metaclass(mc)
... class Object(object):
...     pass

Not method _ of ExtensibleType. This method is used to create metaclass for specific object. It receives one argument - string that will be used as name of class generated by this metaclass

Next we may define extension for this class. It is very simple. Just subclass previously defined class:

>>> class ObjectExtension(Object):
...     cool_attribute = 1
...     def method1(self):
...         return "Test"

So… at this momet we have base class and extension. And here is that core magic occures. Metaclass that was created at the begining automaticaly collects all subclasses of base class. So it is posible now to create new class that is subclass of all subclasses of base class using multiple inheritance. And metaclass mc will do it for You:

>>> cls = mc.get_class()

And now You can use cls for Your needs, instead of base class. It can do all that base class can, and all that extensions can:

>>> obj = cls()
>>> obj.method1()
'Test'
>>> obj.cool_attribute
1

Extensible

This class provides one more level of abstraction, allowing to hide all metaclass magic behide the scene. So, using it You don’t need to worry about metaclasses and class creation process. Just inherit extensions form base class, and use in Your program instances of base class. Let’s see it in example:

>>> class MyCoolClass(Extensible):
...     my_attr_1 = 25
...     def my_method1(self, arg1):
...         print('Hello, %s' % arg1)

>>> class MyCoolClassExtension1(MyCoolClass):
...     def my_method1(self, arg1):
...         super(MyCoolClassExtension1, self).my_method1(arg1.upper())
...
...     def my_method2(self, arg1):
...         print("Good by, %s" % arg1)

And now using simply instances of base class You have all abilities that provided by extensions:

>>> my_cool_obj = MyCoolClass()
>>> print(my_cool_obj.my_attr_1)
25
>>> my_cool_obj.my_method1('World')
Hello, WORLD
>>> my_cool_obj.my_method2('World')
Good by, World

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

extend_me-1.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

extend_me-1.1.0-py2.py3-none-any.whl (9.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file extend_me-1.1.0.tar.gz.

File metadata

  • Download URL: extend_me-1.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for extend_me-1.1.0.tar.gz
Algorithm Hash digest
SHA256 37a2bb38b78cc6fe33dce36f9a4449255485e260ecb8ad8e5b7cb2f8d9e8c7e8
MD5 64aad3b42a662fe8621fd0ff4d4e5afd
BLAKE2b-256 cdb9be38246cf18353a56a396f764950cd0b829920f4068f660b576624cd6bf1

See more details on using hashes here.

File details

Details for the file extend_me-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for extend_me-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f8de93ba8d73600347eb91a92b13a0d6ddbe32b368c447ba7d65d078835b062a
MD5 36dd1e3b6886f4fae1363f42e4a92a57
BLAKE2b-256 a50df40afc7c0f175935c6ce923e18ee1aef5316da2b6d0ea6b32e2e19e5da40

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