Re: My first ever Python program, comments welcome

Discussion in 'Python' started by Dave Angel, Jul 21, 2012.

  1. Dave Angel

    Dave Angel Guest

    On 07/21/2012 03:08 PM, 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
    >


    You already have comments from Ian and MRAB, and I'll try to point out
    only things that they did not.

    Congratulations on getting your first program running. And when reading
    the following, remember that getting it right is more important than
    getting it pretty.

    > 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=[]


    I prefer to initialize an empty collection just before the loop that's
    going to fill it. Then if you later decide to generalize some other
    part of the code, it's less likely to break. So i'd move this line to
    right-before the for loop.

    > t = tuple


    Even if you were going to use this initialization later, it doesn't do
    what you think it does. It doesn't create a tuple, it just makes
    another reference to the class. If you had wanted an empty tuple, you
    should either do t=tuple(), or better t=()

    > filename=argv[1]
    > lineCount=10
    >


    I'd suggest getting into the habit of doing all your argv parsing in one
    place. So check for argv[2] here, rather than inside the loop below.
    Eventually you're going to have code complex enough to use an argument
    parsing library. And of course, something to tell your use what the
    arguments are supposed to be.

    > with fileinput.input(files=(filename)) as f:


    fileinput is much more general than you want for processing a single
    file. That may be deliberate, if you're picturing somebody using
    wildcards on their input. But if so, you should probably use a
    different name, something that indicates plural.

    > for line in f:
    > t=(line.split('\t'))
    > t[0]=int(t[0])
    > l.append(t)



    > l=sorted(l, key=itemgetter(0))
    >


    Your sample data has duplicate numbers. So you really ought to decide
    how you'd like such lines sorted in the output. Your present code
    simply preserves the present order of such lines. But if you remove the
    key parameter entirely, the default sort order will sort with t[0] as
    primary key, and t[1] as tie-breaker. That'd probably be what I'd do,
    after trying to clarify with the client what the desired sort order was.

    > 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='')
    >
    > Thanks
    >
    > Lipska
    >
    >


    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.

    --

    DaveA
     
    Dave Angel, Jul 21, 2012
    #1
    1. Advertising

  2. On Sat, 21 Jul 2012 16:10:51 -0400, Dave Angel wrote:

    >> with fileinput.input(files=(filename)) as f:

    >
    > fileinput is much more general than you want for processing a single
    > file. That may be deliberate, if you're picturing somebody using
    > wildcards on their input. But if so, you should probably use a
    > different name, something that indicates plural.


    Also, fileinput is more a convenience module than a serious production
    quality tool. It works, it does the job, but it can be slow. From the
    source:

    Performance: this module is unfortunately one of the slower ways of
    processing large numbers of input lines.


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

  3. Dave Angel

    rusi Guest

    On Jul 22, 1:10 am, 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.


    Question in a similar vein: What development environment do you use?
    My impression is that the majority of pythonistas use a non-ide editor
    like vi or emacs
    Ive been using emacs for 20 years and python-mode of emacs is very
    useful but I am increasingly concerned that emacs is refusing to move
    with the times.

    Which is why I am particularly curious how an ol Java-head finds
    eclipse+python (http://pydev.org/ )
     
    rusi, Jul 22, 2012
    #3
  4. On 22/07/2012 03:55, rusi wrote:
    > On Jul 22, 1:10 am, 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.

    >
    > Question in a similar vein: What development environment do you use?
    > My impression is that the majority of pythonistas use a non-ide editor
    > like vi or emacs
    > Ive been using emacs for 20 years and python-mode of emacs is very
    > useful but I am increasingly concerned that emacs is refusing to move
    > with the times.
    >
    > Which is why I am particularly curious how an ol Java-head finds
    > eclipse+python (http://pydev.org/ )
    >


    Wouldn't describe myself as "an ol Java-head" but I disliked eclipse 10
    years ago. I tried it again earlier this year and still disliked it.
    It's like entering a legless cart horse for the Derby or the Grand National.

    YMMV.

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Jul 22, 2012
    #4
  5. Dave Angel

    Andrew Berg Guest

    On 7/22/2012 3:37 AM, Lipska the Kat wrote:
    > 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.

    There are Mercurial (aka Hg) and Bazaar as well for DVCS. AFAIK, git,
    Mercurial, and Bazaar are all fine choices and the one to use will
    mainly boil down to personal preference. I prefer Mercurial myself.
    --
    CPython 3.3.0b1 | Windows NT 6.1.7601.17803
     
    Andrew Berg, Jul 22, 2012
    #5
  6. On Sun, Jul 22, 2012 at 6:49 PM, Andrew Berg <> wrote:
    > On 7/22/2012 3:37 AM, Lipska the Kat wrote:
    >> 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.

    > There are Mercurial (aka Hg) and Bazaar as well for DVCS. AFAIK, git,
    > Mercurial, and Bazaar are all fine choices and the one to use will
    > mainly boil down to personal preference. I prefer Mercurial myself.


    Agreed. I poked around with Bazaar a bit this year, and it seems to
    lack some features. But certainly hg and git are both excellent
    choices, with bzr not significantly far behind. I prefer git,
    personally; on Windows, though, I would recommend hg.

    Probably the best feature of any of them (one which, I believe, is now
    standard in all three) is 'bisect' with a command. It's "git bisect
    run", or "hg bisect -c", or "bzr bisect run". You can search back
    through a huge time period without any human interaction. I did that a
    while ago with a docs-building problem; the current state wouldn't
    successfully generate its docs from a fresh start, even though it
    could update them from a previous state. It took 45 minutes (!) of
    chuggity-chug compilation to find the actual cause of the problem, and
    no effort from me (since "make doc" already gave the right exit
    codes). Use source control now; you'll reap the benefits later!

    ChrisA
     
    Chris Angelico, Jul 22, 2012
    #6
  7. Dave Angel

    David Guest

    On 22/07/2012, Lipska the Kat <> 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
     
    David, Jul 22, 2012
    #7
  8. Dave Angel

    rusi Guest

    On Jul 22, 2:20 pm, Lipska the Kat <> wrote:

    > Well I have to say that I've used Eclipse with the myEclipse plugin for
    > a number of years now and although it has it's moments it has earned me
    > LOADS of MONEY so I can't really criticise it.


    Ive probably tried to use eclipse about 4 times in the last 8 years.
    Always run away in terror.
    Still I'm never sure whether eclipse is stupid or I am...

    First time I'm hearing of myEclipse. Thanks. What does it have/do that
    standard eclipse (JDT?) does not?


    > Python and eclipse ... Noooooooooooooooooooooooooooooooooooooooooo ;-)



    Very curious about this. You made 'Loads of money' with eclipse but
    want to stay away from it?
    Simply cannot make out this thing called 'java-programmer-culture'...
     
    rusi, Jul 22, 2012
    #8
  9. Dave Angel

    rusi Guest

    On Jul 22, 10:23 pm, Lipska the Kat <> wrote:

    > Heh heh, Nothing to do with Eclipse, just another thing to get my head
    > around. For work and Java IMHO you can't beat eclipse...
    > at the moment I'm getting my head around git,


    Bumped into this yesterday. Seems like a good aid to git-comprehension
    https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

    > reminding myself of C, learning Python
    > and re-learning make. Enough already; but if there's a python plugin I
    > guess I'll get around to it eventually


    Seems like a strange combo. It should be
    (C&make)|(python&X)|(Java&Ant)
    where X could range from
    Setup http://docs.python.org/distutils/setupscript.html
    to distribute http://guide.python-distribute.org/
    to scons http://www.scons.org/

    Why burden yourself by making the '|'s into '&'s?
     
    rusi, Jul 24, 2012
    #9
    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:
    264
    Paul Scott
    Apr 8, 2008
  2. Ian Foote
    Replies:
    0
    Views:
    163
    Ian Foote
    Jul 21, 2012
  3. MRAB
    Replies:
    4
    Views:
    193
    Dave Angel
    Jul 22, 2012
  4. Peter Otten
    Replies:
    0
    Views:
    170
    Peter Otten
    Jul 22, 2012
  5. Ivan@work
    Replies:
    0
    Views:
    168
    Ivan@work
    Jul 23, 2012
Loading...

Share This Page