spy is a package that enables automated cloning of user-defined Python classes.
from spy.cc import clone
class SimpleOriginal:
def __init__(self, name):
self.name = name
def __repr__(self):
return f"{self.__class__.__name__}({self.name})"
def foo(self):
return self.name + 'foo'
def bar(self):
return self.name + 'bar'
@clone(SimpleOriginal)
class SimpleClone:
pass
# tests
sc_obj = SimpleClone('test')
print(sc_obj.foo()) # testfoo
print(sc_obj.bar()) # testbar
print(sc_obj) # SimpleClone(test)
print(type(sc_obj)) # <class '__main__.SimpleClone'>
print(sc_obj.foo, sc_obj.bar)
# <bound method SimpleClone.foo of SimpleClone(test)> <bound method SimpleClone.bar of SimpleClone(test)>
Tested for Python 3.7, compatible with Python 3.6+. See the tests folder.
Clone this repo, and do:
cd spy
python setup.py install
spy does not work for built-in types.
A BuiltinTypeNotSupportedException
is raised if you try to clone builtin types.
See tests for more information.