Skip to content
Snippets Groups Projects

Document backends.json

Merged Stephan Seitz requested to merge seitz/pystencils:documentation-for-json-backend into master
1 file
+ 25
41
Compare changes
  • Side-by-side
  • Inline
+ 25
41
@@ -9,30 +9,22 @@
@@ -9,30 +9,22 @@
"""
"""
import json
import json
 
from pystencils.astnodes import NodeOrExpr
from pystencils.backends.cbackend import CustomSympyPrinter, generate_c
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):
def expr_to_dict(expr_or_node: NodeOrExpr, with_c_code=True, full_class_names=False):
raise ImportError('toml not installed')
"""Converts a SymPy expression to a serializable dict (mainly for debugging purposes)
try:
The dict recursively contains all args of the expression as ``dict``s
import yaml
except Exception:
class yaml:
def dumps(self, *args):
raise ImportError('pyyaml not installed')
def dump(self, *args):
See :func:`.write_json`
raise ImportError('pyyaml not installed')
Args:
def expr_to_dict(expr_or_node, with_c_code=True, full_class_names=False):
expr_or_node (NodeOrExpr): a SymPy expression or a :class:`pystencils.astnodes.Node`
 
with_c_code (bool, optional): include C representation of the nodes
 
full_class_names (bool, optional): use full class names (type(object) instead of ``type(object).__name__``
 
"""
self = {'str': str(expr_or_node)}
self = {'str': str(expr_or_node)}
if with_c_code:
if with_c_code:
@@ -49,34 +41,26 @@ def expr_to_dict(expr_or_node, with_c_code=True, full_class_names=False):
@@ -49,34 +41,26 @@ def expr_to_dict(expr_or_node, with_c_code=True, full_class_names=False):
return self
return self
def print_json(expr_or_node):
def print_json(expr_or_node: NodeOrExpr):
dict = expr_to_dict(expr_or_node)
"""Print debug JSON of an AST to string
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)
 
Args:
 
expr_or_node (NodeOrExpr): a SymPy expression or a :class:`pystencils.astnodes.Node`
def write_toml(filename, expr_or_node):
Returns:
 
str: JSON representation of AST
 
"""
dict = expr_to_dict(expr_or_node)
dict = expr_to_dict(expr_or_node)
with open(filename, 'w') as f:
return json.dumps(dict, indent=4)
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_json(filename: str, expr_or_node: NodeOrExpr):
 
"""Writes debug JSON represenation of AST to file
def write_yaml(filename, expr_or_node):
Args:
 
filename (str): Path for the file to write
 
expr_or_node (NodeOrExpr): a SymPy expression or a :class:`pystencils.astnodes.Node`
 
"""
dict = expr_to_dict(expr_or_node)
dict = expr_to_dict(expr_or_node)
with open(filename, 'w') as f:
with open(filename, 'w') as f:
yaml.dump(dict, f)
json.dump(dict, f, indent=4)
Loading