Class Ratings
source code
Ratings is mostly like a dictionary, with extra features: the value
corresponding to each key is the 'score' for that key, and all keys are
ranked in terms their scores. Values must be comparable; keys, as well
as being hashable, must be comparable if any two keys may ever have the
same corresponding value (i.e., may be "tied" on score). All
mapping-like behavior is just as you would expect, such as:
>>> r = Ratings({"bob": 30, "john": 20})
>>> r.update({"paul": 20, "tom": 10})
>>> r.keys()
['tom', 'john', 'paul', 'bob']
>>> len(r)
4
>>> "paul" in r
True
>>> [r[key] for key in ["bob", "paul", "john", "tom"]]
[30, 20, 20, 10]
>>> r.get("nobody"), r.get("nobody", 0)
(None, 0)
In addition to the mapping interface, we offer rating-specific
methods. r.rating(key) returns the ranking of a 'key' in the ratings,
with a ranking of 0 meaning the lowest score (when two keys have equal
scores, the keys themselves are compared, to 'break the tie', and the
lesser key gets a lower ranking):
>>> [r.rating(key) for key in ["bob", "paul", "john", "tom"]]
[3, 2, 1, 0]
getValueByRating(ranking) and getKeyByRating(ranking) return the score
and key, respectively, for a given ranking index:
>>> [r.getValueByRating(rating) for rating in range(4)]
[10, 20, 20, 30]
>>> [r.getKeyByRating(rating) for rating in range(4)]
['tom', 'john', 'paul', 'bob']
An important feature is that the keys() method returns keys in
ascending order of ranking, and all other related methods return lists or
iterators fully consistent with this ordering:
>>> [key for key in r]
['tom', 'john', 'paul', 'bob']
>>> r.values()
[10, 20, 20, 30]
>>> r.items()
[('tom', 10), ('john', 20), ('paul', 20), ('bob', 30)]
An instance can be modified (adding, changing and deleting key-score
correspondences), and every method of that instance reflects the
instance's current state at all times:
>>> r["tom"] = 100
>>> r.items()
[('john', 20), ('paul', 20), ('bob', 30), ('tom', 100)]
new empty dictionary
|
|
a shallow copy of D
|
|
|
|
|
|
|
|
True if D has a key k, else False
|
|
True if D has a key k, else False
|
|
None
|
update(D,
E=...,
**F)
Update D from mapping/iterable E and F. |
|
|
an iterator over the keys of D
|
|
an iterator over the keys of D
|
|
list of D's keys
|
|
an iterator over the (key, value) items of D
|
|
list of D's (key, value) pairs, as 2-tuples
|
|
an iterator over the values of D
|
|
list of D's values
|
|
|
|
|
|
|
|
|
|
Inherited from dict :
__cmp__ ,
__eq__ ,
__ge__ ,
__getattribute__ ,
__getitem__ ,
__gt__ ,
__le__ ,
__lt__ ,
__ne__ ,
__new__ ,
__repr__ ,
__sizeof__ ,
clear ,
fromkeys ,
get ,
pop ,
popitem ,
setdefault ,
viewitems ,
viewkeys ,
viewvalues
Inherited from object :
__delattr__ ,
__format__ ,
__reduce__ ,
__reduce_ex__ ,
__setattr__ ,
__str__
|
Inherited from _abcoll.Sized :
__subclasshook__
|
|
_rating = [ ]
The crucial auxiliary data structure.
|
|
__abstractmethods__ = frozenset([ ])
|
|
_abc_cache = <_weakrefset.WeakSet object at 0x26bb850>
|
|
_abc_negative_cache = <_weakrefset.WeakSet object at 0x26bb8d0>
|
|
_abc_negative_cache_version = 18
|
|
_abc_registry = <_weakrefset.WeakSet object at 0x26bb790>
|
Inherited from dict :
__hash__
|
Inherited from object :
__class__
|
__init__(self,
*args,
**kwds)
(Constructor)
| source code
|
This class gets instantiated just like 'dict'.
- Returns:
new empty dictionary
- Overrides:
object.__init__
|
Provide an identical but independent copy.
- Returns: a shallow copy of D
- Overrides:
dict.copy
|
__setitem__(self,
k,
v)
(Index assignment operator)
| source code
|
Besides delegating to dict, we maintain self._rating.
- Overrides:
_abcoll.MutableMapping.__setitem__
|
__delitem__(self,
k)
(Index deletion operator)
| source code
|
Besides delegating to dict, we maintain self._rating.
- Overrides:
_abcoll.MutableMapping.__delitem__
|
__len__(x)
(Length operator)
|
|
len(x)
- Overrides:
_abcoll.Sized.__len__
|
__contains__(D,
k)
(In operator)
|
|
- Returns: True if D has a key k, else False
- Overrides:
_abcoll.Container.__contains__
|
- Returns: True if D has a key k, else False
- Overrides:
dict.has_key
|
Update D from mapping/iterable E and F. If E present and has a .keys()
method, does: for k in E: D[k] = E[k] If E present and lacks .keys()
method, does: for (k, v) in E: D[k] = v In either case, this is
followed by: for k, v in F.items(): D[k] = v
- Returns: None
- Overrides:
_abcoll.MutableMapping.update
|
iter(x)
- Returns: an iterator over the keys of D
- Overrides:
_abcoll.Iterable.__iter__
|
iter(x)
- Returns: an iterator over the keys of D
- Overrides:
_abcoll.Mapping.iterkeys
- (inherited documentation)
|
- Returns: list of D's keys
- Overrides:
_abcoll.Mapping.keys
- (inherited documentation)
|
- Returns: an iterator over the (key, value) items of D
- Overrides:
_abcoll.Mapping.iteritems
- (inherited documentation)
|
- Returns: list of D's (key, value) pairs, as 2-tuples
- Overrides:
_abcoll.Mapping.items
- (inherited documentation)
|
- Returns: an iterator over the values of D
- Overrides:
_abcoll.Mapping.itervalues
- (inherited documentation)
|
- Returns: list of D's values
- Overrides:
_abcoll.Mapping.values
- (inherited documentation)
|
_rating
The crucial auxiliary data structure. A list of all (value, key)
pairs, kept in "natural"ly-sorted order.
- Value:
-
|