Skip to content

Instantly share code, notes, and snippets.

@rande
Created April 2, 2014 07:51
Show Gist options
  • Save rande/9929712 to your computer and use it in GitHub Desktop.
Save rande/9929712 to your computer and use it in GitHub Desktop.
Services as Methods - python mode
# vim: set fileencoding=utf-8 :
import unittest
from element.plugins.node.mapper import Manager, Meta, MetaCollection, MetaListener
from element.node import Node
from ioc.event import Event
import datetime
class NodeImage(Node):
pass
class MapperTest(unittest.TestCase):
def test_add_method(self):
class Manager(object):
def get_uuid(self, node, *args, **kwargs):
return "%s: %s" % (node.uuid, kwargs)
def wrapper(function):
return lambda node, *args, **kwargs: function(node, *args, **kwargs)
def my_method(node):
return "hello world! ~ uuid: %s" % node.uuid
m = Manager()
# attach new methods to the class
setattr(NodeImage, 'my_method', my_method)
setattr(NodeImage, 'class_method', lambda node, *args, **kwargs: m.get_uuid(node, *args, **kwargs))
setattr(NodeImage, 'wrapper_method', wrapper(m.get_uuid))
n = NodeImage("550e8400-e29b-41d4-a716-446655440000")
self.assertEquals("hello world! ~ uuid: 550e8400-e29b-41d4-a716-446655440000", n.my_method())
self.assertEquals("550e8400-e29b-41d4-a716-446655440000: {'foo': 'bar'}", n.class_method(foo='bar'))
self.assertEquals("550e8400-e29b-41d4-a716-446655440000: {'foo': 'bar'}", n.wrapper_method(foo='bar'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment