Python nested objects, best practices.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 6
Reputation: Wraithan is an unknown quantity at this point 
Solved Threads: 0
Wraithan Wraithan is offline Offline
Newbie Poster

Python nested objects, best practices.

 
0
  #1
Jun 5th, 2009
  1. class Item:
  2. def __init__(self, id, content, userid, duedate, collapsed, inhistory, priority, itemorder, faded, projectid, checked, datestring):
  3. self.id = id
  4. self.content = content
  5. self.userid = userid
  6. self.duedate = duedate
  7. self.collapsed = collapsed
  8. self.inhistory = inhistory
  9. self.priority = priority
  10. self.itemorder = itemorder
  11. self.faded = faded
  12. self.projectid = projectid
  13. self.checked = checked
  14. self.datestring = datestring
  15. def __str__(self):
  16. return self.content
  17.  
  18. class Project:
  19. def __init__(self, id, name, userid, color, collapsed, itemorder, indent, order):
  20. self.id = id
  21. self.name = name
  22. self.userid = userid
  23. self.color = color
  24. self.collapsed = collapsed
  25. self.itemorder = itemorder
  26. self.indent = indent
  27. self.order = order
  28. self.items = {}
  29.  
  30. def __str__(self):
  31. return self.name
  32.  
  33. def __name__(self):
  34. return 'Project'
  35.  
  36. def addItem(self,item):
  37. self.items.update({item.id:item})
  38.  
  39. def delItem(self,item):
  40. del self.items[item.id]
  41.  
  42. class TodoList:
  43. def __init__(self):
  44. self.projects = {}
  45.  
  46. def addProject(self,project):
  47. self.projects.update({project.id:project})
  48.  
  49. def delProject(self,project):
  50. del self.projects[project.id]
  51.  
  52. def addItem(self,item):
  53. self.projects[item.projectid].addItem(item)
  54.  
  55. def delItem(self,item):
  56. self.projects[item.projectid].delItem(item)

Some of the above code is changing as we speak but I am curious if I should make the projects dict and items dicts private and use accessor functions to move items between projects or change the attributes of a project or item.

Another thought is to have items and projects both be members of TodoList as I am provided with the Item's project ID when I get the Item, so I could just have a method that returned all the items with a given project ID and not bother with separating them into the individual projects.

I am just trying to design the best I can as I plan on using this app as an example of my Python coding ability, though I just started with python about 3 days ago.
Last edited by Wraithan; Jun 5th, 2009 at 11:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 128
Reputation: slate is an unknown quantity at this point 
Solved Threads: 31
slate slate is offline Offline
Junior Poster

Re: Python nested objects, best practices.

 
0
  #2
Jun 7th, 2009
We do not make private attributes in python, except in very rare cases. You can shadow the attribute access with "property" decorator later.

The Item and the Project class is trying to reimplement the named tuple, which is present in python 2.6 or later.

From a design point of view I find it unfortunate, that you provide interface in the TodoList to add an item to a project.

Another problem is that you do not have any Item without Project. So Items aggregate (in uml sense) to a Project.

It is unforunate too, that your TodoList.addItem calls the items project to add the item to its own project
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 212 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC