Mailing List Archive

Alg Problem (new to Python)
Hello,
The out put of a program is below....the problem is the tasks are being
added
to every project vs. to the specific project....it sees like an alg program
but
I can't see it....brand new to Python...have mercy.

project key: z125 name: Project C
task key: t1241
task key: t1231
project key: y124 name: Project B
task key: t1241
task key: t1231
project key: x123 name: Project A
task key: t1241
task key: t1231

the program below is in on .py file


import sys

projectDict = {} #MAPS PROJECT id string to PROJECT object
taskDict = {} #MAPS TASK id string to TASK object

class Project:
_id = ''
_name = ''
_descr = ''
_tasks = {} #list of Tasks I contain

def __init__(self, projectID):
self._id = projectID

#setters
def set_id(self, anID):
self._id = anID

def set_name(self, aName):
self._name = aName

def set_descr(self, aDescr):
self._descr = aDescr

def addTask(self, aTask):
#require aTask not null
self._tasks[aTask.id()] = aTask

#getters
def id(self):
return self._id

def name(self):
return self._name

#end class Project



class Task:
_id = ''
_name = ''
_descr = ''
_owner = '' #Person responsible for the task
_project = '' #the project that contains me

def __init__(self,ID):
self._id = ID

def test(self):
#dump internals
print('in Task test')

#setters
def set_id(self,anID):
self._id = anID

def set_name(self, aName):
self._name = aName

def set_descr(self, aDescr):
self._descr = aDescr

def set_project(self, aProject):
#aProject: PROJECT
self._project = aProject

#getters
def id(self):
return self._id

def name(self):
return self._name

def descr(self):
return self._descr


#end class Task


#---------------Call back functions for GUI------------------------

def addTaskToProject(aTaskID, aProjectID):
#require valid id's and exist in the dictionaries
prj = projectDict[aProjectID]
task = taskDict[aTaskID]
prj.addTask(task)

#end addTaskToProject

def createNewProject(anID,aName):
p = Project(anID)
p.set_name(aName)
projectDict[p.id()] = p

def createNewTask(anID,aName):
t = Task(anID)
t.set_name(aName)
taskDict[t.id()]= t

def dumpProjectDictContents():
for prjKey in projectDict.keys():
print 'project key: ' + prjKey + ' name: ' + projectDict[prjKey].name()


def dumpTaskDictContents():
for key in taskDict.keys():
print 'task key: ' + key + ' name: ' + taskDict[key].name()

def projectReport():
for prjKey in projectDict.keys():
print 'project key: ' + prjKey + ' name: ' + projectDict[prjKey].name()
for taskKey in projectDict[prjKey]._tasks.keys():
print 'task key: ' + taskKey
taskKey = ''


#end createNewProject

#--------------------------------------------------------------------

def testRun():
createNewProject('x123','Project A')
createNewProject('y124','Project B')
createNewProject('z125','Project C')
# dumpProjectDictContents()

createNewTask('t1231','Task A')
createNewTask('t1232','Task B')
createNewTask('t1233','Task C')
createNewTask('t1241','Task D')
createNewTask('t1242','Task E')
createNewTask('t1251','Task F')
createNewTask('t1252','Task G')
# dumpTaskDictContents()

addTaskToProject('t1231','x123') #some how all the tasks are added to all
the projects
addTaskToProject('t1241','y124')

projectReport()


#end testRun

if __name__ == '__main__':
testRun()
Alg Problem (new to Python) [ In reply to ]
In article <7k38gf$khr$1@holly.prod.itd.earthlink.net>, Steve Hunter
<Steve@HOSYS.com> wrote:

> The out put of a program is below....the problem is the tasks are
>being added to every project vs. to the specific project....it sees
>like an alg program but I can't see it....brand new to Python...have
>mercy.
>
> [...]
>
>the program below is in on .py file
>
>
>import sys
>
>projectDict = {} #MAPS PROJECT id string to PROJECT object
>taskDict = {} #MAPS TASK id string to TASK object
>
>class Project:
> _id = ''
> _name = ''
> _descr = ''
> _tasks = {} #list of Tasks I contain

The dictionary Project._tasks is a mutable object, so when your method
for adding a task adds a new task to the dictionary, it adds the task to
the class attribute (Instead of making a new object as with strings and
integers, for instance.). Putting something like
self.tasks = {}
into the __init__-method of the class Project will solve your problem.
Don't worry, the behaviour of mutable versus immutable objects is one of
the few things that I think often bites people new to Python.
--
Steinar