I want to have a class with dictionaries that contain nodes, but nodes can be of several sorts: points, vertices,... (and possible others) I want to efficiently loop and access over either one of these (over all nodes, just over points, just over vertices...).
The class would therefore be:
class A:
_nodes: dict
_points: dict
_vertices: dict
Bu surely, if I modify points, it will not modify nodes and vice-versa.
My first idea was to use data descriptors to keep them in sync, but this didn't really work (or at least I don't know how to implement this). Is it possible?
I can just implement nodes a dict of dicts and implement vertices and points as properties.
class A:
_nodes = dict{"points": dict(), "vertices": dict()}
@property
def _points(self):
return(self._nodes["points"])
@property
def _vertices(self):
return(self._nodes["vertices"])
But I'm thinking that it might be time consuming to compute the hash and have additional dictionary lookups every time I will access either points or vertices.
And also, ideally, I wanted to have a public cached property view for nodes, vertices and points (and updating these using data desciptors)
class NodeDescriptor():
def __set__(self, obj, value):
od = obj.__dict__
od["_nodes"] = value
if "nodes" in od: del od["nodes"]
class PointDescriptor(): ...
class VertexDescriptor(): ...
class A:
_nodes = NodeDesciptor()
_points = PointDesciptor()
_vertices = VertexDesciptor()
@cached_property
def nodes(self): return NodeView(self._nodes)
@cached_property
def points(self): return NodeView(self._points)
@cached_property
def vertices(self): return NodeView(self._vertices)
But here the idea to keep nodes as a dict of dict does not work, since we cannot call the descriptor whenever we change nodes["vertex"] = new_dict()