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!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'subject:Python': 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; 'slightly': 0.15; '/usr/bin/env': 0.16; 'balls': 0.16; 'brutal': 0.16; 'from:addr:ian': 0.16; 'holy': 0.16; 'indexerror:': 0.16; 'itemgetter': 0.16; 'received:208.97.132.81': 0.16; 'sorts': 0.16; 'subject:ever': 0.16; 'subject:program': 0.16; 'tuple,': 0.16; 'verbose': 0.16; 'wrote:': 0.17; 'numerical': 0.17; 'shell': 0.18; 'input': 0.18; 'file.': 0.20; 'sort': 0.21; 'trying': 0.21; 'import': 0.21; 'supposed': 0.21; 'object.': 0.22; 'posted': 0.22; "i'd": 0.22; 'work.': 0.23; 'split': 0.23; 'task': 0.23; '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; 'all.': 0.28; '"do': 0.29; 'crash': 0.29; 'optional': 0.29; 'prints': 0.29; 'received:208.97': 0.29; 'received:208.97.132': 0.29; 'received:dreamhost.com': 0.29; 'received:g.dreamhost.com': 0.29; "i'm": 0.29; 'error': 0.30; 'helpful': 0.30; 'point': 0.31; 'implement': 0.32; 'to:addr:python-list': 0.33; "can't": 0.34; 'thanks': 0.34; 'needed': 0.35; 'described': 0.35; 'continue': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'wanted': 0.36; 'brian': 0.36; 'should': 0.36; 'display': 0.36; 'two': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'received:192': 0.39; 'short': 0.39; 'little': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'most': 0.61; 'subject:, ': 0.61; 'first': 0.61; 'back': 0.62; 'received:208': 0.63; 'more': 0.63; 'here': 0.65; 'life': 0.66; 'greetings': 0.69; 'lives': 0.71; 'obvious': 0.71; 'secret': 0.71; 'lemon': 0.84; 'feelings': 0.91; 'kat': 0.91; 'write:': 0.91 DomainKey-Signature: a=rsa-sha1; c=nofws; d=feete.org; h=message-id:date:from :mime-version:to:subject:references:in-reply-to:content-type: content-transfer-encoding; q=dns; s=feete.org; b=J0zr02/IbWPOJyr ZWEZ4bM9vXWkqQi8jKaGHvQ1myffoNQa8MCPTl9hWhED+z1VuP8hsCC13fXfCNqB 6tHnQ3VLnYVMBci2zb/VLVT03lkjkWBW+CEzs+NGeSvfTDD4PambacbdjeKTPMr8 9TeMOp2wzM+ofRQyiESBYgnM8RQQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=feete.org; h=message-id :date:from:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; s=feete.org; bh=vIms0b3 OyR6QB4G4vLyaXjWH/A8=; b=E/5H4EXzKfP+52cv4qQqBSgcxb6sS7XS7vd9glQ sZ8NWuz9h+vBllMim02tZL/tbRBiDGfxHqAejvihLGIbyAngflvX6LqhDPu4Xy3u mXCaQ3iMQiqViSkKw8982UeA84VN3ev8/VQRJY9YNkdd1Iwluo4hi9ArHBZw8CdC 5XqU= Date: Sat, 21 Jul 2012 20:34:48 +0100 From: Ian Foote User-Agent: Mozilla/5.0 (X11; Linux i686; rv:14.0) Gecko/20120714 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 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: 1342899297 news.xs4all.nl 6880 [2001:888:2000:d::a6]:59955 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25763 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