I am trying to understand what's the best way to store class instances in an app, so that I access the attributes right and invoke each class method right. I would like the solution to work with ORM i.e. SqlAlchemy and add GUI in the end.
Let's say I have Notebook class. There's also Note class that can belong to one Notebook. And there's a Picture class - its instance can appear in multiple Notes that belong to different Notebook instances.
For now I thought of approach below (I've made it simpler/without ORM just to get the idea):
class Notebook(object):
def __init__(self):
self.notesid=[]
def view_notes(self,notes,pictures):
for key,item in notes.items():
if key in self.notesid:
item.show(pictures)
class Note(object):
def __init__(self,id,content,picture):
self.id = id
self.content = content
self.picturesid = [picture.id]
def show(self,pictures):
print(self.id, self.content)
for item in pictures:
if item.id in self.picturesid:
print(item)
class Picture(object):
def __init__(self,id,path):
self.id = id
self.path = path
def __str__(self):
'''shows picture'''
return "and here's picture %s" % (self.id)
# main program
notesdict={}
pictureslist=[]
notebook1=Notebook()
picture1 = Picture('p1','path/to/file')
note1=Note('n1','hello world',picture1)
notesdict[note1.id] = note1
pictureslist.append(picture1)
notebook1.notesid.append(note1.id)
notebook1.view_notes(notesdict,pictureslist)
I am not sure if that's the proper approach as even in this simple example I need to throw all the dictionaries/instance containers into the view_notes() method. It feels like there must be a simplier/less error prone way.
All the articles I find say about class creation but I can't find anything about putting it all together in an app and "class instances management", storing multiple class instances (with both one-to-many or many-to-many links at the same time) of classes of a different kind.
Could You please direct me to proper thinking/approach either by using the code above/links to articles/books?