Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25763
| Date | 2012-07-21 20:34 +0100 |
|---|---|
| From | Ian Foote <ian@feete.org> |
| Subject | Re: My first ever Python program, comments welcome |
| References | <pZqdnadVUa-EY5fNnZ2dnUVZ7rednZ2d@bt.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2394.1342899297.4697.python-list@python.org> (permalink) |
On 21/07/12 20:08, Lipska the Kat wrote:
> Greetings Pythoners
>
> A short while back I posted a message that described a task I had set
> myself. I wanted to implement the following bash shell script in Python
>
> Here's the script
>
> sort -nr $1 | head -${2:-10}
>
> this script takes a filename and an optional number of lines to display
> and sorts the lines in numerical order, printing them to standard out.
> if no optional number of lines are input the script prints 10 lines
>
> Here's the file.
>
> 50 Parrots
> 12 Storage Jars
> 6 Lemon Currys
> 2 Pythons
> 14 Spam Fritters
> 23 Flying Circuses
> 1 Meaning Of Life
> 123 Holy Grails
> 76 Secret Policemans Balls
> 8 Something Completely Differents
> 12 Lives of Brian
> 49 Spatulas
>
>
> ... and here's my very first attempt at a Python program
> I'd be interested to know what you think, you can't hurt my feelings
> just be brutal (but fair). There is very little error checking as you
> can see and I'm sure you can crash the program easily.
> 'Better' implementations most welcome
>
> #! /usr/bin/env python3.2
>
> import fileinput
> from sys import argv
> from operator import itemgetter
>
> l=[]
> t = tuple
What is this line supposed to do? If you're trying to make an empty
tuple, you can write:
t = ()
But I don't think this is needed at all.
> filename=argv[1]
> lineCount=10
>
> with fileinput.input(files=(filename)) as f:
> for line in f:
> t=(line.split('\t'))
> t[0]=int(t[0])
> l.append(t)
> l=sorted(l, key=itemgetter(0))
>
> try:
> inCount = int(argv[2])
> lineCount = inCount
I don't think you need to split this into two lines here.
try:
lineCount = int(argv[2])
should work.
> except IndexError:
> #just catch the error and continue
> None
I would use pass instead of None here - I want to "do nothing" rather
than create a None object.
> for c in range(lineCount):
> t=l[c]
> print(t[0], t[1], sep='\t', end='')
>
> Thanks
>
> Lipska
>
My only other point is that you might find it helpful to use slightly
more verbose names than l or t - its not immediately obvious to the
reader what these are intended to represent.
Regards,
Ian
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
My first ever Python program, comments welcome Lipska the Kat <lipska@lipskathekat.com> - 2012-07-21 20:08 +0100
Re: My first ever Python program, comments welcome Ian Foote <ian@feete.org> - 2012-07-21 20:34 +0100
Re: My first ever Python program, comments welcome MRAB <python@mrabarnett.plus.com> - 2012-07-21 20:40 +0100
Re: My first ever Python program, comments welcome Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-22 00:32 +0000
Re: My first ever Python program, comments welcome MRAB <python@mrabarnett.plus.com> - 2012-07-22 02:56 +0100
Re: My first ever Python program, comments welcome Chris Angelico <rosuav@gmail.com> - 2012-07-22 11:59 +1000
Re: My first ever Python program, comments welcome Dave Angel <d@davea.name> - 2012-07-21 22:01 -0400
Re: My first ever Python program, comments welcome Dave Angel <d@davea.name> - 2012-07-21 16:10 -0400
Re: My first ever Python program, comments welcome Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-22 00:56 +0000
Re: My first ever Python program, comments welcome rusi <rustompmody@gmail.com> - 2012-07-21 19:55 -0700
Re: My first ever Python program, comments welcome Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-07-22 09:14 +0100
Re: My first ever Python program, comments welcome Lipska the Kat <lipska@lipskathekat.com> - 2012-07-22 10:20 +0100
Re: My first ever Python program, comments welcome rusi <rustompmody@gmail.com> - 2012-07-22 09:18 -0700
Re: My first ever Python program, comments welcome Lipska the Kat <lipska@lipskathekat.com> - 2012-07-22 18:23 +0100
Re: My first ever Python program, comments welcome rusi <rustompmody@gmail.com> - 2012-07-23 22:13 -0700
Re: My first ever Python program, comments welcome Lipska the Kat <lipskathekat@yahoo.co.uk> - 2012-07-24 12:34 +0100
Re: My first ever Python program, comments welcome Lipska the Kat <lipska@lipskathekat.com> - 2012-07-22 09:37 +0100
Re: My first ever Python program, comments welcome Andrew Berg <bahamutzero8825@gmail.com> - 2012-07-22 03:49 -0500
Re: My first ever Python program, comments welcome Chris Angelico <rosuav@gmail.com> - 2012-07-22 20:17 +1000
Re: My first ever Python program, comments welcome Lipska the Kat <lipska@lipskathekat.com> - 2012-07-22 13:36 +0100
Re: My first ever Python program, comments welcome David <bouncingcats@gmail.com> - 2012-07-22 20:46 +1000
Re: My first ever Python program, comments welcome Peter Otten <__peter__@web.de> - 2012-07-22 09:56 +0200
Re: My first ever Python program, comments welcome "Ivan@work" <ivan.cvetkovic@pakel.hr> - 2012-07-23 09:12 +0200
csiph-web