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)) 
Tool to produce system call tables from Linux source code.

Syscalls Tool to generate system call tables from the linux source tree. Example The following will produce a markdown (.md) file containing the table

7 Jul 30, 2022
A primitive Python wrapper around the Gromacs tools.

README: GromacsWrapper A primitive Python wrapper around the Gromacs tools. The library is tested with GROMACS 4.6.5, 2018.x, 2019.x, 2020.x, and 2021

Becksteinlab 140 Dec 28, 2022
New time-based UUID formats which are suited for use as a database key

uuid6 New time-based UUID formats which are suited for use as a database key. This module extends immutable UUID objects (the UUID class) with the fun

26 Dec 30, 2022
Plone Interface contracts, plus basic features and utilities

plone.base This package is the base package of the CMS Plone https://plone.org. It contains only interface contracts and basic features and utilitie

Plone Foundation 1 Oct 03, 2022
python-codicefiscale: a tiny library for encode/decode Italian fiscal code - codifica/decodifica del Codice Fiscale.

python-codicefiscale python-codicefiscale is a tiny library for encode/decode Italian fiscal code - codifica/decodifica del Codice Fiscale. Features T

Fabio Caccamo 53 Dec 14, 2022
A python script to generate wallpaper

wallpaper eits Warning You need to set the path to Robot Mono font in the source code. (Settings are in the main function) Usage A script that given a

Henrique Tsuyoshi Yara 5 Dec 02, 2021
Napari plugin for loading Bitplane Imaris files .ims

napari-imaris-loader Napari plugin for loading Bitplane Imaris files '.ims'. Notes: For this plugin to work "File/Preferences/Experimental/Render Imag

Alan Watson 4 Dec 01, 2022
Simple python module to get the information regarding battery in python.

Battery Stats A python3 module created for easily reading the current parameters of Battery in realtime. It reads battery stats from /sys/class/power_

Shreyas Ashtamkar 5 Oct 21, 2022
🦩 A Python tool to create comment-free Jupyter notebooks.

Pelikan Pelikan lets you convert notebooks to comment-free notebooks. In other words, It removes Python block and inline comments from source cells in

Hakan Ă–zler 7 Nov 20, 2021
Python Yeelight YLKG07YL/YLKG08YL dimmer handler

With this class you can receive, decrypt and handle Yeelight YLKG07YL/YLKG08YL dimmer bluetooth notifications in your python code.

12 Dec 26, 2022
.bvh to .mcfunction file converter.

bvh-to-mcf .bvh file to .mcfunction converter

Hanmin Kim 28 Nov 21, 2022
✨ Un générateur d'adresse IP aléatoire totalement fait en Python par moi, et en français.

IP Generateur ❗ Un générateur d'adresse IP aléatoire totalement fait en Python par moi, et en français. 🔮 Avec l'utilisation du module "random", j'ai

MrGabin 3 Jun 06, 2021
A library from RCTI+ to handle RabbitMQ tasks (connect, send, receive, etc) in Python.

Introduction A library from RCTI+ to handle RabbitMQ tasks (connect, send, receive, etc) in Python. Requirements Python =3.7.3 Pika ==1.2.0 Aio-pika

Dali Kewara 1 Feb 05, 2022
A simple package for handling variables in string.

A simple package for handling string variables. Welcome! This is a simple package for handling variables in string, You can add or remove variables wi

1 Dec 31, 2021
Check the basic quality of any dataset

Data Quality Checker in Python Check the basic quality of any dataset. Sneak Peek Read full tutorial at Medium. Explore the app Requirements python 3.

MalaDeep 8 Feb 23, 2022
A Python script that transcript Arcaea chart file (.aff file) into AutoJS touchscreen script which automatically plays the Arcaea chart

ArcaeaAutoplay (AutoJS Version) A Python script that transcript Arcaea chart file (.aff file) into AutoJS touchscreen script which automatically plays

7 Dec 03, 2021
Pyfunctools is a module that provides functions, methods and classes that help in the creation of projects in python

Pyfunctools Pyfunctools is a module that provides functions, methods and classes that help in the creation of projects in python, bringing functional

Natanael dos Santos Feitosa 5 Dec 22, 2022
Python program for Linux users to change any url to any domain name they want.

URLMask Python program for Linux users to change a URL to ANY domain. A program than can take any url and mask it to any domain name you like. E.g. ne

2 Jun 20, 2022
A fixture that allows runtime xfail

pytest-runtime-xfail pytest plugin, providing a runtime_xfail fixture, which is callable as runtime_xfail(), to allow runtime decisions to mark a test

Brian Okken 4 Apr 06, 2022
A fancy and practical functional tools

Funcy A collection of fancy functional tools focused on practicality. Inspired by clojure, underscore and my own abstractions. Keep reading to get an

Alexander Schepanovski 2.9k Jan 07, 2023