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
f6bb5f02
Commit
f6bb5f02
authored
5 years ago
by
Stephan Seitz
Browse files
Options
Downloads
Patches
Plain Diff
Add pystencils.backends.json
parent
1c0665c4
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!56
Interpolation 24.0.9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pystencils/backends/json.py
+82
-0
82 additions, 0 deletions
pystencils/backends/json.py
pystencils_tests/test_json_backend.py
+28
-0
28 additions, 0 deletions
pystencils_tests/test_json_backend.py
with
110 additions
and
0 deletions
pystencils/backends/json.py
0 → 100644
+
82
−
0
View file @
f6bb5f02
# -*- 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
)
This diff is collapsed.
Click to expand it.
pystencils_tests/test_json_backend.py
0 → 100644
+
28
−
0
View file @
f6bb5f02
# -*- 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
))
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