Giter VIP home page Giter VIP logo

c3linearize's Introduction

C3Linearize

C3Linearize is a Python implementation of the C3 linearization algorithm that Python uses for its class method resolution order ("MRO"). The difference is that now we can perform this on any type of object!

That being said, I'll demonstrate by just using classes anyways. (This is using Python 2.6 syntax, but this module works with 2.3 through 3.2.)

from c3linearize import linearize, class_graph

class meta(type):
    def __repr__(cls):
        return cls.__name__

class A(object): __metaclass__ = meta
class B(object): __metaclass__ = meta
class C(object): __metaclass__ = meta
class D(object): __metaclass__ = meta
class E(object): __metaclass__ = meta
class K1(A,B,C): pass
class K2(D,B,E): pass
class K3(D,A):   pass
class Z(K1,K2,K3): pass

print 'native MRO: ', Z.__mro__
print 'c3linearize:', linearize(class_graph(Z))[Z]

This prints out:

native MRO:  (Z, K1, K2, K3, D, A, B, C, E, <type 'object'>)
c3linearize: [Z, K1, K2, K3, D, A, B, C, E, <type 'object'>]

linearize operates on a mapping that represents the dependency graph; keys map to a sequence of their dependencies. It will then return a dictionary mapping each and every object to its linearization.

One can either contruct these graphs by hand, or use the build_graph function which will construct one of these mappings from a given object and a function which returns the bases of any given object.

The docstrings offer a little more information on this process, and see http://www.python.org/download/releases/2.3/mro/ for more info on the algorithm itself.

c3linearize's People

Contributors

mikeboers avatar

Watchers

James Cloos avatar Sijoy Joseph avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.