[Tutor] newb problem running the translator (fwd)

2005-04-09 Thread Danny Yoo


-- Forwarded message --
Date: Fri, 8 Apr 2005 22:03:58 EDT
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: newb problem running the translator

I will try to make this quick. I am a newb to python, and programming at
that, but i am realy interested. I have been looking at simple programs to be
run through the shell but i cannot figure out the translator. When i save
program i have tried to tell it to opoen it in the translator. I have been 
looking
through several websites and looked through the documents my python
installation came with. On one computer i am running Windows Xp and on another 
win98SE.
If you could explain to me how to open a program through the translator i
would appreciate it alot.


Thanx

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


Re: [Tutor] str.split and quotes

2005-04-09 Thread smiles
Kent Johnson wrote:

Marilyn Davis wrote:> Is there a reason to prefer one over the
other?  Is one faster?  I > compiled my regular _expression_ to make it
quicker. The only way to know which is faster is to time
them both. The timeit module makes it pretty easy to do
this.Here is a simple example of using timeit for a different
problem; you can adapt it to your own needs:d = dict( ((i,i,i),
i) for i in range(1000))
[cut]
I was glad to see your post showing
how to run a list of functions through the timer.  That's a nice
way to do it!  You better slip some square brackets into your
definition of d though:
 

d = dict( [((i,i,i), i) for i in
range(1000)])
/c

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


RE: [Tutor] Talking to mssql?

2005-04-09 Thread j2
>I had the same question.  The best I found is this:
>http://www.object-craft.com.au/projects/mssql/ 
>but that has a lot of strong language saying "this isn't ready for use".
>
>I found it perfectly usable for simply running queries, but haven't tried
>to do anything more complicated with it.  Not sure how
>well it works on Linux, but he has binaries that worked for me.

Well, doesn't fly for me, because I can't install it. Can you offer any help
on the below?


I am trying to install http://www.object-craft.com.au/projects/mssql/ on my
Debian system, but when i try to build, i end up with the below. Also, my
package list is at the bottom.

hooch:~/Download/Asterisk/MSSQL-0.09# python setup.py install
running install
running build
running build_py
not copying MSSQL.py (output up-to-date)
running build_ext
building 'mssqldb' extension
creating build/temp.linux-i686-2.2
gcc -DNDEBUG -g -O3 -fno-strict-aliasing -Wall -Wstrict-prototypes -fPIC
-DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD
-DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE
-I/usr/include -I/usr/include/python2.2 -c mssqldb.c -o
build/temp.linux-i686-2.2/mssqldb.o
gcc -DNDEBUG -g -O3 -fno-strict-aliasing -Wall -Wstrict-prototypes -fPIC
-DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD
-DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE
-I/usr/include -I/usr/include/python2.2 -c loginrec.c -o
build/temp.linux-i686-2.2/loginrec.o
gcc -DNDEBUG -g -O3 -fno-strict-aliasing -Wall -Wstrict-prototypes -fPIC
-DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD
-DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE
-I/usr/include -I/usr/include/python2.2 -c dbproc.c -o
build/temp.linux-i686-2.2/dbproc.o
dbproc.c:199: warning: `wrap_DbProcIntFunc_bool' defined but not used
gcc -DNDEBUG -g -O3 -fno-strict-aliasing -Wall -Wstrict-prototypes -fPIC
-DHAVE_FREETDS -DHAVE_DBALTBIND -DHAVE_DBCLROPT -DHAVE_DBCURCMD
-DHAVE_DBCURROW -DHAVE_DBISOPT -DHAVE_DBNUMCOMPUTE -DHAVE_DBRETTYPE
-I/usr/include -I/usr/include/python2.2 -c databuf.c -o
build/temp.linux-i686-2.2/databuf.o
databuf.c: In function `get_buff_value':
databuf.c:86: error: `DBVARYCHAR' undeclared (first use in this function)
databuf.c:86: error: (Each undeclared identifier is reported only once
databuf.c:86: error: for each function it appears in.)
databuf.c:86: error: parse error before ')' token
databuf.c:87: error: parse error before ')' token
databuf.c:90: error: `DBBIT' undeclared (first use in this function)
databuf.c:90: error: parse error before ')' token
databuf.c: In function `DataBuf_set':
databuf.c:155: error: `DBVARYCHAR' undeclared (first use in this function)
databuf.c:155: error: parse error before ')' token
databuf.c:160: error: parse error before ')' token
databuf.c:161: error: parse error before ')' token
databuf.c:169: error: `DBBIT' undeclared (first use in this function)
databuf.c:169: error: parse error before ')' token
databuf.c: In function `DataBuf__init__':
databuf.c:270: error: `DBVARYCHAR' undeclared (first use in this function)
databuf.c:273: error: `DBBIT' undeclared (first use in this function)
error: command 'gcc' failed with exit status 1
hooch:~/Download/Asterisk/MSSQL-0.09#


+++-==-==-==
==
ii  adduser3.51   Add and remove users and groups
ii  apache 1.3.29.0.1-3   Versatile, high-performance HTTP
server
ii  apache-common  1.3.29.0.1-3   Support files for all Apache
webservers
ii  apache-utils   1.3.29.0.1-3   Utility programs for webservers
ii  apt0.5.4  Advanced front-end for dpkg
ii  apt-utils  0.5.4  APT utility programs
ii  arping 1.05-1 sends IP and/or ARP pings (to the
MAC address)
ii  asterisk   1.0.7.dfsg.1-2 open source Private Branch
Exchange (PBX)
ii  asterisk-config1.0.7.dfsg.1-2 config files for asterisk
ii  asterisk-prompt-se 0.8-2  Swedish voice prompts for Asterisk
ii  asterisk-sounds-ma 1.0.7.dfsg.1-2 sound files for asterisk
ii  at 3.1.8-11   Delayed job execution and batch
processing
ii  atftpd 0.6.2  Advanced TFTP server.
ii  aub2.1.3-1Assembles binary files from USENET
ii  autoconf   2.57-10automatic configure script builder
ii  autoconf2.13   2.13-48automatic configure script builder
(obsolete version
ii  automake   1.4-p4-1.1 A tool for generating GNU
Standards-compliant Makefi
ii  autoproject0.17-1 create a skeleton source package
for a new program
ii  autotools-dev  20030717.1 Update infrastructure for
config.{guess,sub} files
ii  backup2l   1.1-gk.1.1 low-maintenance backup/restore
tool for mountable me
ii  base-config1.72   

Re: [Tutor] newb problem running the translator (fwd)

2005-04-09 Thread Max Noel
On Apr 9, 2005, at 10:36, Danny Yoo wrote:

-- Forwarded message --
Date: Fri, 8 Apr 2005 22:03:58 EDT
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: newb problem running the translator
I will try to make this quick. I am a newb to python, and 
programming at
that, but i am realy interested. I have been looking at simple 
programs to be
run through the shell but i cannot figure out the translator. When i 
save
program i have tried to tell it to opoen it in the translator. I have 
been looking
through several websites and looked through the documents my python
installation came with. On one computer i am running Windows Xp and on 
another win98SE.
If you could explain to me how to open a program through the 
translator i
would appreciate it alot.
	What exactly do you call the "translator"?
	If you mean the compiler, it's normal that you can't find it: there is 
none. Python is an interpreted language (well, bytecode, but let's not 
get into that, shall we?). To run your Python programs, you call the 
Python interpreter on them: "python foo.py", for example, runs the 
foo.py program.

-- 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] str.split and quotes

2005-04-09 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
I was glad to see your post showing how to run a list of functions 
through the timer.  That's a nice way to do it!  You better slip some 
square brackets into your definition of d though:

 

d = dict( [((i,i,i), i) for i in range(1000)])
In Python 2.4 they are not needed. For earlier versions, you are right. (((i,i,i), i) for i in 
range(1000)) is a generator expression. It's value is a generator object that supports the iterator 
protocol.

 >>> g=(((i,i,i), i) for i in range(1000))
 >>> g

 >>> g.next()
((0, 0, 0), 0)
 >>> g.next()
((1, 1, 1), 1)
In general, if you replace the [] of a list comprehensions with () you will get a generator 
comprehension. The () are required but syntactically they can be part of the function call syntax - 
you don't have to add another () if one is present.

BTW a note about timing for optimization - it is important to do your timing on the same Python 
version that you will deploy with; there have been significant speedups in portions of Python 2.4 
and 2.3. For example list comprehension has gotten much faster.

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


[Tutor] chr() and extended ASCII?

2005-04-09 Thread Liam Clarke
Hi all, 

Just working on some validators for text fields, and I started getting unusual errors - 

  File "C:\Python23\lib\site-packages\PythonCard\components\textfield.py", line 338, in _dispatch
    widget.Widget._dispatch(self, aWxEvent)
  File "C:\Python23\lib\site-packages\PythonCard\widget.py", line 402, in _dispatch
    handler(background, aWxEvent)
  File "C:\Python23\SEDB\generic.py", line 53, in on_keyPress
    elif len(eventSourceWidget.text) >=
self.restrictions[eventSourceWidget.name]['length']  or not
self.isOK(eventSourceWidget.name, event.keyCode):
  File "C:\Python23\SEDB\generic.py", line 69, in isOK
    xR = self.integerRE.search(chr(keyCode))
ValueError: chr() arg not in range(256)
(And yes, I know wxPython has validators already, I was 90% through
mine when I found them, and so I'll finish off the last 10% out of
stubborn pride.)

For the life of me, I couldn't figure out what was happening, so I stuck in a try clause. 

***
Oops
keyCode = 333
Widget = dateBooked
***

***
Oops
keyCode = 332
Widget = dateBooked
***

I was entering numeric data, and as I do a lot of data entry at work,
by default I use the numeric keypad, so I guess I just found that
extended ASCII stuff. Is there a easy way around this with chr()?

Or is it a case of making my own keycode to character dictionary? I'm feeling lazy at the moment.

Regards, 


Liam Clarke
-- '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


Re: [Tutor] Support

2005-04-09 Thread Alberto Troiano
Hey dudes
This code worked fine
The one you gave me worked as well but when I wanteed to store it in the database it says that the packet was too large
Whit this code it doesn't complain but now I don't know  how to retrieve the image
This is what I'm doing:
import MySQLdb
import Image
import Image 
from StringIO import StringIO 
im = Image.open("auto.jpg") 
outFile = StringIO() 
im.save(outFile, "JPEG") 
db=MySQLdb.connect(connection string)
cursor=db.cursor()
cursor.execute("insert into image values('',%s)",outfile)
ima = Image.open(outFile) >##
Then I retrieve it like this:
db=MySQLdb.connect(connection string)
cursor=db.cursor()
cursor.execute("select * from image")
res=cursor.fetchall()
Now I have the binary string in res[1] but when I try to open it it gives me an error (I'm sorry I'm not in the machine that has the database and I forgot to copy the error :/)
the fields in MySQL version 4 are Cod_Imagen int(4) auto_increment primary key, data longblob)
Thanks in advanced
Alberto
 >From: Kent Johnson <[EMAIL PROTECTED]> >CC: tutor@python.org >Subject: Re: [Tutor] Support >Date: Fri, 08 Apr 2005 15:04:42 -0400 > >Alberto Troiano wrote: >>Thanks >> >>Apparently it worked but one question do >> >>What kind of data is the return of the function tostring() > >It's a string >> >>Can i put it in a blob type of a databaseor maybe in a >>longtext?? > >I would try longtext. > >Kent > >___ >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] str.split and quotes

2005-04-09 Thread Alberto Troiano
Sorry to bother you that much. I know I have a lot to learn yet but I hope you can teach me.
I see that someone posted something about running functions with a timer. How can I do this???
I tried to emulate this with the function time.sleep(sec) but all widgets gets locked and they don't refresh themself until the sleep goes off.
Thanks in advanced for the help
Alberto>From: Kent Johnson <[EMAIL PROTECTED]> >CC: tutor@python.org >Subject: Re: [Tutor] str.split and quotes >Date: Sat, 09 Apr 2005 07:45:27 -0400 > >[EMAIL PROTECTED] wrote: >>I was glad to see your post showing how to run a list of functions >>through the timer. That's a nice way to do it! You better slip >>some square brackets into your definition of d though: >> >> >> >> d = dict( [((i,i,i), i) for i in range(1000)]) > >In Python 2.4 they are not needed. For earlier versions, you are >right. (((i,i,i), i) for i in range(1000)) is a generator >_expression_. It's value is a generator object that supports the >iterator protocol. > > >>> g=(((i,i,i), i) for i in range(1000)) > >>> g > > >>> g.next() >((0, 0, 0), 0) 
> >>> g.next() >((1, 1, 1), 1) > >In general, if you replace the [] of a list comprehensions with () >you will get a generator comprehension. The () are required but >syntactically they can be part of the function call syntax - you >don't have to add another () if one is present. > >BTW a note about timing for optimization - it is important to do >your timing on the same Python version that you will deploy with; >there have been significant speedups in portions of Python 2.4 and >2.3. For example list comprehension has gotten much faster. > >Kent > >___ >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] How can I suspend...

2005-04-09 Thread John Carmona
I am off to sunny Spain for the next 2 weeks and I would like to suspend the 
emailing from Python.tutor for that period. Could someone put me in the 
right direction on how to do that, thanks.

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


[Tutor] import.... (Joseph Q.)

2005-04-09 Thread Joseph Quigley
I don't think I have said it already, but I'll say it again just incase 
:-), I'm new to python. I started Feb. this year.

import is handy. But my questions are:
is there a type of unimport or something to unload a module? Foo: What's it 
good for?

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


[Tutor] Re: Help with classes (Joseph Q.)

2005-04-09 Thread Joseph Quigley
class Message:
  def init(self, p = 'Hello world'):
 self.text = p
  def sayIt(self):
 print self.text
m = Message()
m.init()
m.sayIt()
m.init('Hiya fred!')
m.sayIt()

> Well this OOP stuff is realy hard for me as I have never even
> programmed it took me a while just to understand defs.
That's OK, OOP is quite a strange concept for many folks. Its
actually easier to learn as a beginner than for folks who
have been programming without OOP for a long time!
> determined to learn how to do it. My biggest problem is with
__init__
> I still don't understand how to use it.
init is simply where you initialise the data attributes of your class.
It could be defined as a normal method and you call it explicitly:
class Message:
  def init(self, txt = 'Hello world'):
 self.text = txt
  def sayIt(self):
 print self.text
And every time you create a message object you explicitly call it:
m = Message()
m.init('Hiya fred!')
m.sayIt()
I was having problems with the __init__ too.
class Message:
  def init(self, txt = 'Hello world'):
 self.text = txt
  def sayIt(self):
 print self.text
And every time you create a message object you explicitly call it:
m = Message()
m.init()
m.sayIt()
m.init("Hello World is so overdone. It hard to break tradition though :->")
m.sayIt()
Why did you put 'Hiya fred!' in place of having m.init print "Hello World"?
Now what are dictionaries and the __name__ really used for? A byte of 
python never got that through my thick skull.

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


RE: [Tutor] import.... (Joseph Q.)

2005-04-09 Thread Alberto Troiano
Hey Joseph
Why do you want to unload a module?
I don't understand why you need to "unimport" but I do know that every module has a constructor and a deconstructor
maybe you can start from there but I will need an example or more info about why you need to do this
Regards
Alberto>From: Joseph Quigley <[EMAIL PROTECTED]> >To: tutor@python.org >Subject: [Tutor] import (Joseph Q.) >Date: Fri, 08 Apr 2005 16:58:37 -0600 > >I don't think I have said it already, but I'll say it again just >incase :-), I'm new to python. I started Feb. this year. > >import is handy. But my questions are: >is there a type of unimport or something to unload a module? Foo: >What's it good for? > >___ >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] str.split and quotes

2005-04-09 Thread Kent Johnson
Alberto Troiano wrote:
Sorry to bother you that much. I know I have a lot to learn yet but I 
hope you can teach me.

I see that someone posted something about running functions with a 
timer. How can I do this???
You can use the timeit module, see my recent post for an example.
You can also use time.time() directly like this:
import time
start = time.time()
# do something that you want to time...
elapsed = time.time() - start
now elapsed is the elapsed time in seconds as a floating point number.
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I suspend...

2005-04-09 Thread Kent Johnson
Go to the address at the bottom of this email. Unsubscribe. When you return, 
re-subscribe.
Kent
John Carmona wrote:
I am off to sunny Spain for the next 2 weeks and I would like to suspend 
the emailing from Python.tutor for that period. Could someone put me in 
the right direction on how to do that, thanks.

Regards
JC
___
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] import.... (Joseph Q.)

2005-04-09 Thread Kent Johnson
Joseph Quigley wrote:
I don't think I have said it already, but I'll say it again just incase 
:-), I'm new to python. I started Feb. this year.

import is handy. But my questions are:
is there a type of unimport or something to unload a module? 
This is not normally needed.
Foo: What's
it good for?
Foo and Bar are popular names used in examples. They don't mean anything. For example to show how to 
define a function I might write

def foo():
  print 'Hello'
I just use foo to avoid having to think of a meaningful name.
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] re: IDLE

2005-04-09 Thread Kevin
Is there a way to get line numbers to show in python IDLE? I looked at
all the preferances but nothing?

Thanks

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


[Tutor] Talking to mssql?

2005-04-09 Thread Lloyd Kvam
I replied earlier from a digest entry (which starts a new thread) so you might 
have missed that reply.
I included a URL for the mssql package which included the compile instructions 
for using freetds.
Here is the link.  Just follow the instructions and you should be able to 
compile cleanly.
http://mail.python.org/pipermail/db-sig/2005-April/004403.html
A new sybase module has been released.  Note the instructions for
installing with freetds.

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


Re: [Tutor] re: IDLE

2005-04-09 Thread joe_schmoe
Kevin wrote:
Is there a way to get line numbers to show in python IDLE? I looked at
all the preferances but nothing?
Thanks
Kevin
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
Not that I am aware of, although when you have the cursor over the 
lines, the line and column numbers are displayed in the bottom left hand 
corner of the window frame.
/j
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: Help with classes (Joseph Q.)

2005-04-09 Thread Andrei
Joseph Quigley wrote on Fri, 08 Apr 2005 16:46:33 -0600:

> Now what are dictionaries and the __name__ really used for? A byte of 
> python never got that through my thick skull.

Dictionaries associate a value with a certain key, sort of like a real
world dictionary where you can take a word and look up its
meaning/translation (except that real world dictionaries are alphabetically
sorted and only contain words, while Python dictionaries are not sorted and
can contain lots of things, including numbers and tuples). 

It's possible to get by without using them directly (other languages don't
even have something resembling a dictionary and they still manage to work
somehow :)), but you might end up building functionality to emulate their
behaviour with for example lists. Let's say you build an application which
allows the user to attach comments to their pictures: you could use a
dictionary for it, mapping a string to each file name:

comments = {}
while True:
photo = raw_input("Picture name: ")
if photo = '':
break
comment = raw_input("Comment: ")
comments[photo] = comment
print comments

You could also think of an address book (where you use names as keys and
addresses as values), a menu system (where certain user input is mapped to
a certain function), a cache system (where results of certain
time-consuming operations are saved in case the user asks for the same
operation again) and there are many, many other uses.

-- 
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


[Tutor] Re: import.... (Joseph Q.)

2005-04-09 Thread Andrei
Joseph Quigley wrote on Fri, 08 Apr 2005 16:58:37 -0600:

> import is handy. But my questions are:
> is there a type of unimport or something to unload a module? 

Things which are not used are cleaned up automatically at some point. What
would you like to do do that this mechanism doesn't provide?

-- 
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


[Tutor] quicksort using list comprehension

2005-04-09 Thread Logesh Pillay
I'm trying to program quicksort using list comprehension.
The following gives me a type mismatch error for "+".
def qsort (t):
if len (t) > 1:
return qsort([x for x in t[1:] if x <= t[0]]) + [t[0]] + qsort([x  
for x in t[1:] if x > t[0]])

I know this sounds strange but I have and idea it (or something similar)  
worked when I last tried Python a yr or so ago. An earlier version of  
Python?

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


Re: [Tutor] Associate functinos with Dictionary/Class Usage

2005-04-09 Thread Shane Liebling
Although I was not the questioner, I must admit this was a brief but
efficient answer.  Reminds me to try something like the last part out
some time...

-Shane

On Apr 7, 2005 4:40 PM, Danny Yoo <[EMAIL PROTECTED]> wrote:
> 
> 
> On Thu, 7 Apr 2005, Luke Jordan wrote:
> 
> > I am looking for a little clarification of how exactly this would work.
> >
> > 1. How do I associate a function to a dict key?
> 
> Hi Luke,
> 
> We're probably already familiar of values like numbers and strings, and
> how to give them names with variables:
> 
> ##
> >>> number = 42
> >>> name = "luke"
> >>> number
> 42
> >>> name
> 'luke'
> ##
> 
> 'number' is a name that refers to the value 42, and 'name' is a name (Doh!
> I must use a better variable name next time...) that refers to the value
> "luke".
> 
> And we also already know how to make functions and to call them:
> 
> ##
> >>> def square(x):
> ... return x * x
> ...
> >>> square(42)
> 1764
> ##
> 
> But what happens if we just say "square" at the interpreter?
> 
> ##
> >>> square
> 
> ##
> 
> The value of 'square' is a function value.
> 
> And just like any other value, we can assign it to another name:
> 
> ##
> >>> anotherNameForSquare = square
> >>> anotherNameForSquare(16)
> 256
> ##
> 
> And just like any other value, we can use it as a dictionary value:
> 
> ##
> >>> operators = {'^2': square}
> >>> operators['^2']
> 
> >>> operators['^2'](4)
> 16
> ##
> 
> Does this make sense so far?  Please feel free to ask more questions about
> this.  Best of wishes!
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
Shane Liebling
Systems Administrator and SAGE Member
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how useful is __del__()

2005-04-09 Thread Carsten Bohnens
hey,
I wrote a small class that does some ftp work for me:
#myftpclass.py
class myftp:
   def __init__(self, ...):
  ...
   def connect_and_login(self):
  ...
   def do_foo(self):
  ...
   def do_bar(self):
  ...
   #here comes the interesting part for me
   def goodbye(self):
  self.currenthost.quit()
  print 'Disconnected.'
my actual scripts then look like that:
#movesomedata.py
from myftpclass import myftp
ftp = myftp(--host, username, etc...--)
ftp.connect_and_login()
ftp.do_foo()
ftp.do_bar()
ftp.do_foo()
ftp.goodbye()
I am wondering if there is a way to get the disconnect without calling 
the funcion goodbye or doing ftp.currenthost.quit() by youself and if 
that could be done by defining a __del__() funcion in myftpclass. like, 
the end of the script is reached, or an exception is raised somewhere, 
disconnect.
or should I do something like this instead:

#movesomedata2.py
from myftpclass import myftp
try:
   ftp = myftp([host, username, etc...])
   ftp.connect_and_login()
   ftp.do_foo()
   ftp.do_bar()
   ftp.do_foo()
finally:
   ftp.goodbye()
thanks for all answers
Carsten
germanator at gmx dot de
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] quicksort using list comprehension

2005-04-09 Thread Kent Johnson
I think you have to return a value when len(t) <= 1. You don't return anything which means you 
return None which can't be added to a list.

Kent
Logesh Pillay wrote:
I'm trying to program quicksort using list comprehension.
The following gives me a type mismatch error for "+".
def qsort (t):
if len (t) > 1:
return qsort([x for x in t[1:] if x <= t[0]]) + [t[0]] + 
qsort([x  for x in t[1:] if x > t[0]])

I know this sounds strange but I have and idea it (or something 
similar)  worked when I last tried Python a yr or so ago. An earlier 
version of  Python?

Logesh Pillay
___
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] Re: Help with classes (Joseph Q.)

2005-04-09 Thread Kent Johnson
Joseph Quigley wrote:
class Message:
  def init(self, p = 'Hello world'):
 self.text = p
  def sayIt(self):
 print self.text
m = Message()
m.init()
m.sayIt()
m.init('Hiya fred!')
m.sayIt()
This is OK but a more conventional usage is to write an __init__() method. This is sometimes called 
a constructor and it is called automatically when you make a new class instance. Using __init__() 
your example would look like this:

class Message:
  def __init__(self, p = 'Hello world'):
 self.text = p
  def sayIt(self):
 print self.text
m = Message()
m.sayIt()
m = Message('Hiya fred!')
m.sayIt()
though this is nemantically a little different than yours because I create a new Message with the 
new string while you reuse the old one.

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


Re: [Tutor] Support

2005-04-09 Thread Kent Johnson
Alberto Troiano wrote:
Hey dudes
This code worked fine
The one you gave me worked as well but when I wanteed to store it in the 
database it says that the packet was too large

Whit this code it doesn't complain but now I don't know  how to retrieve 
the image
I retrieve it like this:

db=MySQLdb.connect(connection string)
cursor=db.cursor()
cursor.execute("select * from image")
res=cursor.fetchall()
Now I have the binary string in res[1] but when I try to open it it 
gives me an error (I'm sorry I'm not in the machine that has the 
database and I forgot to copy the error :/)
I think res is going to be a sequence of sequences. So I think the first image will be at res[0][1]. 
Otherwise the error message will be helpful.

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


Re: [Tutor] quicksort using list comprehension

2005-04-09 Thread Max Noel
On Apr 9, 2005, at 21:50, Kent Johnson wrote:
I think you have to return a value when len(t) <= 1. You don't return 
anything which means you return None which can't be added to a list.

Kent
	Yup. Here's a quicksort I did, adapting an example from the Wikipedia 
article on Haskell:

def qsort(toSort):
if toSort == []:
return []
else:
x = toSort[0]
elts_lt_x = [y for y in toSort if y < x]
elts_gt_x = [y for y in toSort if y > x]
return qsort(elts_lt_x) + [x] + qsort(elts_gt_x)

	It is, of course, nowhere near as optimal as a "real" quicksort 
algorithm (like the one the list.sort method implements).

-- 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


[Tutor] Re: Tutor Digest, Vol 14, Issue 32

2005-04-09 Thread Rick Muller
On Apr 9, 2005, at 1:54 PM, [EMAIL PROTECTED] wrote:
Message: 8
Date: Sat, 09 Apr 2005 20:58:49 +0200
From: "Logesh Pillay" <[EMAIL PROTECTED]>
Subject: [Tutor] quicksort using list comprehension
To: "Discussion for learning programming with Python"

Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; format=flowed; delsp=yes;
charset=iso-8859-15
I'm trying to program quicksort using list comprehension.
The following gives me a type mismatch error for "+".
def qsort (t):
 if len (t) > 1:
 return qsort([x for x in t[1:] if x <= t[0]]) + [t[0]] + 
qsort([x
for x in t[1:] if x > t[0]])

I know this sounds strange but I have and idea it (or something 
similar)
worked when I last tried Python a yr or so ago. An earlier version of
Python?

Logesh Pillay
I think the following recipe is what you want:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66473
Rick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What is "foo"?

2005-04-09 Thread Danny Yoo
> > Foo: What's it good for?
>
> Foo and Bar are popular names used in examples. They don't mean
> anything. For example to show how to define a function I might write
>
> def foo():
>print 'Hello'
>
> I just use foo to avoid having to think of a meaningful name.


Hi Joseph,

I sometimes get lazy and use 'foo' in my own code examples too.  But I
find that it's usually a lax idea, precisely because it has no meaning.

Because that "foo" nonsense word confuses beginners quite a bit, I'm
trying to be more conscious to make good variable names, even for
throwaway code, although I sometimes slip.

There's a nice article in the wikipedia that explains Foo:

http://en.wikipedia.org/wiki/Foo

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


[Tutor] problems with doctest: apparent interferance between tests (LONG)

2005-04-09 Thread Brian van den Broek
Hi all,
I must apologize for the length of the post. I have explained my 
problem as tersely as able, and have done my level-best to produce the 
minimum code snippet illustrative of my difficulties. But, as (I hope) 
will become clear, any substantial attempts to further cull it made 
the problem go away. (For what it is worth, this is the product of 
several hours of investigating and attempting to cull.)

The quick story:
I have apparent interference between doctests embedded in the 
docstrings of different methods, and this interference also appears to 
be influenced by seemingly irrelevant things such as whether the 
module has a (non-doctest-containing) docstring or not. I'm flummoxed 
as to why there is this apparent interference. The long code snippet 
below is the minimal snippet I was able to make which still exhibited 
the problems; it is commented with indications of the dependencies of 
the tests.


Background:
(Perhaps the the reader could skip this `Background' and jump down to 
`The Problem', returning here if need be.) I have cut enough of the 
original code that the point of what remains may be obscure. (The 
original code includes complete documentation, more methods, etc.) A 
bit of explanation is perhaps needed to give the context of my problem.

I've been writing a module to allow convenient wall clock timings and 
generate a report of same on demand. The idea is to import it, make an 
instance of the Wall_clock class, and then call Wall_clock methods at 
places of interest in the code, so as to add _check_point's and start 
and stop _intervals. (Never mind if this isn't too useful or 
duplicates functionality available elsewhere--it is largely a learning 
exercise to explore various Python features, many of which are not 
reflected in the snippet below.)

All the timings are relative to the creation of the Wall_clock 
instance. To prevent confusion in the reports, my original design idea 
was to make the Wall_clock class `Singleton-like' in that there could 
only be one instance alive at a time, and any attempt to create 
another concurrent instance would raise an exception. (I've since 
decided to opt for using the Borg pattern 
, with 
a class attribute storing the time of first instantiation, but my 
issues depend on the first approach I explored.)

I made my `Singleton-like' class by having a class attribute 
is_instanced, set to True by the class __init__ method, and False by 
its __del__. The __init__ method raises an error if the attribute is 
True, thus preventing concurrent instances. (See the final code 
snippet for a working model with tests that pass as expected.)

So far, so good.

The Problem:
doctest is one of the aspects of Python that I've been trying to learn 
with this project. I have 2 problems using doctest on my module:

1) There is a test neat the start of Wall_clock.check_point's 
docstring that attempts to instantiate the Wall_clock class. It fails, 
in the manner an attempt to create a *second* concurrent instance of 
Wall_clock should fail. But, since the relevant test is not preceded 
in the docstring by any tests which create a Wall_clock instance, I 
don't understand how this could be happening. I had theorized that 
perhaps there was interference in that previously run tests might 
leave Wall_clock.is_instanced == True. The first test in 
Wall_clock.check_point appears to bare this out. Yet this doesn't make 
sense either. And it won't explain:

2) In my attempts to produce the minimal working sample that 
illustrated my problem, I found that removing any of:

a) the Wall_clock class docstring (which contains no tests),
b) the entire Wall_clock.stop_interval method (this method
   is not called anywhere in the code in the snippet, nor
   does it contain any tests), or
c) a portion of the Wall_clock.interval method's doctests
   (this portion does not do anything to affect affect the
   Wall_clock.is_instanced class attribute)
all caused the failing test to pass. (These are all identified by 
comments in the code.) I cannot understand why these things, not in 
Wall_clock.check_point's doctest, should affect whether its tests pass 
or fail. I am utterly perplexed. I *am* sure there is a bug, but also 
think it much more likely to be mine than Tim Peters' :-)

In addition to the culled-code where I originally observed this 
problem, I have produced a similar bit of skeleton code which works as 
expected. I don't understand why they operate differently. First, my 
culled code, then the skeleton that works.


The Culled Code:
When I run this, the Wall_clock.interval method is tested first, then 
the Wall_clock.check_point method. (Assessed by the commented out 
tests that were intentional failures.) It is the 
Wall_clock.check_point docstring that is the locus of the problem. 
(The heavy edit on the code has robbed the tests and much of the code 
of their sense and rel

Re: [Tutor] UselessPython 2.0

2005-04-09 Thread Dick Moores
Sean Steeg wrote at 11:22 4/7/2005:
So we're requesting that anyone with
one-offs, snippets, mini-apps, full-fledged apps and the like make a
submission to the new and improved UselessPython.  The code doesn't
have to be pretty, but it does have to work.
I'm a newbie, but would this qualify as a contribution to UselessPython 2.0?

#isPalindrome.py
# revision of http://www.uselesspython.com/glpalindrome.py to handle 
palindromes
#   such as "Radar" and "A man, a plan, a canal, Panama!"

# http://www.palindromelist.com/ has many palindromes
# string.ascii_lowercase is 'abcdefghijklmnopqrstuvwxyz'
import string
def isPalindrome(w):
return w == '' or (w[0]==w[-1]) and isPalindrome(w[1:-1]) # recursive
def makeStringAllLowercaseAlpha(s):
"""
Take any string, convert all uppercase alphabetic characters to 
lower case,
then strip all non-alphabetic characters
"""
s1 = string.lower(userstring)
s2 = ""
for index in range(len(s1)):
if s1[index] in string.ascii_lowercase:
s2 += s1[index]
return s2

userstring = raw_input('Enter a word or sentence to test: ')
userstringRevised = makeStringAllLowercaseAlpha(userstring)
if isPalindrome(userstringRevised):
print userstring, "is a palindrome."
else:
print userstring, "is not a palindrome."

Dick Moores
[EMAIL PROTECTED]


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