943,545 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 538
  • Python RSS
Jun 5th, 2009
0

Python nested objects, best practices.

Expand Post »
python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Wraithan is offline Offline
6 posts
since Jan 2007
Jun 7th, 2009
0

Re: Python nested objects, best practices.

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
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Py2exe problem
Next Thread in Python Forum Timeline: help in this code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC