Skip to main content

This package provides you with a module that allows you to easily define classes that are stacked on top of each other and add features to each other, creating a whole new class.

Project description

Add-On Class Package

This package provides you with a module that allows you to easily define classes stacked on top of each other and add features to each other, creating a whole new class. You can see an example of this application below.

from add_on_class import AOC

class Parent:
    def __init__(self):
        self.parrent_property = 1

    def parent_functionality(self):
        return 10

class Child(Parent):
    def __init__(self):
        super().__init__()
        self.child_property = 2
        self.parrent_property = 67

    def child_functionality(self):
        return 20

class FirstAddOn(AOC):
    def __post_init__(self):
        self.first_added_property = 3
    
    def first_added_functionality(self):
        return 30

    def child_functionality(self):
        return self.__core.child_functionality(self)*2

class SecondAddOn(AOC):
    def __pre_init__(self, pre):
        self.pre = pre

    def __post_init__(self, post):
        self.post = post
        self.child_property = 12

    def child_functionality(self):
        return self.__core.child_functionality(self)*3


added = SecondAddOn(FirstAddOn(Child))(pre=4, post=8)
print(added.parrent_property)
# >>> 67
print(added.child_property)
# >>> 12
print(added.first_added_property)
# >>> 3
print(added.parent_functionality())
# >>> 10
print(added.child_functionality())
# >>> 120
print(added.first_added_functionality())
# >>> 30
print(issubclass(type(added), Parent))
# >>> True
print(added.pre)
# >>> 4
print(added.post)
# >>> 8
  • The output of the AOC constructor is a class itself, so you need to get an instance from it again.
  • Based on order of calling add-on classes and according to __pre_init__ and __post_init__ functions, overrides occur. Use this logic to customize what you need.
  • Use self.__core to access the inner class directly. It's helpful to call a core class function overridden by the add-on one (See the example above).

Another example is here. Note the type of E(D(B)). It is BCoveredByDCoveredByE and is a subclass of B:

from add_on_class import AOC

class A:
    def __init__(self):
        pass
    
    def main(self):
        return "(A.main->"+self.function()+"->A.main.end)"
    
    def function(self):
        return "(A.function->A.function.end)"

class B(A):
    def main(self):
        return "(B.main->"+super().main()+"->B.main.end)"
    
    def function(self):
        return "(B.function->B.function.end)"
    
class D(AOC):
    def __post_init__(self):
        self.new_attr = 2
        
    def function(self):
        return "(D.function->"+self.__core.function(self)+"->D.function.end)"

class E(AOC):
    def __pre_init__(self):
        self.new_attr = 3
        
    def function(self):
        return "(E.function->"+self.__core.function(self)+"->E.function.end)"

e = E(D(B))()
print(e.main())
# >>> (B.main->(A.main->(E.function->(D.function->(B.function->B.function.end)->D.function.end)->E.function.end)->A.main.end)->B.main.end)
print(e.new_attr)
# >>> 2
print(issubclass(type(e), B))
# >>> True
print(E(D(B)).__name__)
# >>> BCoveredByDCoveredByE
  • NOTE: We can not add AOC to a class that receives "*args" or "**kwargs" as initialization input.

A class decorator (covering_around) is also available to limit the core class type. See the example below:

from add_on_class import AOC, covering_around

class A:
    def __init__(self):
        pass

class B(A):
    pass
    
class C:
    def __init__(self):
        pass

@covering_around([A])
class D(AOC):
    def __pre_init__(self):
        self.new_attr = 3
        
    def function(self):
        return "function"


a = D(A)()
print(a.new_attr)
print(a.function())

b = D(B)()
print(b.new_attr)
print(b.function())

c = D(C)()

Installation

pip install add-on-class

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

add-on-class-0.1.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

add_on_class-0.1.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file add-on-class-0.1.3.tar.gz.

File metadata

  • Download URL: add-on-class-0.1.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for add-on-class-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a0e6675868696e88d2956ff97fdd4965385538c0d81ffecffff6a479300e22bf
MD5 3fe63fa0ebf8c3f43807b1f6cf934243
BLAKE2b-256 bae6777ccda11ebc74d3d7f1bac3d4ca782df3c327898ab34b5df6e1b12646ed

See more details on using hashes here.

File details

Details for the file add_on_class-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: add_on_class-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for add_on_class-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bf63ddaebaddd60710d0c27adf0d347cd93fd92c30cdb8c40a29bba0100369c4
MD5 34705d18ded0188d82c52571714a36f6
BLAKE2b-256 ddf41ca0c0595e386090594e58dc25ed277ca7a4044bd85db1c64a10a81dee1e

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