Re: [Tutor] Print List

2012-09-13 Thread Alan Gauld

On 13/09/12 00:36, aklei...@sonic.net wrote:


To make it even more generic I would suggest replacing
"""   print(theHeader) """
with
"""   if theHeader:
   print(theHeader)
"""
to avoid a blank line if you don't need/want a header line.


Good catch, yes, that would be better.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print List

2012-09-12 Thread Steven D'Aprano

On 13/09/12 10:57, bob gailer wrote:

On 9/12/2012 11:36 AM, Ashley Fowler wrote:

I am trying to complete the following below:
You also need to write a function "printList" of one parameter that
takes a list as its input and neatly prints the entire contents of the
list in a column.
Any Suggestions or Corrections?


I would correct the use of "neatly" and "column"- these are ill-defined terms!

Specifications should leave nothing to be guessed or assumed



Bob, "neatly" and "column" are perfectly clear and simple English words
that don't need additional definition. One might just as well say that
your use of the words "correct", "terms", "leave", "nothing", "guessed"
and "assumed" are ill-defined.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print List

2012-09-12 Thread bob gailer

On 9/12/2012 11:36 AM, Ashley Fowler wrote:

I am trying to complete the following below:
You also need to write a function "printList" of one parameter that
takes a list as its input and neatly prints the entire contents of the
list in a column.
Any Suggestions or Corrections?
I would correct the use of "neatly" and "column"- these are ill-defined 
terms!


Specifications should leave nothing to be guessed or assumed

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print List

2012-09-12 Thread akleider
> On 12/09/12 16:36, Ashley Fowler wrote:
>
>> def printList(lists):
>>  print("First Name\tLast Name\tCredits\tGPA")
>>  for i in lists:
>>  print (i)
>>
>>
>> Any Suggestions or Corrections?
>
> The input parameter is called 'lists' which implies that the input is
> more than one list. Try to make your input parameter names as accurate
> as possible. In this case you might think 'list' would be good, but its
> no, because list is a Python builtin word. So we would be better to
> choose something like aList or theList.
>
> Your function could have been generic in that it printed any kind of
> list but by printing a header line you have made it specific to a list
> of students. So you could call the input studentList.
>
> In general, in Python, generic functions are favoured. One way to have a
> header and be generic would be to pass the header in as a parameter too:
>
> def printList(theList, theHeader=""):
> print(theHeader)
> for item in theList:
>print item
>
>
> And then you would call it with:
>
> printList(myStudentList, "First Name\tLast Name\tCredits\tGPA")
>
> Or
>
> printList(myPetList, "Name, Breed, Age")
>
> Or
>
> printList(myBlankList)   # uses the default empty header
>
> or whatever...
>
> --
> Alan G

To make it even more generic I would suggest replacing
"""   print(theHeader) """
with
"""   if theHeader:
  print(theHeader)
"""
to avoid a blank line if you don't need/want a header line.
ak
> def printList(theList, theHeader=""):
> print(theHeader)
> for item in theList:
>print item
>


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print List

2012-09-12 Thread Alan Gauld

On 12/09/12 16:36, Ashley Fowler wrote:


def printList(lists):
 print("First Name\tLast Name\tCredits\tGPA")
 for i in lists:
 print (i)


Any Suggestions or Corrections?


The input parameter is called 'lists' which implies that the input is 
more than one list. Try to make your input parameter names as accurate 
as possible. In this case you might think 'list' would be good, but its 
no, because list is a Python builtin word. So we would be better to 
choose something like aList or theList.


Your function could have been generic in that it printed any kind of 
list but by printing a header line you have made it specific to a list 
of students. So you could call the input studentList.


In general, in Python, generic functions are favoured. One way to have a 
header and be generic would be to pass the header in as a parameter too:


def printList(theList, theHeader=""):
   print(theHeader)
   for item in theList:
  print item


And then you would call it with:

printList(myStudentList, "First Name\tLast Name\tCredits\tGPA")

Or

printList(myPetList, "Name, Breed, Age")

Or

printList(myBlankList)   # uses the default empty header

or whatever...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print List

2012-09-12 Thread Brannon, Terrence


From: Tutor 
[mailto:tutor-bounces+terrence.brannon=bankofamerica@python.org] On Behalf 
Of Ashley Fowler
Sent: Wednesday, September 12, 2012 11:36 AM
To: tutor@python.org
Subject: [Tutor] Print List


I am trying to complete the following below:



You also need to write a function "printList" of one parameter that

takes a list as its input

[Terrence Brannon] since it is taking a list as its input, why not call it 
students or simply list? Calling it "lists" implies that it is a list that 
contains other lists

and neatly prints the entire contents of the

list in a column. Each student in the list should be printed using __str__.



So far i have:



def printList(lists):

print("First Name\tLast Name\tCredits\tGPA")

for i in lists:

print (i)



Any Suggestions or Corrections?

[Terrence Brannon]

def print_list(students):

for student in students:

   print("{0}\t{1}\t{2}\t{3}".format(student.first_name, student.last_name, 
student.credits, student.gpa))



OHHH Wait a minute... that means you need to redefine the __str__() method 
of your Student class:



class Student(object):

  

 def __str__(self):

return "{0}\t{1}\t{2}\t{3}".format(student.first_name, student.last_name, 
student.credits, student.gpa)



And then print_list is simply:

def print_list(students):

for student in students:

   print student







--
This message w/attachments (message) is intended solely for the use of the 
intended recipient(s) and may contain information that is privileged, 
confidential or proprietary. If you are not an intended recipient, please 
notify the sender, and then please delete and destroy all copies and 
attachments, and be advised that any review or dissemination of, or the taking 
of any action in reliance on, the information contained in or attached to this 
message is prohibited. 
Unless specifically indicated, this message is not an offer to sell or a 
solicitation of any investment products or other financial product or service, 
an official confirmation of any transaction, or an official statement of 
Sender. Subject to applicable law, Sender may intercept, monitor, review and 
retain e-communications (EC) traveling through its networks/systems and may 
produce any such EC to regulators, law enforcement, in litigation and as 
required by law. 
The laws of the country of each sender/recipient may impact the handling of EC, 
and EC may be archived, supervised and produced in countries other than the 
country in which you are located. This message cannot be guaranteed to be 
secure or free of errors or viruses. 

References to "Sender" are references to any subsidiary of Bank of America 
Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are 
Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a 
Condition to Any Banking Service or Activity * Are Not Insured by Any Federal 
Government Agency. Attachments that are part of this EC may have additional 
important disclosures and disclaimers, which you should read. This message is 
subject to terms available at the following link: 
http://www.bankofamerica.com/emaildisclaimer. By messaging with Sender you 
consent to the foregoing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print List

2012-09-12 Thread Oscar Benjamin
On Sep 12, 2012 4:53 PM, "Ashley Fowler" 
wrote:
>
> I am trying to complete the following below:
>
>
> You also need to write a function "printList" of one parameter that
> takes a list as its input and neatly prints the entire contents of the
> list in a column. Each student in the list should be printed using
__str__.
>
>
> So far i have:
>
>
> def printList(lists):
> print("First Name\tLast Name\tCredits\tGPA")
> for i in lists:
> print (i)
>
>
> Any Suggestions or Corrections?

Looks good to me.

My only suggestion is to use more descriptive names. I would have called it
'students' instead of 'lists' and 'student' instead of 'i'. I don't really
like 'printList' either but I guess your stuck with that.

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Print List

2012-09-12 Thread Ashley Fowler
I am trying to complete the following below:


You also need to write a function "printList" of one parameter that
takes a list as its input and neatly prints the entire contents of the
list in a column. Each student in the list should be printed using __str__.



So far i have:


def printList(lists):
print("First Name\tLast Name\tCredits\tGPA")
for i in lists:
print (i)


Any Suggestions or Corrections?

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print list vs telnetlib.telnet.write differences

2006-03-06 Thread Python
On Tue, 2006-03-07 at 13:39 +1000, STREET Gideon (SPARQ) wrote:
> The second banner exec is red, the previous commands I send are in
> green.  So I take it to mean that it is an echo produced by the router.
> MMmm strange as the echo is overwriting what I'm sending initially.

Well the device is designed for interactive use by a human who does not
spew characters nearly has quickly as your script.  In this case you
probably need to read the response to the exec banner command before
sending the banner.  Most commands are done in a single line, but banner
is an exception.

This kind of scripting is often done with expect and I believe that
there is some expect-like module for Python.

As I suggested earlier, getting tftp to send command files will make a
much better long term solution.  This script would only have to manage
logging in and starting tftp.  Once that works all other commands would
be in the file that tftp transfers.  Linux/Unix systems already have
tftp built in.  Cisco provides a tftp implementation for Windows.

It is easy to put comments into the command files and also use
subversion or some other version control package to track changes.

> 
> The banner appears to work but is then overwritten, maybe I need to come
> up with another way of sending the commands so I get around the echo.
> 
> Thanks
> 
> Gideon
> 
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
> Behalf Of Python
> Sent: Tuesday, 7 March 2006 12:52 PM
> To: Tutor Python
> Subject: Re: [Tutor] Print list vs telnetlib.telnet.write differences
> 
> On Tue, 2006-03-07 at 11:31 +1000, STREET Gideon (SPARQ) wrote:
> > Enter configuration commands, one per line.  End with CNTL/Z.
> > switch01(config)#banner exec ^
> >
> >  ##
> >
> >  switch01
> >
> > Level XX, XX Some Street, Somewhere
> >Site contact: John Citizen 555 
> >  System Contact: Helpdesk  555 5556
> >
> >  ##
> >
> >  ^
> > banner exec ^  < this is the second time this command is sent to
> > the device Enter TEXT message.  End with the character '^'.
> >
> >  ###exit
> > 
> 
> My tcpwatch.py display shows different colors for each side of the
> conversation.  Do you get two colors?  are both in the same color?
> 
> Isn't the second banner exec simply the echo back from the cisco device?
> It is giving you your entry instructions.
> 
> 1.  Is the banner exec command working?
> If it works, then the funny duplication is almost certainly just an
> artifact of characters getting echoed from the cisco device.
> 
> --
> Lloyd Kvam
> Venix Corp
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> This e-mail (including any attachments) may contain confidential or
> privileged information and is intended for the sole use of the person(s) to
> whom it is addressed. If you are not the intended recipient, or the person
> responsible for delivering this message to the intended recipient, please
> notify the sender of the message or send an e-mail to
> mailto:[EMAIL PROTECTED] immediately, and delete all copies. Any
> unauthorised review, use, alteration, disclosure or distribution of this
> e-mail by an unintended recipient is prohibited. Ergon Energy accepts no
> responsibility for the content of any e-mail sent by an employee which is of
> a personal nature.
> 
> Ergon Energy Corporation Limited  ABN 50 087 646 062
> Ergon Energy Pty Ltd  ABN 66 078 875 902
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Print list vs telnetlib.telnet.write differences

2006-03-06 Thread Python
On Tue, 2006-03-07 at 11:31 +1000, STREET Gideon (SPARQ) wrote:
> Enter configuration commands, one per line.  End with CNTL/Z.
> switch01(config)#banner exec ^
> 
>  ##
> 
>  switch01
> 
> Level XX, XX Some Street, Somewhere
>Site contact: John Citizen 555 
>  System Contact: Helpdesk  555 5556
> 
>  ##
> 
>  ^
> banner exec ^  < this is the second time this command is sent to the 
> device
> Enter TEXT message.  End with the character '^'.
> 
>  ###exit
> 

My tcpwatch.py display shows different colors for each side of the
conversation.  Do you get two colors?  are both in the same color?

Isn't the second banner exec simply the echo back from the cisco device?
It is giving you your entry instructions.

1.  Is the banner exec command working?
If it works, then the funny duplication is almost certainly just an
artifact of characters getting echoed from the cisco device.

-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Print list vs telnetlib.telnet.write differences

2006-03-06 Thread STREET Gideon (SPARQ)

Lloyd/David and all,

I'm still seeing something a bit weird with sending commands back to the
router via tn.write().  This is with python 2.4.2

As per David's last email I replaced:

tn.read_until('#')
for item in lines:
x = item
tn.write(x)

With:

blob = string.join(lines)

print blob

#tn.write("\03")   #  Assures the device is in enable mode
x = tn.read_until("#")#  The "x =" seems to help flush the read
buffer
tn.write("conf t\n")
x = tn.read_until("#")
tn.write(blob)

tn.read_until('#')
tn.write('exit' '\n') #disconnect from the session


But I am still seeing a command being sent to the device twice :(  As
per the above I've got a print command outputting what I'm sending back
to the router.  The output of the print blob looks like (output shown
between the lines):
___
banner exec ^

 ##

 switch01

Level XX, XX Some Street, Somewhere
   Site contact: John Citizen 555 
 System Contact: Helpdesk  555 5556

 ##

 ^
___

Yet the output on TCPWatch looks like (output shown between the lines):

Enter configuration commands, one per line.  End with CNTL/Z.
switch01(config)#banner exec ^

 ##

 switch01

Level XX, XX Some Street, Somewhere
   Site contact: John Citizen 555 
 System Contact: Helpdesk  555 5556

 ##

 ^
banner exec ^  < this is the second time this command is sent to the
device
Enter TEXT message.  End with the character '^'.

 ###exit




Any explanation of what I'm doing wrong to get duplication of the sent
commands?  It looks like the script is looping on the print blob, but I
can't see why it should do that?

Thanks

Gideon

(I can send through the entire script but it's a 170+ lines and didn't
want to send an epic email to the list)


-Original Message-----
From: Python [mailto:[EMAIL PROTECTED]
Sent: Saturday, 4 March 2006 3:42 AM
To: STREET Gideon (SPARQ)
Cc: Tutor Python
Subject: Re: [Tutor] Print list vs telnetlib.telnet.write differences

On Fri, 2006-03-03 at 15:41 +1000, STREET Gideon (SPARQ) wrote:
> The problem I'm stumbling over is that when I print x, the output is
> what I want.  If I delete the print x and #, leaving only tn.write(x)
> on the last line everything looks good except it writes the 1st item
> in "lines" (the banner exec command) twice, once at the beginning
> where it's supposed to, but then once again at the end. See except
> below for example.
>
> Anyone able to explain why that is happening or is it me just not
> understanding what I'm doing?  Hope my explanation is clear I'm still
> unfamiliar with the exact phrasology.
>
Could that be your data being echoed back to you?  Is the router
configured correctly after the script runs?

I normally feed commands to Cisco devices using tftp.  It is relatively
easy to edit a file to get the commands correct.  Then you could limit
your "conversational script" to logging in and running tftp to transfer
the command file.

It looks like you are pretty close to having this working.

--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:  603-653-8139
fax:320-210-3409
--
Lloyd Kvam
Venix Corp




This e-mail (including any attachments) may contain confidential or
privileged information and is intended for the sole use of the person(s) to
whom it is addressed. If you are not the intended recipient, or the person
responsible for delivering this message to the intended recipient, please
notify the sender of the message or send an e-mail to
mailto:[EMAIL PROTECTED] immediately, and delete all copies. Any
unauthorised review, use, alteration, disclosure or distribution of this
e-mail by an unintended recipient is prohibited. Ergon Energy accepts no
responsibility for the content of any e-mail sent by an employee which is of
a personal nature.

Ergon Energy Corporation Limited  ABN 50 087 646 062
Ergon Energy Pty Ltd  ABN 66 078 875 902
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print list vs telnetlib.telnet.write differences

2006-03-03 Thread Python
On Fri, 2006-03-03 at 15:41 +1000, STREET Gideon (SPARQ) wrote:
> The problem I'm stumbling over is that when I print x, the output is
> what I want.  If I delete the print x and #, leaving only tn.write(x)
> on
> the last line everything looks good except it writes the 1st item in
> "lines" (the banner exec command) twice, once at the beginning where
> it's supposed to, but then once again at the end. See except below for
> example.
> 
> Anyone able to explain why that is happening or is it me just not
> understanding what I'm doing?  Hope my explanation is clear I'm still
> unfamiliar with the exact phrasology.
> 
Could that be your data being echoed back to you?  Is the router
configured correctly after the script runs?

I normally feed commands to Cisco devices using tftp.  It is relatively
easy to edit a file to get the commands correct.  Then you could limit
your "conversational script" to logging in and running tftp to transfer
the command file.

It looks like you are pretty close to having this working.

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:  603-653-8139
fax:320-210-3409
-- 
Lloyd Kvam
Venix Corp

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


Re: [Tutor] Print list vs telnetlib.telnet.write differences

2006-03-02 Thread David Heiser

I believe you can submit the new config content as a blob where blob =
string.join(lines). It looks like your "switch" uses IOS, not CatOS, so
make sure you send "config t" first.

And I would strip out the \r's.

Then maybe:

tn.write("\03")   #  Assures the device is in enable mode
x = tn.read_until("#")#  The "x =" seems to help flush the read
buffer
tn.write("conf t\n")
x = tn.read_until("#")
tn.write(blob)




-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of STREET Gideon (SPARQ)
Sent: Thursday, March 02, 2006 10:42 PM
To: tutor@python.org
Subject: [Tutor] Print list vs telnetlib.telnet.write differences



Hi all,

I've got the following snippet in a script I'm playing around with and
need some help.

Basically the script telnets to a cisco switch, "tn.read_until" a
subsection of the config, outputs to a file, then using readlines brings
it back into the script as a list where I'm changing some fields and
then outputting it again.  (the script is long and messy but it's my
second attempt at writing something to make my network admin life
easier, I'm sure over time I can make it a bit more robust).

Anyway, "lines" is the list read from a text file, and a "print lines"
looks like:

['banner exec ^\r\n',
'##\r\n',
'   EMKYSW34  - User Access Switch, 2nd Floor\r\n', "  Corner of XX
and 
 St's XX XXX\r\n", 'Site Contact: KXXX SX  XX XXX0\r\n',
'
System Contact: X Helpdesk XX\r\n',
'### ###\r\n', '^\r\n']

As I'm trying to get the above cleaned up banner sent to the telnet
session I use the following command (without the print x # part).

tn.read_until('#')
for item in lines:
x = item
print x #tn.write(x) 

The problem I'm stumbling over is that when I print x, the output is
what I want.  If I delete the print x and #, leaving only tn.write(x) on
the last line everything looks good except it writes the 1st item in
"lines" (the banner exec command) twice, once at the beginning where
it's supposed to, but then once again at the end. See except below for
example.

Anyone able to explain why that is happening or is it me just not
understanding what I'm doing?  Hope my explanation is clear I'm still
unfamiliar with the exact phrasology.


emkysw34#conf t
conf t
Enter configuration commands, one per line.  End with CNTL/Z.
emkysw34(config)#banner exec ^
##
EMKYSW34  - User Access Switch, 2nd Floor
  Corner of XX and XX St's XX XXX
Site Contact:  XX  XX 
   System Contact: X Helpdesk 
##
^
banner exec ^  <heres the second instance being printed for no
reason I can understand Enter TEXT message.  End with the character '^'.
exit ## 

Thanks

Gideon

This e-mail (including any attachments) may contain confidential or
privileged information and is intended for the sole use of the person(s)
to whom it is addressed. If you are not the intended recipient, or the
person responsible for delivering this message to the intended
recipient, please notify the sender of the message or send an e-mail to
mailto:[EMAIL PROTECTED] immediately, and delete all copies. Any
unauthorised review, use, alteration, disclosure or distribution of this
e-mail by an unintended recipient is prohibited. Ergon Energy accepts no
responsibility for the content of any e-mail sent by an employee which
is of a personal nature.

Ergon Energy Corporation Limited  ABN 50 087 646 062
Ergon Energy Pty Ltd  ABN 66 078 875 902
___
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] Print list vs telnetlib.telnet.write differences

2006-03-02 Thread STREET Gideon (SPARQ)

Hi all,

I've got the following snippet in a script I'm playing around with and
need some help.

Basically the script telnets to a cisco switch, "tn.read_until" a
subsection of the config, outputs to a file, then using readlines brings
it back into the script as a list where I'm changing some fields and
then outputting it again.  (the script is long and messy but it's my
second attempt at writing something to make my network admin life
easier, I'm sure over time I can make it a bit more robust).

Anyway, "lines" is the list read from a text file, and a "print lines"
looks like:

['banner exec ^\r\n',
'##\r\n',
'   EMKYSW34  - User Access Switch, 2nd Floor\r\n', "  Corner of XX
and 
 St's XX XXX\r\n", 'Site Contact: KXXX SX  XX XXX0\r\n',
'
System Contact: X Helpdesk XX\r\n',
'###
###\r\n', '^\r\n']

As I'm trying to get the above cleaned up banner sent to the telnet
session I use the following command (without the print x # part).

tn.read_until('#')
for item in lines:
x = item
print x #tn.write(x) 

The problem I'm stumbling over is that when I print x, the output is
what I want.  If I delete the print x and #, leaving only tn.write(x) on
the last line everything looks good except it writes the 1st item in
"lines" (the banner exec command) twice, once at the beginning where
it's supposed to, but then once again at the end. See except below for
example.

Anyone able to explain why that is happening or is it me just not
understanding what I'm doing?  Hope my explanation is clear I'm still
unfamiliar with the exact phrasology.


emkysw34#conf t
conf t
Enter configuration commands, one per line.  End with CNTL/Z.
emkysw34(config)#banner exec ^
##
EMKYSW34  - User Access Switch, 2nd Floor
  Corner of XX and XX St's XX XXX
Site Contact:  XX  XX 
   System Contact: X Helpdesk 
##
^
banner exec ^