#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    3
    Rep Power
    0

    Question My list is empty.. help


    How do i keep the values in a list throught the whole instance of the program. Mine list keeps on emptying because it gets reinitalized in the constructor.
    i have a doc / view architecture using wxpython.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    could you please post some code? atleast the offending parts?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    3
    Rep Power
    0
    Code:
    #Boa:Doc:ThermoDoc
    
    from wxPython.wx import *
    import ThermoView
    import time
    
    def create():
        return ThermoDoc()
    
    ID_Timer = wxNewId()
    
    
    class ThermoDoc(wxEvtHandler):
        def __init__(self):
            wxEvtHandler.__init__(self)
            self.view = ThermoView.create(None, self)
            self.stop = True
            self.onTimer(None)
            self.days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            self.hour = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','16','17','18','19','20','21','22','23']
            self.today = -1
            self.now = -1
            self.value = -1
            self.item = []
            self.max = 59
            self.min = 0
            self.temp = 0
            self.maxt = 35
            self.mint = 10
            self.Timer = wxTimer(self, ID_Timer) 
            EVT_TIMER(self, ID_Timer, self.onTimer)
            self.Timer.Start(2000)
    
    def loadItem(self):
            in_file = open("Temp.txt","r")
            self.i = 1
            while 1:
                in_line = in_file.readline()
                if len(in_line) == 0:
                    break
                in_line = in_line[:-1]
                self.item.append(in_line)
                self.i = self.i+1
            in_file.close()
        
        def delItem(self,i):
            del self.item[i]
            self.addAllItems()
        
        def clearFile(self):
            out_file = open("Temp.txt","w")
            out_file.write('')
            out_file.close()
        
        def checkFile(self,i):
            in_file = open("Temp.txt","r")
            #self.i = 1
            i = i[:-2]
            while 1:
                
                in_line = in_file.readline()
                if len(in_line) == 0:
                    break
                in_line2 = in_line[-3:]
                in_line = in_line[:-3]
                if i == in_line:
                    return in_line2
                #self.i = self.i+1
            in_file.close()
            
        def countItem(self):
            return len(self.item)
        
        def getItem(self,i):
            return self.item[i]
    
    #----------------------------------------------------------
    
    #Boa:Frame:ThermoView
    
    from wxPython.wx import *
    from wxPython.lib.buttons import *
    from time import *
    
    def create(parent,doc):
        return ThermoView(parent,doc)
    
    
    [wxID_THERMOVIEW, wxID_THERMOVIEWADDBUTTON, wxID_THERMOVIEWDELBUTTON, 
     wxID_THERMOVIEWLISTEDITCTRLS, wxID_THERMOVIEWMODESELECT, wxID_THERMOVIEWPROG, 
     wxID_THERMOVIEWRUN, wxID_THERMOVIEWSPINBUTTON, wxID_THERMOVIEWSPINBUTTON1, 
     wxID_THERMOVIEWTABBUTTON, wxID_THERMOVIEWTABLABEL, wxID_THERMOVIEWTEXTCTRL, 
    ] = map(lambda _init_ctrls: wxNewId(), range(12))
    
    class ThermoView(wxFrame):
    
    
    def OnSpinDown(self, event):
            n = self.field
            p = self.textCtrl.GetStringSelection()
           
            count = self.doc.countItem()
            if self.mode == 1:
                 if self.i<count:
               
                    self.textCtrl.SetValue(self.doc.getItem(self.i))
                    self.i = self.i+1
            else:
                
                if self.field == 0:
                    self.textCtrl.Replace(self.fieldLimits[2*n], self.fieldLimits[2*n+1],self.doc.decdays(p))
                if self.field == 1:
                    self.textCtrl.Replace(self.fieldLimits[2*n], self.fieldLimits[2*n+1],self.doc.decHour(p))
                if self.field == 2:
                    self.textCtrl.Replace(self.fieldLimits[2*n], self.fieldLimits[2*n+1],self.doc.decMin(p))
                if self.field == 3:
                    self.textCtrl.Replace(self.fieldLimits[2*n], self.fieldLimits[2*n+1],self.doc.decTemp(p))
                self.showSelection()
                event.Skip()
    these are only some of the functions in each class
    It doesn't seem to matter which class i keep the list called item. it always gets reset.
    when countItem is called in the view class it gives back an empty list
    Last edited by netytan; April 14th, 2004 at 12:08 AM.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Which list are we talking about here, and can you give us some idea of whats going on. Of course if your creating new instances of your class then the list will not persist since the values are local to each instance, and not a global used by the class. This what you trying to do?

    Mark.

    P.S. please use CODE tags, its what they are there for. Check out the stickies at the top for more info on posting
    programming language development: www.netytan.com Hula

  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    As Netytan says - make your list a global list (declare it in the module outside of a function or class).

    When you assign data to the the list from within a function or class method then you should use the global command to make sure python knows you a referring to an item declared in the global (module) namespace.

    If you are just modifying the list contents using methods or indexing then you don't need the global command:
    Code:
    mylist = []
    
    class aclass:
        def amethod(self):
            global mylist
            mylist= [1,2,3,4,"Hello World"]
    
    def afunc():
        print mylist
        mylist.append("Added by afunc")
    
    
    class bclass:
        def bmethod(self):
            mylist[0]= "modified by bclass.bmethod"
    
    
    
    print mylist
    a = aclass()
    a.amethod()
    print mylist
    afunc()
    print mylist
    b = bclass()
    b.bmethod()
    print mylist
    To understand more - read up on Namespaces and garbage collection.
    Grim
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Alternatively you could define the list at class level, by defining it outside of any class methods. This will make the list common across all the instances of the class. You can then access the list through self.item or ThermoDoc.item.

    For example:
    Code:
    >>> class Test:
    ... 	items = []
    ... 	def __init__(self, value):
    ... 		self.items.append(value)
    ... 	def add(self, value):
    ... 		self.items.append(value)
    ... 	def show(self):
    ... 		print self.items
    ... 		
    >>> t1 = Test('foo')
    >>> t1.show()
    ['foo']
    >>> t2 = Test('bar')
    >>> t1.show()
    ['foo', 'bar']
    >>> t2.show()
    ['foo', 'bar']
    >>> t2.add(23)
    >>> t1.show()
    ['foo', 'bar', 23]
    >>> Test.items
    ['foo', 'bar', 23]
    >>>
    When using class-level attributes you have to be careful about assigning to it, since it will create a new attribute bound to the instance and not change the class attribute. e.g.

    Code:
    >>> t1.items = []
    >>> t1.show()
    []
    >>> t2.show()
    ['foo', 'bar', 23]
    >>> 
    >>> Test.items
    ['foo', 'bar', 23]
    Sometimes this is what you want - the class attribute holds a default value which can be overriden in specific instances.

    Dave - The Developers' Coach
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    3
    Rep Power
    0

    Thanks!


    Thanks for the ideas! I will try the global variable.

IMN logo majestic logo threadwatch logo seochat tools logo