forked from EntilZha/PyFunctional
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to add execution engines as classes and split seq and pseq
- Loading branch information
Showing
6 changed files
with
77 additions
and
36 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 @@ | ||
from functional.util import compose, parallelize | ||
|
||
|
||
class ExecutionStrategies(object): | ||
""" | ||
Enum like object listing the types of execution strategies | ||
""" | ||
PRE_COMPUTE = 0 | ||
PARALLEL = 1 | ||
|
||
|
||
class ExecutionEngine(object): | ||
|
||
def evaluate(self, sequence, transformations): | ||
result = sequence | ||
for transform in transformations: | ||
strategies = transform.execution_strategies | ||
if (strategies is not None | ||
and ExecutionStrategies.PRE_COMPUTE in strategies): | ||
result = transform.function(list(result)) | ||
else: | ||
result = transform.function(result) | ||
return iter(result) | ||
|
||
|
||
class ParallelExecutionEngine(ExecutionEngine): | ||
|
||
def evaluate(self, sequence, transformations): | ||
result = sequence | ||
staged = [] | ||
for transform in transformations: | ||
strategies = transform.execution_strategies | ||
if strategies and ExecutionStrategies.PRE_COMPUTE in strategies: | ||
result = list(result) | ||
if strategies and ExecutionStrategies.PARALLEL in strategies: | ||
staged.insert(0, transform.function) | ||
else: | ||
if staged: | ||
result = parallelize(compose(*staged), result) | ||
staged = [] | ||
result = transform.function(result) | ||
if staged: | ||
result = parallelize(compose(*staged), result) | ||
return iter(result) |
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
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