Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example for a chasing distributable #276

Merged
merged 2 commits into from
Apr 21, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples/chase_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from os import path
import sys
# Append to path so that this script can access the dipla package
sys.path.append(path.abspath('../dipla'))

from dipla.api import Dipla

# This example finds the number of rows away from the bottom that a
# one exists in a grid. Normally this would require processing a whole
# row before the program can start on the next row, using the
# information from the previous row. This one sends the first half of a
# row as soon as it finds it so that the a worker can start on the next
# stage of processing

@Dipla.chasing_distributable(count=2, chasers=5)
def rows_since_found(row, last_calculated, index, count):
interval = len(row)//count
# If we are in the first row, return 1 if we found a 1 and zero
# otherwise
if last_calculated == None:
if interval*index + interval >= len(row):
return row[index*interval:]
else:
return row[index*interval:index*interval+interval]
# If we're not on the first row, increase each number by 1 so that
# in the end we'll have values for how far away from the bottom each
# one was
else:
last = last_calculated
out = []
for i in range(len(last)):
if last_calculated[i] == 0:
out.append(row[i+interval*index])
else:
out.append(last[i] + 1)

return out

grid = [
[1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0]
]

results = Dipla.apply_distributable(rows_since_found, grid)
out = Dipla.get(results)
outlist = []
for o in out:
outlist = outlist + o
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be clearer if you used outlist.extend(o)

print(outlist)