A newbie dictionary question

Eric Jacobs x at x.x
Sat Nov 6 12:25:35 EST 1999


Jan Vroonhof wrote:
> 
> I have the distinct feeling this is FAQ, but if so I missed it. It is
> late (or better early) here.
> I am trying to teach  myself python with a little problem I have that
> involves parsing ChangeLog files.
> 
> Thus I wrote a module that creates a dictionary-like class with the
> text of the ChangeLog entry as the key. Later I want to use this to
> check whether specific changes were applied. However I want to do the
> comparisons regardless of whitespace. Thus I a made small class that
> has __cpm__ and __hash__ functions that ignore whitespace.
> 
> However looking up objects in the hashes doesn't seem to work. Can
> somebody tell me what obvious thing I am missing?
> 
> The code is below. Note that I added the complete module (which is 20
> lines or so more than a trivial example) in the hope that somebody
> comment on it stylewise as this is my first Python program.
> In particular I have the feeling the while loops in the scan(self)
> function could be done much clearer
> 
> #!/usr/bin/env python
> """This class implements a dictonary like class,
> ChangeLog, that represents entries from ChangeLog file"""
> import re
> import sys
> import string
> from UserDict import UserDict
> 
> authpat = re.compile(
>     r"^(\d\d\d\d-\d\d-\d\d|\w\w\w \w\w\w\s+\d+\s+\d\d:\d\d:\d\d \d\d\d\d)"
>     +r"\s+([^<]+)\s+<(.+)>\s?$")
> emptypat = re.compile(r"^\s*$")
> startpat = re.compile("^(?:\t| {4})\\s*\\* (.*)")
> contpat =  re.compile("^\\s*(?:\t| {4})\\s*(\\S.+)")
> 
> _debug = 0
> 
> class ChangeAuthor:
>     def __init__(self,name,address):
>         self.name = name
>         self.address = address
> 
>     def __str__(self):
>         return self.name + "<" + self.address + ">"
> 
> class ChangeEntry:
>     def __init__(self,value):
>         self.value = value
> 
>     def __str__(self):
>         self.value

Unrelated, but did you mean "return self.value"? It's
legal to throw away values in Python.

> 
>     # bad approximation?
>     def nospaces(self):
>         return string.join(string.split(self.value))
> 
>     def __cmp__(self,other):
>         return self.nospaces() == other.nospaces()

The __cmp__ is returning the wrong thing. It should return
-1 for <, 0 for ==, or 1 for >. You probably want

      def __cmp__(self, other):
           return cmp(self.nospaaces(), other.nospaces())

The way it's written above, objects that are supposed to
be equal will appear one greater than the other, and this
will break it. Dictionaries require their key objects to
have consistent __cmp__ and __hash__ functions.




More information about the Python-list mailing list