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,7 +178,9 @@ 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:
for item in self.items_dict.items():
yield item
key = str(key_type) key = str(key_type)
if key in self.__dict__: if key in self.__dict__:
for key, keys in self.__dict__[key].items(): for key, keys in self.__dict__[key].items():
@@ -189,9 +191,6 @@ class multi_key_dict(object):
yield keys, self.items_dict[keys] yield keys, self.items_dict[keys]
else: else:
yield key, self.items_dict[keys] yield key, self.items_dict[keys]
else:
for keys, value in self.items_dict.items():
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,14 +223,13 @@ class multi_key_dict(object):
for value in self.items_dict.values(): for value in self.items_dict.values():
yield value yield value
if _python3:
items = iteritems
else:
def items(self, key_type=None, return_all_keys=False): def items(self, key_type=None, return_all_keys=False):
result = self.iteritems(key_type, return_all_keys) return list(self.iteritems(key_type, return_all_keys))
if not _python3:
result = list(result)
return result
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.
@param key_type if specified, only keys for this type will be returned. @param key_type if specified, only keys for this type will be returned.