Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25761 > unrolled thread
| Started by | Lipska the Kat <lipska@lipskathekat.com> |
|---|---|
| First post | 2012-07-21 20:08 +0100 |
| Last post | 2012-07-23 09:12 +0200 |
| Articles | 3 on this page of 23 — 13 participants |
Back to article view | Back to comp.lang.python
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
Page 2 of 2 — ← Prev page 1 [2]
| From | David <bouncingcats@gmail.com> |
|---|---|
| Date | 2012-07-22 20:46 +1000 |
| Message-ID | <mailman.2429.1342953989.4697.python-list@python.org> |
| In reply to | #25800 |
On 22/07/2012, Lipska the Kat <lipska@lipskathekat.com> wrote: > On 21/07/12 21:10, Dave Angel wrote: >> >> A totally off-the-wall query. Are you using a source control system, >> such as git ? It can make you much braver about refactoring a working >> program. > > Thanks for your comments, I've taken them on board, > I'm most familiar with with cvs and svn for source control. I've also > used Microsoft source safe. I generally just use what's given to me by > whoever is paying me and don't worry too much about the details. Many in > the Linux world seem to use git. Seeing as I've been using Linux at home > since the early days of slackware I suppose I'd better look into it. What Dave said. I used CVS briefly and then git and its gui tools for last 5 years. Took me a while to get comfortable with it, but now it turns managing complex, evolving text files into fun and I cannot imagine working without its power and flexibility. First thing I do on any programming task: git init
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-07-22 09:56 +0200 |
| Message-ID | <mailman.2420.1342943873.4697.python-list@python.org> |
| In reply to | #25761 |
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
> filename=argv[1]
> lineCount=10
>
> with fileinput.input(files=(filename)) as f:
Note that (filename) is not a tuple, just a string surrounded by superfluous
parens.
>>> filename = "foo.bar"
>>> (filename)
'foo.bar'
>>> (filename,)
('foo.bar',)
>>> filename,
('foo.bar',)
You are lucky that FileInput() tests if its files argument is just a single
string.
> 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
> except IndexError:
> #just catch the error and continue
> None
>
> for c in range(lineCount):
> t=l[c]
> print(t[0], t[1], sep='\t', end='')
>
I prefer a more structured approach even for such a tiny program:
- process all commandline args
- read data
- sort
- clip extra lines
- write data
I'd break it into these functions:
def get_commmandline_args():
"""Recommended library: argparse.
Its FileType can deal with stdin/stdout.
"""
def get_quantity(line):
return int(line.split("\t", 1)[0])
def sorted_by_quantity(lines):
"""Leaves the lines intact, so you don't
have to reassemble them later on."""
return sorted(lines, key=get_quantity)
def head(lines, count):
"""Have a look at itertools.islice() for a more
general approach"""
return lines[:count]
if __name__ == "__main__":
# protecting the script body allows you to import
# the script as a library into other programs
# and reuse its functions and classes.
# Also: play nice with pydoc. Try
# $ python -m pydoc -w ./yourscript.py
args = get_commandline_args()
with args.infile as f:
lines = sorted_by_quantity(f)
with args.outfile as f:
f.writelines(head(lines, args.line_count))
Note that if you want to handle large files gracefully you need to recombine
sorted_by_quantity() and head() (have a look at heapq.nsmallest() which was
already mentioned in the other thread).
[toc] | [prev] | [next] | [standalone]
| From | "Ivan@work" <ivan.cvetkovic@pakel.hr> |
|---|---|
| Date | 2012-07-23 09:12 +0200 |
| Message-ID | <juitg3$62c$1@ls237.t-com.hr> |
| In reply to | #25761 |
On 21.07.2012 21: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=[]
You can do without this, see below.
> t = tuple
This initialization does nothing. Assignment t=(line.split('\t')) makes
`t` a list (not a tuple), discarding any previous value. And you don't
really need t:
> with fileinput.input(files=(filename)) as f:
> for line in f:
> t=(line.split('\t'))
> t[0]=int(t[0])
> l.append(t)
List comprehension is your friend, and now you don't need to initialize
l to an empty list.
with open(filename) as f:
l = [line.split('\t') for line in f]
The first element of each row is now a string, but it's easy to fix:
> l=sorted(l, key=itemgetter(0))
Use in-place sorting and cast the sorting element to int
l.sort(key=lambda t: int(t[0]))
> inCount = int(argv[2])
> lineCount = inCount
lineCount = int(argv[2]) works just fine
>
> for c in range(lineCount):
> t=l[c]
> print(t[0], t[1], sep='\t', end='')
Whenever you write "for i in range(n)" you're (probably) doing it wrong.
Here you can use list slicing, and as a bonus the program doesn't bomb
when lineCount is greater than length(l)
for t in l[:lineCount]:
print(t[0], t[1], sep='\t', end='')
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web