For the module in the standard library, we briefly covered the new print() function in Python 2.6:
from __future__ import print_function # Python 2.6and then the new .format() method on strings, as an alternative to the C style sprintf("%s %d", a, b) approach. The format() approach is more powerful than the % operator but takes some getting used to.
print(objects..., sep='', end='\n', file=sys.stdout)
Feedback from some attendees is that it is too complicated, so we also covered the much simpler but less powerful Template module in the standard library.
>>> from string import Template
>>> s = Template("$who likes $what")
>>> s.substitute(who='tim', what='cake')
tim likes cake
>>> s.substitute(who='tim') # error
tim likes $what
For the programming concept, we studied sequence slicing and subscripting.
While most of us have a good grasp on simple subscripting and from:to slicing, there are some deeper aspects here, a few of which can be seen in:
a[4:5:5] # extended slicing, with a strideWe also looked at the deceptively simple shallow copy operation:
a[3, 4, 5] # a slice list
a[2:4, 5:7] # list of slices
a['a':'c'] # applying slicing to non-integers and non-ordinal content
a[:]and how it differs between mutable (list) and immutable (tuple) types.
And then we looked at the little-known builtin type 'Ellipsis' and how it can be used in your own programs.
a[...]In order to experiment with the various subscript/slicing syntaxes, we defined our own __getitem__, __setitem__ and __delitem__ methods to print out what was received:
a[..., 0] -> mapobj[:,:,:,0] # PyNumeric interpretation
>>> class alpha:
... def __getitem__(self, key):
... print "Key is %s" % repr(key)
... return None
>>> a = alpha()
>>> a[3, 4. 5]
Next we took a little trip over to look at the issues and concerns involved in the simple idea of sorting items in a collection:
We studied the difference between the .sort() method and the sorted() builtin function, and the three arguments (cmpfn, key, reverse) you can pass to either one.
This also brought us into the operator module which has several useful functions for extracting values on which to sort from items, such as:
key=operator.itemgetter(1)and we talked about (but didn't dig into) the well-known design pattern of "decorate-sort-undecorate".
For the questions of "how would you do this?" we discussed:
- How can you test for whether a variable is defined?
- How do you tell if a method is bound or unbound?
- How do you 'unbind' a method?
- How do you set an attribute whose name is not a legal Python name?
- How can you conditionally define methods in a class?
- How do you change the data underneath a mutable sequence without breaking any existing bindings to the sequence itself? Hint: Look in site.py.
For the source code walkthrough we examined the implementation of the string Template class in lib/python/string.py. The interesting part was its use of a metaclass to post-process the extraction strings defined in the class into compiled regular expression objects.
And by special request by a member, we looked into how Python at startup arranges its module import path (sys.path) to find zip/eggs and parses 'path configuration files' (.pth) that alter that path. We also covered the privileged status of 'site' directories over other directories on the path and did a very quick walkthrough of the source in lib/python/site.py as well as the lib/python/site-packages/site.py installed by setuptools.
That filled our 3-hour session and we wrapped up with chatter about the rapidly approaching PyCon and the early registration deadline.