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
Stephan Seitz
pystencils
Commits
f6bb5f02
Commit
f6bb5f02
authored
Sep 24, 2019
by
Stephan Seitz
Browse files
Add pystencils.backends.json
parent
1c0665c4
Changes
2
Hide whitespace changes
Inline
Side-by-side
pystencils/backends/json.py
0 → 100644
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
)
pystencils_tests/test_json_backend.py
0 → 100644
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
))
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