Last active
May 15, 2019 16:57
-
-
Save gterzian/6400170 to your computer and use it in GitHub Desktop.
Ruby's method_missing in Python...
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
import unittest | |
from functools import partial | |
class MethodMissing: | |
def method_missing(self, name, *args, **kwargs): | |
'''please implement''' | |
raise NotImplementedError('please implement a "method_missing" method') | |
def __getattr__(self, name): | |
return partial(self.method_missing, name) | |
class Wrapper(object, MethodMissing): | |
def __init__(self, item): | |
self.item = item | |
def method_missing(self, name, *args, **kwargs): | |
if name in dir(self.item): | |
method = getattr(self.item, name) | |
if callable(method): | |
return method(*args, **kwargs) | |
else: | |
raise AttributeError(' %s has not method named "%s" ' % (self.item, name)) | |
class Item(object): | |
def __init__(self, name): | |
self.name = name | |
def test(self, string): | |
return string + ' was passed on' | |
class EmptyWrapper(object, MethodMissing): | |
'''not implementing a missing_method''' | |
pass | |
class TestWrapper(unittest.TestCase): | |
def setUp(self): | |
self.item = Item('test') | |
self.wrapper = Wrapper(self.item) | |
self.empty_wrapper = EmptyWrapper() | |
def test_proxy_method_call(self): | |
string = self.wrapper.test('message') | |
self.assertEqual(string, 'message was passed on') | |
def test_normal_attribute_not_proxied(self, ): | |
with self.assertRaises(AttributeError): | |
self.wrapper.name | |
self.wrapper.name() | |
def test_empty_wrapper_raises_error(self, ): | |
with self.assertRaises(NotImplementedError): | |
self.empty_wrapper.test('message') | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: solve the above issue, latest implementation does provide very similar(identical?) functionality as Ruby's method_missing concept.