Package wsatools :: Module Utilities :: Class Ratings
[hide private]

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)]
Nested Classes [hide private]

Inherited from _abcoll.Sized: __metaclass__

Instance Methods [hide private]
new empty dictionary

__init__(self, *args, **kwds)
This class gets instantiated just like 'dict'.
source code
a shallow copy of D
copy(self)
Provide an identical but independent copy.
source code
 
__setitem__(self, k, v)
Besides delegating to dict, we maintain self._rating.
source code
 
__delitem__(self, k)
Besides delegating to dict, we maintain self._rating.
source code
 
__len__(x)
len(x)
True if D has a key k, else False
__contains__(D, k)
True if D has a key k, else False
has_key(D, k)
None
update(D, E=..., **F)
Update D from mapping/iterable E and F.
an iterator over the keys of D
__iter__(self)
iter(x)
source code
an iterator over the keys of D
iterkeys(self)
iter(x)
source code
list of D's keys
keys(self) source code
an iterator over the (key, value) items of D
iteritems(self) source code
list of D's (key, value) pairs, as 2-tuples
items(self) source code
an iterator over the values of D
itervalues(self) source code
list of D's values
values(self) source code
 
rating(self, key) source code
 
getValueByRating(self, rating) source code
 
getKeyByRating(self, rating) source code
 
getCountsPerRating(self) source code

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__

Class Methods [hide private]

Inherited from _abcoll.Sized: __subclasshook__

Class Variables [hide private]
  _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__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, *args, **kwds)
(Constructor)

source code 

This class gets instantiated just like 'dict'.

Returns:
new empty dictionary

Overrides: object.__init__

copy(self)

source code 

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__

has_key(D, k)

 
Returns: True if D has a key k, else False
Overrides: dict.has_key

update(D, E=..., **F)

 

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__(self)

source code 

iter(x)

Returns: an iterator over the keys of D
Overrides: _abcoll.Iterable.__iter__

iterkeys(self)

source code 

iter(x)

Returns: an iterator over the keys of D
Overrides: _abcoll.Mapping.iterkeys
(inherited documentation)

keys(self)

source code 
Returns: list of D's keys
Overrides: _abcoll.Mapping.keys
(inherited documentation)

iteritems(self)

source code 
Returns: an iterator over the (key, value) items of D
Overrides: _abcoll.Mapping.iteritems
(inherited documentation)

items(self)

source code 
Returns: list of D's (key, value) pairs, as 2-tuples
Overrides: _abcoll.Mapping.items
(inherited documentation)

itervalues(self)

source code 
Returns: an iterator over the values of D
Overrides: _abcoll.Mapping.itervalues
(inherited documentation)

values(self)

source code 
Returns: list of D's values
Overrides: _abcoll.Mapping.values
(inherited documentation)

Class Variable Details [hide private]

_rating

The crucial auxiliary data structure. A list of all (value, key) pairs, kept in "natural"ly-sorted order.

Value:
[]