Re: My first ever Python program, comments welcome

Discussion in 'Python' started by MRAB, Jul 21, 2012.

  1. MRAB

    MRAB Guest

    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='')
    >
     
    MRAB, Jul 21, 2012
    #1
    1. Advertising

  2. On Sat, 21 Jul 2012 20:40:46 +0100, MRAB wrote:

    > On 21/07/2012 20:08, Lipska the Kat wrote:
    >> l=sorted(l, key=itemgetter(0))

    >
    > Short is:
    >
    > l.sort(key=itemgetter(0))


    Shorter, and the semantics are subtly different.

    The sorted function returns a copy of the input list.

    The list.sort method sorts the list in place.



    --
    Steven
     
    Steven D'Aprano, Jul 22, 2012
    #2
    1. Advertising

  3. MRAB

    MRAB Guest

    On 22/07/2012 01:32, Steven D'Aprano wrote:
    > On Sat, 21 Jul 2012 20:40:46 +0100, MRAB wrote:
    >
    >> On 21/07/2012 20:08, Lipska the Kat wrote:
    >>> l=sorted(l, key=itemgetter(0))

    >>
    >> Short is:
    >>
    >> l.sort(key=itemgetter(0))

    >
    > Shorter, and the semantics are subtly different.
    >
    > The sorted function returns a copy of the input list.
    >
    > The list.sort method sorts the list in place.
    >

    Since the result is bound to the original name, the
    result is the same.
     
    MRAB, Jul 22, 2012
    #3
  4. On Sun, Jul 22, 2012 at 11:56 AM, MRAB <> wrote:
    > Since the result is bound to the original name, the
    > result is the same.


    Yes, assuming there are no other refs.

    >>> a=[3,2,1]
    >>> b=a
    >>> a=sorted(a)
    >>> a

    [1, 2, 3]
    >>> b

    [3, 2, 1]

    ChrisA
     
    Chris Angelico, Jul 22, 2012
    #4
  5. MRAB

    Dave Angel Guest

    On 07/21/2012 09:56 PM, MRAB wrote:
    > On 22/07/2012 01:32, Steven D'Aprano wrote:
    >> On Sat, 21 Jul 2012 20:40:46 +0100, MRAB wrote:
    >>
    >>> On 21/07/2012 20:08, Lipska the Kat wrote:
    >>>> l=sorted(l, key=itemgetter(0))
    >>>
    >>> Short is:
    >>>
    >>> l.sort(key=itemgetter(0))

    >>
    >> Shorter, and the semantics are subtly different.
    >>
    >> The sorted function returns a copy of the input list.
    >>
    >> The list.sort method sorts the list in place.
    >>

    > Since the result is bound to the original name, the
    > result is the same.
    >


    In this particular program, yes. But if there's another variable bound
    to the same list, then the fact that there's a new object from sorted()
    makes a difference.



    --

    DaveA
     
    Dave Angel, Jul 22, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Scott
    Replies:
    4
    Views:
    262
    Paul Scott
    Apr 8, 2008
  2. Ian Foote
    Replies:
    0
    Views:
    162
    Ian Foote
    Jul 21, 2012
  3. Dave Angel
    Replies:
    8
    Views:
    268
  4. Peter Otten
    Replies:
    0
    Views:
    168
    Peter Otten
    Jul 22, 2012
  5. Ivan@work
    Replies:
    0
    Views:
    166
    Ivan@work
    Jul 23, 2012
Loading...

Share This Page