Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Iliya Manolov
erp5
Commits
02105680
Commit
02105680
authored
Jun 14, 2017
by
Iliya Manolov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Shortened the warnings on importing new modules in Jupyter to just one and fixed some indentation
parent
5fb7eb21
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
43 deletions
+87
-43
bt5/erp5_data_notebook/ExtensionTemplateItem/portal_components/extension.erp5.JupyterCompile.py
...teItem/portal_components/extension.erp5.JupyterCompile.py
+20
-13
bt5/erp5_data_notebook/TestTemplateItem/portal_components/test.erp5.testExecuteJupyter.py
...ateItem/portal_components/test.erp5.testExecuteJupyter.py
+67
-30
No files found.
bt5/erp5_data_notebook/ExtensionTemplateItem/portal_components/extension.erp5.JupyterCompile.py
View file @
02105680
...
...
@@ -298,6 +298,20 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
print_fixer
=
PrintFixer
()
environment_collector
=
EnvironmentParser
()
ast_node
=
import_fixer
.
visit
(
ast_node
)
# Whenever we have new imports we need to warn the user about the
# environment
if
(
import_fixer
.
warning_module_names
!=
[]):
warning
=
(
"print '"
"WARNING: You imported from the modules %s without "
"using the environment object, which is not recomended. "
"Your import was automatically converted to use such method. "
"The setup functions were named as *module*_setup. "
"'"
)
%
(
', '
.
join
(
import_fixer
.
warning_module_names
))
tree
=
ast
.
parse
(
warning
)
tree
.
body
[
0
].
lineno
=
ast_node
.
body
[
-
1
].
lineno
+
5
ast_node
.
body
.
append
(
tree
.
body
[
0
])
ast_node
=
print_fixer
.
visit
(
ast_node
)
ast
.
fix_missing_locations
(
ast_node
)
...
...
@@ -759,6 +773,7 @@ class ImportFixer(ast.NodeTransformer):
def
__init__
(
self
):
self
.
import_func_dict
=
{}
self
.
warning_module_names
=
[]
def
visit_FunctionDef
(
self
,
node
):
"""
...
...
@@ -878,8 +893,8 @@ class ImportFixer(ast.NodeTransformer):
empty_function
.
body
=
[
node
,
return_dict
]
environment_set
=
self
.
newEnvironmentSetCall
(
"%s_setup"
%
result_name
)
warning
=
self
.
newImportWarningCall
(
root_module_name
,
result_name
)
return
[
empty_function
,
environment_set
,
warning
]
self
.
newImportWarningCall
(
root_module_name
,
result_name
)
return
[
empty_function
,
environment_set
]
else
:
return
node
...
...
@@ -914,18 +929,10 @@ class ImportFixer(ast.NodeTransformer):
def
newImportWarningCall
(
self
,
module_name
,
function_name
):
"""
Return an AST.Expr representanting a print statement with a warning to an
user about the import of a module named `module_name` and instructs him
on how to fix it.
Adds a new module to the warning to the user about the importing of new
modules.
"""
warning
=
(
"print '"
"WARNING: Your imported from the module %s without "
"using the environment object, which is not recomended. "
"Your import was automatically converted to use such method."
"The setup function was named as: %s_setup.
\
\
n"
"'"
)
%
(
module_name
,
function_name
)
tree
=
ast
.
parse
(
warning
)
return
tree
.
body
[
0
]
self
.
warning_module_names
.
append
(
module_name
)
def
renderAsHtml
(
self
,
renderable_object
):
...
...
bt5/erp5_data_notebook/TestTemplateItem/portal_components/test.erp5.testExecuteJupyter.py
View file @
02105680
...
...
@@ -882,7 +882,7 @@ print dig
self
.
assertEquals
(
result
,
True
)
def
testNPArrayPrint
(
self
):
def
testNPArrayPrint
(
self
):
self
.
login
(
'dev_user'
)
import_code
=
'''
import numpy as np
...
...
@@ -896,7 +896,6 @@ import numpy as np
result
=
json
.
loads
(
result
)
self
.
assertEquals
(
result
[
'status'
],
'ok'
)
jupyter_code
=
'''
print np.random.rand(256, 256, 256)
'''
...
...
@@ -923,4 +922,42 @@ print np.random.randint(low = 2 ** 63 - 1, size = (256, 256, 256), dtype = 'int6
result
=
json
.
loads
(
result
)
self
.
assertEquals
(
result
[
'status'
],
'ok'
)
def
testImportWarning
(
self
):
'''
This test checks the warning output for imports in Jupyter.
'''
self
.
login
(
'dev_user'
)
import_code
=
'''
import numpy as np
import matplotlib.pyplot as plt
import datetime
'''
reference
=
'Test.Notebook.EnvironmentObject.Errors.Import'
result
=
self
.
portal
.
Base_executeJupyter
(
reference
=
reference
,
python_expression
=
import_code
)
self
.
tic
()
expected_result
=
(
u'WARNING: You imported from the modules numpy'
u', matplotlib.pyplot, datetime without using the environment'
u' object, which is not recomended. Your import was'
u' automatically converted to use such method. The setup'
u' functions were named as *module*_setup.'
)
result
=
json
.
loads
(
result
)
self
.
assertEquals
(
result
[
'status'
],
'ok'
)
self
.
assertEquals
(
result
[
'code_result'
].
strip
(),
expected_result
)
jupyter_code
=
'''
print np.array([1, 2, 3])
'''
result
=
self
.
portal
.
Base_executeJupyter
(
reference
=
reference
,
python_expression
=
jupyter_code
)
self
.
tic
()
result
=
json
.
loads
(
result
)
self
.
assertEquals
(
result
[
'status'
],
'ok'
)
self
.
assertEquals
(
result
[
'code_result'
].
strip
(),
u'[1 2 3]'
)
\ No newline at end of file
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