forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyutils.py
More file actions
24 lines (18 loc) · 835 Bytes
/
pyutils.py
File metadata and controls
24 lines (18 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from sympy.printing.pycode import PythonCodePrinter
""" This module collects utilities for rendering Python code. """
def render_as_module(content, standard='python3'):
"""Renders python code as a module (with the required imports)
Parameters
==========
standard
See the parameter ``standard`` in
:meth:`sympy.printing.pycode.pycode`
"""
printer = PythonCodePrinter({'standard':standard})
pystr = printer.doprint(content)
if printer._settings['fully_qualified_modules']:
module_imports_str = '\n'.join('import %s' % k for k in printer.module_imports)
else:
module_imports_str = '\n'.join(['from %s import %s' % (k, ', '.join(v)) for
k, v in printer.module_imports.items()])
return module_imports_str + '\n\n' + pystr