O'Reilly の Python Cookbook (第 2 版) のレシピを実装しようとしていますが、レシピのごく一部が正常に機能しません。誰かが理由を理解するのを手伝ってくれることを望んでいました.
レシピは「5.14:辞書型をレーティング機能で強化」です。ほとんどのコードは機能していますが、キーの「ランキング」を変更しようとするとエラーが発生します。私はすべてのコメントを著者のものとして残しておきました. 特に、何らかの形でエラーを引き起こしていると思われる「_rating」の側面。
エラー:
File "rating.py", line 119, in <module>
r["john"]=20
File "rating.py", line 40, in __setitem__
del self._rating[self.rating(k)] ##
AttributeError: 'Ratings' object has no attribute 'rating'
コード:
class Ratings(UserDict.DictMixin, dict):
'''The implementation carefully mixes inheritance and delegation
to achieve reasonable performance while minimizing boilerplate,
and, of course, to ensure semantic correctness as above. All
mappings' methods not implemented below get inherited, mostly
from DictMixin, but, crucially!, __getitem__ from dict. '''
def __init__(self, *args, **kwds):
''' This class gets instantiated just like 'dict' '''
dict.__init__(self, *args, **kwds)
# self._rating is the crucial auxiliary data structure: a list
# of all (value, key) pairs, kept in "natural"ly-sorted order
self._rating = [ (v, k) for k, v in dict.iteritems(self) ]
self._rating.sort()
def copy(self):
''' Provide an identical but independent copy '''
return Ratings(self)
def __setitem__(self, k, v):
''' besides delegating to dict, we maintain self._rating '''
if k in self:
del self._rating[self.rating(k)] ##
dict.__setitem__(self, k, v)
insort_left(self._rating, (v, k))
def __delitem__(self, k):
''' besides delegating to dict, we maintain self._rating '''
del self._rating[self.rating(k)]
dict.__delitem__(self, k)
''' delegate some methods to dict explicitly to avoid getting
DictMixin's slower (though correct) implementations instead '''
__len__ = dict.__len__
__contains__ = dict.__contains__
has_key = __contains__
''' the key semantic connection between self._rating and the order
of self.keys( ) -- DictMixin gives us all other methods 'for
free', although we could implement them directly for slightly
better performance. '''
def __iter__(self):
for v, k in self._rating:
yield k
iterkeys = __iter__
def keys(self):
return list(self)
#the three ratings-related methods
def rating(self, key):
item = self[key], key
i = bisect_left(self._rating, item)
if item == self._rating[i]:
return i
raise LookUpError, "item not found in rating"
def getValueByRating(self, rating):
return self._rating[rating][0]
def getKeyByRating(self, rating):
return self._rating[rating][1]
def _test( ):
''' we use doctest to test this module, which must be named
rating.py, by validating all the examples in docstrings. '''
import doctest, rating
doctest.testmod(rating)
if __name__ == "__main__":
r = Ratings({"bob":30, "john":30})
print "r is"
print r
print "\n"
print "len(r) is"
print len(r)
print "\n"
print "updating with {'paul': 20, 'tom': 10} "
r.update({"paul": 20, "tom": 10})
print "\n"
print "now r is"
print r
print "\n"
print "r.has_key('paul') is"
print r.has_key("paul")
print "\n"
print " 'paul' in r is"
print ("paul" in r)
print "\n"
print "r.has_key('alex') is"
print r.has_key("alex")
print "\n"
print " 'alex' in r is"
print ("alex" in r)
print '\n'
print 'r is'
print r
print "changing john to '20' with 'r['john']= 20' doesn't work. "
r["john"]=20