Re: [Tutor] Perfect Python web stack?

2007-01-19 Thread OkaMthembo

Thanks folks,
Just to add a bit of chaos :-)...i am settled on using Quicksilver:
http://www.quicksilver.net
I guess with so many frameworks out there, it all boils down to specific
implementations and
how much rolling their own one can do.

I hope to benchmark Quicksilver performance and i'll report back.

Happy coding,
"Shortash"

On 1/19/07, Alan Gauld <[EMAIL PROTECTED]> wrote:



"Dave Kuhlman" <[EMAIL PROTECTED]> wrote

> I'm currently learning Pylons.  The tutorials for Pylons discuss
> two object-relational mappers for Python: SQLObject and SQLAlchemy.
> If you want to view your database from a higher, more abstract
> level, you might want to look at them, too.

FWIW.

Both of these are standard in TurboGears too.

SQL Alchemy seems to get the expert users vote. SQLObjects seems
to be older and works OK for the simple stuff I've been playing with
so far.

Alan G.


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





--
"The Stupidry Foundry"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-19 Thread Karl Wittgenstein

Dear Smart Caring Dude,
I've been dabbling into Python for about 6 weeks now.I'm a Social Sciences
student who just got interested in programming and chose Python as first
language.I have little time to practice and I am just getting into
programming concepts,so please be patient,in case you are so kind as to
enlighten this poor soul.
I am trying to write this program which should compare values that are set
by the program  through user's choice to values that the user enters on a
prompt.I use SPE on windows xp,and it tells me that there are indentation
erros on the definitions.Isn't it legal to start a new block of code when
starting a definition?And how come it returns 'variable' not defined,when
they are defined by the = ??Should i make them global?
I would be very grateful to the patient soul that answers these questions,as
my learning interest is sincere and the knowledge sources so disperse.
Here goes the code:

#Ok,this is supposed to be a 2 option choice between values 1 and 4,
#i want the value to determine the variable values inside the function
def porao():
   porao = raw_input()
   if porao == 1 :
   global altura_aeronave = 111
   global largura_aeronave = 112
   global comprimento = 211
   elif porao == 4:
  global altura_aeronave = 112
  global largura_aeronave = 113
  global comprimento = 212
   else:
   print "Porão inexistente"
#These three functions were supposed to get input from user so it can be
compared
#with the values determinated(determined?)above
def largura():
  global largura=input()
def altura():
  global altura=input()
def comprimento():
  global comprimento = input()
#These are the comparison functions
def largura_compativel ():
   if not largura <= largura_aeronave:
   print 'Volume largo demais!'
def altura_compativel ():
   if not altura <= altura_aeronave:
   print 'Volume alto demais!'
def comprimento_compativel ():
   if not comprimento<=comprimento_aeronave:
   print 'Volume comprido demais!'
#Try to run this damn thing,man!1
porao()
largura()
altura()
comprimento()
largura_compativel()
altura_compativel
comprimento_compativel()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Perfect Python web stack?

2007-01-19 Thread Kent Johnson
OkaMthembo wrote:
> Thanks folks,
> Just to add a bit of chaos :-)...i am settled on using Quicksilver: 
> http://www.quicksilver.net

Is that the right URL? It doesn't work for me.

Kent

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


Re: [Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-19 Thread Geoframer

Alright this code is actually littered with bugs, but don't feel bad,
because as you state you are new to the whole programming thing. I'll try to
explain the things i see going wrong (please take into account that i'm also
a new pythonist ;-))

***
You get the error 'variable undefined' because you are declaring a global in
an incorrect manner. The correct manner is to define the variable in the
global scope then start a definition, declare that you are using a global
variable and then assign it. Like this :

bla = 'Hello world'

def func():
   global bla
   print bla
***
If you do :
porao = raw_input()
You'll define the integer value as a string, not an integer. If you are sure
that only an integer will be entered use input() (slower) or
int(raw_input()). In either case you should use try and except clauses more
in your code because if not a 1 or 4 is entered your code will continue
without having the variables declared! Leading to other errors.
***
Now suppose you get the definitions using globals correct then you'll get
new errors stating that an 'int' is not callable.
This happens because you define for example altura as an integer and then
also define a function called altura. If you now use :
altura()
It'll try to call the integer object and will fail.
Easy solution would be to make the function something like alturadef()
***
As for your identation you should use consistent identation... I suggest
using python -t or python -tt to compile your script and it will give
warnings/errors where the identation goes wrong.
***
Here's how i altered your code (quick and dirty probably, it should be more
clean using try and except clauses and what not) to get it to work but you
can probably better look that up in a python book and then return to the
list if you don't understand it :

#Ok,this is supposed to be a 2 option choice between values 1 and 4,
#i want the value to determine the variable values inside the function

altura_aeronave = 0
largura_aeronave = 0
comprimento_aeronave = 0
comprimento = 0
largura = 0
altura = 0

def porao():
   global altura_aeronave, largura_aeronave, comprimento
   porao = input()
   if porao == 1 :
   altura_aeronave = 111
   largura_aeronave = 112
   comprimento = 211
   elif porao == 4:
   altura_aeronave = 112
   largura_aeronave = 113
   comprimento = 212
   else:
   print "Porão inexistente"

#These three functions were supposed to get input from user so it can be
compared
#with the values determinated(determined?)above
def larguradef():
   global largura
   largura=input()

def alturadef():
   global altura
   altura=input()

def comprimentodef():
   global comprimento
   comprimento = input()

#These are the comparison functions
def largura_compativel ():
   global largura, largura_aeronave
   if not largura <= largura_aeronave:
   print 'Volume largo demais!'

def altura_compativel ():
   global altura, altura_aeronave
   if not altura <= altura_aeronave:
   print 'Volume alto demais!'

def comprimento_compativel ():
   global comprimento, comprimento_aeronave
   if not comprimento<=comprimento_aeronave:
   print 'Volume comprido demais!'

#Try to run this damn thing,man!1
porao()
#print altura_aeronave, largura_aeronave, comprimento
larguradef()
alturadef()
comprimentodef()
largura_compativel()
altura_compativel
comprimento_compativel()








On 1/19/07, Karl Wittgenstein <[EMAIL PROTECTED]> wrote:


Dear Smart Caring Dude,
I've been dabbling into Python for about 6 weeks now.I'm a Social Sciences
student who just got interested in programming and chose Python as first
language.I have little time to practice and I am just getting into
programming concepts,so please be patient,in case you are so kind as to
enlighten this poor soul.
I am trying to write this program which should compare values that are set
by the program  through user's choice to values that the user enters on a
prompt.I use SPE on windows xp,and it tells me that there are indentation
erros on the definitions.Isn't it legal to start a new block of code when
starting a definition?And how come it returns 'variable' not defined,when
they are defined by the = ??Should i make them global?
I would be very grateful to the patient soul that answers these
questions,as my learning interest is sincere and the knowledge sources so
disperse.
Here goes the code:

#Ok,this is supposed to be a 2 option choice between values 1 and 4,
#i want the value to determine the variable values inside the function
def porao():
porao = raw_input()
if porao == 1 :
global altura_aeronave = 111
global largura_aeronave = 112
global comprimento = 211
elif porao == 4:
   global altura_aeronave = 112
   global largura_aeronave = 113
   global comprimento = 212
else:
print "Porão inexistente"
#These three functions were supposed to get input from user so it can be
compared
#with the values determinated(determined?)above
def largura():

[Tutor] Getting GID info

2007-01-19 Thread Steve Nelson
Hello all,

I want to produce stats on file ownership.  I am aware that I can use
stat to obtain a file statistics tuple, and with the pwd method I can
convert the UID to username.  However, is there a similar method that
will tell me that GID 1 == "staff"?

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


Re: [Tutor] Getting GID info

2007-01-19 Thread Steve Nelson
On 1/19/07, Steve Nelson <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I want to produce stats on file ownership.  I am aware that I can use
> stat to obtain a file statistics tuple, and with the pwd method I can
> convert the UID to username.  However, is there a similar method that
> will tell me that GID 1 == "staff"?

Hrm... and an inspired guess import grp is what I needed.

> S.

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


Re: [Tutor] Filesystem Usage

2007-01-19 Thread Steve Nelson
On 9/22/06, Steve Nelson <[EMAIL PROTECTED]> wrote:

> In the end I just did:
>
> def fsUsage(dir):
>   """Returns the % usage of a given filesystem"""
>   stat = os.statvfs(dir)
>   from statvfs import F_BLOCKS, F_BFREE
>   total = stat[F_BLOCKS]
>   avail = stat[F_BFREE]
>   used = total-avail
>   percent = used/total*100
>   return percent

Can someone explain how I manged to import F_BLOCKS and F_BFREE?

I want to do the same with pwd and grp:

>>> gstat = grp.getgrgid(1)
>>> dir(gstat)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__',
'__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmul__', '__setattr__', '__str__', 'gr_gid', 'gr_mem',
'gr_name', 'gr_passwd', 'n_fields', 'n_sequence_fields',
'n_unnamed_fields']

>>> gstat[0]
'staff'
>>> gstat[GR_GID]
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'GR_GID' is not defined

What's the difference?

It just seems that specifying the location in the tuple is not very
clear or self-documenting, and using GR_GID is better.

S.
> S.
> >
> > hope this helps a little!
> > -- wesley
> > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> > "Core Python Programming", Prentice Hall, (c)2007,2001
> > http://corepython.com
> >
> > wesley.j.chun :: wescpy-at-gmail.com
> > python training and technical consulting
> > cyberweb.consulting : silicon valley, ca
> > http://cyberwebconsulting.com
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Filesystem Usage

2007-01-19 Thread Kent Johnson
Steve Nelson wrote:
> On 9/22/06, Steve Nelson <[EMAIL PROTECTED]> wrote:
> 
>> In the end I just did:
>>
>> def fsUsage(dir):
>>   """Returns the % usage of a given filesystem"""
>>   stat = os.statvfs(dir)
>>   from statvfs import F_BLOCKS, F_BFREE
>>   total = stat[F_BLOCKS]
>>   avail = stat[F_BFREE]
>>   used = total-avail
>>   percent = used/total*100
>>   return percent
> 
> Can someone explain how I manged to import F_BLOCKS and F_BFREE?

With the statement
   from statvfs import F_BLOCKS, F_BFREE
> 
> I want to do the same with pwd and grp:
> 
 gstat = grp.getgrgid(1)
 dir(gstat)
> ['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
> '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__',
> '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
> '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
> '__repr__', '__rmul__', '__setattr__', '__str__', 'gr_gid', 'gr_mem',
> 'gr_name', 'gr_passwd', 'n_fields', 'n_sequence_fields',
> 'n_unnamed_fields']
> 
 gstat[0]
> 'staff'
 gstat[GR_GID]
> Traceback (most recent call last):
>   File "", line 1, in ?
> NameError: name 'GR_GID' is not defined

Try gstat.gr_gid, etc
> 
> What's the difference?

F_BLOCKS is an attribute of the statvfs module; it is a constant giving 
the offset into the stats structure.

gr_gid is an attribute of the gstat object, it is the actual value that 
you want.

The objects returned by statvfs and getgrgid are unusual in that their 
attributes can be accessed in two ways, as list values and as object 
attributes.

The list-like access uses brackets [] and an index. In the case of 
statvfs, the constants are named in the statvfs module; in the case of 
getgrgid() there don't seem to be symbolic names available.

The attribute access uses . notation and an attribute name. You can do 
stat.f_blocks and gstat.gr_gid. Python takes care of looking up the 
actual attribute value.

I suggest you use the attribute form for both, it is more compact and 
readable, consistent between both types of objects, and doesn't require 
the import of statvfs.

Kent

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


Re: [Tutor] Filesystem Usage

2007-01-19 Thread Steve Nelson
On 1/19/07, Kent Johnson <[EMAIL PROTECTED]> wrote:

> The attribute access uses . notation and an attribute name. You can do
> stat.f_blocks and gstat.gr_gid. Python takes care of looking up the
> actual attribute value.

Excellent - thank you.

> I suggest you use the attribute form for both, it is more compact and
> readable, consistent between both types of objects, and doesn't require
> the import of statvfs.

Done.

> Kent

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


Re: [Tutor] Perfect Python web stack?

2007-01-19 Thread OkaMthembo

Im thoroughly embarrassed. its http://www.clearsilver.net and not
quicksilver as i said earlier.

thanks Kent.

"Shortash"

On 1/19/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


OkaMthembo wrote:
> Thanks folks,
> Just to add a bit of chaos :-)...i am settled on using Quicksilver:
> http://www.quicksilver.net

Is that the right URL? It doesn't work for me.

Kent





--
"The Stupidry Foundry"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-19 Thread Geoframer

Accidentally the number of function definitions does not help the clarity of
the program, even though it's completely unclear to me what exactly you are
trying to do. :-)
I rewrote your program a bit combining the definition of the functions, the
comparision of the functions and removed the chance that your program goes
on the define/compare values that have not been defined.

*** Insert redone code ***
altura_aeronave = 0
largura_aeronave = 0
comprimento_aeronave = 0
comprimento = 0
largura = 0
altura = 0

def compativel():
   global altura, altura_aeronave, comprimento, comprimento_aeronave, \
  largura, largura_aeronave
   if not largura <= largura_aeronave:
   print 'Volume largo demais!'
   elif not altura <= altura_aeronave:
   print 'Volume alto demais!'
   elif not comprimento<=comprimento_aeronave:
   print 'Volume comprido demais!'

def define():
   global largura, altura, comprimento
   largura=input()
   altura=input()
   comprimento=input()

def porao():
   global altura_aeronave, largura_aeronave, comprimento
   porao = input()
   if porao == 1 :
   altura_aeronave = 111
   largura_aeronave = 112
   comprimento = 211
   return 1
   elif porao == 4:
   altura_aeronave = 112
   largura_aeronave = 113
   comprimento = 212
   return 1
   else:
   print "Porao inexistente!"


if porao():
   define()
   compativel()

On 1/19/07, Geoframer <[EMAIL PROTECTED]> wrote:


Alright this code is actually littered with bugs, but don't feel bad,
because as you state you are new to the whole programming thing. I'll try to
explain the things i see going wrong (please take into account that i'm also
a new pythonist ;-))

***
You get the error 'variable undefined' because you are declaring a global
in an incorrect manner. The correct manner is to define the variable in the
global scope then start a definition, declare that you are using a global
variable and then assign it. Like this :

bla = 'Hello world'

def func():
global bla
print bla
***
If you do :
porao = raw_input()
You'll define the integer value as a string, not an integer. If you are
sure that only an integer will be entered use input() (slower) or
int(raw_input()). In either case you should use try and except clauses more
in your code because if not a 1 or 4 is entered your code will continue
without having the variables declared! Leading to other errors.
***
Now suppose you get the definitions using globals correct then you'll get
new errors stating that an 'int' is not callable.
This happens because you define for example altura as an integer and then
also define a function called altura. If you now use :
altura()
It'll try to call the integer object and will fail.
Easy solution would be to make the function something like alturadef()
***
As for your identation you should use consistent identation... I suggest
using python -t or python -tt to compile your script and it will give
warnings/errors where the identation goes wrong.
***
Here's how i altered your code (quick and dirty probably, it should be
more clean using try and except clauses and what not) to get it to work but
you can probably better look that up in a python book and then return to the
list if you don't understand it :

#Ok,this is supposed to be a 2 option choice between values 1 and 4,
#i want the value to determine the variable values inside the function

altura_aeronave = 0
largura_aeronave = 0
comprimento_aeronave = 0
comprimento = 0
largura = 0
altura = 0

def porao():
global altura_aeronave, largura_aeronave, comprimento
porao = input()
if porao == 1 :
altura_aeronave = 111
largura_aeronave = 112
comprimento = 211
elif porao == 4:
altura_aeronave = 112
largura_aeronave = 113
comprimento = 212
else:
print "Porão inexistente"

#These three functions were supposed to get input from user so it can be
compared
#with the values determinated(determined?)above
def larguradef():
global largura
largura=input()

def alturadef():
global altura
altura=input()

def comprimentodef():
global comprimento
comprimento = input()

#These are the comparison functions
def largura_compativel ():
global largura, largura_aeronave
if not largura <= largura_aeronave:
print 'Volume largo demais!'

def altura_compativel ():
global altura, altura_aeronave
if not altura <= altura_aeronave:
print 'Volume alto demais!'

def comprimento_compativel ():
global comprimento, comprimento_aeronave
if not comprimento<=comprimento_aeronave:
print 'Volume comprido demais!'

#Try to run this damn thing,man!1
porao()
#print altura_aeronave, largura_aeronave, comprimento
larguradef()
alturadef()
comprimentodef()
largura_compativel()
altura_compativel
comprimento_compativel()








On 1/19/07, Karl Wittgenstein <[EMAIL PROTECTED] > wrote:

> Dear Smart Caring Dude,
> I've been dabbling into Python for about 

[Tutor] MD5 Digest for files

2007-01-19 Thread Steve Nelson
Hello,

I want to create a dictionary of files and md5sums for a given
directory.  It seems, however, that md5 works with strings or
read-only buffers, and can't be passed a file.

What I want to do is something like:

for f is os.listdir("."):
  d[f] = someFunctionThatReturnsMD5Sum(f)

Has this wheel already been invented?  I can't see how to operate on
the file itself.

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


Re: [Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-19 Thread Geoframer

As i stated i'm somewhat new to python myself so i defined the variables  in
a somewhat old fashion manner. The correct code is below. I also added some
comments because i think you defined the wrong variable somewhere also...
Because your code led to comprimento being compared to comprimento_aeronave
which was never defined anywhere, so i took it you ment comprimento_aeronave
there

This code below is fully functional without any errors (not even identation
warnings ;-) ). So if this doesn't work at your computer something else is
possible wrong. Incidentally if you hit the following sequence : 1 2 3 4.
The program just terminates without a message... You'd have to insert what's
happening in such a case yourself.

 insert code 
global altura_aeronave, largura_aeronave, comprimento_aeronave, comprimento,
\
  largura, altura

def compativel():
   global altura, altura_aeronave, comprimento, comprimento_aeronave, \
  largura, largura_aeronave
   if not largura <= largura_aeronave:
   print 'Volume largo demais!'
   elif not altura <= altura_aeronave:
   print 'Volume alto demais!'
   elif not comprimento<=comprimento_aeronave:
   print 'Volume comprido demais!'

def define():
   global largura, altura, comprimento
   largura=input()
   altura=input()
   comprimento=input()

def porao():
   global altura_aeronave, largura_aeronave, comprimento_aeronave
   porao = input()
   if porao == 1 :
   altura_aeronave = 111
   largura_aeronave = 112
   comprimento_aeronave = 211 #You originally had comprimento here?
   return 1
   elif porao == 4:
   altura_aeronave = 112
   largura_aeronave = 113
   comprimento_aeronave = 212 #Same here
   return 1
   else:
   print "Porao inexistente!"


if porao():
   define()
   compativel()

*** end inserted code ***

HTH - Geoframer

On 1/19/07, Geoframer <[EMAIL PROTECTED]> wrote:


Accidentally the number of function definitions does not help the clarity
of the program, even though it's completely unclear to me what exactly you
are trying to do. :-)
I rewrote your program a bit combining the definition of the functions,
the comparision of the functions and removed the chance that your program
goes on the define/compare values that have not been defined.

*** Insert redone code ***
altura_aeronave = 0
largura_aeronave = 0
comprimento_aeronave = 0
comprimento = 0
largura = 0
altura = 0

def compativel():
global altura, altura_aeronave, comprimento, comprimento_aeronave, \
   largura, largura_aeronave
if not largura <= largura_aeronave:
print 'Volume largo demais!'
elif not altura <= altura_aeronave:
print 'Volume alto demais!'
elif not comprimento<=comprimento_aeronave:
print 'Volume comprido demais!'

def define():
global largura, altura, comprimento
largura=input()
altura=input()
comprimento=input()

def porao():
global altura_aeronave, largura_aeronave, comprimento
porao = input()
if porao == 1 :
altura_aeronave = 111
largura_aeronave = 112
comprimento = 211
return 1
elif porao == 4:
altura_aeronave = 112
largura_aeronave = 113
comprimento = 212
return 1
else:
print "Porao inexistente!"


if porao():
define()
compativel()

On 1/19/07, Geoframer <[EMAIL PROTECTED]> wrote:
>
> Alright this code is actually littered with bugs, but don't feel bad,
> because as you state you are new to the whole programming thing. I'll try to
> explain the things i see going wrong (please take into account that i'm also
> a new pythonist ;-))
>
> ***
> You get the error 'variable undefined' because you are declaring a
> global in an incorrect manner. The correct manner is to define the variable
> in the global scope then start a definition, declare that you are using a
> global variable and then assign it. Like this :
>
> bla = 'Hello world'
>
> def func():
> global bla
> print bla
> ***
> If you do :
> porao = raw_input()
> You'll define the integer value as a string, not an integer. If you are
> sure that only an integer will be entered use input() (slower) or
> int(raw_input()). In either case you should use try and except clauses more
> in your code because if not a 1 or 4 is entered your code will continue
> without having the variables declared! Leading to other errors.
> ***
> Now suppose you get the definitions using globals correct then you'll
> get new errors stating that an 'int' is not callable.
> This happens because you define for example altura as an integer and
> then also define a function called altura. If you now use :
> altura()
> It'll try to call the integer object and will fail.
> Easy solution would be to make the function something like alturadef()
> ***
> As for your identation you should use consistent identation... I suggest
> using python -t or python -tt to compile your script and it will give
> warnings/errors where the identat

Re: [Tutor] MD5 Digest for files

2007-01-19 Thread Christopher Arndt
Steve Nelson schrieb:
> I want to create a dictionary of files and md5sums for a given
> directory.  It seems, however, that md5 works with strings or
> read-only buffers, and can't be passed a file.
> 
> What I want to do is something like:
> 
> for f is os.listdir("."):
>   d[f] = someFunctionThatReturnsMD5Sum(f)
> 
> Has this wheel already been invented?  I can't see how to operate on
> the file itself.

Just open and read in the file and then calculate the MD5 sum from the contents:

import os
import md5

def md5sums_for_dir(directory):
d = {}
for fn in os.listdir(directory):
fp = os.path.join(directory, fn)
if os.path.isfile(fp):
try:
fo = open(fp, 'rb')
except (IOError, OSError):
print "Could not open file '%s', skipping..." % fp
continue
else:
fcontent = fo.read()
digest = md5.new()
digest.update(fcontent)
d[fn] = digest.hexdigest() # or .digest()
# the above four lines can be shortened to:
d[fn] = md5.new(fo.read()).hexdigest()
fo.close()
return d


if __name__ == '__main__':
import sys
from pprint import pprint
pprint(md5sums_for_dir(sys.argv[1]))

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


Re: [Tutor] 'elp!!!!!!!1Totally Clueless Newbie In Distress

2007-01-19 Thread Danny Yoo

> I've been dabbling into Python for about 6 weeks now.I'm a Social 
> Sciences student who just got interested in programming and chose Python 
> as first language.

Out of curiosity, what materials are you using to learn how to program?



> Isn't it legal to start a new block of code when starting a 
> definition?And how come it returns 'variable' not defined,when they are 
> defined by the = ??Should i make them global?

Wait, wait.  I think you may be misunderstanding the use of 'global'. 
You should not be using global unless you really need it.



I see three variables here that you are interested in:

 altura_aeronave
 largura_aeronave
 comprimento

Are these always collected together?  If they are related, you should have 
a single structure that holds them together, rather than represent them as 
three separate variables.


Concretely, you can represent these three values as a single tuple.  You 
can think of it as a "vector" from your mathematics class.  For example:

#
def make_measure(start, stop):
 """make_measure: number number -> measure
 Creates a new measure from start and stop."""
 return (start, stop)

def measure_start(a_measure):
 """measure_start: measure -> number
 Selects the start portion of a measure."""
 return a_measure[0]

def measure_stop(a_measure):
 """measure_end: measure -> end
 Selects the stop portion of a measure."""
 return a_measure[1]
#


That is, these functions take inputs and produce outputs.  That should be 
a concept that you are familiar with from your previous experience:

 f(x) = 2x (math notation)

is a function that takes a number and produces the double of that number. 
We write this in Python as:


def double(x):
 """double: number -> number
 Returns the double of x."""
 return x * 2



Getting back to the measure example: once we have these functions to build 
measures and take them apart, we can then use these like this:


## Small test program
m1 = make_measure(3, 4)
m2 = make_measure(17, 42)
print "m1", measure_start(m1), measure_stop(m1)
print "m2", measure_start(m2), measure_stop(m2)


If we dislike the duplication of those last two statements here, we can 
create a function that doesn't produce an output, but it still takes 
input:



def print_measure(header_name, a_measure):
 """print_measure: measure string -> None
 Prints out the measurement.
 """
 print header_name, measure_start(a_measure), measure_stop(a_measure)



After we define this helper function "print_measure()", our little program 
can now look like this:

#
## Small test program
m1 = make_measure(3, 4)
m2 = make_measure(17, 42)
print_measure("m1", m1)
print_measure("m2", m2)
#

Notice that, here, we do not need to say anything about "globals" to make 
effective programs.  We are simply passing values back and forth as 
parameters.


Does this make sense so far?  If you have any questions, please feel free 
to ask.  Please continue to reply to Tutor by using your email client's 
Reply to All feature.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] (no subject)

2007-01-19 Thread Max Jameson
I know this is totally off-the-wall, but what am I doing wrong here?  I am 
using Python 2.5, and I am just learning...please be kind!


>>> aList = [2,3]
>>> bList = [5,7,9]
>>> aList.append(bList)
>>> print aList
[2, 3, [5, 7, 9]]
>>> print aList [1] [2]
Traceback (most recent call last):
  File "", line 1, in 
print aList [1] [2]
TypeError: 'int' object is unsubscriptable
>>> print aList [0] [1]
Traceback (most recent call last):
  File "", line 1, in 
print aList [0] [1]
TypeError: 'int' object is unsubscriptable
>>>___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2007-01-19 Thread Danny Yoo


On Fri, 19 Jan 2007, Max Jameson wrote:

> I know this is totally off-the-wall, but what am I doing wrong here?  I 
> am using Python 2.5, and I am just learning...please be kind!


Hi Max,

Let's take a look.


###
>>> aList = [2,3]
>>> bList = [5,7,9]
>>> aList.append(bList)
>>> print aList
[2, 3, [5, 7, 9]]
###

Ok, looks good so far.  You have a list of three elements:

 2
 3
 [5, 7, 9]

where the third element in the list is itself a list.



###
>>> print aList [1] [2]
Traceback (most recent call last):
   File "", line 1, in 
 print aList [1] [2]
TypeError: 'int' object is unsubscriptable
###

Can you explain what you're trying to do at this point?  Pretend that we 
are clueless for the moment.

What are you expecting to get out from this?  We know you don't want to 
get the error, of course.  But what do you want to get?


Also, have you had a chance to look at something like:

http://swaroopch.info/text/Byte_of_Python:Data_Structures
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject) (fwd)

2007-01-19 Thread Danny Yoo
[Forwarding to tutor.  Can someone answer Max?  Slightly busy at the 
moment.  Quick note: get him to realize that the list he was playing with 
originally had three elements.  The example in his book has two elements. 
Something has to change.  *grin*]



-- Forwarded message --
Date: Fri, 19 Jan 2007 10:10:04 -0800 (PST)
From: Max Jameson <[EMAIL PROTECTED]>
To: Danny Yoo <[EMAIL PROTECTED]>
Subject: Re: [Tutor] (no subject)

Thanks, Danny.

I expected to get the third element of the second list printed, as shown on the 
on-line tutorial I am reading: http://www.freenetpages.co.uk/hp/alan.gauld/

This is exactly how the tutorial reads (I omited the definition of the aList 
and anoterh):

aList.append(another)
print aList

[42, [1, 2, 7]]

Notice how the result is a list of two elements but the second element is itself a list (as shown by the []¢s around it). We can now access the element 7 by using a double index: 

print aList[1][2]

7

The first index, 1, extracts the second element which is in turn a list. The 
second index, 2, extracts the third element of the sublist.


- Original Message 
From: Danny Yoo <[EMAIL PROTECTED]>
To: Max Jameson <[EMAIL PROTECTED]>
Cc: tutor@python.org
Sent: Friday, January 19, 2007 11:41:53 AM
Subject: Re: [Tutor] (no subject)


On Fri, 19 Jan 2007, Max Jameson wrote:

I know this is totally off-the-wall, but what am I doing wrong here?  I 
am using Python 2.5, and I am just learning...please be kind!



Hi Max,

Let's take a look.


###

aList = [2,3]
bList = [5,7,9]
aList.append(bList)
print aList

[2, 3, [5, 7, 9]]
###

Ok, looks good so far.  You have a list of three elements:

 2
 3
 [5, 7, 9]

where the third element in the list is itself a list.



###

print aList [1] [2]

Traceback (most recent call last):
   File "", line 1, in 
 print aList [1] [2]
TypeError: 'int' object is unsubscriptable
###

Can you explain what you're trying to do at this point?  Pretend that we 
are clueless for the moment.


What are you expecting to get out from this?  We know you don't want to 
get the error, of course.  But what do you want to get?



Also, have you had a chance to look at something like:

http://swaroopch.info/text/Byte_of_Python:Data_Structures___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is a mixin class?

2007-01-19 Thread Danny Yoo
> Wow, Danny.  I am impressed.  I am not sure that this should not be 
> called a 'mash-up' rather than a 'mix-in' but it has really been food 
> for thought.

Thank you, but this isn't an original contribution.  I mostly adapted the 
stuff in papers like:

 http://www.cs.utah.edu/plt/publications/aplas06-fff.pdf


> What else have you got stashed away?  Have you got a book on the way?

Not yet.


> You should at least put this recipe into the Python Cookbook. There is 
> not much on mix-ins in there.

The example I gave isn't quite right because of the thread-safety issue. 
I should revisit that observer code later to handle thread safety before 
putting it into the Cookbook.


Best of wishes!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject) (fwd)

2007-01-19 Thread Eric Walstad
Hey Max,
Danny Yoo wrote:
> [Forwarding to tutor.  Can someone answer Max?  Slightly busy at the
> moment.  Quick note: get him to realize that the list he was playing
> with originally had three elements.  The example in his book has two
> elements. Something has to change.  *grin*]
> 
> 
> -- Forwarded message --
> Date: Fri, 19 Jan 2007 10:10:04 -0800 (PST)
> From: Max Jameson <[EMAIL PROTECTED]>
> To: Danny Yoo <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] (no subject)
> 
[...]
> This is exactly how the tutorial reads (I omited the definition of the
> aList and anoterh):
 aList.append(another)
 print aList
> [42, [1, 2, 7]]
I suspect I didn't see the definition of 'aList' on Alan's website, but
the values look different than what you posted originally.

Based on the output above, I'd say:
aList = [42,]
another = [1, 2, 7]

Whereas your definition, in your original email:
aList = [2,3]
bList = [5,7,9]

Do you see the difference between the two (aside from the different
variable names)?


> I expected to get the third element of the second list printed
In your example, that would be the 'second' item in your bList (because
lists are zero-based):
bList[2] == 9

After 'append'ing bList to aList, aList has three items.  Try this at
the python command line:
print aList[0]
print aList[1]
print aList[2]

Two of those will be integers, One will be a list.

I hope that helps.

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


Re: [Tutor] (no subject)

2007-01-19 Thread Kent Johnson
Max Jameson wrote:
> I know this is totally off-the-wall, but what am I doing wrong here?  I 
> am using Python 2.5, and I am just learning...please be kind!
> 
>  
> 
> 
>  >>> aList = [2,3]
>  >>> bList = [5,7,9]
>  >>> aList.append(bList)
>  >>> print aList
> [2, 3, [5, 7, 9]]
>  >>> print aList [1] [2]

What did you expect to happen here?
alist[1][2] is the same as (alist[1])[2]; in other words, it is the 
third item in the second item in alist. The second item (alist[1]) is 
the integer 3 which is not subscriptable; if you try 3[2] at the 
interactive prompt you will get the same error.

If you are trying to get the elements of bList out of aList you should 
try alist[2][0], alist[2][1] etc.

Kent

> 
> Traceback (most recent call last):
>   File "", line 1, in 
> print aList [1] [2]
> TypeError: 'int' object is unsubscriptable
>  >>> print aList [0] [1]
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> print aList [0] [1]
> TypeError: 'int' object is unsubscriptable
>  >>>
> 
> 
> 
> 
> ___
> 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] Correct Way to Start a Python Program Under Linux

2007-01-19 Thread pytutor . 20 . 247ob
>  wrote
> 
> I'm not sure what you mean by a filelauncher.
> In my window manager I just add the file path to my menu
> by editing a dialog box or config file. which environment are you 
> using?

Im using gnome and creating a 'custom application launcher' by rt click
a panel and choosing 'add to panel'. It is just a case of adding a file
path, the only option is you can run in a terminal (which works but
fires up a terminal window which I do not want)

> > my program uses subprocess.Popen({command string}, etc)
> > and these calls to Popen do not work, only if I run the program
> from 
> > a
> > shell window.
> 
> How do you know it doesn't work? Do you get an error or just silence?
> If you launch a program that writes to stdout then you had better
> be sure it has a stdout to write to! Are you sure its not just
> dumping its output to /dev/null?

when it's working you can see the processes created by doing 'ps -ef'
in another shell window, the python program spawns a child process
running tcpdump.

> OTOH it could also be that the
> import
> path is not set correctly(try checking your PYTHONPATH environment
> variable)

from a shell window typing echo $PYTHONPATH returns nothing, is this
normal, should I be setting this or is it optional? 
from within a python session importing sys and typing > sys.path
I get a long list of paths which I assume is right as its searching and
finding all the standard library modules.

> Can you give us a bit more info about what you mean by the
> statement that it doesn't work?

I mean the call to subprocess.Popen  should create a new process
when viewed with 'ps -ef' from another command window, normally I see
the new process running the command 'tcpdump .' but when running
the application from the launcher this just does not happen. The main
pygtk program is running fine, just the new process does not start.

> As a check try writing a very simple program with no external
> dependencies and see if it works...

You have helped crack it, I just wrote a small program like this, it
reads 400 packets from the eth0 interface saving it to a file:


#!/usr/bin/env python
#test prog
import curses.ascii, sys, os, subprocess, tempfile
def Readtraffic():
myerr = tempfile.TemporaryFile()
myout = tempfile.TemporaryFile()
command = "sudo /usr/sbin/tcpdump -c 400 -q -t -s 192 -i eth0 -w
/var/tmp/testdump -Z wayne"
myproc = subprocess.Popen(command, executable="/bin/bash",
shell=True, stderr=myerr, stdout=myout)
return (myproc)

proc = Readtraffic()
stat = proc.poll()
print "stat = " + str(stat)

while stat == None:
stat = proc.poll()
print "stat = " + str(stat)
print "finished"


This works fine, using another shell window I can see the subprocess
and tcpdump running under a new pid as expected, it's VERY similar to
the code in the program (which has been chopped about quite a bit by
now) with the difference being I changed the variable 'command' to be a
pure text string instead of a concatenation of variables and strings.
 
Substituting the code above into my program works! 
Now the puzzling bit identifying the actual error,

the code below works until I un-comment the line below '#oldcommand
...', I have even changed the variable name to 'oldcommand' so in
effect it's not even used, but just uncommenting it causes the problem,
ie, the subprocess does not start up when run from an application
launcher (yet still works when started from a shell!).


#oldcommand = "sudo " + self.exe['tcpdump'] + " -c " + str(packets) + "
-q -t -s " + str(packetsize) \
#+ " -i " + self.nic + " -w " + self.savepath + self.dumpfilename + "
-Z " + os.getlogin()
command = "sudo /usr/sbin/tcpdump -c 400 -q -t -s 192 -i eth0 -w
/var/tmp/testdump -Z wayne"
myproc = subprocess.Popen(command, executable="/bin/bash", shell=True,
stderr=myerr, stdout=myout)
return (myproc)


After going through the command piece by piece I found the problem, the
call to os.getlogin() works fine when run from a shell BUT fails when
run from a launcher, back to the docs I found:

user = pwd.getpwuid(os.getuid()) [0]

and this works!!!

Thanks for your help,

Wayne.




___ 
What kind of emailer are you? Find out today - get a free analysis of your 
email personality. Take the quiz at the Yahoo! Mail Championship. 
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Set changing order of items?

2007-01-19 Thread Adam Cripps
I'm adding strings to a Set to prevent duplicates. However, the
strings are meant to be in the correct order. When I add the string to
the Set, the order seems to change (and I don't seem to be able to
predict what order they are in).

The string is a simple addition question which should look like this
1) 20 + 3 = 23

I've kept the question ("1) 20 + ") separate from the answer ("23") so
that I can hide the answers later on.

The code that produces this is:

problem_set.add(str(count) +") "+ str(a)+ " + "+ str(b)+ " = ")

problem_set.add(str(a + b) + "\n")

However str(problem_set) returns something more like:
(['18\n', '2) 6 + 9 = ', '15\n', '1) 9 + 9 = '])

Is there a better way of organising this? Any suggestions gratefully
received.  In writing this email, I've realised that already I run the
risk of having different problems (20 + 1 and 19 + 2) which have the
same answer (21) which won't be allowed in the set.

The whole code can be seen at[1].

TIA
Adam
-- 
http://www.monkeez.org
PGP key: 0x7111B833

[1]
def add (lowrange, hirange, i):
"""Returns a list of additions for i iterations."""
count = 1
sums = []
problem_set = set()
while len(problem_set)<=i*2:
#while count <=i:
a = randint(lowrange, hirange)
b = randint (lowrange, hirange)
problem_set.add(str(count) +") "+ str(a)+ " + "+ str(b)+ " = ")
problem_set.add(str(a + b) + "\n")
count = count + 1
#sums.append(sum)
print "len problem_set " + str(len(problem_set))
print "problem_set " + str(problem_set)
sums = list(problem_set)
print "len sums " + str(len(sums))
#sums1 = sums.reverse()
print "sums " + str(sums)
return sums
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Set changing order of items?

2007-01-19 Thread Simon Brunning
On 1/19/07, Adam Cripps <[EMAIL PROTECTED]> wrote:
> I'm adding strings to a Set to prevent duplicates. However, the
> strings are meant to be in the correct order. When I add the string to
> the Set, the order seems to change (and I don't seem to be able to
> predict what order they are in).

Sets, like dictionaries, hold their items in an arbitrary order - see
.

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


Re: [Tutor] Set changing order of items?

2007-01-19 Thread Adam Cripps
On 1/19/07, Simon Brunning <[EMAIL PROTECTED]> wrote:
> On 1/19/07, Adam Cripps <[EMAIL PROTECTED]> wrote:
> > I'm adding strings to a Set to prevent duplicates. However, the
> > strings are meant to be in the correct order. When I add the string to
> > the Set, the order seems to change (and I don't seem to be able to
> > predict what order they are in).
>
> Sets, like dictionaries, hold their items in an arbitrary order - see
> .

OK - thanks for that - so it seems that using a Set will complicate
the matter more.

Really, I want to have a set of sets which won't have any duplicates.
An example might look something ilke this:

sums = [['1) 10 + 2 =', '12'], ['2) 13 + 4 =', '17']]
return sums

How might I achieve this list, without the duplicates (the duplicate
bit is the bit I'm stuck on).

Adam
-- 
http://www.monkeez.org
PGP key: 0x7111B833
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] messagebox positioning

2007-01-19 Thread Ketan Maheshwari
Hi! All:
tkMessageBox.showinfo("done", "You are done!!!")
I need to show a message similar to this when some activity is done.
However, I want to position this message box in a different part of 
the screen so that it does not occlude the background.
How could I do it?

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


Re: [Tutor] Set changing order of items?

2007-01-19 Thread Danny Yoo


On Fri, 19 Jan 2007, Adam Cripps wrote:

> I'm adding strings to a Set to prevent duplicates. However, the strings 
> are meant to be in the correct order. When I add the string to the Set, 
> the order seems to change (and I don't seem to be able to predict what 
> order they are in).


Hi Adam,

You can create your own data structure.  You don't have to work with raw 
data structures: you can make your own.


It sounds like you want a few operations from this data structure, which 
we'll call "UniqueList" for the moment:

 append(x): add x to the uniqueList if we don't already have seen it.

 elements(): return all the unique elements, in the order that we saw
 them.

Does that sound ok, or do you need more operations?



We can even start writing test cases for this, even without writing 
implementation:

#
def simple_test():
ulist = UniqueList()
ulist.append("a")
ulist.append("b")
ulist.append("b")
ulist.append("c")
assert ["a", "b", "c"] == ulist.elements()
#

Would the behavior here be reasonable to you?



There's a naive way to implement this, which is:

##
class UniqueList:
 def __init__(self):
 self.elts = []

 def append(self, item):
 if item not in self.elts:
 self.elts.append(item)

 def elements(self):
 return self.elts
##


And now we can make instances of UniqueLists, and we can see that it 
passes our simple test function:

###
>>> simple_test()
>>> 
>>> ulist = UniqueList()
>>> ulist.append("hello")
>>> ulist.append("hello")
>>> ulist.append("world")
>>> ulist.append("testing")
>>> ulist.append("hello")
>>> ulist.append("world")
>>> ulist
<__main__.UniqueList instance at 0xb7d1c3cc>
>>> ulist.elements()
['hello', 'world', 'testing']
###


Would you be able to work with UniqueList here?  Would it do the job for 
you?

If not, then we should find out why.  Only after we get the functionality 
down should we think about efficiency.  It does us no good to make things 
fast if they don't do what you want.


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


Re: [Tutor] Set changing order of items?

2007-01-19 Thread Python
On Fri, 2007-01-19 at 20:05 +, Adam Cripps wrote:
> On 1/19/07, Simon Brunning <[EMAIL PROTECTED]> wrote:
> > On 1/19/07, Adam Cripps <[EMAIL PROTECTED]> wrote:
> > > I'm adding strings to a Set to prevent duplicates. However, the
> > > strings are meant to be in the correct order. When I add the string to
> > > the Set, the order seems to change (and I don't seem to be able to
> > > predict what order they are in).
> >
> > Sets, like dictionaries, hold their items in an arbitrary order - see
> > .
> 
> OK - thanks for that - so it seems that using a Set will complicate
> the matter more.
> 
> Really, I want to have a set of sets which won't have any duplicates.
> An example might look something ilke this:
> 
> sums = [['1) 10 + 2 =', '12'], ['2) 13 + 4 =', '17']]
> return sums

It looks like you have embedded the numbering into your strings.  If the
numbers were removed from the strings would you still care about keeping
the strings in order?

>>> sums = set([('10 + 2 =', '12'), ('13 + 4 =', '17')])

I changed the inside lists of pairs to tuples of pairs

>>> for ndx,(query,answer) in enumerate(sums):
... print "%d) %s %s" % (ndx+1, query, answer)
... 
1) 10 + 2 = 12
2) 13 + 4 = 17


> 
> How might I achieve this list, without the duplicates (the duplicate
> bit is the bit I'm stuck on).
> 
> Adam
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Set changing order of items?

2007-01-19 Thread Kent Johnson
Adam Cripps wrote:
> On 1/19/07, Simon Brunning <[EMAIL PROTECTED]> wrote:
>> On 1/19/07, Adam Cripps <[EMAIL PROTECTED]> wrote:
>>> I'm adding strings to a Set to prevent duplicates. However, the
>>> strings are meant to be in the correct order. When I add the string to
>>> the Set, the order seems to change (and I don't seem to be able to
>>> predict what order they are in).
>> Sets, like dictionaries, hold their items in an arbitrary order - see
>> .
> 
> OK - thanks for that - so it seems that using a Set will complicate
> the matter more.
> 
> Really, I want to have a set of sets which won't have any duplicates.
> An example might look something ilke this:
> 
> sums = [['1) 10 + 2 =', '12'], ['2) 13 + 4 =', '17']]
> return sums
> 
> How might I achieve this list, without the duplicates (the duplicate
> bit is the bit I'm stuck on).

What is your criterion for uniqueness? Are you looking at just the sum, 
as you mentioned earlier, or the whole problem? Why is the order 
important, since the problems are randomly generated?

You should think about a more abstract way of representing a problem. 
For example, the two problems above could be represented as the tuples
(10, 2, 12) and (13, 4, 17). If you want the whole problems to be 
unique, then creating a set of tuples like this should do what you want. 
(I used tuples instead of lists because you can't make a set of lists.)

If you want only one problem with a given sum, then I would represent a 
problem as an entry in a dictionary where the sum is the key and the 
value is a tuple containing the two addends. Then you can keep putting 
problems in the dictionary until it is the size you want.

Once you have the final set (or dict) of problems, then you can convert 
them to a string representation and print them.

Kent

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


[Tutor] wixi.cc to teach python?

2007-01-19 Thread duke
hi.. http://wixi.cc is an idea to teach python
using a real-world example of a real-world software
for the OLPC: http://wiki.laptop.org/go/wixi

also: for dutch developers there's a €30K opportunity
http://wixi.cc/index.php?title=digital_pioneers
but deadline is soon (5 FEB)

feedback welcome.. thanks

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


Re: [Tutor] Redirect from a CGI script

2007-01-19 Thread Paulino

Thank you Andre,

well it doesn't work either!

Paulino

Andre Engels escreveu:
2007/1/18, Paulino <[EMAIL PROTECTED]>:

How can i redirect to another URL from a python CGI script.

Is's suposed to be as simply as:

print "Location : http://newurl "
It's not working.

this simple code does't work - < redir.pyw>
'print "Content-Type:text/html\n\n"
'print "Location : /cgi-bin/ecodiv.pyw "
'print

I use CGIHTTPServer, the server script is as follows:

'from BaseHTTPServer import HTTPServer
'from CGIHTTPServer import CGIHTTPRequestHandler
'HTTPServer(("localhost", 80), CGIHTTPRequestHandler).serve_forever()

instead of redirecting, it only prints 'Location :
/cgi-bin/ecodiv.pyw' inthe
browser



I haven't tested it, but I think I had a similar error recently, and 
that was solved by removing the \n\n at the end of the Content-Type 
line. You could try that.





--
Andre Engels, [EMAIL PROTECTED] 
ICQ: 6260644  --  Skype: a_engels 


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


Re: [Tutor] Redirect from a CGI script

2007-01-19 Thread Python
On Sat, 2007-01-20 at 02:10 +, Paulino wrote:
> Thank you Andre,
> 
> well it doesn't work either!

This works, 

#!/usr/bin/python
print "Location:http://python.org/\r\n\r";

as does this

#!/usr/bin/python
print "Content-type:text/html\r"
print "Location:http://python.org/\r\n\r";


Tested using Apache on Linux.  A redirect should have a 3xx status.
Apache saw the location header and fixed the status to be 302.  My
browser (firefox) received the 302 status with the new location and
requested the new URL.

Each header line should be separated by \r\n.  A Python print on linux
will only output \n.  In actual practice, that appears to work OK.
either the browsers tolerate the missing \r or Apache fixes the data
stream.

Firefox plugins called tamperdata and liveheaders can be very helpful
for debugging these kinds of interactions.

Get the redirect to a real web site working.  Then fix it to redirect to
your script.  Use tamperdata to see what is going on if you have trouble
making it work.

> 
> Paulino
> > Andre Engels escreveu:
> > 2007/1/18, Paulino <[EMAIL PROTECTED]>: 
> > How can i redirect to another URL from a python CGI script.
> > 
> > Is's suposed to be as simply as:
> > 
> > print "Location : http://newurl "
> > It's not working.
> > 
> > this simple code does't work - < redir.pyw>
> > 'print "Content-Type:text/html\n\n"
> > 'print "Location : /cgi-bin/ecodiv.pyw "
> > 'print
> > 
> > I use CGIHTTPServer, the server script is as follows:
> > 
> > 'from BaseHTTPServer import HTTPServer 
> > 'from CGIHTTPServer import CGIHTTPRequestHandler
> > 'HTTPServer(("localhost", 80),
> > CGIHTTPRequestHandler).serve_forever()
> > 
> > instead of redirecting, it only prints
> > 'Location : /cgi-bin/ecodiv.pyw' inthe 
> > browser
> > 
> > 
> > I haven't tested it, but I think I had a similar error recently, and
> > that was solved by removing the \n\n at the end of the Content-Type
> > line. You could try that.
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > Andre Engels, [EMAIL PROTECTED]
> > ICQ: 6260644  --  Skype: a_engels
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Redirect from a CGI script

2007-01-19 Thread Danny Yoo


On Sat, 20 Jan 2007, Paulino wrote:

> well it doesn't work either!

Hi Paulino,

Try being more descriptive: what does your program look like now?  It 
sounds like you've made a few changes.  If you can show us the program, 
we'll do what we can to pinpoint the problems.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor