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

drop string.Template from tests and migrate from format to f-strings #248

Merged
merged 4 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion tests/assemblers/test_lightgbm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import lightgbm
import numpy as np
from tests import utils

from m2cgen import assemblers, ast
from tests import utils


def test_binary_classification():
Expand Down
1 change: 1 addition & 0 deletions tests/assemblers/test_svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
from sklearn import svm
from lightning.classification import KernelSVC

from m2cgen import assemblers, ast
from tests import utils

Expand Down
3 changes: 2 additions & 1 deletion tests/assemblers/test_xgboost.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import xgboost
import numpy as np
import os
from tests import utils

from m2cgen import assemblers, ast
from tests import utils


def test_binary_classification():
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/executors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ def prepare(self):
@classmethod
def prepare_global(cls, **kwargs):
for key, value in kwargs.items():
setattr(cls, "_{}".format(key), value)
setattr(cls, f"_{key}", value)
28 changes: 13 additions & 15 deletions tests/e2e/executors/c.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import string
import subprocess

from m2cgen import interpreters, assemblers
Expand All @@ -10,32 +9,32 @@
EXECUTOR_CODE_TPL = """
#include <stdio.h>

${model_code}
{model_code}

int main(int argc, char *argv[])
{
{{
double input [argc-1];
for (int i = 1; i < argc; ++i) {
for (int i = 1; i < argc; ++i) {{
sscanf(argv[i], "%lf", &input[i-1]);
}
}}

${print_code}
{print_code}

return 0;
}
}}
"""

EXECUTE_AND_PRINT_SCALAR = """
printf("%f\\n", score(input));
"""

EXECUTE_AND_PRINT_VECTOR_TPL = """
double result[${size}];
double result[{size}];
score(input, result);

for (int i = 0; i < ${size}; ++i) {
for (int i = 0; i < {size}; ++i) {{
printf("%f ", *(result+i));
}
}}
"""


Expand All @@ -61,18 +60,17 @@ def predict(self, X):
def prepare(self):

if self.model_ast.output_size > 1:
print_code = (
string.Template(EXECUTE_AND_PRINT_VECTOR_TPL).substitute(
size=self.model_ast.output_size))
print_code = EXECUTE_AND_PRINT_VECTOR_TPL.format(
size=self.model_ast.output_size)
else:
print_code = EXECUTE_AND_PRINT_SCALAR

executor_code = string.Template(EXECUTOR_CODE_TPL).substitute(
executor_code = EXECUTOR_CODE_TPL.format(
model_code=self.interpreter.interpret(self.model_ast),
print_code=print_code)

file_name = os.path.join(
self._resource_tmp_dir, "{}.c".format(self.model_name))
self._resource_tmp_dir, f"{self.model_name}.c")

with open(file_name, "w") as f:
f.write(executor_code)
Expand Down
23 changes: 11 additions & 12 deletions tests/e2e/executors/c_sharp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import string
import subprocess

from m2cgen import assemblers, interpreters
Expand All @@ -9,17 +8,17 @@
EXECUTOR_CODE_TPL = """
using System;

namespace TestConsoleApp {
class Program {
static void Main(string[] args) {
namespace TestConsoleApp {{
class Program {{
static void Main(string[] args) {{
double[] input_ = new double[args.Length];
for(int i = 0; i < input_.Length; ++i) {
for(int i = 0; i < input_.Length; ++i) {{
input_[i] = double.Parse(args[i]);
}
${print_code}
}
}
}
}}
{print_code}
}}
}}
}}
"""

EXECUTE_AND_PRINT_SCALAR = """
Expand Down Expand Up @@ -74,7 +73,7 @@ def prepare(self):
print_code = EXECUTE_AND_PRINT_VECTOR
else:
print_code = EXECUTE_AND_PRINT_SCALAR
executor_code = string.Template(EXECUTOR_CODE_TPL).substitute(
executor_code = EXECUTOR_CODE_TPL.format(
print_code=print_code)
model_code = self.interpreter.interpret(self.model_ast)

Expand All @@ -88,6 +87,6 @@ def prepare(self):
subprocess.call([self._dotnet,
"build",
os.path.join(self._global_tmp_dir,
"{}.csproj".format(self.project_name)),
f"{self.project_name}.csproj"),
"--output",
self.target_exec_dir])
15 changes: 7 additions & 8 deletions tests/e2e/executors/dart.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import os
import string

from m2cgen import assemblers, interpreters
from tests import utils
from tests.e2e.executors import base

EXECUTOR_CODE_TPL = """
${model_code}
{model_code}

void main(List<String> args) {
void main(List<String> args) {{
List<double> input_ = args.map((x) => double.parse(x)).toList();
${print_code}
}
{print_code}
}}
"""

EXECUTE_AND_PRINT_SCALAR = """
Expand Down Expand Up @@ -40,7 +39,7 @@ def __init__(self, model):

def predict(self, X):
file_name = os.path.join(self._resource_tmp_dir,
"{}.dart".format(self.executor_name))
f"{self.executor_name}.dart")
exec_args = [self._dart,
file_name,
*map(str, X)]
Expand All @@ -53,11 +52,11 @@ def prepare(self):
print_code = EXECUTE_AND_PRINT_SCALAR

model_code = self.interpreter.interpret(self.model_ast)
executor_code = string.Template(EXECUTOR_CODE_TPL).substitute(
executor_code = EXECUTOR_CODE_TPL.format(
model_code=model_code,
print_code=print_code)

executor_file_name = os.path.join(
self._resource_tmp_dir, "{}.dart".format(self.executor_name))
self._resource_tmp_dir, f"{self.executor_name}.dart")
with open(executor_file_name, "w") as f:
f.write(executor_code)
23 changes: 10 additions & 13 deletions tests/e2e/executors/go.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import string
import subprocess

from m2cgen import assemblers, interpreters
Expand All @@ -15,24 +14,24 @@
"strconv"
)

${model_code}
{model_code}

func main() {
func main() {{
input := make([]float64, 0, len(os.Args)-1)
for _, s := range os.Args[1:] {
for _, s := range os.Args[1:] {{
f, _ := strconv.ParseFloat(s, 64)
input = append(input, f)
}
}}

${print_code}
}
{print_code}
}}
"""

EXECUTE_AND_PRINT_SCALAR = """
fmt.Printf("%f\\n", score(input))
"""

EXECUTE_AND_PRINT_VECTOR_TPL = """
EXECUTE_AND_PRINT_VECTOR = """
result := score(input)

for _, v := range result {
Expand Down Expand Up @@ -62,18 +61,16 @@ def predict(self, X):
def prepare(self):

if self.model_ast.output_size > 1:
print_code = (
string.Template(EXECUTE_AND_PRINT_VECTOR_TPL).substitute(
size=self.model_ast.output_size))
print_code = EXECUTE_AND_PRINT_VECTOR
else:
print_code = EXECUTE_AND_PRINT_SCALAR

executor_code = string.Template(EXECUTOR_CODE_TPL).substitute(
executor_code = EXECUTOR_CODE_TPL.format(
model_code=self.interpreter.interpret(self.model_ast),
print_code=print_code)

file_name = os.path.join(
self._resource_tmp_dir, "{}.go".format(self.model_name))
self._resource_tmp_dir, f"{self.model_name}.go")

with open(file_name, "w") as f:
f.write(executor_code)
Expand Down
15 changes: 7 additions & 8 deletions tests/e2e/executors/haskell.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import os
import string
import subprocess

from m2cgen import assemblers, interpreters
from tests import utils
from tests.e2e.executors import base

EXECUTOR_CODE_TPL = """
module ${executor_name} where
module {executor_name} where
import System.Environment (getArgs)
import ${model_name}
import {model_name}

main = do
args <- getArgs
let inputArray = [read i::Double | i <- args]
let res = score inputArray
${print_code}
{print_code}
"""

PRINT_SCALAR = "print res"
Expand Down Expand Up @@ -48,23 +47,23 @@ def prepare(self):
print_code = PRINT_VECTOR
else:
print_code = PRINT_SCALAR
executor_code = string.Template(EXECUTOR_CODE_TPL).substitute(
executor_code = EXECUTOR_CODE_TPL.format(
executor_name=self.executor_name,
model_name=self.model_name,
print_code=print_code)
model_code = self.interpreter.interpret(self.model_ast)

executor_file_name = os.path.join(
self._resource_tmp_dir, "{}.hs".format(self.executor_name))
self._resource_tmp_dir, f"{self.executor_name}.hs")
model_file_name = os.path.join(
self._resource_tmp_dir, "{}.hs".format(self.model_name))
self._resource_tmp_dir, f"{self.model_name}.hs")
with open(executor_file_name, "w") as f:
f.write(executor_code)
with open(model_file_name, "w") as f:
f.write(model_code)

exec_args = [self._ghc, executor_file_name,
"-i{}".format(self._resource_tmp_dir),
f"-i{self._resource_tmp_dir}",
"-o", os.path.join(self._resource_tmp_dir,
self.executor_name)]
subprocess.call(exec_args)
8 changes: 5 additions & 3 deletions tests/e2e/executors/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def prepare(self):
# Create files generated by exporter in the temp dir.
code = m2c.export_to_java(self.model, class_name=self.class_name)
code_file_name = os.path.join(self._resource_tmp_dir,
"{}.java".format(self.class_name))
f"{self.class_name}.java")

with open(code_file_name, "w") as f:
f.write(code)
Expand All @@ -42,6 +42,8 @@ def prepare(self):
self._resource_tmp_dir)

# Compile all files together.
exec_args = [self._javac_bin, code_file_name] + (
[os.path.join(self._resource_tmp_dir, "Executor.java")])
exec_args = [
self._javac_bin,
code_file_name,
os.path.join(self._resource_tmp_dir, "Executor.java")]
subprocess.call(exec_args)
3 changes: 2 additions & 1 deletion tests/e2e/executors/javascript.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from py_mini_racer import py_mini_racer

import m2cgen as m2c
from tests.e2e.executors import base

Expand All @@ -15,7 +16,7 @@ def predict(self, X):
with open(file_name, 'r') as myfile:
code = myfile.read()

caller = "score([" + ",".join(map(str, X)) + "]);\n"
caller = f"score([{','.join(map(str, X))}]);\n"

ctx = py_mini_racer.MiniRacer()
result = ctx.eval(caller + code)
Expand Down
Loading