Minor grammar and whitespace fixes

Refactors 'item' to only be a separate function in Python2
This commit is contained in:
Joao S. O. Bueno
2015-03-18 11:35:22 -03:00
parent 48a4dcb1f8
commit 92b2935f20

View File

@@ -28,18 +28,18 @@ DEALINGS IN THE SOFTWARE.
''' '''
import sys import sys
_python3 = sys.version_info.major == 3 _python3 = sys.version_info.major >= 3
del sys del sys
class multi_key_dict(object): class multi_key_dict(object):
""" Purpose of this type is to provie a multi-key dictionary. """ The purpose of this type is to provide a multi-key dictionary.
This kind of dictionary has a similar interface to the standard dictionary, and indeed if used This kind of dictionary has a similar interface to the standard dictionary, and indeed if used
with single key key elements - it's behaviour is the same as for a standard dict(). with single key key elements - it's behaviour is the same as for a standard dict().
However it also allows for creation elements using multiple keys (using tuples/lists). However it also allows for creation of elements using multiple keys (using tuples/lists).
Such elements can be accessed using either of those keys (e.g read/updated/deleted). Such elements can be accessed using either of those keys (e.g read/updated/deleted).
Dictionary provides also extended interface for iterating over items and keys by the key type. Dictionary provides also an extended interface for iterating over items and keys by the key type.
This can be useful e.g.: when creating dictionaries with (index,name) allowing to iterate over This can be useful e.g.: when creating dictionaries with (index,name) allowing one to iterate over
items using either: names or indexes. It can be useful for many many other similar use-cases, items using either: names or indexes. It can be useful for many many other similar use-cases,
and there is no limit to the number of keys used to map to the value. and there is no limit to the number of keys used to map to the value.
@@ -178,20 +178,19 @@ class multi_key_dict(object):
i.e. (tuple of keys, values) pairs for all items in this dictionary will be generated. i.e. (tuple of keys, values) pairs for all items in this dictionary will be generated.
@param return_all_keys if set to True - tuple of keys is retuned instead of a key of this type.""" @param return_all_keys if set to True - tuple of keys is retuned instead of a key of this type."""
used_keys = set() used_keys = set()
if key_type is not None: if key_type is None:
key = str(key_type) for item in self.items_dict.items():
if key in self.__dict__: yield item
for key, keys in self.__dict__[key].items(): key = str(key_type)
if keys in used_keys: if key in self.__dict__:
continue for key, keys in self.__dict__[key].items():
used_keys.add(keys) if keys in used_keys:
if return_all_keys: continue
yield keys, self.items_dict[keys] used_keys.add(keys)
else: if return_all_keys:
yield key, self.items_dict[keys] yield keys, self.items_dict[keys]
else: else:
for keys, value in self.items_dict.items(): yield key, self.items_dict[keys]
yield keys, value
def iterkeys(self, key_type=None, return_all_keys=False): def iterkeys(self, key_type=None, return_all_keys=False):
""" Returns an iterator over the dictionary's keys. """ Returns an iterator over the dictionary's keys.
@@ -224,13 +223,12 @@ class multi_key_dict(object):
for value in self.items_dict.values(): for value in self.items_dict.values():
yield value yield value
def items(self, key_type=None, return_all_keys=False): if _python3:
result = self.iteritems(key_type, return_all_keys) items = iteritems
if not _python3: else:
result = list(result) def items(self, key_type=None, return_all_keys=False):
return result return list(self.iteritems(key_type, return_all_keys))
items.__doc__ = iteritems.__doc__ items.__doc__ = iteritems.__doc__
def keys(self, key_type=None): def keys(self, key_type=None):
""" Returns a copy of the dictionary's keys. """ Returns a copy of the dictionary's keys.