Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
pystencils
Commits
68c4f9b7
Commit
68c4f9b7
authored
5 years ago
by
Stephan Seitz
Browse files
Options
Downloads
Patches
Plain Diff
Document backends.json and remove code for toml/yaml
parent
f1f2faee
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pystencils/backends/json.py
+25
-41
25 additions, 41 deletions
pystencils/backends/json.py
with
25 additions
and
41 deletions
pystencils/backends/json.py
+
25
−
41
View file @
68c4f9b7
...
...
@@ -9,30 +9,22 @@
"""
import
json
from
pystencils.astnodes
import
NodeOrExpr
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
'
)
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
)
try
:
import
yaml
except
Exception
:
class
yaml
:
def
dumps
(
self
,
*
args
):
raise
ImportError
(
'
pyyaml not installed
'
)
The dict recursively contains all args of the expression as ``dict``s
def
dump
(
self
,
*
args
):
raise
ImportError
(
'
pyyaml not installed
'
)
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
)}
if
with_c_code
:
...
...
@@ -49,34 +41,26 @@ def expr_to_dict(expr_or_node, with_c_code=True, full_class_names=False):
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
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`
def
write_toml
(
filename
,
expr_or_node
):
Returns:
str: JSON representation of AST
"""
dict
=
expr_to_dict
(
expr_or_node
)
with
open
(
filename
,
'
w
'
)
as
f
:
toml
.
dump
(
dict
,
f
)
return
json
.
dumps
(
dict
,
indent
=
4
)
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
)
with
open
(
filename
,
'
w
'
)
as
f
:
yaml
.
dump
(
dict
,
f
)
json
.
dump
(
dict
,
f
,
indent
=
4
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment