Re: [Tutor] Dictionary blues...

2005-03-24 Thread Alan Gauld
 if D.has_key(event.keysym):
   str=D[event.keysym]
 self.text.insert(END,str)
 
 You can remove the 'text.' thats only used if text 
 were part of a class, which in this case it isn't.

Oops, that should say remove 'self.' not text.

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


Re: [Tutor] blocking user access

2005-03-24 Thread Kent Johnson
Diana Hawksworth wrote:
Liam, I am using IDLE - and Tkinter, John and Liam. I have been working
through the book Python Programming by Michael Dawson.  One of his
programs calls for the entry of a password, then reveals a message.  What I
would like to do is make the Text widget that reveals the message, unusable
by a user!
I tried the state = DISABLED in the 2nd line of code below, where the widget
to display the text is created - and it did disable the Text widget - to the
extent that it wouldn't reveal the message!  I have tried multiple other
places to put the DISABLED - at the end of this code for instance, but it
doesn't seem to work there! Where can I place it so that it will prevent a
user access to that Text widget?
Also - assuming I can get it to work - what is the term I need to use to
enable to Text widget again so that it will continue to work the next time
around. I tried ENABLED - but was told that global name is not defined!
Here is a snippet from Fredrik Lundh's An Introduction to Tkinter that shows how to enable the 
widget, insert some text, then disable it again:
text.config(state=NORMAL)
text.delete(1.0, END)
text.insert(END, text)
text.config(state=DISABLED)

from http://www.pythonware.com/library/tkinter/introduction/x8309-patterns.htm
Kent
Any ideas? Thanks in advance for suggestions.
Diana
Here is the code:
# create text widget to display message
self.secret_txt = Text(self, width = 35, height = 5, wrap = WORD)
self.secret_txt.grid(row = 3, column = 0, columnspan = 2, sticky =
W)
def reveal(self):
 Display message based on password. 
contents = self.pw_ent.get()
if contents == secret:
message = Here's the secret to living to 100: live to 99  \
  and then be VERY careful.
else:
message = That's not the correct password, so I can't share  \
  the secret with you.
self.secret_txt.delete(0.0, END)
self.secret_txt.insert(0.0, message)


Hi Diana,
Welcome.
Are you using IDLE? Could you provide a copy of your code?
Regards,
Liam Clarke
Still a newbie, but not as much. :)
On Wed, 23 Mar 2005 20:59:02 +1100, Diana Hawksworth
[EMAIL PROTECTED] wrote:
Hi!  I need help on blocking user access to a message box - for example,
the
program could provide an answer to an input.  At the moment, the user
has
access to - and can type into - that answer space. How do I prevent
that
from happening please?
Diana - a very newbie!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


--
'There is only one basic human right, and that is to do as you damn well
please.
And with it comes the only basic human duty, to take the consequences.

___
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] Mysqldb module - where ?????

2005-03-24 Thread Vicki Stanfield
I am googling for a slackware package containing the Mysqldb module, but
all I am coming up with is MySQLdb. I am not hallucinating right? They are
indeed two separate modules?

Vicki

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


[Tutor] re question

2005-03-24 Thread Ertl, John
All

I have a string that has a bunch of numbers with the units attached to them.
I want to strip off the units.  I am using a regular expression and sub to
do this.  This works great for almost all of the cases.  

These are the type of lines:

SigWind:  857hPa,  ,  21.0C,  20.1C, 210 @  9kts
SigWind:  850hPa±, ,   ,   , 205 @ 11kts
Std Lvl:  850hPa, 1503m,  16.8C,  15.7C, 205 @ 11kts

I am using the following cleanstring = re.compile( '(hPa|hPa\xb1|m|C|kts)'
).  And then the cleanstring.sub(,line).  I have tried using numerous \ to
escape the \xb1.

I also tried replacing all non numeric characters that are part of a
number-character string but I could not make that work. The idea was replace
all non-number characters in a word that is made up of numbers followed by
numbers.

I then split the line at the commas so in the current thinking I need the
commas for the split.  How do I deal with the hPa±?  When I print it out it
looks like it is a hexadecimal escape character (\xb1) but I am note sure
how to deal with this.

Any ideas?

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


Re: [Tutor] Changing a class into a subclass

2005-03-24 Thread Max Noel
On Mar 24, 2005, at 18:07, Ismael Garrido wrote:
Hello.
I have a program that saves/loads to/from XML. I have a main class 
Building, and a subclass House(Building). When I save the code I 
instruct each object to save itself to XML (using ElementTree), so 
House adds itself to the XML tree.
My problem is when I load the XML. Before having subclasses what I did 
was to send the XML object (again in ElementTree) to the object and 
let itself load. Now, with subclasses, if I send the House XML to 
Building I get a Building instance, when I need a House instance.
	You can't make an object transform into an object of a different 
class. You need to identify what class the object will be an instance 
of before loading it. Which, if you're using XML tags like Building 
type=House or House, shouldn't be very difficult to do.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?

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


Re: [Tutor] Changing a class into a subclass

2005-03-24 Thread Bob Gailer


At 10:30 AM 3/24/2005, Max Noel wrote:
On Mar 24, 2005, at 18:07,
Ismael Garrido wrote:
Hello.
I have a program that saves/loads to/from XML. I have a main class
Building, and a subclass House(Building). When I save the code I instruct
each object to save itself to XML (using ElementTree), so House adds
itself to the XML tree.
My problem is when I load the XML. Before having subclasses what I did
was to send the XML object (again in ElementTree) to the object and let
itself load. Now, with subclasses, if I send the House XML to Building I
get a Building instance, when I need a House instance.
You can't
make an object transform into an object of a different
class
Are you referring to a Python object? If so you may assign a class to the
object's __class__ attribute.
class A:pass
class B:pass
b = b()
print b.__class__ # displays class __main__.B at 0x011BB9C0
b.__class__ = A
print b.__class__ # displays class __main__.A at
0x011BBA20
[snip]

Bob Gailer
mailto:[EMAIL PROTECTED]
510 558 3275 home
720 938 2625 cell 

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


Re: [Tutor] I need some guidance

2005-03-24 Thread Alan Gauld
 Alan, I am a bit lost when you wrote:
 
 Read a lot and experiment a lot. Thats where Pythons  prompt
really
 helps. You can build quite sophisticated programs very quickly

 by the way Windows XP and the Python v2.4). Do you mean writing from
the
 Command Line windows instead to use the IDLE one?

No, I mean any  prompt whether in IDLE or the command line.
The point is, you can experiment at the  prompt before typing
the code into a file. Calling a function lots of times with different
values for example is a great way to learn how to use it, and much
faster than writing a full program, saving it, then running it and
then changing it, and repeating.

Just type the command in at the  prompt and hit Enter.
Then hit up arrow or CTRL P to bring the line back, change
the values and hit Enter again...

Once you've got it just how you want it, type the line into a
real program. That's still how I get to grips with any new modules
that I haven't used before.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] Changing a class into a subclass

2005-03-24 Thread Alan Gauld
  let itself load. Now, with subclasses, if I send the House XML to
  Building I get a Building instance, when I need a House instance.

 You can't make an object transform into an object of a different
 class. You need to identify what class the object will be an
instance
 of before loading it. Which, if you're using XML tags like Building
 type=House or House, shouldn't be very difficult to do.

Absolutely, looks like you answered your own question... :-)

But if you want an OOP approach thre are some things to try.
First you can create a BuildingFactory class that has a
single instance (or indeed no instances because you could
use a static method... or get really fancy and create a
meta-class!). The factory class then takes the XML string
fragment and figures out which subclass of Building to
create and returns the required object. You can avoid
having to recode the factory class each time by using a
dictionary object and each subclass definition adds an entry
to the Factory class dictionary - possibly by calling a
class method of Building in the __init__() code


class Shack(Building):
   def __init__(self,p1,p2...):
  Building.__init__(self,...)
  Building.register(self,'Shack', Shack)
  ...rest of init...

Now the factory code can go
 name = self.getClassName(xmlString)
 obj = self.classList[name]()

and instantiate a Shack object.

But unless you expect to create an awful lot of subclasses,
or just want to be purist in approach its probably overkill!

Alan G.

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


Re: [Tutor] Changing a class into a subclass

2005-03-24 Thread Alan Gauld
 Are you referring to a Python object? If so you may assign a class
to the
 object's __class__ attribute.
 class A:pass
 class B:pass
 b = b()
 print b.__class__ # displays class __main__.B at 0x011BB9C0
 b.__class__ = A
 print b.__class__ # displays class __main__.A at 0x011BBA20

Interesting Bob, but presumably that doesn't change the internal
state so the OP would need to run an initialise method using
the XML? Or no, I suppose if he did this inside the class's
init method, as the first step, it would then initialise as
usual?

I feel a spell at the  coming on...

The problem is you still need to identify the class itself
(not the class name) before you can do this, but I guess you
might get that from either the builtins dictionary or the
Building module if it has all the Building subclasses in?

But it certainly does allow you to 'convert' from one class
to another, much like the C++ cast operator.

Alan g.
Learning something new every day.

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


Re: [Tutor] Changing a class into a subclass

2005-03-24 Thread Kent Johnson
Alan Gauld wrote:
But if you want an OOP approach thre are some things to try.
First you can create a BuildingFactory class that has a
single instance (or indeed no instances because you could
use a static method... or get really fancy and create a
meta-class!). 
or make it a static method of Building or get really simple and use a 
module-level function...
The factory class then takes the XML string
fragment and figures out which subclass of Building to
create and returns the required object. You can avoid
having to recode the factory class each time by using a
dictionary object and each subclass definition adds an entry
to the Factory class dictionary - possibly by calling a
class method of Building in the __init__() code
class Shack(Building):
   def __init__(self,p1,p2...):
  Building.__init__(self,...)
  Building.register(self,'Shack', Shack)
  ...rest of init...
I think you want to register Shack before you ever create one. You could do 
it after Shack is defined:
class Shack(Building):
   def __init__(self,p1,p2...):
  Building.__init__(self,...)
  ...rest of init...
Building.register('Shack', Shack)
or you could probably get tricky and do it in a metaclass...but I'm not that 
tricky.
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Defining functions

2005-03-24 Thread John Carmona
Hi there,
I have written (well almost as I copied some lines from an existing example) 
this little programme - part of an exercise.

#By J Carmona
#Programme that compute area for
#circle, square, rectangle
def area_rect():
   length = input(Length: )
   width = input (Width: )
   print The area is: ,length*width
def area_circ():
   radius = input(What is the radius?: )
   print The area is approximately: , 3.14159*(radius**2)
def area_squ():
   side = input (What is the length of one side?: )
   print The area is: , side*side
def print_options():
   print --
   print Options:
   print 1. print options
   print 2. calculate circle area
   print 3. calculate square area
   print 4. calculate rectangle area
   print 5. quit the programme
   print --
   choice = input(Choose an option: )
   if choice == 1:
   print_options()
   elif choice == 2:
   area_circ()
   elif choice == 3:
   area_squ()
   elif choice == 4:
   area_rect()
   elif choice == 5:
   print_options()
#Call starting menu
print_options()
If I try to change the 1, 2, 3, 4 or 5 by a letter i.e. a, b, c, d, e the 
programme stop functionning. I get an error message saying that

Traceback (most recent call last):
 File C:/Python24/Example/area_cir_squ_regt.py, line 39, in -toplevel-
   print_options()
 File C:/Python24/Example/area_cir_squ_regt.py, line 27, in print_options
   choice = input(Choose an option: )
 File string, line 0, in -toplevel-
NameError: name 'c' is not defined
What am I missing? Thanks
JC
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining functions

2005-03-24 Thread Kent Johnson
John Carmona wrote:
Hi there,
I have written (well almost as I copied some lines from an existing 
example) this little programme - part of an exercise.

def print_options():
   print --
   print Options:
   print 1. print options
   print 2. calculate circle area
   print 3. calculate square area
   print 4. calculate rectangle area
   print 5. quit the programme
   print --
   choice = input(Choose an option: )
   if choice == 1:
   print_options()
   elif choice == 2:
   area_circ()
   elif choice == 3:
   area_squ()
   elif choice == 4:
   area_rect()
   elif choice == 5:
   print_options()
If I try to change the 1, 2, 3, 4 or 5 by a letter i.e. a, b, c, d, e 
the programme stop functionning. I get an error message saying that

Traceback (most recent call last):
 File C:/Python24/Example/area_cir_squ_regt.py, line 39, in -toplevel-
   print_options()
 File C:/Python24/Example/area_cir_squ_regt.py, line 27, in print_options
   choice = input(Choose an option: )
 File string, line 0, in -toplevel-
NameError: name 'c' is not defined
The input() function evaluates the input as if it is Python code. So you can type 1+2 to input and 
it will return 3, for example:

  print input('type a valid expression: ')
type a valid expression: 1+2
3
If you type a bare string, Python expects this to be a variable name:
  print input('type a valid expression: ')
type a valid expression: a
Traceback (most recent call last):
  File stdin, line 1, in ?
  File string, line 0, in ?
NameError: name 'a' is not defined
This is exactly the same error you would get if you just typed a bare 'a' at 
the interpreter prompt:
  a
Traceback (most recent call last):
  File stdin, line 1, in ?
NameError: name 'a' is not defined
To get a string from input(), you have to type it with quotes:
  print input('type a valid expression: ')
type a valid expression: 'abcd'
abcd
raw_input() will return the literal string the user typed. If you input is a string, this is what 
you want:
  print raw_input('type anything: ')
type anything: 1+2
1+2
  print raw_input('type anything: ')
type anything: abcd
abcd

In general, raw_input() is safer than input(), which is vulnerable to abuse. Even if you want an 
integer input, you can use raw_input() and int():
  print int(raw_input('type a number: '))
type a number: 35
35

Kent
PS you do need to quote the letters in your if / elif also.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: xmlrpc server

2005-03-24 Thread Andrei
Luis N wrote on Thu, 24 Mar 2005 05:31:03 +0200:

snip 
 py-xmlrpc uses a scant 4mb of memory, does only one thing, and does it
 well, serve xmlrpc requests. It appears significantly faster than
 Twisted.
 
 SimpleXMLRPCServer, as a CGI solution, appears acceptable, given that
 it be run from FastCGI or mod_python to give it that extra boost.

Unless you are aware right now of certain limitations which make one of the
solutions a priori unusable for whatever you want to do, I'd say just go
with the one which you find easiest to use and try to code in such a way
that it's reasonably easy to replace it with something else later. For
example 20 MB memory use doesn't sound like much to me on a modern
computer, but I'd leave Nevow out of the choice of your XMLRPC server.

-- 
Yours,

Andrei

=
Real contact info (decode with rot13):
[EMAIL PROTECTED] Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.

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


Re: [Tutor] Changing a class into a subclass

2005-03-24 Thread Ismael Garrido
Alan Gauld wrote:
Absolutely, looks like you answered your own question... :-)
But if you want an OOP approach thre are some things to try.
First you can create a BuildingFactory class that has a
single instance (or indeed no instances because you could
use a static method... or get really fancy and create a
meta-class!). The factory class then takes the XML string
fragment and figures out which subclass of Building to
create and returns the required object. You can avoid
having to recode the factory class each time by using a
dictionary object and each subclass definition adds an entry
to the Factory class dictionary - possibly by calling a
class method of Building in the __init__() code
 

My current aproach is something like that. I subclassed 
Building(Building) :-)  and wrote there the factory which returns self 
of the created instance. But that didn't go with all my other classes 
that have, as a design rule, that they manage the loading and saving 
from/to XML. That meant that Building was different to the other 
classes, and instead of doing:
a = Ship()
a.fromXML(xml)
listOfA.append(a)

I had to do:
a = Building()
listOfBuildings.append(a.fromXML(xml))
When I wrote my factory I thought about the dictionary approach, but I 
didn't write it because I hoped I could find something like the 
__class__ thing. Which, btw, is pure magic :-)

But there's something that I couldn't understand. In the following code, 
my guess would be that I'm back from the death would never get 
printed... but it is... and twice! Why?

 class A:
   def pong(self):
   print self.__class__
   print Ping!
   self.times -=1
   if self.times = 0:
   self.__class__ = B
   self.pong()
 class B:
   def pong(self):
   print self.__class__
   print Pong!
   self.times -=1
   self.__class__ = A
   self.pong()
   print I'm back from the death
  
 a = A()
 a.times = 3
 a.pong()
__main__.A
Ping!
__main__.B
Pong!
__main__.A
Ping!
__main__.B
Pong!
__main__.A
Ping!
I'm back from the death  ##Weird!
I'm back from the death
 a.__class__
class __main__.A at 0x00C1AF00

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


Re: [Tutor] Changing a class into a subclass

2005-03-24 Thread Kent Johnson
Ismael Garrido wrote:
But there's something that I couldn't understand. In the following code, 
my guess would be that I'm back from the death would never get 
printed... but it is... and twice! Why?
It's printed every time B.pong() is called, just as Pong is. You print 
each one twice - no mystery!
Kent
  class A:
   def pong(self):
   print self.__class__
   print Ping!
   self.times -=1
   if self.times = 0:
   self.__class__ = B
   self.pong()
  class B:
   def pong(self):
   print self.__class__
   print Pong!
   self.times -=1
   self.__class__ = A
   self.pong()
   print I'm back from the death
   a = A()
  a.times = 3
  a.pong()
__main__.A
Ping!
__main__.B
Pong!
__main__.A
Ping!
__main__.B
Pong!
__main__.A
Ping!
I'm back from the death  ##Weird!
I'm back from the death
  a.__class__
class __main__.A at 0x00C1AF00
Thanks,
Ismael
___
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] Trying to use MySQLdb.cursor

2005-03-24 Thread Vicki Stanfield
I finally gave up and used MySQLdb to connect to my database. It connects
okay, and returns data, but now I have a new question. I use the code
below to print the data returned from my query, but I would like to make
labels at the top of the columns. How do I do this dynamically? I would
like to get the fieldnames as defined by mysql and print them before
printing each column. Is there a way to do this?

Here is the relevant portion of the code:

def getdata():
 conn = MySQLdb.Connect(
 host='localhost', user='user',
 passwd='password', db='sample',compress=1,
 cursorclass=MySQLdb.cursors.DictCursor)
 cursor = conn.cursor()
 cursor.execute(SELECT computers.comp_location FROM computers, mice
WHERE mice.mouse_type = USB
AND computers.comp_location like A%
AND mice.mouse_comp = computers.comp_id;)
 rows = cursor.fetchall()
 cursor.close()
 conn.close()

 print '''
 table border=1 cellpadding=5
 '''

 for row in rows:
  print tr
  for cell in row:
  print td %s /td % row[cell]

  print /tr

Thanks for helping me get going.
Vicki

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


Re: [Tutor] Trying to use MySQLdb.cursor

2005-03-24 Thread Danny Yoo


On Fri, 25 Mar 2005, Vicki Stanfield wrote:

 I finally gave up and used MySQLdb to connect to my database. It
 connects okay, and returns data, but now I have a new question. I use
 the code below to print the data returned from my query, but I would
 like to make labels at the top of the columns. How do I do this
 dynamically?

Hi Vicki,

Yes, there's a special cursor type that, instead of returning rows of
tuples, returns rows of dictionaries.  Here's an example:

##
 import MySQLdb
 import MySQLdb.cursors
 conn = MySQLdb.connect(db='test_adb',
...cursorclass=MySQLdb.cursors.DictCursor)

 cursor = conn.cursor()
 cursor.execute(select * from Locus limit 10)
10L
 cursor.fetchone()
{'orientation_is_5': 1, 'last_updated': DateTime object for '2005-03-24
13:48:06.00' at 402faad8, 'is_deleted': 0, 'name': 'At1g08520',
'representative_model': None, 'is_pseudogene': 0, 'last_updated_by': 1L,
'assigned_to': None, 'update_needed': None, 'gene_model_type': 0L, 'id':
1L, 'chromosome': 1L, 'locked_for_pasa': 0}
##


Hmmm.. that's a little messy.  Let me clean up the output of that a bit
with the pretty printing module 'pprint':

##
 import pprint
 pprint.pprint(cursor.fetchone())
{'assigned_to': None,
 'chromosome': 1L,
 'gene_model_type': 0L,
 'id': 2L,
 'is_deleted': 0,
 'is_pseudogene': 0,
 'last_updated': DateTime object for '2005-03-24 13:48:02.00' at
403104b8,
 'last_updated_by': 1L,
 'locked_for_pasa': 0,
 'name': 'At1g08530',
 'orientation_is_5': 1,
 'representative_model': None,
 'update_needed': None}
##

The output's content itself is probably a bit bizarre to you (It's a gene
from the Arabidopsis plant database)  But I hope the code is clear.
*grin*


If you have more questions, please feel free to ask!

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


Re: [Tutor] Getting file sizes (fwd) [oops, old message!]

2005-03-24 Thread Danny Yoo

Hi everyone,

My apologies about the repeated message; I just got an old message from
last year, and didn't look closely enough to see that it was a repeat from
last year!  The mail queue of some folks is really ancient; I wonder why
that bounced to me today.  Odd.

Anyway, sorry about that!

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