-
Notifications
You must be signed in to change notification settings - Fork 243
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
fbdee6d
commit 00abbff
Showing
8 changed files
with
105 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from tests.e2e.executors.java import JavaExecutor | ||
from tests.e2e.executors.python import PythonExecutor | ||
|
||
__all__ = [ | ||
JavaExecutor, | ||
PythonExecutor, | ||
] |
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,22 @@ | ||
import contextlib | ||
|
||
from tests import utils | ||
|
||
|
||
class BaseExecutor: | ||
|
||
_resource_tmp_dir = None | ||
|
||
@contextlib.contextmanager | ||
def prepare_then_cleanup(self): | ||
with utils.tmp_dir() as tmp_dirpath: | ||
self._resource_tmp_dir = tmp_dirpath | ||
self.prepare() | ||
|
||
try: | ||
yield | ||
finally: | ||
self._resource_tmp_dir = None | ||
|
||
def prepare(self): | ||
raise NotImplementedError |
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,39 @@ | ||
import importlib | ||
import os | ||
import sys | ||
|
||
from m2cgen import exporters | ||
from tests.e2e.executors import base | ||
|
||
|
||
class PythonExecutor(base.BaseExecutor): | ||
|
||
def __init__(self, model): | ||
self.model = model | ||
self.exporter = exporters.PythonExporter(model) | ||
|
||
def predict(self, X): | ||
# Hacky way to dynamically import generated function | ||
|
||
parent_dir = os.path.dirname(self._resource_tmp_dir) | ||
package = os.path.basename(self._resource_tmp_dir) | ||
|
||
sys.path.append(parent_dir) | ||
|
||
try: | ||
score = importlib.import_module("{}.model".format(package)).score | ||
finally: | ||
sys.path.pop() | ||
|
||
return score(X) | ||
|
||
def prepare(self): | ||
exported_models = self.exporter.export() | ||
assert len(exported_models) == 1 | ||
|
||
_, code = exported_models[0] | ||
|
||
file_name = os.path.join(self._resource_tmp_dir, "model.py") | ||
|
||
with open(file_name, "w") as f: | ||
f.write(code) |
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