From cee35c568f1346c077113ab57376b843e90410f3 Mon Sep 17 00:00:00 2001 From: Tim Huegerich Date: Thu, 17 Oct 2024 13:40:16 -0500 Subject: [PATCH 1/6] Add initial StataTranslator tests --- papermill/tests/test_translators.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/papermill/tests/test_translators.py b/papermill/tests/test_translators.py index 0edc1f07..8a1345f7 100644 --- a/papermill/tests/test_translators.py +++ b/papermill/tests/test_translators.py @@ -628,3 +628,41 @@ def test_translate_comment_sh(test_input, expected): ) def test_translate_codify_sh(parameters, expected): assert translators.BashTranslator.codify(parameters) == expected + + +# Stata section +@pytest.mark.parametrize( + "test_input,expected", + [ + ("foo", """`"foo"'"""), + ("foo bar", """`"foo bar"'"""), + ('foo"bar', """`"foo"bar"'"""), + (12345, '12345'), + (-54321, '-54321'), + (1.2345, '1.2345'), + (-5432.1, '-5432.1'), + (True, '1'), + (False, '0'), + (None, '""'), + ], +) +def test_translate_type_r(test_input, expected): + assert translators.StataTranslator.translate(test_input) == expected + + +@pytest.mark.parametrize("test_input,expected", [("", '*'), ("foo", '* foo'), ("['best effort']", "* ['best effort']")]) +def test_translate_comment_r(test_input, expected): + assert translators.StataTranslator.comment(test_input) == expected + + +@pytest.mark.parametrize( + "parameters,expected", + [ + ({"foo": "bar"}, '* Parameters\nglobal foo = "bar"\n'), + ({"foo": True}, '* Parameters\nglobal foo = 1\n'), + ({"foo": 5}, '* Parameters\nglobal foo = 5\n'), + ({"foo": 1.1}, '* Parameters\nglobal foo = 1.1\n'), + ], +) +def test_translate_codify_r(parameters, expected): + assert translators.StataTranslator.codify(parameters) == expected From c03a5bcef3bcdb52c6fe8adb2c10bd8daa7b2c4b Mon Sep 17 00:00:00 2001 From: Tim Huegerich Date: Thu, 17 Oct 2024 13:56:01 -0500 Subject: [PATCH 2/6] Add initial StataTranslator --- papermill/translators.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/papermill/translators.py b/papermill/translators.py index 1cb43d89..fbc6cec4 100644 --- a/papermill/translators.py +++ b/papermill/translators.py @@ -545,6 +545,32 @@ def assign(cls, name, str_val): return f'{name}={str_val}' +class StataTranslator(Translator): + @classmethod + def translate_escaped_str(cls, str_val): + """Reusable by most interpreters""" + if isinstance(str_val, str): + str_val = str_val.encode('unicode_escape') + str_val = str_val.decode('utf-8') + return f"""`"{str_val}"'""" + + @classmethod + def translate_none(cls, val): + return '""' + + @classmethod + def translate_bool(cls, val): + return '1' if val else '0' + + @classmethod + def comment(cls, cmt_str): + return f'* {cmt_str}'.strip() + + @classmethod + def assign(cls, name, str_val): + return f'global {name} = {str_val}' + + # Instantiate a PapermillIO instance and register Handlers. papermill_translators = PapermillTranslators() papermill_translators.register("python", PythonTranslator) @@ -559,6 +585,7 @@ def assign(cls, name, str_val): papermill_translators.register("sparkkernel", ScalaTranslator) papermill_translators.register("sparkrkernel", RTranslator) papermill_translators.register("bash", BashTranslator) +papermill_translators.register("stata", StataTranslator) def translate_parameters(kernel_name, language, parameters, comment='Parameters'): From 16936704ff9e2aca71b37698727acd8e50dda707 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 17 Oct 2024 18:59:21 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- papermill/translators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/papermill/translators.py b/papermill/translators.py index fbc6cec4..9dae2f96 100644 --- a/papermill/translators.py +++ b/papermill/translators.py @@ -553,7 +553,7 @@ def translate_escaped_str(cls, str_val): str_val = str_val.encode('unicode_escape') str_val = str_val.decode('utf-8') return f"""`"{str_val}"'""" - + @classmethod def translate_none(cls, val): return '""' From 48ca21ac5a286e650233aeb4c05297473ab4794b Mon Sep 17 00:00:00 2001 From: Tim Huegerich Date: Thu, 17 Oct 2024 14:13:57 -0500 Subject: [PATCH 4/6] Fix StataTranslator tests --- papermill/tests/test_translators.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/papermill/tests/test_translators.py b/papermill/tests/test_translators.py index 8a1345f7..97a227b1 100644 --- a/papermill/tests/test_translators.py +++ b/papermill/tests/test_translators.py @@ -646,23 +646,23 @@ def test_translate_codify_sh(parameters, expected): (None, '""'), ], ) -def test_translate_type_r(test_input, expected): +def test_translate_type_stata(test_input, expected): assert translators.StataTranslator.translate(test_input) == expected @pytest.mark.parametrize("test_input,expected", [("", '*'), ("foo", '* foo'), ("['best effort']", "* ['best effort']")]) -def test_translate_comment_r(test_input, expected): +def test_translate_comment_stata(test_input, expected): assert translators.StataTranslator.comment(test_input) == expected @pytest.mark.parametrize( "parameters,expected", [ - ({"foo": "bar"}, '* Parameters\nglobal foo = "bar"\n'), + ({"foo": "bar"}, '* Parameters\nglobal foo = `"bar"'\n'), ({"foo": True}, '* Parameters\nglobal foo = 1\n'), ({"foo": 5}, '* Parameters\nglobal foo = 5\n'), ({"foo": 1.1}, '* Parameters\nglobal foo = 1.1\n'), ], ) -def test_translate_codify_r(parameters, expected): +def test_translate_codify_stata(parameters, expected): assert translators.StataTranslator.codify(parameters) == expected From f55f914f177b314a14e664c18b61fd3a32a91366 Mon Sep 17 00:00:00 2001 From: Tim Huegerich Date: Thu, 17 Oct 2024 14:16:14 -0500 Subject: [PATCH 5/6] Fix test_translate_codify_stata string issue --- papermill/tests/test_translators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/papermill/tests/test_translators.py b/papermill/tests/test_translators.py index 97a227b1..7be403c5 100644 --- a/papermill/tests/test_translators.py +++ b/papermill/tests/test_translators.py @@ -658,7 +658,7 @@ def test_translate_comment_stata(test_input, expected): @pytest.mark.parametrize( "parameters,expected", [ - ({"foo": "bar"}, '* Parameters\nglobal foo = `"bar"'\n'), + ({"foo": "bar"}, '''* Parameters\nglobal foo = `"bar"'\n'''), ({"foo": True}, '* Parameters\nglobal foo = 1\n'), ({"foo": 5}, '* Parameters\nglobal foo = 5\n'), ({"foo": 1.1}, '* Parameters\nglobal foo = 1.1\n'), From e3e7ce6ee8767a26c7603b0d5a79525c3c8493a5 Mon Sep 17 00:00:00 2001 From: Tim Huegerich Date: Thu, 17 Oct 2024 14:38:21 -0500 Subject: [PATCH 6/6] Remove holdover docstring --- papermill/translators.py | 1 - 1 file changed, 1 deletion(-) diff --git a/papermill/translators.py b/papermill/translators.py index 9dae2f96..d04b305e 100644 --- a/papermill/translators.py +++ b/papermill/translators.py @@ -548,7 +548,6 @@ def assign(cls, name, str_val): class StataTranslator(Translator): @classmethod def translate_escaped_str(cls, str_val): - """Reusable by most interpreters""" if isinstance(str_val, str): str_val = str_val.encode('unicode_escape') str_val = str_val.decode('utf-8')