Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Jonas Plewinski
pystencils
Commits
44da4538
Commit
44da4538
authored
Sep 26, 2019
by
Stephan Seitz
Browse files
Document backends.json
parent
f1f2faee
Changes
1
Hide whitespace changes
Inline
Side-by-side
pystencils/backends/json.py
View file @
44da4538
...
@@ -9,6 +9,7 @@
...
@@ -9,6 +9,7 @@
"""
"""
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
:
try
:
...
@@ -28,11 +29,19 @@ except Exception:
...
@@ -28,11 +29,19 @@ except Exception:
def
dumps
(
self
,
*
args
):
def
dumps
(
self
,
*
args
):
raise
ImportError
(
'pyyaml not installed'
)
raise
ImportError
(
'pyyaml not installed'
)
def
dump
(
self
,
*
args
):
raise
ImportError
(
'pyyaml not installed'
)
def
expr_to_dict
(
expr_or_node
:
NodeOrExpr
,
with_c_code
=
True
,
full_class_names
=
False
):
"""Converts a SymPy expression to a serializable dict (mainly for debugging purposes)
The dict recursively contains all args of the expression as ``dict``s
See :func:`.write_json`
def
expr_to_dict
(
expr_or_node
,
with_c_code
=
True
,
full_class_names
=
False
):
Args:
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,12 +58,26 @@ def expr_to_dict(expr_or_node, with_c_code=True, full_class_names=False):
...
@@ -49,12 +58,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
):
"""Print debug JSON of an AST to string
Args:
expr_or_node (NodeOrExpr): a SymPy expression or a :class:`pystencils.astnodes.Node`
Returns:
str: JSON representation of AST
"""
dict
=
expr_to_dict
(
expr_or_node
)
dict
=
expr_to_dict
(
expr_or_node
)
return
json
.
dumps
(
dict
,
indent
=
4
)
return
json
.
dumps
(
dict
,
indent
=
4
)
def
write_json
(
filename
,
expr_or_node
):
def
write_json
(
filename
:
str
,
expr_or_node
:
NodeOrExpr
):
"""Writes debug JSON represenation of AST to file
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
:
json
.
dump
(
dict
,
f
,
indent
=
4
)
json
.
dump
(
dict
,
f
,
indent
=
4
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment