Re: [Tutor] py in a nut

2005-10-25 Thread Frank Moore
[EMAIL PROTECTED] wrote:

> Hope this is not too off topic:
> I noticed that the Python in a Nutshell book is version 2.2 and a few 
> years old.  Does anyone know if there are plans to bring out a new 
> edition anytime soon?  I checked the O'reilly page and didnt find 
> anything, also googled it.
> If anyone knows anything let me know.
> Thanks,
> Carl
>
>
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>
Carl,

I mailed O'Reilly in June of this year to ask about any Python books on 
the horizon.
I particularly wanted to know if an 'Advanced Python' book was on the 
cards.
Something that included subjects like Descriptors, Generators, 
Iterators, Metaclasses, Decorators, Design patterns, etc.
And this was the response I got from them:


We do have a couple of Python books in the works.

Twisted: A Developer's Notebook - should be released in October.

Programming Python, 3E  which should be released in December.

Python in a Nutshell, 2nd Edition - scheduled to be released
in March of next year.


So there is no Advanced Python book on the horizon, which is a shame.
But as you can see, Python in a Nutshell 2nd edition is due out next March.

Cheers,
F.

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


Re: [Tutor] testing for modules?

2005-10-25 Thread Shitiz Bansal
try:
   import 
except:
   
   import 


--- Ed Hotchkiss <[EMAIL PROTECTED]> wrote:

> i have a generic script that is using several
> modules on windows and linux
> boxes. i need to have the scripts test if a module
> is installed, and then if
> not - then to install the module. can anyone give me
> a headsup on how to
> test for a module, returning something to indicate
> whether or not it is
> installed, then after that executing file to install
> the module.? just
> somewhere to start would be helpful! thanks in
> advance.
> 
> --
> edward hotchkiss
> > -- 
> http://mail.python.org/mailman/listinfo/python-list


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] manipulating list of lists

2005-10-25 Thread Kent Johnson
John Fouhy wrote:
> On 25/10/05, Brian van den Broek <[EMAIL PROTECTED]> wrote:
> 
>>To sort by the second item, try
>>
>> >>> def sort_by_second(sequence):
>>decorated = [(x[1], x) for x in sequence]
>>decorated.sort()
>>return [x[1] for x in decorated]
> 
> 
> With python2.4, you can use the key= argument to sort.
> 
> eg:
> 
arr = [('a', 5), ('b', 3), ('c', 7), ('d', 1), ('e', 2)]
arr.sort(key=lambda x: x[1])

and you can use operator.itemgetter() instead of a lambda - it is intended for 
this usage:
 >>> import operator
 >>> arr = [('a', 5), ('b', 3), ('c', 7), ('d', 1), ('e', 2)]
 >>> arr.sort(key=operator.itemgetter(1))
 >>> arr
[('d', 1), ('e', 2), ('b', 3), ('a', 5), ('c', 7)]

Kent

-- 
http://www.kentsjohnson.com

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


Re: [Tutor] py in a nut

2005-10-25 Thread Kent Johnson
Frank Moore wrote:
> I mailed O'Reilly in June of this year to ask about any Python books on 
> the horizon.
> I particularly wanted to know if an 'Advanced Python' book was on the 
> cards.
> Something that included subjects like Descriptors, Generators, 
> Iterators, Metaclasses, Decorators, Design patterns, etc.

Have you looked at Python Cookbook 2E? Chapter 19 is "Iterators and 
Generators". Chapter 20 is "Descriptors, Decorators and Metaclasses".

> And this was the response I got from them:
> 
> 
> We do have a couple of Python books in the works.
> 
> Twisted: A Developer's Notebook - should be released in October.

This one is available:
http://www.oreilly.com/catalog/twistedadn/

Kent
-- 
http://www.kentsjohnson.com

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


Re: [Tutor] testing for modules?

2005-10-25 Thread Frank Moore
Shitiz Bansal wrote:

>try:
>   import 
>except:
>   
>   import 
>
>
>--- Ed Hotchkiss <[EMAIL PROTECTED]> wrote:
>
>  
>
Explicit is always better than implicit...

try:
import 
except ImportError:# Only catch import errors
   
import 

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


Re: [Tutor] py in a nut

2005-10-25 Thread Frank Moore
Kent Johnson wrote:

>Have you looked at Python Cookbook 2E? Chapter 19 is "Iterators and 
>Generators". Chapter 20 is "Descriptors, Decorators and Metaclasses".
>  
>
I have. But I'd rather have a complete book on these subjects ;-)
I'm sure someone will write one in the end. Hopefully Alex Martelli 
will. He currently runs a course on these subjects.

The reason I wrote to O'Reilly originally was because the 2nd Edition of 
the 'Advanced Perl Programming' book had just come out and I noticed how 
perl had so many books (almost 40) written about it but Python only had 
9. An advanced Python book seemed to be the one that was so obviously 
missing.

>This one is available:
>http://www.oreilly.com/catalog/twistedadn/
>  
>
I'm still waiting for O'Reilly to publish the Table of Contents so I can 
see what's in it.
As everyone else has noted, the cover is definitely one of their best.

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


Re: [Tutor] py in a nut

2005-10-25 Thread Damien Gouteux




In french, 'Python précis & concis', from O'Reilly is now out :
from Mark Lutz, it covers python 2.4 (and decorators).
I assume there is a english edition of this book.

Frank Moore wrote:

  Kent Johnson wrote:

  
  
Have you looked at Python Cookbook 2E? Chapter 19 is "Iterators and Generators". Chapter 20 is "Descriptors, Decorators and Metaclasses".
 


  
  I have. But I'd rather have a complete book on these subjects ;-)
I'm sure someone will write one in the end. Hopefully Alex Martelli 
will. He currently runs a course on these subjects.

The reason I wrote to O'Reilly originally was because the 2nd Edition of 
the 'Advanced Perl Programming' book had just come out and I noticed how 
perl had so many books (almost 40) written about it but Python only had 
9. An advanced Python book seemed to be the one that was so obviously 
missing.

  
  
This one is available:
http://www.oreilly.com/catalog/twistedadn/
 


  
  I'm still waiting for O'Reilly to publish the Table of Contents so I can 
see what's in it.
As everyone else has noted, the cover is definitely one of their best.

Cheers,
F.
___
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] py in a nut

2005-10-25 Thread Frank Moore
Damien Gouteux wrote:

> In french, 'Python précis & concis', from O'Reilly is now out : from 
> Mark Lutz, it covers python 2.4 (and decorators).
> I assume there is a english edition of this book.


Yes, that's the Python Pocket Reference.
It does have 1 (small) page on decorators, but nothing on metaclasses, 
descriptors etc...

The Pocket Reference is useful as a reminder on how something works 
syntactically,
but it's probably not the best place to find an in-depth explanation on 
how something works in the first place.

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


Re: [Tutor] py in a nut

2005-10-25 Thread Kent Johnson
Damien Gouteux wrote:
> In french, 'Python précis & concis', from O'Reilly is now out : from 
> Mark Lutz, it covers python 2.4 (and decorators).
> I assume there is a english edition of this book.

Probably Python Pocket Reference, Third Edition
http://www.oreilly.com/catalog/pythonpr3/

Kent

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


[Tutor] identify python interpreters platform?

2005-10-25 Thread Ed Hotchkiss
how can i use the os module (or something else?) to determine what platform the interpreter is running on?-- edward hotchkiss 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] identify python interpreters platform?

2005-10-25 Thread Kent Johnson
Ed Hotchkiss wrote:
> 
> how can i use the os module (or something else?) to determine what 
> platform the interpreter is running on?

Try sys.platform

Kent
-- 
http://www.kentsjohnson.com

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


Re: [Tutor] identify python interpreters platform?

2005-10-25 Thread Ed Hotchkiss
Well, on windows XP it returns "nt" for os.name and for sys.platform it returns win32. how can i determine win2k, xp, 9x, etc etc ...
 
-edward
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] identify python interpreters platform?

2005-10-25 Thread Kent Johnson
Ed Hotchkiss wrote:
> Well, on windows XP it returns "nt" for os.name  and for 
> sys.platform it returns win32. how can i determine win2k, xp, 9x, etc 
> etc ...

I don't know...maybe something in os.environ?

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


Re: [Tutor] identify python interpreters platform?

2005-10-25 Thread Ed Hotchkiss

print os.environ("OS") - i need to run it on diff platforms that i dont have with me now, but i think it should work fine, otherwise ill get a workaround then post it to the list. thanks
 
-edward
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python myspace module?

2005-10-25 Thread Ed Hotchkiss
looking to write a myspace wrapper/module. what is the best way (hopefully using the stdlib not an outside module) to connect to a website and (if possible, otherwise ill have to code it?) access forms with GET POST blah blah blah ... 

 
thanks!-- edward hotchkiss 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] py in a nut

2005-10-25 Thread Alan Gauld
> Programming Python, 3E  which should be released in December.
>
> So there is no Advanced Python book on the horizon, which is a shame.
> But as you can see, Python in a Nutshell 2nd edition is due out next 
> March.

If PP continues the trend of the 2nd edition it could well be the book you 
want.
The first ed was a beginners book, the second covered a lot of the more 
advanced
topics and I would not be surprised if the 3rd ed dropped any pretense at
being an intro - Learning Python  fills that role.

But I know nothing... :-)

Alan G. 

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


Re: [Tutor] python myspace module?

2005-10-25 Thread Danny Yoo


On Tue, 25 Oct 2005, Ed Hotchkiss wrote:

> looking to write a myspace wrapper/module. what is the best way
> (hopefully using the stdlib not an outside module) to connect to a
> website and (if possible, otherwise ill have to code it?) access forms
> with GET POST blah blah blah ...

Hi Ed,

We do have some rudimentary access by using libraries like urllib and
urllib2.  Let's look at urllib for the moment:

http://www.python.org/doc/lib/module-urllib.html

By default, it send GET queries, but if we pass in a data argument to its
open() function, it can perform a POST.  So for really quick-and-dirty
stuff, urllib can do the trick.


For anything more sophisticated, we may want to look at some tools like
mechanize:

http://wwwsearch.sourceforge.net/mechanize/

which can help provide the illusion of a stateful session with a web
server.


Best of wishes!

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


[Tutor] downloading remote files

2005-10-25 Thread Ed Hotchkiss
is there a method in the stdlib of downloading a remote file via HTTP? or do i need sockets ...-- edward hotchkiss 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] downloading remote files

2005-10-25 Thread Kent Johnson
Ed Hotchkiss wrote:
> is there a method in the stdlib of downloading a remote file via HTTP? 
> or do i need sockets ...

for simple needs, you can use urllib:
import urllib
data = urllib.urlopen('http://www.google.com').read()

For more control, see urllib2. As suggested recently, Dive into Python has a 
good chapter on this which gives you several reasons why the above one-liner is 
a bad idea :-)
http://diveintopython.org/http_web_services/index.html

Kent

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


Re: [Tutor] identify python interpreters platform?

2005-10-25 Thread Brian van den Broek
Kent Johnson said unto the world upon 2005-10-25 11:46:
> Ed Hotchkiss wrote:
> 
>> Well, on windows XP it returns "nt" for os.name 
>> and for sys.platform it returns win32. how can i determine win2k,
>> xp, 9x, etc etc ...
> 
> 
> I don't know...maybe something in os.environ?
> 

>>> sys.getwindowsversion()
(5, 1, 2600, 2, 'Service Pack 2')
>>> help(sys.getwindowsversion)
Help on built-in function getwindowsversion in module sys:

getwindowsversion(...)
 getwindowsversion()

 Return information about the running version of Windows.
 The result is a tuple of (major, minor, build, platform, text)
 All elements are numbers, except text which is a string.
 Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows
NT/2000/XP

>>> 

The chm help also says:
> This function wraps the Win32 GetVersionEx() function; see the
> Microsoft documentation for more information about these fields.
> 
> Availability: Windows. New in version 2.3.
> 

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


Re: [Tutor] python myspace module?

2005-10-25 Thread nephish
Hey there Ed, 
if you pull this off, let us know. i have wanted to do this very
thing, but just don't have the time.
sk


On Tue, 2005-10-25 at 14:24 -0700, Danny Yoo wrote:
> 
> On Tue, 25 Oct 2005, Ed Hotchkiss wrote:
> 
> > looking to write a myspace wrapper/module. what is the best way
> > (hopefully using the stdlib not an outside module) to connect to a
> > website and (if possible, otherwise ill have to code it?) access forms
> > with GET POST blah blah blah ...
> 
> Hi Ed,
> 
> We do have some rudimentary access by using libraries like urllib and
> urllib2.  Let's look at urllib for the moment:
> 
> http://www.python.org/doc/lib/module-urllib.html
> 
> By default, it send GET queries, but if we pass in a data argument to its
> open() function, it can perform a POST.  So for really quick-and-dirty
> stuff, urllib can do the trick.
> 
> 
> For anything more sophisticated, we may want to look at some tools like
> mechanize:
> 
> http://wwwsearch.sourceforge.net/mechanize/
> 
> which can help provide the illusion of a stateful session with a web
> server.
> 
> 
> Best of wishes!
> 
> ___
> 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] threading issues

2005-10-25 Thread Chris Hallman

You're right. I'm inexperienced with classes and threading. The example
you gave (www.wellho.net) was the same example I used as the basis for
my script however I wasn't able to get it to work. I couldn't figure
out what the -q parameter is (it's not a valid parameter on the *nix I
have access to) and when I gave it an IP address range with reachable
and unreachable hosts, it reported them all as alive therefore this
example script isn't a good example. I was able to Google for more
examples.

I was finally able to get my script to not show duplicate PINGs. I also
realized that my script was not PINGing all the hosts in the input
file. Here is my latest version:


import os, re, string, sys, threading, time
from threading import Thread
from time import strftime

ms = re.compile("Reply")
rpath = (r"c:\utils\network_ping_devices.txt")

if os.path.exists(r"c:\utils\network_ping_again.txt"):
    rpath = (r"c:\utils\network_ping_again.txt")
wpath = (r"c:\logs\network_ping.out")
tpath =  (r"c:\temp\ping.txt")
if os.path.exists(tpath):
    os.remove(tpath)
temp = open(tpath, "w")
output = open(wpath, "a")
output.write("\n" + "Network PING test started -" + strftime(" %H:%M:%S %x") + "\n")
output.flush()

class PingIT(threading.Thread):
    def __init__(self,rtr):
        Thread.__init__(self)
        self.rtr = rtr

    def run(self):
        pingaf = os.popen('ping -n 1 -w 3000 ' + self.rtr)
        pingas = string.join(pingaf.readlines())
        if ms.search(pingas):
#            print
(re.sub('\n','',self.rtr)) + " responded."    #for
debugging
            return
        else:
            pingaf = os.popen('ping -n 1 -w 3000 ' + self.rtr)
            pingas = string.join(pingaf.readlines())
            if ms.search(pingas):
#           
    print (re.sub('\n','',self.rtr)) + "
responded."    # for debugging
                return
            else:
           
    temp.write(re.sub('\n','',self.rtr) + " did not
respond.\n")

pinglist = []
for rtr in file(rpath):
    current = PingIT(rtr)
    pinglist.append(current)
    current.start()

for pingle in pinglist:
    pingle.join()

temp.close()
temp = open(tpath, "r")
lines = []
for line in temp:
    lines.append(line.rstrip())
    lines.sort()
for line in lines:
    print >>output, line

output.write("Network PING test completed -" + strftime(" %H:%M:%S %x") + "\n")
output.flush()
output.close()


All the hosts in the input file are sorted alphabetically but my
results were output out of order?! (possibly due to latency in PING
responses or some latency in the thread joining; not sure of either). I
had to do the crude temp output file so that I could sort the results,
but it works!!  
On 10/23/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
Chris Hallman wrote:>> I made a mistake in my first email. I meant that I can't get fc to> write to the file. Here is the error:>> Traceback (most recent call last):>   File "thread_test_ping.py", line 37, in ?
> output.write(fc + " failures found.\n")> TypeError: unsupported operand type(s) for +: 'int' and 'str'This
is a pretty easy error to understand. It is saying that you can't add
an int and a string. Looking at your code, you are trying to add fc + "
failures found.\n". fc is indeed an int. You have to change it to a
string to be able to add it to another string. You can do this with the
str() function. So the correct statement is  output.write(str(fc) + " failures found.\n")>> I tried the suggestions you made, but I can't get it to work. Is this> what you meant?:
No, not quite.>> #!/usr/bin/env python>> #Let us profile code which uses threads> import os, re, string, thread, threading, time> from time import strftime> #from threading import Thread
>> class PingThread(threading.Thread):> def __init__(self, rtr):> threading.Thread.__init__(self)> self.rtr = rtrThe
init method initializes a PingThread. It is passed self as an argument,
plus whatever parameters you pass in when you create the PingThread.
You call the superclass constructor and save the value of the
parameter. So far so good...but this next line is creating a
PingThread, it belongs below in your loop.> PingThread(rtr).start(rtr)>> def run(self):Here
you should reference self.rtr so you get the value that was stored in
the call to __init__(), and similarly for each reference to rtr in the
run method.> pingaf = os.popen('ping -n 1 -w 3 ' + rtr)> pingas = string.join(pingaf.readlines())> if ms.search(pingas):>
print (re.sub('\n','',rtr)) + " responded."#for
debugging> else:> pingaf = os.popen('ping -n 2 -w 3 ' + rtr)> pingas = string.join(pingaf.readlines())> if ms.search(pingas):>
print (re.sub('\n','',rtr)) + " responded."# for> debugging> else:> fc=fc+1>
output.write(re.sub('\n','',rtr) + " did not respond.\n")>> fc = 0# failure counter> ms = re.compile("Reply from")> rpa

[Tutor] new user question about while loops

2005-10-25 Thread Nick Eberle
Sorry for potential double post, error with first send
--

Hello all,
 
I had a new question about python. I am pretty much 
in tutorial learning stages, but attempting to create sample programs not in my 
book to learn how to construct scripts.
 
I understand the format of while loops, but is it 
possible to use the random.randrange function in them?
 
My goal, create a program that flips a coin 100 
times, at the end it says the number of times it flipped heads and flipped 
tails.
 
My dilemia, how do I get this to work with a while 
loop? I tried intially assigning
 
heads=0
tails=1
 
then I figured I could just do a while loop and 
then use the random function each time. At the end it could count the number of 
times the random generated a 0 or a 1.
 
However I can't seem to construct it in a way that 
makes or works. 
 
Thanks!___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] new user question about while loops

2005-10-25 Thread bob


At 10:53 PM 10/25/2005, Nick Eberle wrote:
Content-class:
urn:content-classes:message
Content-Type: multipart/alternative;
boundary="_=_NextPart_001_01C5D9F1.A836CF4F"
Sorry for potential double post, error with first send
--
Hello all,
 
I had a new question about python. I am pretty
much in tutorial learning stages, but attempting to create sample
programs not in my book to learn how to construct scripts.
 
I understand the format of while loops, but is
it possible to use the random.randrange function in them?
 
My goal, create a program that flips a coin 100
times, at the end it says the number of times it flipped heads and
flipped tails.
 
My dilemia, how do I get this to work with a
while loop? I tried intially assigning
 
heads=0
tails=1
 
then I figured I could just do a while loop and
then use the random function each time. At the end it could count the
number of times the random generated a 0 or a 1.
 
However I can't seem to construct it in a way
that makes or works. 
Show us your code, as flawed as it might be. Then we can advise
you.

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


Re: [Tutor] new user question about while loops

2005-10-25 Thread Nick Eberle




print "This is a 
coin flip game"
 
print "\n100 flips will be 
made and it will show the amount of times heads or tails came up"
 
import random
#assigning variables to track the amount of times heads or 
tails comes up
heads=0tails=1
 
flip=100while flip < 
100:    flip -= 1    
random.randrange(2)    if flip < 
100:    break
 
print "\nThe amount of times 
tails came up was" , tails , "The amount of times heads came up was" , 
heads
 
raw_input()


From: bob 
[mailto:[EMAIL PROTECTED]Sent: Tue 10/25/2005 10:58 
PMTo: Nick Eberle; tutor@python.orgSubject: Re: [Tutor] 
new user question about while loops
At 10:53 PM 10/25/2005, Nick Eberle wrote:
Content-class: 
  urn:content-classes:messageContent-Type: 
  multipart/alternative;boundary="_=_NextPart_001_01C5D9F1.A836CF4F"Sorry for potential double post, error with first send--Hello 
  all, I had a new question about 
  python. I am pretty much in tutorial learning stages, but attempting to create 
  sample programs not in my book to learn how to construct 
  scripts. I understand the format 
  of while loops, but is it possible to use the random.randrange function in 
  them? My goal, create a program 
  that flips a coin 100 times, at the end it says the number of times it flipped 
  heads and flipped tails. My 
  dilemia, how do I get this to work with a while loop? I tried intially 
  assigning heads=0tails=1 then I 
  figured I could just do a while loop and then use the random function each 
  time. At the end it could count the number of times the random generated a 0 
  or a 1. However I can't seem to 
  construct it in a way that makes or works. Show us your 
code, as flawed as it might be. Then we can advise you.
 


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