Re: [Tutor] multiple assignment

2005-10-18 Thread Kent Johnson
Vincent Gulinao wrote:
> I was fascinated when I learned that I can do this in Python:
> 
> (str1, str2, str3, rest) = str.split(" ", 3)
> 
> Later that I realize that str could contain values of less than 4 
> strings, in which case it would complain something like -- ValueError: 
> unpack list of wrong size.
> 
> Now I don't want to spoil the fun.
> 
> In essence, I'd like to capture the first 3 words in a string. If str is 
> less than 3 words then any or all of the containers could have a None value.

AFAIK there is no pretty solution but this works:
str1, str2, str3 = (s.split(" ", 3) + [None]*3)[:3]

Kent

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


Re: [Tutor] multiple assignment

2005-10-18 Thread Kent Johnson
Vincent Gulinao wrote:
> Haha. Cool.
> 
> I think it's better than...
> 
> if str.find(" "):
> str1, str2, str3 = str.split(" ", 3)
> else:
> str1, str2, str3, rest = str, None, None, None

Less buggy, too - your version will fail if str="1 2" or "1 2 3 4"

BTW don't use str as the name of a string, it is already the name of the string 
*type*. In general you should avoid using the names of built-ins for variable 
names.

Kent

PS Please reply on list so others can benefit

> 
> On 10/18/05, *Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> AFAIK there is no pretty solution but this works:
> str1, str2, str3 = (s.split(" ", 3) + [None]*3)[:3]

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


[Tutor] "a cute recipe for getting the help command text"

2005-10-18 Thread Dick Moores
Found this in comp.lang.python. The Google link is 


=
import subprocess as subp

p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
 stdout=subp.PIPE, stderr=subp.STDOUT)
flag = "(@@}"
print >>p.stdin, "PROMPT", flag
print >>p.stdin, "HELP"
print >>p.stdin, "EXIT"
text = p.stdout.read()
p.wait()
helptext = text[text.index(flag + 'HELP') + len(flag) + 4 :
  text.index(flag + 'EXIT')]
words = [line.split(None, 1)[0]
   for line in helptext.split('\n')
   if line.strip()]
commands = [word for word in words if word.isupper()]

dest = open('cmd_help.txt', 'wb')
p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
 stdout=dest, stderr=subp.STDOUT)
print >>p.stdin, "PROMPT ()"
print >>p.stdin, "HELP"
for command in commands:
  print >>p.stdin, "HELP", command
print >>p.stdin, "EXIT"
p.wait()
dest.close()
=end of script=

When I run this I get no text, but just a series of cmd.exe windows 
entitled HELP . How do I get any text?

Thanks,

Dick Moores

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


Re: [Tutor] hand-holding for web development

2005-10-18 Thread nitin chandra
Thanks Jay

On 10/18/05, Jay Loden <[EMAIL PROTECTED]> wrote:
> You need an Apache config section to tell it what to use mod_python on and
> how. For example, in httpd.conf or a separate file in your conf.d directory

i guess the  can be added only in one of the conf file.
Which one should i add in?


> for apache:
>
> 
>   AddHandler  mod_python .py
>   PythonHandler test
>   PythonDebug On
> 
>
> tells Apache to use mod_python on all .py files in my /var/www/html directory
> (which is also the document root on my server).
>
> Once that's loaded you should be able to run whatever your python code through
> a handler. For example, saving this as test.py:
>
> #
> #mod_python super basic test script
> #
> from mod_python import apache
>
> def handler(req):
>   req.content_type = "text/html"
>   req_file = basename(req.uri)
>   if req.header_only:
>  return apache.OK
>
>   else:
>  req.write("mod_python is working")
>  return apache.OK
>
> # end of script
>
> This should print "mod_python is working" to the browser if you visit
> http://yoursite.com/test.py
>
> Remember that you need to restart/reload apache after any config change. The
> basic gist is that any .py file you load in the /var/www/html runs the script
> "test.py" (PythonHandler test) and executes the "handler()" function within
> the script. NOTE: this can be very non-intuitive, becuase running
> http://yoursite.com/test.py has exactly the same result as
> http;//yoursite.com/foo.py - because they're both simply running the handler
> script, test.py, for ALL .py files in the directory. That means that your
> PythonHandler script needs to dispatch other scripts or load special pages in
> order to get different code to run. Also note that there are some pre-written
> Handler scripts that come with mod_python that can often do most of your work
> for you.
>
> You'll probably want a good tutorial on using mod_python to make sense of all
> this, because it's not really intuitive and it works differently than
> traditional cgi programming. Take a look at:
> http://modpython.org/live/current/doc-html/
> to get you started, it's a lot more detailed and will probably answer your
> questions better.

Thaks you. could you please guide how to interface Django with
mod_python, for effectively using in web site development?

>
> -Jay

Thanks You

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


Re: [Tutor] "a cute recipe for getting the help command text"

2005-10-18 Thread Kent Johnson
It makes a file called cmd_help.txt in the current directory. Did you look for 
that? The file contains the result of asking for help on every command. Kind of 
cool!

Kent

Dick Moores wrote:
> Found this in comp.lang.python. The Google link is 
> 
> 
> =
> import subprocess as subp
> 
> p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
>  stdout=subp.PIPE, stderr=subp.STDOUT)
> flag = "(@@}"
> print >>p.stdin, "PROMPT", flag
> print >>p.stdin, "HELP"
> print >>p.stdin, "EXIT"
> text = p.stdout.read()
> p.wait()
> helptext = text[text.index(flag + 'HELP') + len(flag) + 4 :
>   text.index(flag + 'EXIT')]
> words = [line.split(None, 1)[0]
>for line in helptext.split('\n')
>if line.strip()]
> commands = [word for word in words if word.isupper()]
> 
> dest = open('cmd_help.txt', 'wb')
> p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
>  stdout=dest, stderr=subp.STDOUT)
> print >>p.stdin, "PROMPT ()"
> print >>p.stdin, "HELP"
> for command in commands:
>   print >>p.stdin, "HELP", command
> print >>p.stdin, "EXIT"
> p.wait()
> dest.close()
> =end of script=
> 
> When I run this I get no text, but just a series of cmd.exe windows 
> entitled HELP . How do I get any text?
> 
> Thanks,
> 
> Dick Moores
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

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


[Tutor] string.split() into a list

2005-10-18 Thread Randy Bush
and stupid question of the morning (for me)

i want to string.split() into a sequence, a la

  l = []
  l = myString.split('|')

but, of course it whines about too many values.  what is the
common way to do this?

randy

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


Re: [Tutor] string.split() into a list

2005-10-18 Thread Jason Massey
Works fine for me:

>>> l = []
>>>a='1|2|3|4'
>>> l=a.split('|')
>>>l
['1', '2', '3', '4']


On 10/18/05, Randy Bush <[EMAIL PROTECTED]> wrote:
> and stupid question of the morning (for me)
>
> i want to string.split() into a sequence, a la
>
>   l = []
>   l = myString.split('|')
>
> but, of course it whines about too many values.  what is the
> common way to do this?
>
> randy
>
> ___
> 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] "a cute recipe for getting the help command text"

2005-10-18 Thread Dick Moores
Ah, the line

   dest = open('cmd_help.txt', 'wb')

Thanks, Kent. Yes, I've got it.

Dick

Kent Johnson wrote at 13:23 10/18/2005:
>It makes a file called cmd_help.txt in the current directory. Did you 
>look for that? The file contains the result of asking for help on every 
>command. Kind of cool!
>
>Kent
>
>Dick Moores wrote:
> > Found this in comp.lang.python. The Google link is
> > 
> 
> >
> > =
> > import subprocess as subp
> >
> > p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
> >  stdout=subp.PIPE, stderr=subp.STDOUT)
> > flag = "(@@}"
> > print >>p.stdin, "PROMPT", flag
> > print >>p.stdin, "HELP"
> > print >>p.stdin, "EXIT"
> > text = p.stdout.read()
> > p.wait()
> > helptext = text[text.index(flag + 'HELP') + len(flag) + 4 :
> >   text.index(flag + 'EXIT')]
> > words = [line.split(None, 1)[0]
> >for line in helptext.split('\n')
> >if line.strip()]
> > commands = [word for word in words if word.isupper()]
> >
> > dest = open('cmd_help.txt', 'wb')
> > p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
> >  stdout=dest, stderr=subp.STDOUT)
> > print >>p.stdin, "PROMPT ()"
> > print >>p.stdin, "HELP"
> > for command in commands:
> >   print >>p.stdin, "HELP", command
> > print >>p.stdin, "EXIT"
> > p.wait()
> > dest.close()
> > =end of script=
> >
> > When I run this I get no text, but just a series of cmd.exe windows
> > entitled HELP . How do I get any text?
> >
> > Thanks,
> >
> > Dick Moores
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] string.split() into a list

2005-10-18 Thread Randy Bush
> >>> l = []
> >>>a='1|2|3|4'
> >>> l=a.split('|')
> >>>l
> ['1', '2', '3', '4']
>> and stupid question of the morning (for me)
>>
>> i want to string.split() into a sequence, a la
>>
>>   l = []
>>   l = myString.split('|')
>>
>> but, of course it whines about too many values.  what is the
>> common way to do this?

maybe it's that i am actually doing

   l = []
   a, l = string.split('|')

?

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


Re: [Tutor] string.split() into a list

2005-10-18 Thread Jason Massey
well that would definitely cause your error.

The string.split sends the list to a, but after that it's looking for
something to assign to l and there's nothing leftover from the command
for it to assign.


On 10/18/05, Randy Bush <[EMAIL PROTECTED]> wrote:
> > >>> l = []
> > >>>a='1|2|3|4'
> > >>> l=a.split('|')
> > >>>l
> > ['1', '2', '3', '4']
> >> and stupid question of the morning (for me)
> >>
> >> i want to string.split() into a sequence, a la
> >>
> >>   l = []
> >>   l = myString.split('|')
> >>
> >> but, of course it whines about too many values.  what is the
> >> common way to do this?
>
> maybe it's that i am actually doing
>
>l = []
>a, l = string.split('|')
>
> ?
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string.split() into a list

2005-10-18 Thread Kent Johnson
Randy Bush wrote:
> maybe it's that i am actually doing
> 
>l = []
>a, l = string.split('|')

How about 
 >>> s='1|2|3|4'
 >>> l=s.split('|')
 >>> a, l = l[0], l[1:]
 >>> a
'1'
 >>> l
['2', '3', '4']
?

Kent


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

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


Re: [Tutor] string.split() into a list

2005-10-18 Thread Randy Bush
> The string.split sends the list to a, but after that it's looking for
> something to assign to l and there's nothing leftover from the command
> for it to assign.

nope, it does not send the list to a

>>> l = []
>>> s = 'a|b'
>>> t, l = s.split('|')
>>> t
'a'
>>> l
'b'

i believe the problem is really that the lhs is of the form
( a, [b] )

but i see no simple intuitive way to unpack into that

> 
> 
> On 10/18/05, Randy Bush <[EMAIL PROTECTED]> wrote:
> > > >>> l = []
> > > >>>a='1|2|3|4'
> > > >>> l=a.split('|')
> > > >>>l
> > > ['1', '2', '3', '4']
> > >> and stupid question of the morning (for me)
> > >>
> > >> i want to string.split() into a sequence, a la
> > >>
> > >>   l = []
> > >>   l = myString.split('|')
> > >>
> > >> but, of course it whines about too many values.  what is the
> > >> common way to do this?
> >
> > maybe it's that i am actually doing
> >
> >l = []
> >a, l = string.split('|')
> >
> > ?
> >
> >

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


[Tutor] Python books: buying advice needed

2005-10-18 Thread David Stotijn
Hi,

I'm planning on buying a book to help me learn Python. Some of the
books I'm considering are a few years old and based on an older version
of Python (e.g. 2.3).
Is it wise to buy a book based on an older version? Are the principles and methods used in those books outdated by now?
Ideally, the book I'm looking for has some "best practice" guidelines and alot of example code. 

Do you have any tips?

Thanks in advance!

David

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


Re: [Tutor] Python books: buying advice needed

2005-10-18 Thread Gian Mario Tagliaretti
2005/10/18, David Stotijn <[EMAIL PROTECTED]>:
>  I'm planning on buying a book to help me learn Python. Some of the books
> I'm considering are a few years old and based on an older version of Python
> (e.g. 2.3).

Python 2.3 is not so old... :)

>  Is it wise to buy a book based on an older version? Are the principles and
> methods used in those books outdated by now?

Most of the books covers 2.3 ATM I guess

>  Ideally, the book I'm looking for has some "best practice" guidelines and
> alot of example code.
>
>  Do you have any tips?

I found very useful the book of Magnus Lie Hatland "Practical Python"
in my opinion very well done, I've heard anyway that he is going to
publish a new revision.

cheers
--
Gian Mario Tagliaretti
PyGTK GUI programming
http://www.parafernalia.org/pygtk/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] script to change device configuration

2005-10-18 Thread Chris Hallman

I created a script that parses a directory, strips the ".txt"
extension off the file, telnet & log on to the device (the filename
is the device name), opens the associated ".txt" file, sends all the
commands containted in the file to the device and then executes some
commands to save the changes. I'm not very experienced with
programming, writing Python, using functions, OOP, etc., therefore I'd like to
know if there is a better way to do this:

#
# This scripts obtains a directory listing, strips the extensions and telnets to the
# device (which is the filename in the directory). Then it writes the commands in the
# file to the device, saves config and writes it back to SYSMAN. It can be run using:
#    python send_file4.py
#
# Note: "os" is imported for future functionality.
#
# by: TCDH
# on: 10/17/05
# revised: 10/18/05    TCDH - Added logic to check for offline devices and sign-on failures.
#

import telnetlib, re, os, string

dirpath = (r"c:\temp\sun")
dirlist = os.listdir(dirpath)
wpath = (r"c:\temp\py\send_file4.out")
output = file(wpath, "w")
for i in dirlist:
    try:
        tn = telnetlib.Telnet(i.rstrip(".txt"))
        tn.read_until("Username: ")
    except:
        output.write(i.rstrip(".txt") + ": not responding.\n")
        continue
    tn.write("cworks\n")
    (index, match, read) = tn.expect(["Password: "], 5)
    if not match:
        output.write(i.rstrip(".txt") + ": sign-on failure.\n")
        continue
    tn.write("\n")
    tn.write("conf t\n")
    rpath = (dirpath + "\\" + i)
    input = file(rpath, "r")
    for lines in file(rpath):
        tn.write(input.readline())
    tn.write("end\n")    # exit config mode
    tn.write("wr\n")    # save config
    tn.read_until("#")
    tn.write("wr net\n")    #write config to TFTP
    tn.read_until("]? ")
    tn.write("172.16.250.22\n")    # TFTP server address
    tn.read_until("]? ")
    tn.write("\n")
    tn.read_until("[confirm]")
    tn.write("\n")
    tn.read_until("#")
    tn.write("exit\n")    # end session

This script needs to be able to log which device is offline and if
there was a sign-on problem. For the first exception (device offline),
I had to use try-except because tn.expect wouldn't work. However, I had
to use tn.expect on the second exception (sign-on problem) because
try-except wouldn't work there.


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


Re: [Tutor] script question

2005-10-18 Thread Chris Hallman
Here is my
latest revision, which works very well.



# This Python script was written to forward interface down messages    

# to ESM1. If the incoming arguements match the interface file

# (c:\utils\interface.ini) then the message is forwarded.

#

# Author:    tcdh

# Date:        09/22/05

# Modified:    10/04/05    changed
os.system to use raw("r") strings. Used % and join method to join

#           
    the sys.argv's. replaced list generating lines with
one line of code.

#                





import ConfigParser, sys, os
section = sys.argv[1]
interface = sys.argv[3]
ini=ConfigParser.ConfigParser()

ini.read("c:\utils\interfaces.ini")

interface_list=ini.items(section)
for i in interface_list:
    if i[1] == interface:

        os.system(r"d:\tnd\bin\delkeep.exe -all -n l17aesm1 %s" % (" ".join(sys.argv[1:3]),)+ "*")

        os.system(r"d:\tnd\bin\cawto.exe
-cat NetNet -n l17aesm1 forward red held %s" % ("
".join(sys.argv[1:5]),))

        break


Again, thank you all for your input. I greatly appreciated it Let me know if you have any further suggestions.

On 9/30/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
Chris Hallman wrote:>> Thanks for all the input!!! I really appreciate it. I need to post a> correction to my script. What I sent was an early version. I made a few> minor modifications:
You don't seem to have incorporated any of the suggestions that you have received!I doubt that a 4000-line ini file will be any problem at all.Kent>> import ConfigParser, string, sys, os
> section = sys.argv[1]> interface = sys.argv[3]> INI=ConfigParser.ConfigParser()> INI.read("c:\utils\interfaces.ini")> interface_entries=[p for p in INI.options> (section)]
> interface_list=[INI.get(section, pw) for pw in interface_entries]> for i in interface_list:>   if i == interface:>  
os.system("d:\\tnd\\bin\\cawto.exe -cat NetNet -n l17aesm1 forward red
held " +> sys.argv[1] + " " + sys.argv[2] + " " + sys.argv[3] + " " + sys.argv[4])>>> I've researched what you all have suggested and I may need to switch to
> reading the ini file line by line or using a different file parser. The> ini file is only 100 lines long now, but will likely grow to 500 line> and maybe to 4000 lines. To give you a better background of what I'm
> doing with this script, it's running on a network management system that> receives traps. After some processing of the raw trap, messages get sent> to this script for further processing. These messages are for interface
> down traps from routers and switches in our network. I use this script> to mitigate the number of interface down messages on our console. I need> to do this because we do not need to know when every port has gone down
> (i.e. user ports). This script searches the ini file for a device name> match. Once it has found a match and if the interface matches as well, I> need to message forwarded to the console (the Windows command). If no
> matching device or interface is found, then the script can just exit.>>> On 9/26/05, *Kent Johnson* <[EMAIL PROTECTED] 
[EMAIL PROTECTED]>> wrote:>> Kent Johnson wrote:>  > *TEST FIRST* Don't optimize until you know it is too slow and you>  > have a test case that you can time to see if your 'optimizations' are
>  > making it faster.>> Pardon my shouting :-)>> Kent>> ___> Tutor maillist  -  
Tutor@python.org Tutor@python.org>> http://mail.python.org/mailman/listinfo/tutor> <
http://mail.python.org/mailman/listinfo/tutor>>>___Tutor maillist  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string.split() into a list

2005-10-18 Thread Randy Bush
> How about 
>  >>> s='1|2|3|4'
>  >>> l=s.split('|')
>  >>> a, l = l[0], l[1:]
>  >>> a
> '1'
>  >>> l
> ['2', '3', '4']

looks kinda readable.  thanks

randy

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


Re: [Tutor] string.split() into a list

2005-10-18 Thread Danny Yoo


On Tue, 18 Oct 2005, Kent Johnson wrote:

> >l = []
> >a, l = string.split('|')
>
> How about
>  >>> s='1|2|3|4'
>  >>> l=s.split('|')
>  >>> a, l = l[0], l[1:]
>  >>> a
> '1'
>  >>> l
> ['2', '3', '4']
> ?


Hi Randy,


I think you're expecting Perl behavior.  The Perl idiom for partially
destructuring an array looks something like this:

### Perl 
my @rest;
my $firstElement;
($firstElement, @rest) = split(/:/, "hello:world:this:is:a:test");
#


But we don't have a direct analog to this in Python, since there's no real
distinction between "scalar" and "array" values in the Python language.
In your original code:

l = []
a, l = string.split('|')

assigning 'l' to an empty list has no effect: it doesn't freeze the type
of 'l' so that it can only be a list.  For example:

##
>>> thing = []
>>> thing = 42
>>> thing = "blah"
##

show that we can easily redirect the name 'thing' to different kinds of
values.  List values in Python aren't any more special than other things.
If you come from a Perl background, the non-existance of array-vs-scalar
context issues is something you might need to keep in mind.


The closest we can probably get is with Kent's example, where the 'rest'
are explicitely bundled together by using list slicing.  Would something
like this be fine for you?

### Python ###
def splitFirstAndRest(delimiter, text):
pieces = text.split(delimiter)
return pieces[0], pieces[1:]

first, rest = splitFirstAndRest(':', "hello:world:this:is:a:test")
##


Best of wishes!

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


Re: [Tutor] string.split() into a list

2005-10-18 Thread Randy Bush
>>  >>> s='1|2|3|4'
>>  >>> l=s.split('|')
>>  >>> a, l = l[0], l[1:]
>>  >>> a
>> '1'
>>  >>> l
>> ['2', '3', '4']

i went with the above, or more specifically

   aList = line[:-1].split('|')
   netName, asList = aList[0], aList[1:]

not gorgeous, but readable

and my instinct for pinning a type is from modula and other
wirthwhile bondage and discipline languages.  and, yes, i
see it does not carry.  

thanks.

randy

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


Re: [Tutor] script to change device configuration

2005-10-18 Thread Kent Johnson
Chris Hallman wrote:
I'm not very experienced with programming, writing 
> Python, using functions, OOP, etc., therefore I'd like to know if there 
> is a better way to do this:

It looks pretty good to me. A few suggestions below.

> import telnetlib, re, os, string
> 
> dirpath = (r"c:\temp\sun")

The parens here and for wpath are not needed.

> dirlist = os.listdir(dirpath)
> wpath = (r"c:\temp\py\send_file4.out")
> output = file(wpath, "w")
> for i in dirlist:

i is not a very descriptive name.
You might want to check that i ends with '.txt'
device = i.rstrip('.txt')
might be handy too.

> try:
> tn = telnetlib.Telnet(i.rstrip(".txt"))
> tn.read_until("Username: ")
> except:
> output.write(i.rstrip(".txt") + ": not responding.\n")
> continue
> tn.write("cworks\n")
> (index, match, read) = tn.expect(["Password: "], 5)
> if not match:
> output.write(i.rstrip(".txt") + ": sign-on failure.\n")
> continue
> tn.write("\n")
> tn.write("conf t\n")
> rpath = (dirpath + "\\" + i)

Use os.path.join(dirpath, i)

> input = file(rpath, "r")

You don't use input

> for lines in file(rpath):
> tn.write(input.readline())
> tn.write("end\n")# exit config mode
> tn.write("wr\n")# save config
> tn.read_until("#")
> tn.write("wr net\n")#write config to TFTP
> tn.read_until("]? ")
> tn.write("172.16.250.22\n")# TFTP server address
> tn.read_until("]? ")
> tn.write("\n")
> tn.read_until("[confirm]")
> tn.write("\n")
> tn.read_until("#")
> tn.write("exit\n")# end session

The above is pretty repetitive. How about something like
  finishCmds = [
("#",   "wr net\n"),#write config to TFTP
("]? ", "172.16.250.22\n"),# TFTP server address
("]? ", "\n"),
("[confirm]", "\n"),
("#",   "exit\n"),# end session
  ]
  for prompt, response in finishCmds:
tn.read_until(prompt)
tn.write(response)

Kent

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


Re: [Tutor] Python books: buying advice needed

2005-10-18 Thread Kent Johnson
David Stotijn wrote:
> Hi,
> 
> I'm planning on buying a book to help me learn Python. Some of the books 
> I'm considering are a few years old and based on an older version of 
> Python (e.g. 2.3).
> Is it wise to buy a book based on an older version? Are the principles 
> and methods used in those books outdated by now?

2.3 is not that old. The basics of Python change slowly, especially the 
beginner-level stuff. The later versions introduce refinements, convenience 
features and new, advanced stuff; they don't generally break much or change the 
overall 'feel' of Python. I don't know if there are any books out yet that 
cover 2.4.

> Ideally, the book I'm looking for has some "best practice" guidelines 
> and alot of example code.
> 
> Do you have any tips?

I like _Learning Python_

Kent

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


Re: [Tutor] Any good Glade and python tutorials?

2005-10-18 Thread Luis N
You might want to search for how it fits together with twisted's reactor

http://www.google.com/search?q=twisted+glade+reactor

Luis.

On 10/15/05, Adam <[EMAIL PROTECTED]> wrote:
> I'm making a Twisted app that needs a client side GUI and GTK seems like the
> best way to go about this. Can anybody point me in the direction of any
> decent tutorials on how to use Glade with python and pyGTK.
>  Thanks.
>  Adam.
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor