views:

919

answers:

14

I am teaching a graduate level Python class at the University of Paris, and the students need to be introduced to the standard library. I want to discuss with them about some of the most important standard modules.

What modules do you think are absolute musts? Even though responses probably vary depending on your field (web programming, science, etc.), I feel that some modules are commonly needed: math, sys, re, os, os.path, logging,… and maybe: collections, struct,…

What modules would you suggest I present, in a 1 or 2 hour slot?

+14  A: 

I'd offer itertools and functools. These modules operate over abstractions that are found everywhere in programming, so I think they are useful to study. Among more practical things, xml modules (xml.dom, xml.sax) can be very useful.

Rorick
+1 itertools. For XML, `xml.etree.ElementTree` is *really* friendly.
kaizer.se
yup, +1 for `itertools` and `functools`
kRON
+1 for itertools, functools is great if you are using newish python. It's good functional style that smart students should know.
Salim Fadhley
+11  A: 

Have a look at PyMOTW (Python Module Of The Week). Although it is not strictly stdlib, it's a great resource of obvious and not so obvious gems of the python stdlib. What's more, it also serves as excellent documentation of the introduced modules.

piquadrat
Yeah, I love PyMOTW! I had not thought of checking it, though: thanks!
EOL
+1  A: 

Aside from those you mentioned, I found subprocess and sqlite3 modules particularly useful. But I would certainly advice to students to take a look at the list of standard library modules themselves. Also, from modules outside of standard library, I would mention numpy (or numarray) and pyparsing.

J S
Yeah, they're science students. Part of the class is devoted to Numpy/Scipy/Matplotlib. I'll check pyparsing, thanks!
EOL
+1  A: 

I'd place some weight on the decimal module. If they are beginners at programming, they certainly won't be aware of the implications of floating point accuracy. The decimal module is extremely valuable if working with currency or other units that must retain exact decimal precision through several mathematic operations.

Of course, you'd probably want to touch on situations when you don't need to be that accurate as well.

Mark Rushakoff
+4  A: 

I would add urllib2 to the list.

Bernd
+1  A: 

Definitely add BeautifulSoup. One of the best (if not the best) HTML parser.

Edit:

Oops, this isn't a "standard" module per se, but it should be if you do HTML parsing.

tgray
`per se` is how this Latin term is spelled
Paul McGuire
When using foreign phrases such as *per se* in English, they should be *italicized* and not `codified`. http://en.wiktionary.org/wiki/per_se#Usage_notes
Mark Rushakoff
I fixed the spelling. Learn something new every day!
tgray
+4  A: 

In only a one-two hour slot, I would introduce easy_install and the PyPI repository: even if they are not in the standard lib, they enable you to install many other external modules, and it is the first place where to look when you can't find in the standard lib.

Apart from that, I would introduce numpy, re, doctest/unittest, and maybe pickle.

dalloliogm
+1 for pickle -> comes in handy all the time
Jiaaro
+2  A: 

Don't forget about datetime, weakref, pickle, StringIO, heapq, may be threading.

And numpy also worths mentioning, although it is not from the standard library.

abbot
+1 for heapq... it's a super-fun class which most folks will never discover by themselves. It's of great importance for algorithms.
Salim Fadhley
+2  A: 

operator, next to what's already mentioned.

kRON
+8  A: 

Modules to cover in a 1-2 hour slot entirely depend on your audience's interest or focus. What other classes are they taking? What are they prepared to make use of immediately?

Be sure to mention math, decimal and datetime and time and re.

For IT-types who will be doing file-oriented work: glob, fnmatch, os, os.path, tempfile, and shutil.

Database folks must hear about sqlite and json.

Simulation audience may want to hear about random.

Web developers must hear about urllib2 from a client point of view. Also Beautiful Soup and an XML parser of your choice.

Web developers must hear about logging and wsgiref from a server point of view.

S.Lott
+1 for being the first to add shutil!
EOL
Beautiful Soup is a bit broken of late. You may want to recommend lxml instead. It handles HTML as well as XML. Ian Bicking has a good article on this. http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-scraping-library/
hughdbrown
+3  A: 

It depends a little on what they will be doing and what level they are. Some modules I wish someone pointed out to me when I started are:

  • StringIO - to stop them from reimplementing it, which they will if they don't discover it.
  • logging - to put them on the right path when it comes to debug printouts
  • pickle - to stop them from trying to use XML everywhere.
  • xml.etree.ElementTree - To save them from the DOM model when they actually need to work with XML.
  • pprint - to make nested structures in python less intimidating.
Mattias Nilsson
+1 for adding pprint to the modules already mentioned!
EOL
+4  A: 

I'd go for a few modules which make the most sense to a typical computer user/programmer performing typical computer tasks. That way, there's the largest chance that they might actually use python on their own time.

In my opinion, the operations most people will likely perform are file operations, for example going over every file in a directory and performing some action on it.

Therefore, I'd say the modules: os and os.path are probably the most important, and also mention glob, fnmatch and shutil. Also, subprocess might be very useful too, since it tends to get used in the above mentioned context.

Lastly, I'd go with optparse, since that will get them into very quickly making usable, programmer-friendly programs, which hopefully will also encourage them to actually write programs that other people want to use.

Edan Maor
+1 for mentioning optparse for the first time in this thread!
EOL
+2  A: 

os and os.path: because those are the core modules which anyone will require to write platform independent code in python and students can switch from shell script to python script after learning os and os.path.

Vijayendra Bapte
+1  A: 

I think everyone here got all the important ones, except for sys. If you look at actual Python code, sys is probably one of the most commonly used modules (usually because of sys.version).

Also, it's not really a module, but I would mention __future__.

And nobody should use Python without doing import this.

asmeurer
+1 for `__future__`! I *did* mention `sys` in the original post. :)
EOL