Skip to content

Instantly share code, notes, and snippets.

@majorz
majorz / nicks.py
Last active December 15, 2015 23:59
MRO iteration solution
from abc import ABCMeta
class Renderer(metaclass=ABCMeta):
def __init__(self, entity):
self._entity = entity
self._render_entity = self._locate_renderer()
def _locate_renderer(self):
for cls in self.__class__.mro():
@majorz
majorz / idea.py
Created April 9, 2013 01:50
The idea
from __future__ import teleport
from abc import ABCMeta
class Renderer(metaclass=ABCMeta):
def __init__(self, entity):
self._entity = entity
@teleport
@majorz
majorz / cooperative.py
Created April 9, 2013 01:48
Using cooperative multiple inheritance
from abc import ABCMeta, abstractmethod
class Renderer(metaclass=ABCMeta):
def __init__(self, entity):
self._entity = entity
@abstractmethod
def __call__(self):
raise TypeError('Not recognized')
@majorz
majorz / composition.py
Created April 9, 2013 01:46
Using composition
from abc import ABCMeta, abstractmethod
class Renderer(metaclass=ABCMeta):
def __init__(self, entity):
self._entity = entity
@abstractmethod
def __call__(self):
raise NotImplementedError
@majorz
majorz / gist:5341333
Created April 8, 2013 23:05
Name mangling and code repetition - cooperative inheritance use case
class Renderer:
def __init__(self, entity):
self._entity = entity
class IntRenderer(Renderer):
__type = int
def __render_entity(self):
return 'INTEGER {}'.format(self._entity)
@majorz
majorz / gist:637452
Created October 20, 2010 22:21
Sample state machine control flow
def initial():
do_some_initialization()
def first_branch(data):
pass
def default_branch(data):
pass
def should_execute_first(data):