tags:

views:

114

answers:

4

I just found out the existence of the optparse module. I personally always used getopt, so I did not care to look for something better. It's clear, however, that optparse is much more advanced, so I would expect it to be the preferred way in the future to get options from the command line.

Anyway, this event struck me. I am now wondering if there are modules or functions out there I am using since the beginning of time, that have much better alternatives in the standard library. Is there such a compact and quick to browse list, on the liking of "previous solutions: getopt. better solution: optparse (since python 2.x)" ?

Edit marked as CW as agreed.

  • parsing command line options: getopt, optparse, argparse
  • package management: distutils, setuptools
+2  A: 

I suggest this might be a good place to start such a list

note that there is pep389 to replace optparse with argparse

collections.defaultdict works nicer in most places you would use dict.setdefault

the collections module is a good one to become familiar with as it has lots of new stuff in Python3

Generator expressions are often better than list comprehensions if you don't need to keep the list

Ternary operator b if a else c instead of a and b or c with all it's problems

multiprocessing replaces any other way you were doing it ;)

itertools.izip_longest avoids having to use workarounds when you are zipping uneven things

gnibbler
ok, let's say that I wait for some time if someone comes out with a magic list from his bookmarks. If not, I rework the question and make it CW so we can write one
Stefano Borini
Sure. i think CW is a good idea. Are you looking for stuff that's new in Python2.6/7 or Python3?
gnibbler
A: 

I wouldn't absolutely agree with a statement "optparse is better than getopt". If optparse suites you better, it doesn't mean someone wouldn't find getopt much preferable. They are intended for different purposes: getopt is much simpler and requires less understanding to start using (especially if you are familiar with getopt from other sources: e.g. shell scripting), optparse is more powerful and is more detailed. However, if I need to just get one or two command lime parameters, I might even use simple if statement.

To summarize, every tool has its purpose, and every situation might require a different tool which is more suitable for that situation.

artdanil
in my post, better == has more features.
Stefano Borini
@Stefano Borini, I got it. Thank you for explanation.
artdanil
+1  A: 

Not exactly compact, and referring only to the standard library (and other parts of standard Python) but not any third-party packages, there are all the "What's New in Python X.X?" essays.

Other than that, and Google, I don't think there are any such lists except in random blogs and so forth.

Peter Hansen
the main problem with google is that you have to be aware of the existence of something before you can search for it. I would never have looked for alternatives to getopt, because I assumed there was nothing else in the standard library to do the same task.
Stefano Borini
I think this is more like a summary of obsolete idioms
gnibbler
@Stefano Borini: "you have to be aware of the existence of something before you can search for it" That's the best excuse I've heard in a long time. I'm going to use that one until my skills become so obsolete I can no longer hold down a paying job in software. A truly brilliant excuse for not keeping up with news, PEP's, library changes, etc.
S.Lott
Lott: so, what do you think I'm doing here? chatting, or keeping my skills updated ? and by the way, I'm also supposed to be a quantum chemist. so excuse me if I don't read the PEPs while I'm busy writing papers.
Stefano Borini
A: 

I use Richard Gruet's Python Quick Reference which is a great reference for all things Python including some of the more important parts of the standard library. It does a good job of making changes in the language and library prevalent using colour coding and notes.

Take a look at his section on getopt, for instance, and the list of modules and packages in base distribution.

It's not been updated for Python 3 yet, but I live in hope!

sj26