Newbie: Looking for code review on my first Python project.

Ben Finney ben+python at benfinney.id.au
Tue Jan 10 21:05:38 EST 2012


Chris Angelico <rosuav at gmail.com> writes:

> On Wed, Jan 11, 2012 at 10:44 AM, HoneyMonster
> <someone at someplace.invalid> wrote:
> > Hi,
> >
> > I'm new to Python and recently completed my first project. I used
> > wxPython with wxGlade to generate the GUI bits.The application seems to
> > work well, but I am entirely self-taught, so have undoubtedly committed a
> > number of howlers in terms of style, design, standards, best practice and
> > so forth.
>
> Welcome!
>
> Ian has already offered some excellent tips, so I'll not repeat him.
>
>
>     log = os.environ['HOME'] + "/log/bbc.log"
>     log = os.environ['HOMEPATH'] + "\\log\\bbc.log"
>
> Python on Windows will support / for paths

Even better, you don't need to worry about what separator to use::

    top_dir = os.environ['HOME']
    log_filepath = os.path.join(top_dir, "log", "bbc.log")

> I'd do this with a triple-quoted string:
>
> about = """Built by Walter Hurry using Python and wxPython,
> with wxGlade to generate the code for the GUI elements.
> Phil Lewis' get_iplayer does the real work.
>
> Version 1.05: January 10, 2012"""

Which you can get indented nicely in the source, and strip off the
indentation at run-time:

    import textwrap

    about = textwrap.dedent("""\
            Built by Walter Hurry using Python and wxPython,
            with wxGlade to generate the code for the GUI elements.
            Phil Lewis' get_iplayer does the real work.

            Version 1.05: January 10, 2012
            """)

>         self.add = self.file.AppendItem(wx.MenuItem(self.file,
> wx.NewId(), "&Add to Queue", "Add a programme to the queue (for
> download later)", wx.ITEM_NORMAL))

Which is a whole lot more readable using the recommendations in PEP 8::

    self.add = self.file.AppendItem(
            wx.MenuItem(
                self.file, wx.NewId(), "&Add to Queue",
                "Add a programme to the queue (for download later)",
                wx.ITEM_NORMAL))

-- 
 \       “I distrust those people who know so well what God wants them |
  `\    to do to their fellows, because it always coincides with their |
_o__)                      own desires.” —Susan Brownell Anthony, 1896 |
Ben Finney



More information about the Python-list mailing list