Created
July 3, 2020 20:43
-
-
Save Gribouillis/3da614c8560b63cf8a8d02d0d394a203 to your computer and use it in GitHub Desktop.
Two functions to glue python objects together
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*-coding: utf8-*- | |
# Terms of the MIT license, which apply to this software | |
# ======================================================== | |
# Copyright (C) 2014 Eric Ringeisen | |
# | |
# Permission is hereby granted, free of charge, to | |
# any person obtaining a copy of this software and | |
# associated documentation files (the "Software"), | |
# to deal in the Software without restriction, | |
# including without limitation the rights to use, | |
# copy, modify, merge, publish, distribute, | |
# sublicense, and/or sell copies of the Software, | |
# and to permit persons to whom the Software is | |
# furnished to do so, subject to the following | |
# conditions: | |
# | |
# The above copyright notice and this permission | |
# notice shall be included in all copies or | |
# substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY | |
# OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | |
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES | |
# OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF | |
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
# OTHER DEALINGS IN THE SOFTWARE. | |
# ======================================================== | |
__version__ = '2014.06.12' | |
__doc__ = '''oocollage | |
This module defines a function `collage()` which purpose is to create | |
superobjects by gluing existing python objects together. | |
Another function `collage_class()` returns a base class for such | |
superobjects, allowing further specialization of these objects. | |
Example: | |
>>> class Lion(object): | |
... def hunt(self): | |
... print("I'm hunting") | |
... | |
>>> class Duck(object): | |
... def quack(self): | |
... print("Quack! Quack!") | |
... | |
>>> class Fish(object): | |
... def swim(self): | |
... print("I'm swimming") | |
... | |
>>> lion = Lion() | |
>>> duck = Duck() | |
>>> fish = Fish() | |
>>> monster = collage(lion, duck, fish) | |
>>> monster.hunt() | |
I'm hunting | |
>>> monster.swim() | |
I'm swimming | |
>>> monster.quack() | |
Quack! Quack! | |
>>> | |
>>> ldf_type = collage_class(lion, duck, fish) | |
>>> class UserMonster(ldf_type): | |
... def go(self): | |
... self.hunt() | |
... self.quack() | |
... self.swim() | |
... | |
>>> m = UserMonster() | |
>>> m.go() | |
I'm hunting | |
Quack! Quack! | |
I'm swimming | |
''' | |
import itertools as itt | |
def linearize(items): | |
### C3 linearization algorithm adapted to our case. | |
### https://www.python.org/download/releases/2.3/mro/ | |
items = list(items) | |
for i, x in enumerate(items): | |
if isinstance(x, BaseCollage): | |
items[i] = x._collage_mro | |
else: | |
items[i] = (x,) | |
return tuple(_c3_merge(items)) | |
def _c3_merge(seqs): | |
seqs = [x for x in seqs if x] | |
while seqs: | |
# find head | |
s = set(itt.chain.from_iterable(L[1:] for L in seqs)) | |
x = itt.dropwhile(lambda pair: pair[1][0] in s, enumerate(seqs)) | |
try: | |
i, item = next(x) | |
except StopIteration: | |
raise RuntimeError("Impossible C3 linearization in ObjectCollage creation") | |
item = item[0] | |
yield item | |
seqs[:] = [tuple(x for x in L if x != item) for L in seqs] | |
seqs = [x for x in seqs if x] | |
class BaseCollage(object): | |
def __getattr__(self, attr): | |
for n in self._collage_mro: | |
try: | |
# print("trying", n, attr) | |
return getattr(n, attr) | |
except AttributeError: | |
pass | |
raise AttributeError(attr) | |
def collage_class(*items): | |
class Collage(BaseCollage): | |
_collage_mro = linearize(items) | |
return Collage | |
def collage(*items): | |
return collage_class(*items)() | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment