general coding issues - coding style...

Discussion in 'Python' started by calmar, Feb 18, 2006.

  1. calmar

    calmar Guest

    Hi all,

    since I'm just a 'handicraft'/beginner or so,

    could anybody provide me with some (rough) hints, about how to enhance the code

    Cheers and thanks a lot


    (o_ It rocks: LINUX + Command-Line-Interface
    calmar, Feb 18, 2006
    1. Advertisements

  2. - why are these {{{ thingies there?

    - use string interpolation like "Foo %s %i" % ("bar", 1) instead of
    concatenating strings.

    - it seems that you could benefit from a class instead of a bunch of
    functions & few globals. No need to go too crazy about OO, but it has
    its merits

    - try using something like glade - creating GUIs by hand sucks
    big-timer :)

    - read up about unicode and encodings, what they mean and why and when
    to use what. Really. Most problems in that field stem from people being
    sort of ignorant on that topic and just wiggling themselves through all
    the time - in the end, messing up stuff. It really _isn't_ that complicated.

    - when creating string-keyed dicts, the idiom


    has its merits.

    Besides that - looks ok to me.

    Diez B. Roggisch, Feb 18, 2006
    1. Advertisements

  3. calmar

    calmar Guest

    Hi Diez,
    markers for folding for vim
    I see, get's shorter and so, and I can break lines.
    (seems to me, after ( or [ or so, I can use a new line without
    to worry)
    I see. Maybe I could then build some classes for that prog,
    especially since I use only one file, it probably would make
    some sense for getting a better structure.
    I should (seriously) check out probably.
    I read up. In fact, basically it does not seem to be that complicate.
    Unicode just a 'numbered' list of all available character, and
    e.g. uft-8 a organized way to 'store' those 'numbers' wisely
    into bytes.

    I still have some problems with that (and gave up), but since I begin to
    now understand it somebit more, I should try/check again.
    I see, I changed too.

    Thanks a lot,


    (o_ It rocks: LINUX + Command-Line-Interface
    calmar, Feb 18, 2006
  4. calmar

    plahey Guest


    1585 if sys.path[0][-12:] == "\": #for py2exe

    how about

    if sys.path[0].endswith( "\\" ):

    (did you really mean one back-slash there?)

    499 tuple = os.path.split(filename)

    bad variable name... tuple(x) converts a sequence to a tuple.

    You have a number of places where you check for len(x)==0:

    674 if len(files) == 0:
    853 if len(file_show) == 0:
    950 if len(imgprocess["files_todo"]) == 0:

    people usually recommend:

    if not files:
    if not file_show:
    if not imgprocess["files_todo"]:

    you should run your code through pychecker (it had a lot to say...).

    You use global alot... that should be a red flag. Like the poster
    above mentioned, you have things that are telling you they want to
    be objects.

    The #{{{ and #}}} stuff is very annoying to other programmers
    (ok, to me...). You might want to investigate a Python aware
    editor (like SPE, which has pychecker built in). This is
    coming from someone who uses vim regularly.
    plahey, Feb 18, 2006
  5. I would look into one of the many Vim scripts which automatically fold
    most large blocks without the ugly {{{.
    Dylan Moreland, Feb 18, 2006
  6. calmar

    Justin Azoff Guest

    Who needs a script?
    "set foldmethod=indent"
    works pretty well for most python programs.
    Justin Azoff, Feb 18, 2006
  7. calmar

    calmar Guest

    Hi all,
    Well, foldmethod=marker does not bother me, because the folds are
    normally closed. With markers, it takes one line per function, with
    indent I see 2, so I prefer markers.

    ...and since I can easily get rid of them, and add them again, I will at
    least remove them before e.g. putting to the web or so.

    Cheers and thanks,


    (o_ It rocks: LINUX + Command-Line-Interface
    calmar, Feb 19, 2006
  8. calmar

    calmar Guest

    cool, thx,
    (yeah, one backslash)
    I see, I changed that to
    path, filen = os.path.split(filename)
    I see. thx
    I see, cool tool that pychecker!
    I can't do something against the 'not used variable' so probably?
    (since pygtk just sends those items anyway)
    I will try to get some order (classes) and maybe remove them.

    thanks a lot!!



    (o_ It rocks: LINUX + Command-Line-Interface
    calmar, Feb 19, 2006
  9. calmar a écrit :
    1/ learn OO and get rid of globals.
    2/ use dict or list based dispatch instead of long if/elif/elif... clauses
    3/ stdout is meant for *normal* program outputs. Errors and verbosity go
    to stderr
    4/ triple quoted strings are fine for multiline text
    5/ os.path is fine for portable filepath operations
    6/ things that dont change during program execution (ie : constants)
    should not be defined inside a function
    Bruno Desthuilliers, Feb 19, 2006
  10. calmar

    calmar Guest

    Well I created two classes now. I put some things global (your point 6).
    e.g. if it's on Windows or not, and other things.
    when I find out what you mean, I will. Probably something lika a 'case'
    thing? #python meant a list containing functions or so. So that the
    values represent the functions to call, isn 'it?
    yeah, changed (somebit)
    yeah. I have lot of triple prints...
    I don't really understant what you mean here, sorry
    As I mentioned above, these I placed globally:

    main gtkwindows,
    smwin or not,
    pyexe or not,
    preferred encoging

    according to your statement?

    Anyway, since I did lot of changes, I'm myself confused actually.

    Will try to cleanup and implement even further all good
    advices from all in some days.

    Thanks a lot!!


    (o_ It rocks: LINUX + Command-Line-Interface
    calmar, Feb 20, 2006
  11. calmar

    JW Guest

    About this line:
    1585 if sys.path[0][-12:] == "\": #for py2exe

    pl... suggested:
    if sys.path[0].endswith( "\\" ):

    and said, "did you really mean one back-slash there?". You responded
    "yeah, one backslash", but I still don't believe you. In this case, it
    happens to work, but you should be aware that the back-slash is an
    escape character, which causes the next character to be interpreted
    differently. Try this in your interpreter:

    print "\a" # System bell - might cause your speaker to beep
    print "\t" # Tab character
    print "\n" # Newline character / sequence

    See for more details
    on the escape sequences that Python recognizes. Here's a summary: if
    the backslash + character is a special escape code, then replace it
    with that, otherwise assume the programmer meant a real backslash.
    That's dangerous, and will break when the name changes from one that
    starts with an L to one that starts with an A, B, F, N, etc. The safe
    way it to tell Python "Yes, I really want a backslash", which is
    indicated with the double backslash:

    print "\\"

    If you don't use the double backslash, you'll eventually have a
    problem, especially in Windows, which unfortunately uses the backslash
    as a directory seperator. You might also want to look at os.sep and
    the os.path.* functions, if you are interested in making your code work
    on different platforms.

    JW, Feb 20, 2006
  12. calmar

    calmar Guest

    Hi JW,
    Yeah, I have two backslashes, but thaks for pointing out.
    Yeah, I will use the os.sep variable, that's a good idea

    thanks a lot,


    (o_ It rocks: LINUX + Command-Line-Interface
    calmar, Feb 21, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.