Reusing Python code¶
When you write a lot of Python code in a project, or accross project, you will often want to make reusable parts of code.
DSS provides several mechanisms for reusing Python code:
- Packaging your code as functions or modules, and making them available in a specific project
- Packaging your code as functions or modules, and making them available in all projects
- Packaging your code as a reusable plugin, and making it available for coder and non-coder users alike
Sharing Python code within a project¶
In each project, you can write Python modules in the Python library editor. The project Python library editor is available by going into the “Code” universe (orange), and selecting the “Libraries” tab
Modules that are written here are automatically available in all Python code in the same project. Import rules are the regular Python rules.
For example, to use the function shown in the above image, use:
from analyticfunctions import build_custom_keras_model
model = build_custom_keras_model()
or:
import analyticfunctions
model = analyticfunctions.build_custom_keras_model()
Note
Don’t forget that if you create folders in the Python library editor, each folder must have a __init__.py
file in order to be a valid Python module
For a file at the root of the library editor:
Use:
from misccustom import get_now
the_time = get_now()
or:
import misccustom
the_time = misccustom.get_now()
Permissions¶
In order to use the project’s Python library editor, you need to be granted:
- Write access on the project
- The global (group-level) permission “Edit lib folders”
Sharing Python code globally¶
In addition to the per-project Python library editor, there is another global Python library editor for the whole instance. The global Python library editor is available from the “Settings” menu.
Modules that are written here are automatically available in all Python code in all projects. Import rules are the regular Python rules (see above for more information)
Permissions¶
In order to use the global Python library editor, you need to be granted the global (group-level) permission “Edit lib folders”
Caution¶
Putting code in the global library increases the risk of having clashes or conflict. Generally speaking, it is preferable to use per-project libraries.
Libraries in the global folder will be importable in all Python code, regardless of the active code environment. You might need to ensure that your code is compatible with both Python 2 and Python 3.
Manually edition of code library folders¶
Although not recommended, if you have shell access to the DSS machine, you can modify the library folders directly:
- Per-project library folder is in
DATA_DIR/config/projects/PROJECT_KEY/lib/python
- Global library folder is in
DATA_DIR/lib/python