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
_python3 = sys.version_info.major == 3
_python3 = sys.version_info.major >= 3
del sys
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
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).
Dictionary provides also 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
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 one to iterate over
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.
@@ -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.
@param return_all_keys if set to True - tuple of keys is retuned instead of a key of this type."""
used_keys = set()
if key_type is not None:
key = str(key_type)
if key in self.__dict__:
for key, keys in self.__dict__[key].items():
if keys in used_keys:
continue
used_keys.add(keys)
if return_all_keys:
yield keys, self.items_dict[keys]
else:
yield key, self.items_dict[keys]
else:
for keys, value in self.items_dict.items():
yield keys, value
if key_type is None:
for item in self.items_dict.items():
yield item
key = str(key_type)
if key in self.__dict__:
for key, keys in self.__dict__[key].items():
if keys in used_keys:
continue
used_keys.add(keys)
if return_all_keys:
yield keys, self.items_dict[keys]
else:
yield key, self.items_dict[keys]
def iterkeys(self, key_type=None, return_all_keys=False):
""" Returns an iterator over the dictionary's keys.
@@ -224,13 +223,12 @@ class multi_key_dict(object):
for value in self.items_dict.values():
yield value
def items(self, key_type=None, return_all_keys=False):
result = self.iteritems(key_type, return_all_keys)
if not _python3:
result = list(result)
return result
items.__doc__ = iteritems.__doc__
if _python3:
items = iteritems
else:
def items(self, key_type=None, return_all_keys=False):
return list(self.iteritems(key_type, return_all_keys))
items.__doc__ = iteritems.__doc__
def keys(self, key_type=None):
""" Returns a copy of the dictionary's keys.