From f6bb5f025ad580887fec320445946c204f39a137 Mon Sep 17 00:00:00 2001
From: Stephan Seitz <stephan.seitz@fau.de>
Date: Tue, 24 Sep 2019 18:15:57 +0200
Subject: [PATCH] Add pystencils.backends.json

---
 pystencils/backends/json.py           | 82 +++++++++++++++++++++++++++
 pystencils_tests/test_json_backend.py | 28 +++++++++
 2 files changed, 110 insertions(+)
 create mode 100644 pystencils/backends/json.py
 create mode 100644 pystencils_tests/test_json_backend.py

diff --git a/pystencils/backends/json.py b/pystencils/backends/json.py
new file mode 100644
index 0000000..ac2b2d4
--- /dev/null
+++ b/pystencils/backends/json.py
@@ -0,0 +1,82 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright © 2019 Stephan Seitz <stephan.seitz@fau.de>
+#
+# Distributed under terms of the GPLv3 license.
+
+"""
+
+"""
+import json
+
+from pystencils.backends.cbackend import CustomSympyPrinter, generate_c
+
+try:
+    import toml
+except Exception:
+    class toml:
+        def dumps(self, *args):
+            raise ImportError('toml not installed')
+
+        def dump(self, *args):
+            raise ImportError('toml not installed')
+
+try:
+    import yaml
+except Exception:
+    class yaml:
+        def dumps(self, *args):
+            raise ImportError('pyyaml not installed')
+
+        def dump(self, *args):
+            raise ImportError('pyyaml not installed')
+
+
+def expr_to_dict(expr_or_node, with_c_code=True, full_class_names=False):
+
+    self = {'str': str(expr_or_node)}
+    if with_c_code:
+        try:
+            self.update({'c': generate_c(expr_or_node)})
+        except Exception:
+            try:
+                self.update({'c': CustomSympyPrinter().doprint(expr_or_node)})
+            except Exception:
+                pass
+    for a in expr_or_node.args:
+        self.update({str(a.__class__ if full_class_names else a.__class__.__name__): expr_to_dict(a)})
+
+    return self
+
+
+def print_json(expr_or_node):
+    dict = expr_to_dict(expr_or_node)
+    return json.dumps(dict, indent=4)
+
+
+def write_json(filename, expr_or_node):
+    dict = expr_to_dict(expr_or_node)
+    with open(filename, 'w') as f:
+        json.dump(dict, f, indent=4)
+
+
+def print_toml(expr_or_node):
+    dict = expr_to_dict(expr_or_node, full_class_names=False)
+    return toml.dumps(dict)
+
+
+def write_toml(filename, expr_or_node):
+    dict = expr_to_dict(expr_or_node)
+    with open(filename, 'w') as f:
+        toml.dump(dict, f)
+
+
+def print_yaml(expr_or_node):
+    dict = expr_to_dict(expr_or_node, full_class_names=False)
+    return yaml.dump(dict)
+
+
+def write_yaml(filename, expr_or_node):
+    dict = expr_to_dict(expr_or_node)
+    with open(filename, 'w') as f:
+        yaml.dump(dict, f)
diff --git a/pystencils_tests/test_json_backend.py b/pystencils_tests/test_json_backend.py
new file mode 100644
index 0000000..bc9765c
--- /dev/null
+++ b/pystencils_tests/test_json_backend.py
@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright © 2019 Stephan Seitz <stephan.seitz@fau.de>
+#
+# Distributed under terms of the GPLv3 license.
+
+"""
+
+"""
+
+import sympy
+
+import pystencils
+from pystencils.backends.json import print_json
+
+
+def test_json_backend():
+
+    z, y, x = pystencils.fields("z, y, x: [20,40]")
+    a = sympy.Symbol('a')
+
+    assignments = pystencils.AssignmentCollection({
+        z[0, 0]: x[0, 0] * sympy.log(a * x[0, 0] * y[0, 0])
+    })
+
+    ast = pystencils.create_kernel(assignments)
+
+    print(print_json(ast))
-- 
GitLab