Python Forum
Organizing several similar classes with overlapping variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Organizing several similar classes with overlapping variables
#1
I am struggling for weeks now how to structarize a bunch of classes that have several similarities.
Perhaps the best analogy would be a class representing a graph, e.g. a bunch of nodes that may be connected with each other.
But I have several classes, where the nodes will have different meanings/behaviours, lets call the special nodes "vertices" and "points".
Every node/vertex/point will be a dictionary of lists (adjacent nodes).

The end user will access the nodes via Desciptors and cached properties (kinda what networkx does), but this is probably not important now.

So I have the base class that performs various operations on the nodes (e.g. can remove them)

class BaseGraph:

    def __init__(self, nodes):
        self._nodes = nodes

    def nodes(self):
        return NodeView(self._nodes)
   
    def remove_node(self, node):
        del self._nodes[node]
     ...
We would create an instance by
my_graph = BaseGraph({"a":["b","c"], "b":["a"], "c":["a"]})
(there are a bunch of methods and functions that modify and compute with the nodes).

But then I have several classes that will have BaseGraph as its parent, but the nodes will have somewhat different meaning, for instance, some can be modified one way, some the other way. Lets call different nodes "vertices" and "points". They classes also have different mathematical meanings, so it would not make sense to keep them as separate classes (kinda what networkx does, it has classes "Graph", "MultiGraph", "DirectedGraph",...)

So we have:

class VertexGraph(BaseGraph)
    def __init__(self, vertices):
        self._nodes = vertices

    def vertices(self):
        return NodeView(self._nodes)
class PointGraph(BaseGraph)
    def __init__(self, points):
        self._nodes = points

    def points(self):
        return NodeView(self._nodes)

    def do_something_with_point(self, point):
        ....
class VertexPointGraph(BaseGraph)
    def __init__(self, vertices, points):
        self._nodes = vertices | points
        self._points = points
        self._vertices = vertices

    def points(self):
        return NodeView(self._points)

    def vertices(self):
        return NodeView(self._vertices)

    def do_something_with_point(self, point):
        ....
I could just write several separate classes, but a lot of methods from BaseGraph performs the same operation on points or vertices, e.g. deletion (and the base class is several hundred lines long)

And a lot of functions func(graph) also do not care if the nodes are verties or graphs, but some do. So therefore it would make sense to store all points and vertices in _nodes, but then I have to keep track that _nodes, _points, and _vertices are always updated (if I change a point, I should also change the node, which is not optimal).

One idea, that would work, would be to keep track of the node type by a dictionary

    def __init__(self, vertices, points):
        self._nodes = vertices | points
        self.kind = {v:"Vertex" for v in vertices} | {p:"Point" for p in points}
But then there would be difficult e.g. to iterate over these, which I do often (e.g. iterate over points or iterate over vertices). Also, I would like to quickly know if a given node is a vertex or point.
Also, I would like the class to be extendible in the future, e.g. if I add a third kind of node.

So perhaps it is good the way it is, I just have to be careful to keep all nodes, vertices and points in sync when modifying and deleting them.

All ideas and thoughts are very welcome Smile
Reply


Messages In This Thread
Organizing several similar classes with overlapping variables - by 6hearts - May-06-2023, 09:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 767 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Find overlapping date in database Hilal 2 1,741 Dec-18-2021, 08:15 PM
Last Post: Hilal
  Sum similar items tester_V 3 2,011 Jun-29-2021, 06:58 AM
Last Post: tester_V
  Organizing list of objects by attribute scarney1988 3 2,256 Mar-11-2020, 03:55 PM
Last Post: scarney1988
  Organizing Data in Dictionary Ranjirock 3 2,664 Aug-27-2019, 02:48 PM
Last Post: Ranjirock
  I can't use file __init__ to store shared variables and classes in the package AlekseyPython 2 3,388 Feb-04-2019, 06:26 AM
Last Post: AlekseyPython
  Python: if 'X' in 'Y' but with two similar strings as 'X' DreamingInsanity 6 3,901 Feb-01-2019, 01:28 PM
Last Post: buran
  How can classes access each other Functions and Variables at the same time PythonOK 4 3,094 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  Similar to Poker bluekade5050 1 34,655 Nov-14-2018, 04:46 PM
Last Post: j.crater
  organizing by distance gonzo620 7 4,006 Oct-16-2018, 01:41 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020