[Tutor] httpFetch Usage

2006-05-19 Thread kieran flanagan
Hi,

Does anyone know where I can get some info on the funtion httpFetch.
Its being imported and used in a file I am adding code to and I would
like to understand what it does. An example of its usage:

response = httpFetch.httpFetch(url, sessionID, someValue)

Thanks
Kieran

-- "Behind every great man, there is a great woman. Behind that woman is Mr.T." 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] httpFetch Usage

2006-05-19 Thread Kent Johnson
kieran flanagan wrote:
> Hi,
> 
> Does anyone know where I can get some info on the funtion httpFetch. Its 
> being imported and used in a file I am adding code to and I would like 
> to understand what it does. An example of its usage:
> 
> response = httpFetch.httpFetch(url, sessionID, someValue)

httpFetch is not part of the standard library. Do you know who wrote it 
or where it came from? If it is written in Python you should have the 
source code for the module.

Try this in the interactive interpreter to locate the file:
import httpFetch
print httpFetch.__file__

This may print the path to a .pyc file; look in the same directory for 
the corresponding .py file.

You can also try
help(httpFetch)
help(httpFetch.httpFetch)
dir(httpFetch)

Kent

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


[Tutor] Lists of lists - different sized lists

2006-05-19 Thread Adam Cripps
I'm trying to sort [1] a list of people randomly into groups. My guess
is that a list of lists will do the trick here. However, the randrange
fails with an empty range. How can I check for this? I thought that my
pupils list wouldn't reach zero with the while len(pupils)>0: would
keep it above 0.

Any help or advice would be appreciated.

TIA
Adam

[1]
def groups(pupils, size):
toplist = []
tmplist = []
print len(tmplist)
while len(pupils)>0:
print len(pupils)
while len(tmplist)<=size:
print "tmplist is ", len (tmplist)
#How many pupils are we talking about here?
listlength = pupilslength(pupils)
rand1 = random.randrange(listlength)
tmplist.append(pupils[rand1])
pupils.pop(rand1)
toplist.append(tmplist)
tmplist = []
for i in toplist:
print i
print i
-- 
http://www.monkeez.org
PGP key: 0x7111B833
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New programmer, need some help getting started on my first project

2006-05-19 Thread Alan Gauld
Chris,

>  I do like the way you simplified using the random function,
>  I need to start thinking like that.

It is quite clever but quite a subtle twist for a beginner to think 
of.
However even in your version there is a pattern you should look
out for:

>> coin = random.randrange(2)
>> if coin == 0:
>> heads += 1
>> else:
>> tails += 1

If you assign a value to a  variable (ie coin) and then only ever
use that variable in the test of a simple if/else construct then
you can always eliminate the assignment:

if random.randrange(2):
   tails += 1:
else: heads += 1

Bob's trick works because you always get 0 or 1 back, but this
shortcut works for any two way check, even ranges:

if somefunc() > keyvalue:
   # do greater than action
else:
   # do other action

The caveat is where you need to use the test value inside
the if/else and then you do need to explicitly make the
assignment.

>> print "The coin was flipped 100 times and it was heads " 
>> +str(heads)+
>> " times and tails " + str(tails) + " times!"

Also you don't need to use str() in. Python to proint things because
print implicitly calls the string conversion for you.

>>> print 5,'+',4,'=',5+4
5 + 4 = 9

Note that it also inserts spaces between  the valiues. For more 
control
use the string formatting technique that Bob demonstrated.

Looking for these common shortcuts or idioms is a big part of gaining
experience with any language.

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


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


Re: [Tutor] Lists of lists - different sized lists

2006-05-19 Thread Kent Johnson
Adam Cripps wrote:
> I'm trying to sort [1] a list of people randomly into groups. My guess
> is that a list of lists will do the trick here. However, the randrange
> fails with an empty range. How can I check for this? I thought that my
> pupils list wouldn't reach zero with the while len(pupils)>0: would
> keep it above 0.

You don't show the pupilslength() function so I will assume it is just 
returning len(pupils). One problem is if len(pupils) < size you will 
exhaust the pupils list before you fill tmplist. This will happen in the 
inner loop so the test for len(pupils) > 0 will not be reached.

One option would be to test len(pupils) > 0 again in the condition for 
the inner loop.

BTW you can just test 'if pupils:' rather than 'if len(pupils) > 0:'

Kent

> 
> Any help or advice would be appreciated.
> 
> TIA
> Adam
> 
> [1]
> def groups(pupils, size):
> toplist = []
> tmplist = []
> print len(tmplist)
> while len(pupils)>0:
> print len(pupils)
> while len(tmplist)<=size:
> print "tmplist is ", len (tmplist)
> #How many pupils are we talking about here?
> listlength = pupilslength(pupils)
> rand1 = random.randrange(listlength)
> tmplist.append(pupils[rand1])
> pupils.pop(rand1)
> toplist.append(tmplist)
> tmplist = []
> for i in toplist:
> print i
> print i


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


Re: [Tutor] New programmer, need some help getting started on my first project

2006-05-19 Thread Terry Carroll
On Thu, 18 May 2006, Chris Delgado wrote:

> Kent, 
> 
> So if you have just been reading, I suggest you try writing a few small 
> programs as warmups."
> 
> I think you are right on the money here. I need to write some more
> simple stuff. 

Chris, I'd also suggest that, once you have a few toy programs (like your 
coin-tosser) under your belt, you find some *small* task that you actually 
want to accomplish, and use Python for that.

It's both motivating and satisfying to address some real-life need you 
have, rather than some arbitrary assignment from a book.

I think my very first Python program was to do something like take a 
comma-separated list of MP3 files, with the directory name in a form that 
includes artist name and album or collection, and format it as a 
hierarchical list, because I wanted to see it that way.

It was more fun to work on a program that actually did something I wanted, 
and I had a purpose in writing it beyond just learning Python.

(Of course, I look back at that code now, and it sure is ugly; but it 
worked!)

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


Re: [Tutor] New programmer, need some help getting started on my first project

2006-05-19 Thread Bob Gailer
Terry Carroll wrote:
> On Thu, 18 May 2006, Chris Delgado wrote:
>
>   
>> Kent, 
>>
>> So if you have just been reading, I suggest you try writing a few small 
>> programs as warmups."
>>
>> I think you are right on the money here. I need to write some more
>> simple stuff. 
>> 
>
> Chris, I'd also suggest that, once you have a few toy programs (like your 
> coin-tosser) under your belt, you find some *small* task that you actually 
> want to accomplish, and use Python for that.
>
> It's both motivating and satisfying to address some real-life need you 
> have, rather than some arbitrary assignment from a book.
>
> I think my very first Python program was to do something like take a 
> comma-separated list of MP3 files, with the directory name in a form that 
> includes artist name and album or collection, and format it as a 
> hierarchical list, because I wanted to see it that way.
>
> It was more fun to work on a program that actually did something I wanted, 
> and I had a purpose in writing it beyond just learning Python.

My first "real" program was an interface to an Oracle application that 
included generating anonymous PL/SQL blocks, passing them to Oracle, 
retrieving results, launching and monitoring Oracle background 
processes. A rather daunting task, but it was fun, relatively easy in 
Python, and certainly helped me get into Python.

The hardest part was wading thru Oracle tech support, and dealing with 
error messages such as "invalid type or number of parameters" in a call 
that involved many parameters. :'(

-- 
Bob Gailer
510-978-4454

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


[Tutor] Fwd: How do you create built-in objects (dicts, lists, etc) without specifying {}, [], etc...

2006-05-19 Thread Bill Carson
Note: forwarded message attached.__Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com --- Begin Message ---
Howdy,  Do you know how to create instances of dictionaries, lists, integers, longs, etc without having to specify it in code...      Instead of specifying:        a={"bla":1}   or ...      a=[1,2,3]     Is there another way??The "new" module does not appear to create built-in types, or am I wrong.  I've read something about "type objects", but I don't know how to instantiate them outside of specifying them as python syntax embedded in code (see examples above).     Thank you for the insights and help.  Bill.   __Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com --- End Message ---
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: How do you create built-in objects (dicts, lists, etc) without specifying {}, [], etc...

2006-05-19 Thread Kent Johnson
Bill Carson wrote:
> Do you know how to create instances of dictionaries, lists, integers, 
> longs, etc without having to specify it in code...
>  
>  Instead of specifying: 
> a={"bla":1}
>  or ...
> a=[1,2,3]
>  
> Is there another way??

I'm not really sure what you are looking for here. You can use the type 
names to create new objects:
a = dict(bla=1)
or
a = dict()
a['bla'] = 1

a = list()
a.append(1)
a.append(2)
a.append(3)

> The "new" module does not appear to create built-in types, or am I wrong.
> I've read something about "type objects", but I don't know how to 
> instantiate them outside of specifying them as python syntax embedded in 
> code (see examples above).

If you could do what you want, what would it look like? (In other words, 
make up some syntax and show us.) Why do you want to do this?

Kent

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


[Tutor] Getting current class method name

2006-05-19 Thread Nelson, Scott








Ok, I’m looking to create a quick debug function that prints
out the current function that is running (to help in debugging what functions
are being run).  I know that I can use a debugger to get a stack trace (and
I do), but I’m still curious about this problem.  Here’s what
I’ve got so far:

    

def Foo():

    PrintCurFunc()

    

class cBase:

    def
Run(self):

    PrintCurFunc(self)

 

class cChild(cBase):

    pass

 

import traceback

def PrintCurFunc(obj = None):

    stack
= traceback.extract_stack()

    scriptName,
lineNum, funcName, lineOfCode = stack[-2]

    if
obj:

    print
'%s.%s()' % (obj.__class__.__name__, funcName)

    else:

    print
'%s()' % funcName

 

def main():

    Foo()

    b
= cBase()

    c
= cChild()

    b.Run()

    c.Run()

    x
= cTwo()

    x.Run()

    

if __name__ == '__main__':

    main()   

 

The output I get is:

 

Foo()

cBase.Run()

cChild.Run()

cTwo.Run()

cTwo.Run()

 

Now, PrintCurFunc() above gets me the output I want in the
fist two cases (Foo() and cBase.Run()).  But, in the case of c.Run(), I
would still like to see it output cBase.Run(), since that is the method that is
being run.  Yes, cChild being derived from cBase does have an inherited
Run method, but I would like my output to display “where the code physically
lives” so that for debugging I can basically get a stack trace to see
what is executed when.  It might be more clear in this example:

 

class cOne:

    def
Run(self):

    PrintCurFunc(self)

 

class cTwo(cOne):

    def
Run(self):

    PrintCurFunc(self)

    cOne.Run(self)

x = cTwo()

x.Run()

 

gets me the output of:

cTwo.Run()

cTwo.Run()

 

when I would prefer it output:

cTwo.Run()

cOne.Run()

 

…which reflects the call stack more clearly. 
Now, I’ve looked into the traceback module but was only able to find the
name of the function.  Passing self into PrintCurFunc was my attempt at
getting at the class name.  But, as you see, it obviously returns the
object’s class name and not the class name the function was defined on. 
I also looked at this recipe http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062
but it only gives me a function name and not the class name as well.

 

Any thoughts?  I’m a bit new to Python’s
reflection features…

 

Thanks tons!

 

-Scott






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


[Tutor] Please help!!

2006-05-19 Thread MATATA EMMANUEL

Hi there,
I'm tasked to write a Paython script which is supposed to hit a web site and download a shapefile from that web site. I don't have any clue and would like your help. I use 9.x if this matter.
Thank you.
Matt.

 

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


[Tutor] Tkinter pass-by-reference query

2006-05-19 Thread Henry Finucane
While attempting to add images to a canvas programmatically, I wrote
the following:
for i in os.listdir('./icons/terrain'):
self.terrainScreen["height"] = str(int(self.terrainScreen["height"])+50)
debug(self.terrainScreen["height"]+" height of terrainScreen",5)
img = PhotoImage(file='./icons/terrain/'+i)

self.terrainScreen.create_image(int(self.terrainScreen["height"])-50,(int(self.terrainScreen["height"])-50),
image=img,tag=None)

I couldn't figure out why it didn't work, until I went in and played
with Tkinter in the python shell, and I came to the conclusion that
the reason nothing was displayed on the canvas was that images are
passed by reference. In other words, unless there is a permanent `img'
variable, the data disappears.

For example (replace w\ your own gifs):
>>> from Tkinter import *
>>> root = Tk()
>>> terrain = Canvas(root,width=50,height=50)
>>> img = PhotoImage(file='MapEditor/icons/terrain/desert.gif')
>>> terrain.create_image(0,0,image=img)
1
>>> terrain.pack()

Works beautifully, eh?

>>> img = PhotoImage(file='MapEditor/icons/terrain/water.gif')

Doh! Now it disappears!

>>> terrain.create_image(0,50,image=img)
2

Working, but there is only one image

>>> terrain.create_image(0,0,image=PhotoImage('MapEditor/icons/terrain/grass.gif'))
3

Nothing gets displayed.

So there is my problem, and what is, I'm fairly certain, the cause of
the problem. Unfortunately, I really have no idea what the proper
solution should be. I'd like to populate the canvas programmatically,
but my brain seems to be choking on possible solutions. Suggestions?
Can I force variables to be passed by value?

-- 
--H.F.
My penguin is bigger than yours, mister...
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please help!!

2006-05-19 Thread Henry Finucane
On 5/19/06, MATATA EMMANUEL <[EMAIL PROTECTED]> wrote:
>
>
>
> Hi there,
Hi!
>
> I'm tasked to write a Paython script which is supposed to hit a web site and
> download a shapefile from that web site.

Cool. Look at urllib in your python documentation.

> I don't have any clue and

Me neither :)

> would like your help. I use 9.x if this matter.

9.x what? Python has yet to release 2.6, if I'm not mistaken.

>
> Thank you.
>
> Matt.

Good luck.

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


-- 
--H.F.
My penguin is bigger than yours, mister...
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please help!!

2006-05-19 Thread Bernd Prager
Hey Matata,

>From the website  http://www.boddie.org.uk/python/HTML.html:

import urllib
# Get a file-like object for the Python Web site's home page.
f = urllib.urlopen("http://www.python.org";)
# Read from the object, storing the page's contents in 's'.
s = f.read()
f.close()


Hope this helps,
-- Bernd

MATATA EMMANUEL wrote:
>
> Hi there,
>
> I'm tasked to write a Paython script which is supposed to hit a web
> site and download a shapefile from that web site. I don't have any
> clue and would like your help. I use 9.x if this matter.
>
> Thank you.
>
> Matt.
>
>  
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

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


Re: [Tutor] New programmer, need some help getting started on my first project

2006-05-19 Thread Chris Delgado
   Terry, Thank you for the suggestions. I am going to stick with these little toy programs as you aptly put them to make sure I have a good grasp on the basics. I completely agree with you that I need to do something that I want to solve a problem with and in fact, thats whats motivated me to find and join this list. Unfortunately, it woudl appear I attempted to bit off more than I could chew. Part of the problem with writing to solve things is that there is just about something for everything out there right now!  I did recently convert my old laptop over to Linux so hopefully as I mess around with that I will find some things that I can focus my attentions on. Thanks for taking the time to respond. Cheers, ChrisTerry Carroll <[EMAIL PROTECTED]> wrote: On Thu, 18 May 2006, Chris Delgado
 wrote:> Kent, > > So if you have just been reading, I suggest you try writing a few small > programs as warmups."> > I think you are right on the money here. I need to write some more> simple stuff. Chris, I'd also suggest that, once you have a few toy programs (like your coin-tosser) under your belt, you find some *small* task that you actually want to accomplish, and use Python for that.It's both motivating and satisfying to address some real-life need you have, rather than some arbitrary assignment from a book.I think my very first Python program was to do something like take a comma-separated list of MP3 files, with the directory name in a form that includes artist name and album or collection, and format it as a hierarchical list, because I wanted to see it that way.It was more fun to work on a program that actually did something I wanted, and I had a
 purpose in writing it beyond just learning Python.(Of course, I look back at that code now, and it sure is ugly; but it worked!)___Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
		Be a chatter box. Enjoy free PC-to-PC calls  with Yahoo! Messenger with Voice.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter pass-by-reference query

2006-05-19 Thread Kent Johnson
Henry Finucane wrote:
> While attempting to add images to a canvas programmatically, I wrote
> the following:
> for i in os.listdir('./icons/terrain'):
>   self.terrainScreen["height"] = str(int(self.terrainScreen["height"])+50)
>   debug(self.terrainScreen["height"]+" height of terrainScreen",5)
>   img = PhotoImage(file='./icons/terrain/'+i)
>   
> self.terrainScreen.create_image(int(self.terrainScreen["height"])-50,(int(self.terrainScreen["height"])-50),
> image=img,tag=None)
> 
> I couldn't figure out why it didn't work, until I went in and played
> with Tkinter in the python shell, and I came to the conclusion that
> the reason nothing was displayed on the canvas was that images are
> passed by reference. In other words, unless there is a permanent `img'
> variable, the data disappears.

For some reason Tkinter doesn't keep a reference to the image you pass 
it. So when your reference goes out of scope, the image is 
garbage-collected and can no longer be displayed. The solution, as you 
have found, is to keep a reference to the image somewhere. It can be in 
a variable, or you could make a list of images.

Kent

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