MCPcopy Create free account
hub / github.com/EntilZha/PyFunctional / to_dict

Method to_dict

functional/pipeline.py:1429–1454  ·  view source on GitHub ↗

Converts sequence of (Key, Value) pairs to a dictionary. >>> type(seq([('a', 1)]).to_dict()) dict >>> seq([('a', 1), ('b', 2)]).to_dict() {'a': 1, 'b': 2} :param default: Can be a callable zero argument function. When not None, the returned

(self, default=None)

Source from the content-addressed store, hash-verified

1427 return self.to_set()
1428
1429 def to_dict(self, default=None):
1430 """
1431 Converts sequence of (Key, Value) pairs to a dictionary.
1432
1433 >>> type(seq([('a', 1)]).to_dict())
1434 dict
1435
1436 >>> seq([('a', 1), ('b', 2)]).to_dict()
1437 {'a': 1, 'b': 2}
1438
1439 :param default: Can be a callable zero argument function. When not None, the returned
1440 dictionary is a collections.defaultdict with default as value for missing keys. If the
1441 value is not callable, then a zero argument lambda function is created returning the
1442 value and used for collections.defaultdict
1443 :return: dictionary from sequence of (Key, Value) elements
1444 """
1445 dictionary = {}
1446 for e in self.sequence:
1447 dictionary[e[0]] = e[1]
1448 if default is None:
1449 return dictionary
1450 else:
1451 if hasattr(default, "__call__"):
1452 return collections.defaultdict(default, dictionary)
1453 else:
1454 return collections.defaultdict(lambda: default, dictionary)
1455
1456 def dict(self, default=None):
1457 """

Callers 5

dictMethod · 0.95
to_jsonMethod · 0.95
test_to_jsonMethod · 0.80
test_to_dictMethod · 0.80

Calls

no outgoing calls

Tested by 3

test_to_jsonMethod · 0.64
test_to_dictMethod · 0.64