Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old April 13th, 2004, 07:43 PM
casio99 casio99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 casio99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old April 13th, 2004, 09:20 PM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 5
could you please post some code? atleast the offending parts?

Reply With Quote
  #3  
Old April 13th, 2004, 10:37 PM
casio99 casio99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 casio99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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 13th, 2004 at 11:08 PM.

Reply With Quote
  #4  
Old April 13th, 2004, 11:17 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #5  
Old April 14th, 2004, 03:30 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #6  
Old April 14th, 2004, 07:47 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,195 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 13 h 31 m 54 sec
Reputation Power: 252
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

Reply With Quote
  #7  
Old April 14th, 2004, 08:03 PM
casio99 casio99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 3 casio99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks!

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > My list is empty.. help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway