Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B-ASIC - Better ASIC Toolbox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Computer Engineering
B-ASIC - Better ASIC Toolbox
Commits
01fd3b0a
Commit
01fd3b0a
authored
4 years ago
by
Jacob Wahlman
Browse files
Options
Downloads
Patches
Plain Diff
implemented recipe functionality
parent
62f421c4
Branches
66-load-save-sfg-to-file
No related tags found
1 merge request
!33
Resolve "Load/Save SFG to file"
Pipeline
#14093
passed
4 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
b_asic/utils.py
+15
-3
15 additions, 3 deletions
b_asic/utils.py
test/test_load_save_structure.py
+39
-14
39 additions, 14 deletions
test/test_load_save_structure.py
with
54 additions
and
17 deletions
b_asic/utils.py
+
15
−
3
View file @
01fd3b0a
...
@@ -5,6 +5,7 @@ This module contains functions that are used as utilities by other modules or by
...
@@ -5,6 +5,7 @@ This module contains functions that are used as utilities by other modules or by
from
typing
import
Optional
from
typing
import
Optional
from
os
import
getcwd
,
path
from
os
import
getcwd
,
path
import
importlib
from
b_asic
import
AbstractOperation
from
b_asic
import
AbstractOperation
...
@@ -55,13 +56,24 @@ def load_structure(_path: str) -> AbstractOperation:
...
@@ -55,13 +56,24 @@ def load_structure(_path: str) -> AbstractOperation:
return
None
return
None
def
load_recipe
(
module
:
str
)
->
None
:
def
load_recipe
(
module
:
str
,
_namespace
:
Optional
[
dict
]
=
None
)
->
"
Module
"
:
"""
Given the module name or the path to a module, import the module and let it evaluate it
'
s content.
"""
Given the module name or the path to a module, import the module and let it evaluate it
'
s content.
Returns
None as the content from
the module
will
be added to the namespace.
Returns
the attributes of
the module
to
be added to the namespace.
This runs .py scripts inline by importing them, it currently does no checks for security measure.
This runs .py scripts inline by importing them, it currently does no checks for security measure.
Arguments:
Arguments:
module: The path or name of the module to import from.
module: The path or name of the module to import from.
Keyword Arguments:
_namespace: The namespace to add the imported vars to. Normally you would want to use globals(). If None return it
'
s own namespace.
"""
"""
pass
try
:
if
_namespace
is
None
:
return
importlib
.
import_module
(
module
)
return
_namespace
.
update
(
importlib
.
import_module
(
module
).
__dict__
)
except
Exception
as
e
:
print
(
"
Unexpected error occured while loading recipe:
"
,
e
)
return
None
This diff is collapsed.
Click to expand it.
test/test_load_save_structure.py
+
39
−
14
View file @
01fd3b0a
...
@@ -53,17 +53,17 @@ class TestSaveStructures:
...
@@ -53,17 +53,17 @@ class TestSaveStructures:
def
test_load_recipe_file
(
self
):
def
test_load_recipe_file
(
self
):
# Create a file that doesn't exist
# Create a file that doesn't exist
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
+
"
.py
"
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
while
path
.
exists
(
path
.
join
(
getcwd
(),
_file
)):
while
path
.
exists
(
path
.
join
(
getcwd
(),
_file
+
"
.py
"
)):
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
+
"
.py
"
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
try
:
try
:
with
open
(
_file
,
"
w+
"
)
as
handle
:
with
open
(
_file
+
"
.py
"
,
"
w+
"
)
as
handle
:
# The string is indented that way so the file is properly indented, .strip() did not work idk why
# The string is indented that way so the file is properly indented, .strip() did not work idk why
handle
.
write
(
handle
.
write
(
"""
"""
from b_asic import SFG, Output, Addition, Constant
from b_asic import SFG, Output, Addition, Constant
sfg = SFG(outputs=[Output(Addition(Addition(Constant(2), Constant(3)), Addition(Constant(4), Constant(5))))])
sfg
_recipe
= SFG(outputs=[Output(Addition(Addition(Constant(2), Constant(3)), Addition(Constant(4), Constant(5))))])
"""
"""
)
)
except
Exception
as
e
:
except
Exception
as
e
:
...
@@ -71,17 +71,42 @@ sfg = SFG(outputs=[Output(Addition(Addition(Constant(2), Constant(3)), Addition(
...
@@ -71,17 +71,42 @@ sfg = SFG(outputs=[Output(Addition(Addition(Constant(2), Constant(3)), Addition(
# Not defined yet
# Not defined yet
with
pytest
.
raises
(
NameError
):
with
pytest
.
raises
(
NameError
):
sfg
.
evaluate
()
==
14
sfg
_recipe
.
evaluate
()
==
14
load_recipe
(
_file
)
load_recipe
(
_file
,
globals
()
)
assert
sfg
.
evaluate
()
==
14
assert
sfg
_recipe
.
evaluate
()
==
14
def
test_load_invalid_recipe_file
(
self
):
def
test_load_recipe_file_namespace
(
self
):
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
+
"
.py
"
# Create a file that doesn't exist
while
path
.
exists
(
path
.
join
(
getcwd
(),
_file
)):
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
+
"
.py
"
while
path
.
exists
(
path
.
join
(
getcwd
(),
_file
+
"
.py
"
)):
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
try
:
with
open
(
_file
+
"
.py
"
,
"
w+
"
)
as
handle
:
# The string is indented that way so the file is properly indented, .strip() did not work idk why
handle
.
write
(
"""
from b_asic import SFG, Output, Addition, Constant
sfg_recipe_2 = SFG(outputs=[Output(Addition(Addition(Constant(2), Constant(3)), Addition(Constant(4), Constant(5))))])
"""
)
except
Exception
as
e
:
assert
False
,
f
"
Could not create file:
{
e
}
"
# Not defined yet
with
pytest
.
raises
(
NameError
):
sfg_recipe_2
.
evaluate
()
==
14
_namespace
=
load_recipe
(
_file
)
assert
_namespace
.
sfg_recipe_2
.
evaluate
()
==
14
assert
_namespace
!=
globals
()
def
test_load_invalid_recipe_file
(
self
):
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
while
path
.
exists
(
path
.
join
(
getcwd
(),
_file
+
"
.py
"
)):
_file
=
""
.
join
(
choice
(
ascii_lowercase
)
for
_
in
range
(
4
))
load_recipe
(
_file
)
load_recipe
(
_file
)
with
pytest
.
raises
(
NameError
):
with
pytest
.
raises
(
NameError
):
assert
sfg
.
evaluate
()
==
14
assert
sfg_recipe_3
.
evaluate
()
==
14
\ No newline at end of file
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