Skip to content

Instantly share code, notes, and snippets.

@jessalfredsen
Created March 11, 2015 10:09
Show Gist options
  • Select an option

  • Save jessalfredsen/00e809e12e26f7604343 to your computer and use it in GitHub Desktop.

Select an option

Save jessalfredsen/00e809e12e26f7604343 to your computer and use it in GitHub Desktop.
Because I'm in school and I fancy cake !
"""
TODO: Example:
I have 5 instances but 12 partitions
I want all instances to consume so one instance must consume on an odd number
"""
import math
def get_chunks(iterable, parts):
"""Yield successive chunks from ``len(iterable) / parts``.
Arguments:
iterable (``list``): The list to operate on
parts (``int``): The amount of chunks to yield
Yields:
``list``
"""
result = []
extras = len(iterable) % parts # The python modulo function is your friend ;)
added_last_time = False
items_pr_part = math.floor(len(iterable)/parts)
for i in iterable:
result.append(i)
if len(result) >= items_pr_part:
if extras and not added_last_time:
extras -= 1
added_last_time = True
continue
yield result
added_last_time = False
del result[:]
if result:
yield result
def test_get_chunks():
partitions = ['3', '2', '10', '1', '0', '7', '6', '5', '4', '9', '8', '11', '14', '20', '23', '90', '11', '34', '83', '92', '182']
instances = ['i-31dab9d7', 'i-900f6274', 'i-950f6271', 'i-b6b7d251',
'i-b7b7d250',]
chunks = list(get_chunks(partitions, len(instances)))
assert len(chunks) == len(instances)
if __name__ == '__main__':
test_get_chunks()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment