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
#6
(May-07-2023, 03:45 AM)deanhystad Wrote: When graphing points or vertices, are they treated differently? Or are you making two classes because you think you need one graph class for each class of item you are trying to graph?

Yes, maybe I am overthinking everythig. I took great inspiration in the networx package, where they have different classes for each graph type (graph, multigraph,..) but there the internal structure is always different, so it makes sense. Maybe not in my case, since only the node types are different. I really liked netwokx structure, where they store all data as dictionaries and then have different views on this data so the user can access it more naturally.


(May-07-2023, 06:14 AM)Gribouillis Wrote: Dataclases are probably the appropriate tools instead of dictionaries, and it solves the problem of identifying the type of the objects in a collection with mixed objects. Namedtuples could perhaps be used too.
Hm... but for the nodes I would really need the functionality of a list, since a node is really just a list of neighbouring nodes. And I will often change particular items at particular indices, so __setitem__ and __getitem__ will be often called.
Surely I could add a data parameter to the dataclass, but then it would be less clear for the end user, they would access the data through node.data[i] instead of node[i].

What if I just wrap a list and have a name parameter

class Vertex(list):
    def __init__(self, data):
        super(Vertex, self).__init__(data)
        self.name = "vertex"
And then use filters so the user can iterate over certain types.
But I kinda hate the idea to have an almost empty class just for determining its "type".
Reply


Messages In This Thread
RE: Organizing several similar classes with overlapping variables - by 6hearts - May-07-2023, 08:01 AM

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,743 Dec-18-2021, 08:15 PM
Last Post: Hilal
  Sum similar items tester_V 3 2,014 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,669 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,389 Feb-04-2019, 06:26 AM
Last Post: AlekseyPython
  Python: if 'X' in 'Y' but with two similar strings as 'X' DreamingInsanity 6 3,904 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,099 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  Similar to Poker bluekade5050 1 34,941 Nov-14-2018, 04:46 PM
Last Post: j.crater
  organizing by distance gonzo620 7 4,008 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