-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fce21a
commit df2cfc3
Showing
2 changed files
with
49 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Utility functions module""" | ||
|
||
|
||
def chunk_it(nodi, n): | ||
""" | ||
Divide nodes in chunks according to number of processes. | ||
:param list nodi: list of nodes | ||
:param int n: number of available processes | ||
:return: list of graph nodes to be assigned to every process | ||
:rtype: list | ||
""" | ||
|
||
avg = len(nodi) / n | ||
out = [] | ||
last = 0.0 | ||
|
||
while last < len(nodi): | ||
out.append(nodi[int(last):int(last + avg)]) | ||
last += avg | ||
return out | ||
|
||
def merge_lists(l1, l2, key): | ||
""" | ||
Merge two lists of dictionaries according to their keys. | ||
:param list l1: first list of dictionaries to be merged | ||
:param list l2: second list of dictionaries to be merged | ||
:param list key: key on which to merge the two lists of dictionaries | ||
:return: the merged list of dictionaries | ||
:rtype: list | ||
""" | ||
|
||
merged = {} | ||
for item in l1 + l2: | ||
if item[key] in merged: | ||
merged[item[key]].update(item) | ||
else: | ||
merged[item[key]] = item | ||
return [val for (_, val) in merged.items()] |