Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'subject:Python': 0.05; 'lines,': 0.05; 'sys': 0.05; 'bash': 0.07; 'filename': 0.07; 'think,': 0.07; 'try:': 0.07; 'python': 0.09; 'tuple': 0.09; '(but': 0.15; 'skip:f 30': 0.15; '/usr/bin/env': 0.16; 'balls': 0.16; 'brutal': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'holy': 0.16; 'indexerror:': 0.16; 'instead:': 0.16; 'itemgetter': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'sorts': 0.16; 'subject:ever': 0.16; 'subject:program': 0.16; 'wrote:': 0.17; 'numerical': 0.17; 'shell': 0.18; 'input': 0.18; 'file.': 0.20; 'sort': 0.21; 'import': 0.21; 'posted': 0.22; "i'd": 0.22; 'statement': 0.23; 'task': 0.23; 'raise': 0.24; 'script': 0.24; 'pass': 0.25; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'checking': 0.27; 'lines': 0.28; 'crash': 0.29; 'optional': 0.29; 'prints': 0.29; 'received:192.168.1.3': 0.29; "i'm": 0.29; 'error': 0.30; 'implement': 0.32; 'received:84': 0.32; 'could': 0.32; 'to:addr :python-list': 0.33; "can't": 0.34; 'described': 0.35; 'continue': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'wanted': 0.36; 'brian': 0.36; 'display': 0.36; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'received:192': 0.39; 'short': 0.39; 'little': 0.39; 'received:192.168': 0.40; 'most': 0.61; 'subject:, ': 0.61; 'first': 0.61; 'back': 0.62; 'life': 0.66; 'header:Reply-To:1': 0.68; 'greetings': 0.69; 'lives': 0.71; 'secret': 0.71; 'reply- to:no real name:2**0': 0.72; 'lemon': 0.84; 'reply- to:addr:python.org': 0.84; 'say:': 0.84; 'feelings': 0.91; 'kat': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=FLuZNpUs c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=DKcI9XZsuF4A:10 a=d7ezg7mMz5sA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=S63XM4I-235vPKCLKuUA:9 a=wPNLvfGTeEIA:10 a=z4-Jy3BZVoCGg4aj:21 a=K8KyAGjDdKXJVC94:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Sat, 21 Jul 2012 20:40:46 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: My first ever Python program, comments welcome References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 85 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342899651 news.xs4all.nl 6864 [2001:888:2000:d::a6]:35646 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25764 On 21/07/2012 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's the purpose of this line? > 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)) Short is: l.sort(key=itemgetter(0)) > > try: > inCount = int(argv[2]) > lineCount = inCount You may as well say: lineCount = int(argv[2]) > except IndexError: > #just catch the error and continue > None The do-nothing statement is: pass > > for c in range(lineCount): > t=l[c] If there are fewer than 'lineCount' lines, this will raise IndexError. You could do this instead: for t in l[ : lineCount]: > print(t[0], t[1], sep='\t', end='') >