Re: [Tutor] New to programming and Python

2006-10-26 Thread Chris Hengge
I'm sure I speak for us all when I ask what sort of things are giving you problems? Maybe that would help us point you in the right direction. Good Luck!On 10/25/06, 
Jorge Azedo [EMAIL PROTECTED] wrote:
Hi guys ( and gals )I'm totally new to the whole programming scene (I decided to enter itfor many reasons, one of which is the fact that I *want* to know how toprogram my computer) and I decided to start out with Python. I'm reading
lots of tutorials on it, trying to understand how to work with it, butfor some reason or another, it all seems a great mess to me. Am I doingsomething wrong? Also, can anyone give me any pointers on how to start
working with Python?Thanks in advance for any help you can give me- Jorge___Tutor maillist-Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Self, Scopes and my unbelievable muddleheadedness.

2006-10-26 Thread Alan Gauld
doug shawhan [EMAIL PROTECTED] wrote
 I'm having a rather difficult time understanding the proper use of 
 self.

Doug, I think you may be having a more fundamental problem.
Looking at your code at the most superficial level it seems
you may not understand objects.

Classes are used to generate objects. Objects represent things.
By logical deduction objects correspond to nouns in language.
Objects do things when they receive messages (Possibly
from other objects). Messages cause methods to be invoked,
methods therefore correspond to verbs in language.

Looking at your code we find:

class Create:   # a verb
def freshDB(self, DBPATH, Fields):   # a noun
def comparisonTable(self, DBPATH, Fields, columns, mode): #a noun

In other words you have the concept inside out.
you are trying to create Create objects and call Table and Database
methods. It doesn't make sense.

Normally we would expect to see DB and Table objects to which
you send create messages.

Now if you think of the objects as verbs then self is a confusing 
concept.
But if you think of the objects as nouns then self is simply a 
reference
to the object instance in question, a particular database or table.

I'd recommend going back and rethinking the design of your
objects before going much further.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


 I have two functions (yes, they are ugly, I was getting ready to 
 split them
 in to smaller bits when this particular hole in my skull opened up) 
 in a
 module. They use the same list of dictionaries to create some tables 
 in a
 gadfly database.

 I know I could fix this in any number of ways (including, but not 
 limited
 to, acting on the same input list twice, or acting on it in such a 
 fashion
 in the first place), but I am trying to actually understand self.

 Here's the horrid, horrid mess:

 class Create:
def freshDB(self, DBPATH, Fields):
self.Fields = Fields
for field in self.Fields.keys():
columns = self.Fields[field]
columns = columns.split(',')
query = ''
for each in columns:
query = %s, %s varchar%(query,each)
query = query[1:]
query = Create Table %s (%s)%(field,query)
self.Fields[field] = query

connection = gadfly.gadfly()
connection.startup(InventoryDB, DBPATH)
cursor = connection.cursor()

for field in self.Fields.keys():
cursor.execute(self.Fields[field])
connection.commit()

for field in self.Fields.keys():
cursor.execute(Select * from %s%field)
print cursor.pp()
connection.close()

def comparisonTable(self, DBPATH, Fields, columns, mode):
query = 
if mode == 'new':
connection = gadfly.gadfly(InventoryDB,DBPATH)
cursor = connection.cursor()
try:
cursor.execute(drop table comparison)
except:
print No old table found.
columns = Fields[Common].split(',')

for each in columns:
query = %s, %s varchar%(query,each)
query = query[1:]
query = Create Table comparison (%s)%query
cursor.execute(query)
connection.commit()
cursor.execute(select * from comparison)
print cursor.pp()

 Now when I run freshDB from the other script:

 Fields = {Common:Inventory_Number, Stock_Number, Location, Year, 
 Make,
 Model, Part_Type, Mileage, Description, Interchange_Data, Condition, 
 Price,
EvilBay:EvilBay_Title, EvilBay_Description,
HappyBase:HappyBase_Description, HappyBase_Long_Description}

 d = DBMod

 d.Create().freshDB(DBPATH, Fields)

 d.Create().comparisonTable(DBPATH, Fields, columns, new)


 the resulting query is:

 Create Table comparison ( Inventory_Number varchar,  Stock_Number 
 varchar,
 Location varchar,  Year varchar,  Make varchar,  Model varchar, 
 Part_Type
 varchar,  Mileage varchar,  Description varchar,  Interchange_Data 
 varchar,
 Condition varchar,  Price varchar)

 The query from comparisonTable gives me:
 Create Table comparison ( Create Table Common ( Inventory_Number 
 varchar
 varchar,   Stock_Number varchar varchar,   Location varchar varchar, 
 Year
 varchar varchar,   Make varchar varchar,   Model varchar varchar,
 Part_Type varchar varchar,   Mileage varchar varchar,   Description 
 varchar
 varchar,   Interchange_Data varchar varchar,   Condition varchar 
 varchar,
 Price varchar) varchar)

 If I restate the Fields input list before running comparisonTable, 
 my
 results are as expected (i.e. exactly like the first query, exept 
 the table
 is named comparison).

 I thought that when self was invoked for a function then that data 
 was
 acted on only for the scope of that function, yet the data is 
 changed in the
 original. Why?

 I'm sure this is so obvious that a 

Re: [Tutor] New to programming and Python

2006-10-26 Thread rolando
Jorge Azedo escreveu:
 Hi guys ( and gals )

 I'm totally new to the whole programming scene (I decided to enter it 
 for many reasons, one of which is the fact that I *want* to know how to 
 program my computer) and I decided to start out with Python. I'm reading 
 lots of tutorials on it, trying to understand how to work with it, but 
 for some reason or another, it all seems a great mess to me. Am I doing 
 something wrong? Also, can anyone give me any pointers on how to start 
 working with Python?

 Thanks in advance for any help you can give me
 - Jorge
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


   
Well, I´m also kind of new to the scene, so I still don´t know much.

But you can try this book that I´ve been reading:
Dive Into Python:
http://www.diveintopython.org/ (you can download the PDF from there, 
which I belive has already a group of exemples to see when using the book).

Also I´ve been reading the Python Wikibook that you can download from here:
http://en.wikibooks.org/wiki/Python (it still has a few parts missing 
but it can give you a few pointers).


(The part below problaby won´t make sense except if you are portuguese)

I don´t know what type of Netcabo you have, if you want I can upload the 
pdf to a national host to count national traffic :)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to programming and Python

2006-10-26 Thread Kent Johnson
Jorge Azedo wrote:
 Hi guys ( and gals )
 
 I'm totally new to the whole programming scene (I decided to enter it 
 for many reasons, one of which is the fact that I *want* to know how to 
 program my computer) and I decided to start out with Python. I'm reading 
 lots of tutorials on it, trying to understand how to work with it, but 
 for some reason or another, it all seems a great mess to me. Am I doing 
 something wrong? Also, can anyone give me any pointers on how to start 
 working with Python?

You don't say which tutorials you are reading; make sure it is a 
beginners tutorial. There are several listed here:
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Spend as much time trying things out as you do reading. Read a little, 
then start up the Python interpreter and type in the examples from the 
tutorial. Change them a little bit. Experiment. Don't go on in the 
reading until you are comfortable using what you have learned so far. 
You have to write, you can't learn to program just by reading.

Ask questions here if you get stuck. Show us what you tried, what 
happened, and what you expected to happen.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to programming and Python (Jorge Azedo)

2006-10-26 Thread paulino1
Parece que ja temos elementos para estabelecer uma comunidade portuguesa de
Python ;-)

Jorge,

Sugiro os tutoriais do site www.devshed.com. são simples e explicam bem o
significado do codigo!

Tb sou novo na programação, tenho ai 6 meses de Python.

A Caixa Magica promove cursos de Python em Lisboa, este ano houve ja 2 de 3
dias, penso que em Fevereiro e Junho.

Força, isto no inicio parece confuso, mas breve faz-se luz!

Paulino


Citando [EMAIL PROTECTED]:

2. New to programming and Python (Jorge Azedo)
7. Re: New to programming and Python (rolando)


 Date: Thu, 26 Oct 2006 09:44:41 +0100
 From: rolando [EMAIL PROTECTED]
 Subject: Re: [Tutor] New to programming and Python
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed

 Jorge Azedo escreveu:
  Hi guys ( and gals )
 
  I'm totally new to the whole programming scene (I decided to enter it
  for many reasons, one of which is the fact that I *want* to know how to
  program my computer) and I decided to start out with Python. I'm reading
  lots of tutorials on it, trying to understand how to work with it, but
  for some reason or another, it all seems a great mess to me. Am I doing
  something wrong? Also, can anyone give me any pointers on how to start
  working with Python?
 
  Thanks in advance for any help you can give me
  - Jorge
  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor
 
 
 
 Well, I?m also kind of new to the scene, so I still don?t know much.

 But you can try this book that I?ve been reading:
 Dive Into Python:
 http://www.diveintopython.org/ (you can download the PDF from there,
 which I belive has already a group of exemples to see when using the book).

 Also I?ve been reading the Python Wikibook that you can download from here:
 http://en.wikibooks.org/wiki/Python (it still has a few parts missing
 but it can give you a few pointers).


 (The part below problaby won?t make sense except if you are portuguese)

 I don?t know what type of Netcabo you have, if you want I can upload the
 pdf to a national host to count national traffic :)


 --

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


 End of Tutor Digest, Vol 32, Issue 110
 **



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] cgi form field and popup window

2006-10-26 Thread paulino1
I would like to have a popup window to show the possible options to a form field
(supplier ID) and that by clicking on the desired item, the field is
automatically filled. The options are a database field values.

I wonder if this is possible to do with python (point me some resources please),
or have I to deal with javascript ?

Many thanks,

Paulino

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to programming and Python (Jorge Azedo)

2006-10-26 Thread rolando
I tought that we could only speak English in here :D

Also I'm newer to python, about a mounth and a half since I started 
studing it.

[EMAIL PROTECTED] escreveu:
 Parece que ja temos elementos para estabelecer uma comunidade portuguesa de
 Python ;-)

 Jorge,

 Sugiro os tutoriais do site www.devshed.com. são simples e explicam bem o
 significado do codigo!

 Tb sou novo na programação, tenho ai 6 meses de Python.

 A Caixa Magica promove cursos de Python em Lisboa, este ano houve ja 2 de 3
 dias, penso que em Fevereiro e Junho.

 Força, isto no inicio parece confuso, mas breve faz-se luz!

 Paulino


 Citando [EMAIL PROTECTED]:

   
2. New to programming and Python (Jorge Azedo)
   
7. Re: New to programming and Python (rolando)


 Date: Thu, 26 Oct 2006 09:44:41 +0100
 From: rolando [EMAIL PROTECTED]
 Subject: Re: [Tutor] New to programming and Python
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed

 Jorge Azedo escreveu:
 
 Hi guys ( and gals )

 I'm totally new to the whole programming scene (I decided to enter it
 for many reasons, one of which is the fact that I *want* to know how to
 program my computer) and I decided to start out with Python. I'm reading
 lots of tutorials on it, trying to understand how to work with it, but
 for some reason or another, it all seems a great mess to me. Am I doing
 something wrong? Also, can anyone give me any pointers on how to start
 working with Python?

 Thanks in advance for any help you can give me
 - Jorge
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor



   
 Well, I?m also kind of new to the scene, so I still don?t know much.

 But you can try this book that I?ve been reading:
 Dive Into Python:
 http://www.diveintopython.org/ (you can download the PDF from there,
 which I belive has already a group of exemples to see when using the book).

 Also I?ve been reading the Python Wikibook that you can download from here:
 http://en.wikibooks.org/wiki/Python (it still has a few parts missing
 but it can give you a few pointers).


 (The part below problaby won?t make sense except if you are portuguese)

 I don?t know what type of Netcabo you have, if you want I can upload the
 pdf to a national host to count national traffic :)


 --

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


 End of Tutor Digest, Vol 32, Issue 110
 **

 


 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to programming and Python (Jorge Azedo)

2006-10-26 Thread Kent Johnson
rolando wrote:
 I tought that we could only speak English in here :D

English is preferred and increases your chances of getting an answer :-)

Kent

 
 Also I'm newer to python, about a mounth and a half since I started 
 studing it.
 
 [EMAIL PROTECTED] escreveu:
 Parece que ja temos elementos para estabelecer uma comunidade portuguesa de
 Python ;-)

 Jorge,

 Sugiro os tutoriais do site www.devshed.com. são simples e explicam bem o
 significado do codigo!

 Tb sou novo na programação, tenho ai 6 meses de Python.

 A Caixa Magica promove cursos de Python em Lisboa, este ano houve ja 2 de 3
 dias, penso que em Fevereiro e Junho.

 Força, isto no inicio parece confuso, mas breve faz-se luz!

 Paulino


 Citando [EMAIL PROTECTED]:

   
2. New to programming and Python (Jorge Azedo)
   
7. Re: New to programming and Python (rolando)


 Date: Thu, 26 Oct 2006 09:44:41 +0100
 From: rolando [EMAIL PROTECTED]
 Subject: Re: [Tutor] New to programming and Python
 To: tutor@python.org
 Message-ID: [EMAIL PROTECTED]
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed

 Jorge Azedo escreveu:
 
 Hi guys ( and gals )

 I'm totally new to the whole programming scene (I decided to enter it
 for many reasons, one of which is the fact that I *want* to know how to
 program my computer) and I decided to start out with Python. I'm reading
 lots of tutorials on it, trying to understand how to work with it, but
 for some reason or another, it all seems a great mess to me. Am I doing
 something wrong? Also, can anyone give me any pointers on how to start
 working with Python?

 Thanks in advance for any help you can give me
 - Jorge
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor



   
 Well, I?m also kind of new to the scene, so I still don?t know much.

 But you can try this book that I?ve been reading:
 Dive Into Python:
 http://www.diveintopython.org/ (you can download the PDF from there,
 which I belive has already a group of exemples to see when using the book).

 Also I?ve been reading the Python Wikibook that you can download from here:
 http://en.wikibooks.org/wiki/Python (it still has a few parts missing
 but it can give you a few pointers).


 (The part below problaby won?t make sense except if you are portuguese)

 I don?t know what type of Netcabo you have, if you want I can upload the
 pdf to a national host to count national traffic :)


 --

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


 End of Tutor Digest, Vol 32, Issue 110
 **

 

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


   
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] cgi form field and popup window

2006-10-26 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
 I would like to have a popup window to show the possible options to a form 
 field
 (supplier ID) and that by clicking on the desired item, the field is
 automatically filled. The options are a database field values.
 
 I wonder if this is possible to do with python (point me some resources 
 please),
 or have I to deal with javascript ?

The browser-side programming has to be in javascript. The server-side 
program that sends the javascript and the list of options to the browser 
can be in Python.

You might want to look at the TurboGears widget set which provides many 
browser-side widgets that are configured from Python. Widgets are part 
of TG 1.0. Unfortunately the docs seem to be a bit thin and sorting it 
all out is probably not a beginner project.
http://www.turbogears.org/
http://tgwidgets.toscat.net/

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python and config files?

2006-10-26 Thread Basil Shubin
Hi, friends!

Is there exist a module for working with config file? Simple enough, 
just read/write config file (like for most unix app), determine where 
row is contain config words with it's parameters (like 
'config_word=parameter') or it's a comments (#)?

-- 
Basil Shubin
Freelance Software Developer
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me.. problem in building calculator application

2006-10-26 Thread Kent Johnson
Asrarahmed Kadri wrote:
  
  
 Hi folks,
  
 I am trying to build a rudimentary calculator application using Tkinter...
  
 I have used one Entry widget for displaying the input and output data.
 All this is put in a class.
 The porblem is when I click teh '+' button, teh callback function is 
 called but it gives me an error message: AttributeError: 'str' object 
 has no attribute 'get'
  
 I have defined a variable, self.str1 = StringVar() to bind it with the 
 entry widget and I am calling the function when + is clciked. The 
 function is as follows:
  
self.buttonPlu = Button(self.container3,text='+',command = lambda: 
 self.oprClick('+',*self.str1.get()))*
  
 Can't we use the get() method to access the contents of the entry 
 widget. Is there any other way to access the contents of the entry 
 widget???

The error message is saying that the object you are calling get() on is 
a string, not a StringVar. Please post complete code and the complete 
error message including traceback.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python and config files?

2006-10-26 Thread Kent Johnson
Basil Shubin wrote:
 Hi, friends!
 
 Is there exist a module for working with config file? Simple enough, 
 just read/write config file (like for most unix app), determine where 
 row is contain config words with it's parameters (like 
 'config_word=parameter') or it's a comments (#)?

Take a look at the ConfigParser module:
http://docs.python.org/lib/module-ConfigParser.html

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Code with traceback...Re: Help me.. problem in building calculator application

2006-10-26 Thread Asrarahmed Kadri

Traceback is as under:

Traceback (most recent call last): File C:\python\lib\lib-tk\Tkinter.py, line 1345, in __call__ return self.func(*args) File calculator_version2.py, line 105, in lambda
 self.buttonPlu = Button(self.container3,text='+',command = lambda: self.oprClick('+',self.str1.get()))AttributeError: 'str' object has no attribute 'get'
from Tkinter import *import string
class Mycalc:
 def buttonClick(self,str2,str3): if str2 == .: if self.dotFlag: pass else: self.dotFlag = True 
self.operand = str3 + str2 else: self.operand = str3 + str2 
 def oprClick(self,str11,oprt):
 self.dotFlag = False  if oprt == +: if self.result == 0: self.result = str11 else: self.result
 = string.atof(self.result) + string.atof(str11)elif oprt == -:
 if self.result == 0: self.result = str11 else: self.result = string.atof(self.result) - string.atof(str11) 
 elif str2 == *: if self.result == 0: self.result = str11 else: self.result = string.atof(self.result) * string.atof(str11)
   elif str2 == /: if self.result == 0: self.result = str11 else: self.result = string.atof
(self.result) / string.atof(str11) 
 self.entryWidget.icursor(0) self.entryWidget.delete(0,END) self.entryWidget.insert(INSERT,self.result)
 
 
 def __init__(self,master):
 self.container1 = Frame(master,width=200,height=200,background='') self.container1.pack() self.str1 = StringVar() self.str1 = 0 self.entryWidget = Entry(
self.container1,textvariable=self.str1)  self.entryWidget.pack(side=TOP)
 self.container2 = Frame(master,background='') self.container2.pack()
 self.container3 = Frame(master,background='') self.container3.pack()
 self.operand = 0 self.result = 0 self.dotFlag = False
 def make_buttons(self): self.buttonOne = Button(self.container2,text='1') self.buttonOne.grid(row=0,column=0) self.buttonTwo = Button(self.container2,text='2') self.buttonTwo.grid
(row=0,column=1) self.buttonThree = Button(self.container2,text='3') self.buttonThree.grid(row=0,column=2) self.buttonFour = Button(self.container2,text='4') self.buttonFour.grid
(row=1,column=0) self.buttonFive = Button(self.container2,text='5') self.buttonFive.grid(row=1,column=1) self.buttonSix = Button(self.container2,text='6') self.buttonSix.grid(row=1,column=2)
 self.buttonSeven = Button(self.container2,text='7') self.buttonSeven.grid(row=2,column=0) self.buttonEight = Button(self.container2,text='8') self.buttonEight.grid(row=2,column=1)
 self.buttonNine = Button(self.container2,text='9') self.buttonNine.grid(row=2,column=2) self.buttonZero = Button(self.container2,text='0') self.buttonZero.grid(row=3,column=0)
 self.buttonPoint = Button(self.container2,text='.') self.buttonPoint.grid(row=3,column=1)
 def make_oprbuttons(self):  self.buttonPlu = Button(self.container3,text='+',command = lambda: self.oprClick('+',self.str1.get())) self.buttonPlu.pack(side=LEFT)
 self.buttonMin = Button(self.container3,text='-',command = lambda: self.oprClick('-',self.str1.get())) self.buttonMin.pack(side=LEFT)
 self.buttonMul = Button(self.container3,text='*',command = lambda: self.oprClick('*',self.str1.get())) self.buttonMul.pack(side=LEFT)
 self.buttonDiv = Button(self.container3,text='/',command = lambda: self.oprClick('/',self.str1.get())) self.buttonDiv.pack(side=LEFT)
 self.buttonEqu = Button(self.container3,text='=',command = lambda: self.oprClick('=',self.str1.get())) self.buttonEqu.pack(side=LEFT)   
root = Tk()mycalc = Mycalc(root)mycalc.make_buttons()mycalc.make_oprbuttons()
root.mainloop()
On 10/26/06, Kent Johnson [EMAIL PROTECTED] wrote:
Asrarahmed Kadri wrote: Hi folks, I am trying to build a rudimentary calculator application using Tkinter...
 I have used one Entry widget for displaying the input and output data. All this is put in a class. The porblem is when I click teh '+' button, teh callback function is called but it gives me an error message: AttributeError: 'str' object
 has no attribute 'get' I have defined a variable, self.str1 = StringVar() to bind it with the entry widget and I am calling the function when + is clciked. The function is as follows:
self.buttonPlu = Button(self.container3,text='+',command = lambda: self.oprClick('+',*self.str1.get()))* Can't we use the get() method to access the contents of the entry widget. Is there any other way to access the contents of the entry
 widget???The error message is saying that the object you are calling get() on isa string, not a StringVar. Please post complete code and the completeerror message including traceback.Kent
___Tutor maillist-Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
-- To HIM you shall return. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code with traceback...Re: Help me.. problem in building calculator application

2006-10-26 Thread Kent Johnson
Immediately following the line
 self.str1 = StringVar()

you have
 self.str1 = 0

at which point self.str1 is a string, not a StringVar. Maybe you mean
self.str1.set(0)
?

Kent

Asrarahmed Kadri wrote:
  
 Traceback is as under:
  
 Traceback (most recent call last):
   File C:\python\lib\lib-tk\Tkinter.py, line 1345, in __call__
 return self.func(*args)
   File calculator_version2.py, line 105, in lambda
 self.buttonPlu = Button(self.container3,text='+',command = lambda: 
 self.oprC
 lick('+',self.str1.get()))
 AttributeError: 'str' object has no attribute 'get'
 
  
 
 from Tkinter import *
 import string
 
 
 class Mycalc:
 
 def buttonClick(self,str2,str3):
 if str2 == .:
 if self.dotFlag:
 pass
 else:
 self.dotFlag = True
 self.operand = str3 + str2
 else:
 self.operand = str3 + str2

 
 
 def oprClick(self,str11,oprt):
 
 self.dotFlag = False

 if oprt == +:
 if self.result == 0:
 self.result = str11
 else:
 self.result = string.atof(self.result) + string.atof(str11)



 elif oprt == -: 
 if self.result == 0:
 self.result = str11
 else:
 self.result = string.atof(self.result) - string.atof(str11)

 
 elif str2 == *:
 if self.result == 0:
 self.result = str11
 else:
 self.result = string.atof(self.result) * string.atof(str11)


 elif str2 == /:
 if self.result == 0:
 self.result = str11
 else:
 self.result = string.atof (self.result) / string.atof(str11)

 
 self.entryWidget.icursor(0)
 self.entryWidget.delete(0,END)
 self.entryWidget.insert(INSERT,self.result)
 

 

 
 def __init__(self,master):
 
 self.container1 = 
 Frame(master,width=200,height=200,background='brown')
 self.container1.pack()
 self.str1 = StringVar()
 self.str1 = 0
 self.entryWidget = Entry( 
 self.container1,textvariable=self.str1)   
 self.entryWidget.pack(side=TOP)
 
 self.container2 = Frame(master,background='cyan')
 self.container2.pack()
 
 self.container3 = Frame(master,background='grey')
 self.container3.pack()
 
 self.operand = 0
 self.result = 0
 self.dotFlag = False
 
 def make_buttons(self):
 self.buttonOne = Button(self.container2,text='1')
 self.buttonOne.grid(row=0,column=0)
 self.buttonTwo = Button(self.container2,text='2')
 self.buttonTwo.grid (row=0,column=1)
 self.buttonThree = Button(self.container2,text='3')
 self.buttonThree.grid(row=0,column=2)
 self.buttonFour = Button(self.container2,text='4')
 self.buttonFour.grid (row=1,column=0)
 self.buttonFive = Button(self.container2,text='5')
 self.buttonFive.grid(row=1,column=1)
 self.buttonSix = Button(self.container2,text='6')
 self.buttonSix.grid(row=1,column=2)
 self.buttonSeven = Button(self.container2,text='7')
 self.buttonSeven.grid(row=2,column=0)
 self.buttonEight = Button(self.container2,text='8')
 self.buttonEight.grid(row=2,column=1)
 self.buttonNine = Button(self.container2,text='9')
 self.buttonNine.grid(row=2,column=2)
 self.buttonZero = Button(self.container2,text='0')
 self.buttonZero.grid(row=3,column=0)
 self.buttonPoint = Button(self.container2,text='.')
 self.buttonPoint.grid(row=3,column=1)
 
 def make_oprbuttons(self):
   
 self.buttonPlu = Button(self.container3,text='+',command = 
 lambda: self.oprClick('+',self.str1.get()))
 self.buttonPlu.pack(side=LEFT)
 
 self.buttonMin = Button(self.container3,text='-',command = 
 lambda: self.oprClick('-',self.str1.get()))
 self.buttonMin.pack(side=LEFT)
 
 self.buttonMul = Button(self.container3,text='*',command = 
 lambda: self.oprClick('*',self.str1.get()))
 self.buttonMul.pack(side=LEFT)
 
 self.buttonDiv = Button(self.container3,text='/',command = 
 lambda: self.oprClick('/',self.str1.get()))
 self.buttonDiv.pack(side=LEFT)
 
 self.buttonEqu = Button(self.container3,text='=',command = 
 lambda: self.oprClick('=',self.str1.get()))
 self.buttonEqu.pack(side=LEFT)   


 
 root = Tk()
 mycalc = Mycalc(root)
 mycalc.make_buttons()
 mycalc.make_oprbuttons()
 
 root.mainloop()
 
 
 On 10/26/06, *Kent Johnson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] 
 wrote:
 
 Asrarahmed Kadri wrote:
 

Re: [Tutor] Python and config files?

2006-10-26 Thread Andreas Kostyrka
Actually, for Unixish config files, shlex seems the better module.

Andreas

Am Donnerstag, den 26.10.2006, 09:24 -0400 schrieb Kent Johnson:
 Basil Shubin wrote:
  Hi, friends!
  
  Is there exist a module for working with config file? Simple enough, 
  just read/write config file (like for most unix app), determine where 
  row is contain config words with it's parameters (like 
  'config_word=parameter') or it's a comments (#)?
 
 Take a look at the ConfigParser module:
 http://docs.python.org/lib/module-ConfigParser.html
 
 Kent
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code with traceback...Re: Help me.. problem in buildingcalculator application

2006-10-26 Thread Alan Gauld
Kent has already pointed out the problem of overwriting 
the stringvar, but there is another problem too:

def oprClick(self,str11,oprt):


self.buttonPlu = Button(self.container3,text='+',
command = lambda: self.oprClick('+',self.str1.get()))


Check the position of the operator string in the definition versus 
the lambda call

Alan G.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] problem importing class

2006-10-26 Thread shawn bright
hey therei have written a module called site.pyin the file i have this:import DbConnectorimport sensorclass Site(object): site object  def __init__(self, id):
 self.id = id self.con = DbConnector.DbConnector()  id = str(self.id) stats = self.con.getOne(selct `group_id`, `name`, `ivr_code`, \
 `site_type`, `notes`, `sensor_id` \ from `sites` where `id` = '+str(id)+' ) self.group_id = stats[0] self.name = stats[1]
 self.ivr_code = stats[2] self.site_type = stats[3] self.notes = stats[4] self.sensor_id = stats[5]  def get_sensor(self): self.sensor = sensor.Sensor
(self.sensor_id) return self.sensorok, in the program i am trying to run i have these lines:import sitenew_site = site.Site(id_number)and this is what my output is:Traceback (most recent call last):
 File ./new_camp.py, line 2014, in on_site_tree_view_row_activated acitve_site = site.Site(id_number)AttributeError: 'module' object has no attribute 'Site'It looks like the same code works for a different module i have ;
i don't get what i could be missing here, any tips?if you have read this far, thanks for your time.sk
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Simon Brunning
On 10/26/06, shawn bright [EMAIL PROTECTED] wrote:
 import site

Try this for a clue:

print site.__file__

-- 
Cheers,
Simon B
[EMAIL PROTECTED]
http://www.brunningonline.net/simon/blog/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Jason Massey
Shawn,Python already has a module called site. From http://docs.python.org/lib/module-site.html :This module is automatically imported during initialization.

The automatic import can be suppressed using the interpreter's
-S option.


Importing this module will append site-specific paths to the module
search path.Easiest thing would be to rename your module.On 10/26/06, shawn bright [EMAIL PROTECTED]
 wrote:hey therei have written a module called site.pyin the file i have this:
import DbConnectorimport sensorclass Site(object): site object  def __init__(self, id):
 self.id = id self.con = DbConnector.DbConnector()  id = str(
self.id) stats = self.con.getOne(selct `group_id`, `name`, `ivr_code`, \
 `site_type`, `notes`, `sensor_id` \ from `sites` where `id` = '+str(id)+' ) self.group_id = stats[0] 
self.name = stats[1]
 self.ivr_code = stats[2] self.site_type = stats[3] self.notes = stats[4] self.sensor_id = stats[5]  def get_sensor(self): self.sensor = sensor.Sensor

(self.sensor_id) return self.sensorok, in the program i am trying to run i have these lines:import sitenew_site = site.Site(id_number)and this is what my output is:Traceback (most recent call last):
 File ./new_camp.py, line 2014, in on_site_tree_view_row_activated acitve_site = site.Site(id_number)AttributeError: 'module' object has no attribute 'Site'It looks like the same code works for a different module i have ;
i don't get what i could be missing here, any tips?if you have read this far, thanks for your time.sk

___Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Kent Johnson
shawn bright wrote:
 hey there
 
 i have written a module called site.py

Use a different name, there is a library module called site that is 
automatically imported when Python starts up. So when you 'import site' 
you are getting the already-imported library module.

Kent

 in the file i have this:
 
 import DbConnector
 import sensor
 
 class Site(object):
 site object

 def __init__(self, id):
 self.id http://self.id = id
 self.con = DbConnector.DbConnector()   
 id = str(self.id http://self.id)
 stats = self.con.getOne(selct `group_id`, `name`, `ivr_code`, \
 `site_type`, `notes`, `sensor_id` \
 from `sites` where `id` = '+str(id)+' )
 self.group_id = stats[0]
 self.name http://self.name = stats[1]
 self.ivr_code = stats[2]
 self.site_type = stats[3]
 self.notes = stats[4]
 self.sensor_id = stats[5]

 def get_sensor(self):
 self.sensor = sensor.Sensor (self.sensor_id)
 return self.sensor
 
 ok, in the program i am trying to run i have these lines:
 import site
 new_site = site.Site(id_number)
 
 and this is what my output is:
 Traceback (most recent call last):
   File ./new_camp.py, line 2014, in on_site_tree_view_row_activated
 acitve_site = site.Site(id_number)
 AttributeError: 'module' object has no attribute 'Site'
 
 It looks like the same code works for a different module i have ;
 i don't get what i could be missing here, any tips?
 
 if you have read this far, thanks for your time.
 sk
 
 
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Danny Yoo

Hi Shawn,

It looks like people have identified the problem, that your site.py module 
isn't being found because it conflicts with something from Python's 
Standard Library.

This isn't the first time this kind of problem has hit people.  This 
problem is well known and is the subject of a Python Enhancement Proposal 
(PEP 328).  If you're using Python 2.5, you can use a new feature called 
'absolute_import' to avoid the import problem.  See:

 http://docs.python.org/whatsnew/pep-328.html

for more details.

In the meantime, if you can't depend on using Python 2.5 yet, just rename 
your site.py module to something else to avoid the collision between 
Python's own site.py module and your own.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to programming and Python

2006-10-26 Thread Jorge Azedo
First off, I don't know if I'm doing this right, I've never used a 
mailing list before, so I'm not sure if I'm sending this to the right place.

Thanks for all the help you guys gave me ( e vocês também pessoal, é bom 
saber que há pessoal português por aqui :-) ). I'll try and read as many 
tutorials as I can and start to work on some programs of my own. Let's 
hope I can make something useful or fun.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread shawn bright
Hey thanks for the help, gents,i renamed site.py to site_obj.py and my import and statement.everything is working now. Thank you guys very very much.shawnOn 10/26/06, 
Danny Yoo [EMAIL PROTECTED] wrote:
Hi Shawn,It looks like people have identified the problem, that your site.py moduleisn't being found because it conflicts with something from Python'sStandard Library.This isn't the first time this kind of problem has hit people.This
problem is well known and is the subject of a Python Enhancement Proposal(PEP 328).If you're using Python 2.5, you can use a new feature called'absolute_import' to avoid the import problem.See: 
http://docs.python.org/whatsnew/pep-328.htmlfor more details.In the meantime, if you can't depend on using Python 2.5 yet, just renameyour site.py
 module to something else to avoid the collision betweenPython's own site.py module and your own.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to programming and Python

2006-10-26 Thread Danny Yoo



On Thu, 26 Oct 2006, Jorge Azedo wrote:

First off, I don't know if I'm doing this right, I've never used a 
mailing list before, so I'm not sure if I'm sending this to the right 
place.


Thanks for all the help you guys gave me ( e vocês também pessoal, é bom 
saber que há pessoal português por aqui :-) ). I'll try and read as many 
tutorials as I can and start to work on some programs of my own. Let's 
hope I can make something useful or fun.



I'd second the recommendation to Dive into Python; it has great practical 
examples.  Feel free to ask questions on the list; we're all here to help 
each other.



As a non-Python tangent: if you'd like to read a textbook on learning 
introductory programming, you might be interested in How to Design 
Programs:


http://www.htdp.org/

The first few chapters might feel a little slow, and the language used is 
not Python.  But even with that, the material in the book is top notch, 
better than most introductory textbooks I've seen.  If I were to point 
someone new to computing toward learning to program, this is the book I'd 
be oblidged to recommend.



Good luck to you!___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem importing class

2006-10-26 Thread Kent Johnson
Danny Yoo wrote:
 Hi Shawn,
 
 It looks like people have identified the problem, that your site.py module 
 isn't being found because it conflicts with something from Python's 
 Standard Library.
 
 This isn't the first time this kind of problem has hit people.  This 
 problem is well known and is the subject of a Python Enhancement Proposal 
 (PEP 328).  If you're using Python 2.5, you can use a new feature called 
 'absolute_import' to avoid the import problem.  See:
 
  http://docs.python.org/whatsnew/pep-328.html
 
 for more details.

Hmm, I don't see how that would help since AFAICT shawn's site.py is not 
in a package. Or maybe I don't understand what you are suggesting?

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Self, Scopes and my unbelievable muddleheadedness.

2006-10-26 Thread doug shawhan
On 10/25/06, Luke Paireepinart [EMAIL PROTECTED] wrote:
 I'm sure this is so obvious that a crack-addled tapeworm head down in a bucket of stupid could understand it, unfortunately, I'm not quite at that level today. Sorry.Uh, I don't understand why you're passing Fields to the functions but
then putting the value in self.Fields...but we'll ignore that for now :)
http://www.penzilla.net/tutorials/python/modules/

I explain my confusion more coherently in my reply to Mr. Gald. :-) 
The problem it sounds like you're having is that you think that passinglists to functions copies them,
when in fact it just creates a reference to them.Take this, for example:

Close! I thought that self would somehow automagically create a copy of the list, leaving a global intact. I am disillusioned.
  def append_five(alist):alist.append(5)  a = [1,2,3,4]
  append_five(a)  a[1, 2, 3, 4, 5]  append_five(a)  a[1, 2, 3, 4, 5, 5]

Which answers my question why is my list of dictionaries turning in to
a list with a dictionary with a list of dictionaries therein! :-)
Other than that,Maybe you think self is used for something other than what it's intended
to be used for...but I can't be sure.
Again, see my reply to Mr. Gald. I am starting to get my head around
self. I searched usenet for explanations, apparently I'm not the only
one. :-). It seems to be pretty simple, for most. I'm reminded of
learning to solve for polynomials or diagram sentences: It's elementary
to some and completely obtuse to others, depending on mindset and
experience.
I'm sure someone else can give you a good example.I, however, have to run.
HTH,-Luke

Thanks! I appreciate it! 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Self, Scopes and my unbelievable muddleheadedness.

2006-10-26 Thread doug shawhan
-- Forwarded message --From: doug shawhan [EMAIL PROTECTED]Date: Oct 26, 2006 1:45 PM
Subject: Re: [Tutor] Self, Scopes and my unbelievable muddleheadedness.To: Alan Gauld [EMAIL PROTECTED]
On 10/26/06, Alan Gauld [EMAIL PROTECTED] wrote:

doug shawhan [EMAIL PROTECTED] wrote I'm having a rather difficult time understanding the proper use of
 self.Doug, I think you may be having a more fundamental problem.
Looking at your code at the most superficial level it seemsyou may not understand objects.
I've always been confused by much of the OOP terminology. I'm about to
go off on a tangent below regarding your explaination. I think it is
helping me, but I want to be sure. Bear with me. :-)

My original question should have been: 

Why is this dictionary coming back weird? The dictionary was just a
placeholder for a future .ini file to be read with ConfigParser. In
playing around with different ideas. I thought I'd like to put each
particular set of activities into modules anyway, so let's just figure
out how that might work. 

I ran onto the problem of the dictionary being changed globally,
when I thought that use of self meant that any changes to that data
were limited to the scope of the method in that particular class (and
it's resultant object), thereby insulating it from other methods in the
same class and other objects which inherit from the same class
(Don't reply to this yet! :-)

I'm sure that I have simply thought that my global declaration was not
going to be changed in place by misunderstanding what self means. My
confusion stemmed from just how weird the change appeard to be. I.e.
But I just looked at the dang thing and it was what I expected! Where
dem extra parentheis come from? Huh?

I apologize for my incoherence in asking the question. I was in the
middle of trying out different ideas and ran onto this weirdness and
could not let it go. I should taken the time to write a generic
example, but frankly, I was frustrated and was not thinking about how
best to frame the question. It was impolite of me to subject the list
to my half-baked meanderings. Again, sorry. You guys have helped so
much and you deserve a little more effort on my part.

So, fortified with 8 hours of sleep and 15mg of Adderall, here we go!


Classes are used to generate objects. Objects represent things.By logical deduction objects correspond to nouns in language.
Objects do things when they receive messages (Possiblyfrom other objects). Messages cause methods to be invoked,methods therefore correspond to verbs in language.

I felt like I was being told an integer is a counting number here. ;-)
Looking at your code we find:class Create: # a verbdef freshDB(self, DBPATH, Fields): # a noun
def comparisonTable(self, DBPATH, Fields, columns, mode): #a nounIn other words you have the concept inside out.you are trying to create Create objects and call Table and Databasemethods. It doesn't make sense.
Normally we would expect to see DB and Table objects to whichyou send create messages.
I was even more confused. Were you objecting to my nomenclature? 

So I thought, If I named the class Steve and called the methods createFreshDB() and createComparisonTable() so it would read:

Steve, create a fresh database which includes tables named for the
keys and columns named for the values in this dictionary I'm handing
you.

Steve, create a comparison table just like above, but with a different key.

which would be followed (eventually, if I hadn't gotten bogged down) by

Steve, tell gadfly to collate those two tables I just had you make and put them in another table so I can generate a report

Would that make any difference?

No especially in how the code was written (it was still ugly and
half-baked, and showing tons of bad habits), but I did suddenly realize
what you meant by noun.

I have been treating objects like a corporate sales droid treats nouns.

I've been Gifting and Tableing the objects TheGift and TheTable
in my mind. (I fear I've also been Impacting the
method impact() without asking Steve.Please() first,
(I.E. not quite understanding why my classes within the modules were
not showing up like I expected, but that's another story.)

I've not been thinking of an object as a verb, rather more like a gerund. Still wrong, but a different kind of wrong.
Now if you think of the objects as verbs then self is a confusing
concept.
But if you think of the objects as nouns then self is simply areferenceto the object instance in question, a particular database or table.
Which leads me back to the why is it there? question I saw on the list recently.

If self is simply a reference to the object instance in
question, then why does one have to basically mirror the variable at
the top of the class?

The example I was working from is at: http://www.penzilla.net/tutorials/python/modules/


# Intro To Python:  Modules# book.pyClass:  Book( title, author, keywords )Each book object takes a title, optional author, and optional 

[Tutor] Mailing list question

2006-10-26 Thread Jorge Azedo
Not so much a question about Python, but here goes:

How do I reply to a specific thread in the mailing list? If I place   
Re:bla bla   in the subject line, I notice that I start a new thread, 
I don't continue one that already exists. How do I go about doing this?
Thanks for any info
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mailing list question

2006-10-26 Thread Luke Paireepinart
Jorge Azedo wrote:
 Not so much a question about Python, but here goes:

 How do I reply to a specific thread in the mailing list? If I place   
 Re:bla bla   in the subject line, I notice that I start a new thread, 
 I don't continue one that already exists. How do I go about doing this?
 Thanks for any info

What e-mail client are you using?  Most have a reply-to-all button you 
can use so that you won't have to start a new message to reply to someone.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mailing list question

2006-10-26 Thread Danny Yoo


 Not so much a question about Python, but here goes:

 How do I reply to a specific thread in the mailing list? If I place 
 Re:bla bla  in the subject line, I notice that I start a new thread, 
 I don't continue one that already exists. How do I go about doing this? 
 Thanks for any info

 What e-mail client are you using?  Most have a reply-to-all button you 
 can use so that you won't have to start a new message to reply to 
 someone.

There's a little bit of extra information that Mailman uses to detect 
threads.  The archives don't cluster messages based on the subject line, 
but instead use a specific header value whose name I'm completely 
forgetting right now... *grin* (I think it's the 'In-Reply-To' header 
line.)  Your email client should be the one responsible for maintaining 
that threading information.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help me.. problem in building calculator application

2006-10-26 Thread Paulino


 Message: 7
 Date: Thu, 26 Oct 2006 13:31:37 +0100
 From: Asrarahmed Kadri [EMAIL PROTECTED]
 Subject: [Tutor] Help me.. problem in building calculator application
 To: pythontutor tutor@python.org
 Message-ID:
   [EMAIL PROTECTED]
 Content-Type: text/plain; charset=iso-8859-1

 Hi folks,

 I am trying to build a rudimentary calculator application using Tkinter...

   
Hi, Asrar!

Try this article instead:

http://www.devshed.com/c/a/Python/Designing-a-Calculator-in-wxPython/


Paulino

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mailing list question

2006-10-26 Thread Jorge Azedo
Danny Yoo wrote:


 Not so much a question about Python, but here goes:

 How do I reply to a specific thread in the mailing list? If I place 
 Re:bla bla  in the subject line, I notice that I start a new 
 thread, I don't continue one that already exists. How do I go about 
 doing this? Thanks for any info

 What e-mail client are you using?  Most have a reply-to-all button 
 you can use so that you won't have to start a new message to reply to 
 someone.

 There's a little bit of extra information that Mailman uses to detect 
 threads.  The archives don't cluster messages based on the subject 
 line, but instead use a specific header value whose name I'm 
 completely forgetting right now... *grin* (I think it's the 
 'In-Reply-To' header line.)  Your email client should be the one 
 responsible for maintaining that threading information.

I'm trying to use the Reply All button on my mail client (I use 
Thunderbird, by the way) like you guys suggested. Let's see if it works  
:-P
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Self, Scopes and my unbelievable muddleheadedness.

2006-10-26 Thread Luke Paireepinart
doug shawhan wrote:
 [lots of stuff!]
You seem to have a penchant for choosing entertaining, albeit confusing, 
words to explain what you're thinking.
I have a similar inclination; however, I'll attempt to avoid this so 
that my explanation will make more sense :)

Now we begin.

First, an example, then an explanation.

#given the following definition...
class AClass(object):
aClassVariable = 'Hello'
def aMethod(self):
print self.hello
def __init__(self):
self.hello = Hi!

  
  AClass.aClassVariable
'Hello'
  anInstance = AClass()
  AClass.hello
Traceback (most recent call last):
  File pyshell#9, line 1, in ?
AClass.hello
AttributeError: type object 'AClass' has no attribute 'hello'
  anInstance.hello
'Hi!'
  AClass.aMethod()
Traceback (most recent call last):
  File pyshell#13, line 1, in ?
AClass.aMethod()
TypeError: unbound method aMethod() must be called with AClass instance 
as first argument (got nothing instead)
  anInstance.aMethod()
Hi!
  anInstance.aClassVariable
'Hello'
  anInstance.aClassVariable = 'Ho!'
  anInstance.aClassVariable
'Ho!'
  AClass.aClassVariable
'Hello'

# end of example.

Believe it or not, this short example should tell you everything you 
need to know about objects ;)
However, that doesn't mean it makes any sense!
So I will try to explain it.
I had a huge problem with Objects when I first ran into them, but now 
they're easy to understand.
Hope I can do the same for you.


Okay, take this example:

class aClass(object):
aVariable = 'hi'

What the interpreter does is creates a variable called aClass that 
points to this object.
Yes, class definitions are objects.
  aClass
class '__main__.aClass'

Since the variable that we defined in the aClass object was defined in 
the main scope of the class,
and not in a class method (it wasn't defined inside a function that was 
in the class) it's global to the class object.
so...
  aClass.aVariable
'hi'

we don't even need to make an instance of the class to access this variable.
We can make an instance if we want...
  b = aClass()
  b.aVariable
'hi'

But there's really no point, is there?

Now take this class.

class aClass(object):
value = 'hi'
def __init__(self):
  self.value = 'hello'

Because the 'self.value' is only defined inside of the '__init__' 
method, it's not global to the class definition,
but the 'value' variable is global to the class definition.

  aClass.value
'hi'

But when we make an instance of this class,

  instance = aClass()

The init method is called,

  instance.value
'hello'

which replaces the class-global 'value' variable with the  
instance-global 'self.variable' version.

One thing it's important to note is this:

#given these two definitions
class aClass(object):
aVar = 1
aList = [1,2,3]

b = aClass()

#we get this.
  aClass.aVar
1
  aClass.aList
[1, 2, 3]
  b.aVar
1
  b.aList
[1, 2, 3]
  b.aVar = 23
  b.aList.append(4)
  b.aVar
23
  b.aList
[1, 2, 3, 4]
  aClass.aVar
1
  aClass.aList
[1, 2, 3, 4]

Notice how changing the value of b.aList changes the value of 
aClass.aList?  So any object you make after this,
  a = aClass()
  a.aList
[1, 2, 3, 4]

will have the modified value of aList,
  a.aVar
1

but not the modified version of aVar.

However, if you were to change aVar in the class definition (and not in 
an instance of the class)
  aClass.aVar = 2
  a.aVar
2

then the value of aVar is changed.
This is interesting and a little scary, since changing a list in an 
instance can change this list in all class instances,
but not if it's an integer or a tuple or something else.
This is a good reason to use 'self'.

That way, each class has its own set of data that can't be messed with.

You say in your e-mail

Does it follow that if one does not prepend self to the variable, any 
change will affect all instances of an object (say in a threading 
situation)? That is the only thing that seems to make sense to me, 
otherwise self would just be silly.


No, this is not the point of self at all.

Imagine that you have this class.

class Poem(object):
def __init__(self,title,text): #args: string, list of strings
   self.title = title
   self.text = text
def readPoem(self):
   print Poem: %s % self.title
   for line in self.text:
  print line
def reversePoem(self):
self.text.reverse()

Now say you want two poems...
  aolHaiku = Poem('Unwanted AOL CD Haiku',['A shiny let-down,','AOL 
CDs are good','Only for artwork'])
  windowsHaiku = Poem('Microsoft Haiku',['Yesterday it 
worked.','Today it is not working.','Windows is like that.'])

And you want to read them.

  aolHaiku.readPoem()
Poem: Unwanted AOL CD Haiku
A shiny let-down,
AOL CDs are good
Only for artwork

  windowsHaiku.readPoem()
Poem: Microsoft Haiku
Yesterday it worked.
Today it is not working.
Windows is like that.

So it doesn't matter to us what's acually _in_ each class instance, we 
know if we call the readPoem on the Poem object, it'll do 

Re: [Tutor] Mailing list question

2006-10-26 Thread Luke Paireepinart


 I'm trying to use the Reply All button on my mail client (I use 
 Thunderbird, by the way) like you guys suggested. Let's see if it 
 works  :-P

That's what I use (Reply All and Thunderbird) and whenever I check my 
gmail account from the website, the messages I write appear threaded 
correctly.
Well, that's assuming threading is the same as gmail conversations.
Hope that helps
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Self, Scopes and my unbelievable muddleheadedness.

2006-10-26 Thread Alan Gauld
 I ran onto the problem of the dictionary being changed globally, 
 when I
 thought that use of self meant that any changes to that data were 
 limited
 to the scope of the method in that particular class

No, but you know that now :-)

Let me try an alternative explanaytion for self, since we already had
a discussion about self recently...

self is simply a pointer to the object whose method is being called.
Lets try writing some OO code without using Pythons OOP
mechanism and see if we can spot why we need self (whether
implicitly or explicitly).

First we need a way to create objects since we dont have
classes...so we write a function that returns a dictionary.
We'll make it simple and have a message class that stores
a string and has a display method.

def display(aMessage):
print aMessage['text']

def Message(theText):
theObject = {}
theObject['text'] = theText
return theObject

Now we create a couple of instances and display them:

m1 = createMessage('Good morning')
m2 = createMessage('Hello world')

display(m1)
display(m2)

Notice how we need to pass the instance that we want
to display into the display function?

Now lets write the same thing using real OOP:

class Message:
def __init__(self, theText):
self.text = theText
def display(self):
print self.text

m1 = Message('Good morning')
m2 = Message('Hello world')
Message.display(m1)
Message.display(m2)

If you compare the definition of the display function and
the definition of the display method can you see the similarity,
and the correspondence of self to aMessage?

Now look at the two calls to display in the real OOP example.
Can you see the similarity to the calls to display in the non OOP
version?

Again can you see the correspondence between the values
passed in to the function and the values passed via the class.
In both cases its m1,m2, ie the instances we created.
In both cases the function needs those values to know
which objects message it should print.

Finally we have the more conventionalOOP notation for calling
methods:

m1.display()
m2.display()

These are exactly the same as the previous calls via the
Message class, but are more intuitive (once you  start
feeling comfortable with OOP notation as used with strings
and the like)

I've never tried explaining self with that set of examples so
it may have confused things even more! But maybe its more
conventional approach to functions v methods might make sense.

The final caveat is that message dispatch gets more complex
when you factor in inheritance so the simple non OOP example
breaks down fairly quickly...

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mailing list question

2006-10-26 Thread rolando
I just use the reply button in my Thunderbird, and then change the email 
I want to send to tutor@python.org



Jorge Azedo escreveu:
 Danny Yoo wrote:
   
 
 Not so much a question about Python, but here goes:

 How do I reply to a specific thread in the mailing list? If I place 
 Re:bla bla  in the subject line, I notice that I start a new 
 thread, I don't continue one that already exists. How do I go about 
 doing this? Thanks for any info
 
 What e-mail client are you using?  Most have a reply-to-all button 
 you can use so that you won't have to start a new message to reply to 
 someone.
   
 There's a little bit of extra information that Mailman uses to detect 
 threads.  The archives don't cluster messages based on the subject 
 line, but instead use a specific header value whose name I'm 
 completely forgetting right now... *grin* (I think it's the 
 'In-Reply-To' header line.)  Your email client should be the one 
 responsible for maintaining that threading information.

 
 I'm trying to use the Reply All button on my mail client (I use 
 Thunderbird, by the way) like you guys suggested. Let's see if it works  
 :-P
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mailing list question

2006-10-26 Thread Alan Gauld
Jorge Azedo [EMAIL PROTECTED] wrote 
 How do I reply to a specific thread in the mailing list? 

Just hit Reply All.
There should be a command or button in your mail tool 
to do that, even if you use web mail.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Self, Scopes and my unbelievable muddleheadedness.

2006-10-26 Thread Alan Gauld
Replying to my own post, how sad...

Alan Gauld [EMAIL PROTECTED] wrote 

 First we need a way to create objects since we dont have
 classes...so we write a function that returns a dictionary.
 We'll make it simple and have a message class that stores
 a string and has a display method.
 
 def display(aMessage):
print aMessage['text']
 
 def Message(theText):
theObject = {}
theObject['text'] = theText
return theObject

Oops I lost the plot somewhere here, the create function 
should have been called createMessage and looked like:

def createMessage(theText):
theObject = {}
theObject['text'] = theText
theObject['display'] = display
return theObject

 Now we create a couple of instances and display them:
 
 m1 = createMessage('Good morning')
 m2 = createMessage('Hello world')
 
 display(m1)
 display(m2)

And the display calls should have been via the dictionary:

m1['display'](m1)
m2['display'](m2)

 Notice how we need to pass the instance that we want
 to display into the display call?

And that should make more sense and be closer to the true 
OOP version...
 
 Now lets write the same thing using real OOP:
 
 class Message:
def __init__(self, theText):
self.text = theText
def display(self):
print self.text
 
 m1 = Message('Good morning')
 m2 = Message('Hello world')
 Message.display(m1)
 Message.display(m2)
 
 If you compare the definition of the display function and
 the definition of the display method can you see the similarity,
 and the correspondence of self to aMessage?
 
 Now look at the two calls to display in the real OOP example.
 Can you see the similarity to the calls to display in the non OOP
 version?
 
 Again can you see the correspondence between the values
 passed in to the function and the values passed via the class.
 In both cases its m1,m2, ie the instances we created.
 In both cases the function needs those values to know
 which objects message it should print.
 
 Finally we have the more conventionalOOP notation for calling
 methods:
 
 m1.display()
 m2.display()
 
 These are exactly the same as the previous calls via the
 Message class, but are more intuitive (once you  start
 feeling comfortable with OOP notation as used with strings
 and the like)
 
 I've never tried explaining self with that set of examples so
 it may have confused things even more! But maybe its more
 conventional approach to functions v methods might make sense.
 
 The final caveat is that message dispatch gets more complex
 when you factor in inheritance so the simple non OOP example
 breaks down fairly quickly...
 
 HTH,
 
 
 -- 
 Alan Gauld
 Author of the Learn to Program web site
 http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mailing list question

2006-10-26 Thread Luke Paireepinart
rolando wrote:
 I just use the reply button in my Thunderbird, and then change the email 
 I want to send to tutor@python.org
It's better to use reply-all, in my opinion, because then, if someone's 
involved in an e-mail thread, they'll get an instant
update on it even if they only get the Tutor Digest and not every 
individual mail.
Cheers,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mailing list question

2006-10-26 Thread Jonathon Sisson
Greetings everyone...

I use Thunderbird, too.  I've noticed that it handles threads a bit 
strange...for instance, I have pytutor set up to send me copies of my 
replies (so I can track threads better), but Thunderbird won't display 
my replies inline with the threads...

If you go to the folder you want to see threads in, then click on view, 
you can look at your thread settings.  Under sort by you should be 
able to set Threaded or unthreaded, and under Threads (under the 
view menu) you should have it set to all.  I know that there are 
subtle differences between Firefox for Windows and Firefox for *nix, but 
I don't know about Thunderbird.  If you're using Windows, there might be 
slight differences between what you see what I see in the menus.

I don't know if that helps at all, but that's how I have Thunderbird set 
up when it comes to threads.

Jonathon


Alan Gauld wrote:
 Jorge Azedo [EMAIL PROTECTED] wrote 
 How do I reply to a specific thread in the mailing list? 
 
 Just hit Reply All.
 There should be a command or button in your mail tool 
 to do that, even if you use web mail.
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor