[Python-talk] PySIG report, 27 Jan 2011
    Lloyd Kvam 
    python at venix.com
       
    Sat Jan 29 11:03:06 EST 2011
    
    
  
On Fri, 2011-01-28 at 20:54 -0500, Bill Sconce wrote:
> One of our membership is introducing himself to Python by discovering
> for himself how to use it to clean up a name-and-address database. 
An all too common sort of problem.  The Python re (regular expression)
module provides the sub method for making string substitutions.
        http://docs.python.org/library/re.html#re.sub
The key thing to note is that the repl parameter does not have to be a
string.  It can be a function!
I wrote a Converter class that takes in a dictionary of string
conversions.  The "old" strings are assumed to be words.  This allows
you to have  a dictionary like dict(
        i = 'I',
        in = 'Out',
        )
where some of the strings are contained in others.  I needed that for
converting field names.  The class combines all of the "old" strings
into a regular expression pattern and constructs the repl function to be
used by the pattern's sub method.
Then the call to pattern.sub is packaged using functools.partial and
named _cvt.  A separate method, convert, documents the remaining free
parameters for the sub call.  I think it makes things less mysterious.
Finally convert is assigned to __call__ so that Converter objects can be
called directly.
The last time I used this was to help my daughter with late changes for
the Ethics Bowl held last Fall at Dartmouth.  
        http://www.dartmouth.edu/~ethics/nereb/index.html
The scheduling program uses place holders for rooms and team names.  She
needed to convert the generated schedule (ethics-bowl.txt) into
something meaningful for the participants.
        <ethics-bowl.txt python fix-ethics-matches.py
did the trick.
As you can tell from the length of this email, I was pretty tickled when
I realized how nicely this kind of logic could be packaged.
-- 
Lloyd Kvam
Venix Corp
DLSLUG/GNHLUG library
http://dlslug.org/library.html
http://www.librarything.com/catalog/dlslug
http://www.librarything.com/catalog/dlslug&sort=stamp
http://www.librarything.com/rss/recent/dlslug
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fix-ethics-matches.py
Type: text/x-python
Size: 1310 bytes
Desc: not available
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20110129/6810d07f/attachment.py 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: converter.py
Type: text/x-python
Size: 1298 bytes
Desc: not available
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20110129/6810d07f/attachment-0001.py 
-------------- next part --------------
  Panel      Round One     Round Two      Round Three
-------     -------     -------      -------
    A        2 vs 6      3 vs 16      9 vs 19
    B        3 vs 17     8 vs 11      10 vs 20
    C        4 vs 9     10 vs 18      11 vs 14
    D        5 vs 10     9 vs 17      2 vs 12
    E        7 vs 11     12 vs 13     1 vs 6
    F        12 vs 15    2 vs 14      3 vs 13
    G        14 vs 16    4 vs 19      5 vs 18
    H        18 vs 19    5 vs 20      7 vs 17
    I        8 vs 20     1 vs 7       15 vs 16
    J        1 vs 13     6 vs 15      4 vs 8
Here are the new instructions for each team:
Team #          Round One          Round Two         Round Three
              Panel Opponent     Panel Opponent    Panel Opponent
------       -----  ------      -----  ------     -----  ------
   1            J--13          I--7         E--6
   2            A--6          F--14         D--12
   3            B--17          A--16         F--13
   4            C--9          G--19         J--8
   5            D--10          H--20         G--18
   6            A--2          J--15         E--1
   7            E--11          I--1         H--17
   8            I--20          B--11         J--4
   9            C--4          D--17         A--19
  10            D--5          C--18         B--20
  11            E--7          B--8         C--14
  12            F--15          E--13         D--2
  13            J--1          E--12         F--3
  14            G--16          F--2         C--11
  15            F--12          J--6         I--16
  16            G--14          A--3         I--15
  17            B--3          D--9         H--7
  18            H--19          C--10         G--5
  19            H--18          G--4         A--9
  20            I--8          H--5         B--10
    
    
More information about the gnhlug-discuss
mailing list