Allows you to canibalize methods from classes effectively implementing trait-oriented programming

Overview

About

This package enables code reuse in non-inheritance way from existing classes, effectively implementing traits-oriented programming pattern.

Story

Once upon a time there was

class AHealthyClass:
    def see(self):
        print('I can see')

    def walk(self):
        print('Walking around')

    def run(self):
        print('I can run')

and he decided to visit his grandma and bring her fresh-baked pie.

But little did he know there was couple of hungry zombies in his merry way waiting to chew on some bones.

They were old decrepit zombies - not very fast nor particulary perseptive.

from canibalize import infect

@infect
class Zombie1:
    def see():
        raise Exception('Arrgh') # he was a pirate, probably

@infect
class Zombie2:
    def walk():
        raise Exception("I've fallen and I can't get up!")

He walked closer and closer to the ambush and then zombies pounced at him. And started eating him.

Zombie1.nomnom(AHealthyClass, 'see')
Zombie2.nomnom(AHealthyClass, ['see', 'walk'])

And then strange things started to happend. Zombie1 who took AHealthyClass eye, start seeing things. And both of them been munching on AHealthyClass legs gain ability of walking again!

z1 = Zombie1()
z1.see()
>>> I can see

z2 = Zombie2()
z2.walk()
>>> Walking around

z1.walk()
>>> Walking around

The miracle is that AHealthyClass can still see and walk and even run. Maybe he was a spider in disguise.

h1 = AHealthyClass()
h1.see()
>>> I can see

h1.walk()
>>> Walking around

And then they took each other's arms(who still had an arm) and walk()-ed into a sunset, which was beutiful to see().

And live happily ever after.

And married each other, probably...

The END

Seriously, though

Lets imagine you have some Library and some Code that uses that Library. You have no controll of both of them, but you need to add/modify functionality to them.

For example, you add serialization/deserialization/validation/schema-generation to classes of that Library, for example with pydantic.

You cannot subclass from Library classes, because Code will not utilize them(without rewriting it ofcause). The only real option you have is to write facade for every class of the Library to add desired behaviour.

With canibalize you can extend Library becaviour without modifying nither Library nor Code.

from Libray import SomeClass
from Code import run

from canibalize import canibalize
from pydantic import BaseModel
from typing import List

class SomeSerialization:
    @classmethod
    def __get_validators__(cls):
        some_class_json_validators = [
            ... SomeClass specific validators
        ]
        for v in some_class_json_validators:
            yield v

    @classmethod
    def __modify_schema__(cls, field_schema):
        field_schema.update(
            ... # update schema based on cls logic
        )


canibalize(
    SomeClass,
    SomeSerialization, 
    ['__get_validators__', __modify_schema__', 'Config']
)

@dataclass
class ResultClass(BaseModel):
    results: List[SomeType]

some = SomeClass(result = [run(*v) for v in mah_values])

# now SomeClass correctly produces json_schema no matter how deep it nested
print(some.json_schema(indent=2)) 
Helpful functions for use alongside the rich Python library.

🔧 Rich Tools A python package with helpful functions for use alongside with the rich python library. 󠀠󠀠 The current features are: Convert a Pandas

Avi Perl 14 Oct 14, 2022
Dependency injection lib for Python 3.8+

PyDI Dependency injection lib for python How to use To define the classes that should be injected and stored as bean use decorator @component @compone

Nikita Antropov 2 Nov 09, 2021
A simple tool to extract python code from a Jupyter notebook, and then run pylint on it for static analysis.

Jupyter Pylinter A simple tool to extract python code from a Jupyter notebook, and then run pylint on it for static analysis. If you find this tool us

Edmund Goodman 10 Oct 13, 2022
A work in progress box containing various Python utilities

python-wipbox A set of modern Python libraries under development to simplify the execution of reusable routines by different projects. Table of Conten

Deepnox 2 Jan 20, 2022
A python package containing all the basic functions and classes for python. From simple addition to advanced file encryption.

A python package containing all the basic functions and classes for python. From simple addition to advanced file encryption.

PyBash 11 May 22, 2022
A simple dork generator written in python that outputs dorks with the domain extensions you enter

Dork Gen A simple dork generator written in python that outputs dorks with the domain extensions you enter in a ".txt file". Usage The code is pretty

Z3NToX 4 Oct 30, 2022
iOS Snapchat parser for chats and cached files

ParseSnapchat iOS Snapchat parser for chats and cached files Tested on Windows and Linux install required libraries: pip install -r requirements.txt c

11 Dec 05, 2022
Give you a better view of your Docker registry disk usage.

registry-du Give you a better view of your Docker registry disk usage. This small tool will analysis your Docker registry(vanilla or Harbor both work)

Nova Kwok 16 Jan 07, 2023
Daiho Tool is a Script Gathering for Windows/Linux systems written in Python.

Daiho is a Script Developed with Python3. It gathers a total of 22 Discord tools (including a RAT, a Raid Tool, a Nuker Tool, a Token Grabberr, etc). It has a pleasant and intuitive interface to faci

AstraaDev 32 Jan 05, 2023
An extremely simple package with a single utillity class used for gracefully handling POSIX shutdown signals.

graceful-killer An extremely simple package with a single utillity class used for gracefully handling POSIX shutdown signals. Installation Use pip to

Sven Ćurković 1 Dec 09, 2021
Hide new MacBook Pro notch with black wallpaper.

Hide new MacBook Pro notch with black wallpaper.

Wang Chao 1 Oct 27, 2021
A python module to update the console without flashing.

A python module to update the console without flashing.

Matthias 112 Dec 19, 2022
A program to convert celcius to faranheit. made with python

Temp-Converter What is Temp-Converter Temp-Converter is little program made with pyhton to convert celcius to faranheit. Needed A python interpreter P

Chandula Janith 0 Nov 27, 2021
Gradually automate your procedures, one step at a time

Gradualist Gradually automate your procedures, one step at a time Inspired by https://blog.danslimmon.com/2019/07/15/ Features Main Features Converts

Ross Jacobs 8 Jul 24, 2022
✨ Voici un code en Python par moi, et en français qui permet d'exécuter du Javascript en Python.

JavaScript In Python ❗ Voici un code en Python par moi, et en français qui permet d'exécuter du Javascript en Python. 🔮 Une vidéo pour vous expliquer

MrGabin 4 Mar 28, 2022
general-phylomoji: a phylogenetic tree of emoji

general-phylomoji: a phylogenetic tree of emoji

2 Dec 11, 2021
Delete all of your forked repositories on Github

Fork Purger Delete all of your forked repositories on Github Installation Install using pip: pip install fork-purger Exploration Under construc

Redowan Delowar 29 Dec 17, 2022
SH-PUBLIC is a python based cloning script. You can clone unlimited UID facebook accounts by using this tool.

SH-PUBLIC is a python based cloning script. You can clone unlimited UID facebook accounts by using this tool. This tool works on any Android devices without root.

(Md. Tanvir Ahmed) 5 Mar 09, 2022
ticktock is a minimalist library to view Python time performance of Python code.

ticktock is a minimalist library to view Python time performance of Python code.

Victor Benichoux 30 Sep 28, 2022
Python implementation of Gorilla time series compression

Gorilla Time Series Compression This is an implementation (with some adaptations) of the compression algorithm described in section 4.1 (Time series c

Ghiles Meddour 19 Jan 01, 2023