forked from apache/incubator-wayang
-
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.
[WAYANG-#8] Seed creation of Platforms/python
Signed-off-by: bertty <[email protected]>
- Loading branch information
Showing
16 changed files
with
236 additions
and
26 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
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
Empty file.
Empty file.
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,60 @@ | ||
from typing import ( Iterable, Callable ) | ||
|
||
class Channel: | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def getchannel(self) -> 'Channel': | ||
return self | ||
|
||
def gettype(self): | ||
return type(self) | ||
|
||
class ChannelDescriptor: | ||
|
||
def __init__(self, channelType: type, isReusable: bool, isSuitableForBreakpoint: bool): | ||
self.channelType = channelType | ||
self.isReusable = isReusable | ||
self.isSuitableForBreakpoint = isSuitableForBreakpoint | ||
|
||
def create_instance(self) -> Channel: | ||
return self.channelType() | ||
|
||
|
||
class PyIteratorChannel(Channel): | ||
|
||
iterable : Iterable | ||
|
||
def __init__(self): | ||
Channel.__init__(self) | ||
|
||
def provide_iterable(self) -> Iterable: | ||
return self.iterable | ||
|
||
def accept_iterable(self, iterable) -> 'PyIteratorChannel': | ||
self.iterable = iterable | ||
return self | ||
|
||
class PyCallableChannel(Channel): | ||
|
||
udf : Callable | ||
|
||
def __init__(self): | ||
Channel.__init__(self) | ||
|
||
def provide_callable(self) -> Callable: | ||
return self.udf | ||
|
||
def accept_callable(self, udf: Callable) -> 'PyCallableChannel': | ||
self.udf = udf | ||
return self | ||
|
||
@staticmethod | ||
def concatenate(function_a: Callable, function_b: Callable): | ||
def executable(iterable): | ||
return function_a(function_b(iterable)) | ||
return executable | ||
|
||
PyIteratorChannelDescriptor = ChannelDescriptor(type(PyIteratorChannel()), False, False) | ||
PyCallableChannelDescriptor = ChannelDescriptor(type(PyCallableChannel()), False, False) |
Empty file.
Empty file.
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,35 @@ | ||
from typing import Dict | ||
|
||
from pywayang.operator.base import WyOperator | ||
from pywayang.platforms.python.operators import * | ||
|
||
class Mapping: | ||
mappings: Dict[str, type] | ||
|
||
def __init__(self): | ||
self.mappings = {} | ||
|
||
def add_mapping(self, operator: PythonExecutionOperator): | ||
self.mappings[operator.name] = type(operator) | ||
|
||
def get_instanceof(self, operator: WyOperator): | ||
template = self.mappings[operator.name] | ||
if template is None: | ||
raise Exception( | ||
"the operator {} does not have valid mapping".format( | ||
operator.name | ||
) | ||
) | ||
return template(operator) | ||
|
||
|
||
def __str__(self): | ||
return str(self.mappings) | ||
|
||
def __repr__(self): | ||
return self.__str__() | ||
|
||
OperatorMappings = Mapping() | ||
|
||
OperatorMappings.add_mapping(PyFilterOperator()) | ||
|
43 changes: 43 additions & 0 deletions
43
python/src/pywayang/platforms/python/operators/PyFilterOperator.py
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,43 @@ | ||
from pywayang.operator.unary import FilterOperator | ||
from pywayang.platforms.python.operators.PythonExecutionOperator import PythonExecutionOperator | ||
from pywayang.platforms.python.channels import (Channel, ChannelDescriptor, PyIteratorChannel, | ||
PyIteratorChannelDescriptor, PyCallableChannelDescriptor, | ||
PyCallableChannel) | ||
from typing import Set | ||
|
||
class PyFilterOperator(FilterOperator, PythonExecutionOperator): | ||
|
||
def __init__(self, origin: FilterOperator = None): | ||
predicate = None if origin is None else origin.predicate | ||
super().__init__(predicate) | ||
pass | ||
|
||
def execute(self, inputs: Channel, outputs: Channel): | ||
self.validateChannels(inputs, outputs) | ||
udf = self.predicate | ||
if isinstance(inputs[0], PyIteratorChannel) : | ||
py_in_iter_channel: PyIteratorChannel = inputs[0] | ||
py_out_iter_channel: PyIteratorChannel = outputs[0] | ||
py_out_iter_channel.accept_iterable(filter(udf, py_in_iter_channel.provide_iterable())) | ||
elif isinstance(inputs[0], PyCallableChannel) : | ||
py_in_call_channel: PyCallableChannel = inputs[0] | ||
py_out_call_channel: PyCallableChannel = outputs[0] | ||
|
||
def func(iterator): | ||
return filter(udf, iterator) | ||
|
||
py_out_call_channel.accept_callable( | ||
PyCallableChannel.concatenate( | ||
func, | ||
py_in_call_channel.provide_callable() | ||
) | ||
) | ||
else: | ||
raise Exception("Channel Type does not supported") | ||
|
||
|
||
def getInputChannelDescriptors(self) -> Set[ChannelDescriptor]: | ||
return {PyIteratorChannelDescriptor, PyCallableChannelDescriptor} | ||
|
||
def getOutputChannelDescriptors(self) -> Set[ChannelDescriptor]: | ||
return {PyIteratorChannelDescriptor, PyCallableChannelDescriptor} |
7 changes: 7 additions & 0 deletions
7
python/src/pywayang/platforms/python/operators/PythonExecutionOperator.py
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,7 @@ | ||
from pywayang.operator.base import WyOperator | ||
from pywayang.platforms.python.channels import Channel | ||
|
||
class PythonExecutionOperator(WyOperator): | ||
|
||
def execute(self, inputs: Channel, output: Channel): | ||
pass |
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,7 @@ | ||
from pywayang.platforms.python.operators.PythonExecutionOperator import PythonExecutionOperator | ||
from pywayang.platforms.python.operators.PyFilterOperator import PyFilterOperator | ||
|
||
__ALL__ = [ | ||
PythonExecutionOperator, | ||
PyFilterOperator | ||
] |
Empty file.
Empty file.
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