I have classes that look like this:
@attr.s
class ImageMagic(object):
path = attr.ib()
_img = attr.ib()
@_img.default
def _img(self):
return Image.open(self.path)
@attr.s
class FileObject(object):
# Standard
path = attr.ib()
# When magic-ed
magic = attr.ib(default=None)
My goal is to have attrs.asdict() to be able to serialize the FileObject by going through all the attrs and initializing the magic attribute only when it's actually called for serialization and not on __init__.
As most of the time I really don't want the Magic library to be inspecting the object as it is an expensive IO operation.
Goal:
a) How to connect the two classes
b) have the magic attribute only instantiate the ImageMagic object when I actually call it.
c) Only once, so that it may be reused later on if called multiple times.
With this I would prefer to use the Attrs library.
General unclean solution would be to have a @property with a getter, getter checks for existence of private _magic attribute and loads if it doesn't exist.
And then somehow registers the property to attrs library so that it may get serialized furthermore.
This is an example of how an actual solution might look like:
@attr.s
class IOExpensiveClass(object):
path = attr.ib()
_hash = attr.ib()
@_hash.default
def _img(self):
return IOOPERATION(self.path)
@attr.s
class FileObject(object):
# Standard
path = attr.ib()
_magic = None
# Missing attrs registration, that I yet don't know how to write
@property
def magic(self):
return self._magic or IOExpensiveClass(self.path)
magicand do the lazy evaluation there.