All Projects → GreenDelta → openlca-python-tutorial

GreenDelta / openlca-python-tutorial

Licence: other
Explains the usage of the openLCA API from Python (Jython)

Programming Languages

Jupyter Notebook
11667 projects
python
139335 projects - #7 most used programming language
Batchfile
5799 projects

Projects that are alternatives of or similar to openlca-python-tutorial

imagej macros and scripts
ImageJ macros and scripts written at the imaging facility MRI
Stars: ✭ 19 (-20.83%)
Mutual labels:  jython
Scripts
🔬🍸 Home of the ImageJ BAR: A collection of Broadly Applicable Routines for ImageJ
Stars: ✭ 18 (-25%)
Mutual labels:  jython
olca-app
Source code of openLCA
Stars: ✭ 111 (+362.5%)
Mutual labels:  openlca
burp-copy-as-ffuf
Burp Extension that copies a request and builds a FFUF skeleton
Stars: ✭ 77 (+220.83%)
Mutual labels:  jython
PySiddhi
Python wrapper for Siddhi engine
Stars: ✭ 22 (-8.33%)
Mutual labels:  jython
PengueeBot
Automation tool, visit our discord channel if you have anything to ask
Stars: ✭ 27 (+12.5%)
Mutual labels:  jython
WDR
Jython framework aiming for simplified WebSphere Application Server scripting
Stars: ✭ 43 (+79.17%)
Mutual labels:  jython
Virtualenv
Virtual Python Environment builder
Stars: ✭ 4,017 (+16637.5%)
Mutual labels:  jython

openLCA Python Tutorial

openLCA is a Java application and, thus, runs on the Java Virtual Machine (JVM). Jython is a Python 2.7 implementation that runs on the JVM. It compiles Python code to Java bytecode which is then executed on the JVM. The final release of Jython 2.7 is bundled with openLCA. Under Window > Developer tools > Python you can find a small Python editor where you can write and execute Python scripts:

Open the Python editor

In order to execute a script, you click on the Run button in the toolbar of the Python editor:

Run a script in openLCA

The script is executed in the same Java process as openLCA. Thus, you have access to all the things that you can do with openLCA via this scripting API (and also to everything that you can do with the Java and Jython runtime). Here is a small example script that will show the information dialog below when you execute it in openLCA:

from org.openlca.app.util import UI, Dialog
from org.openlca.app import App

def say_hello():
    Dialog.showInfo(UI.shell(), 'Hello from Python (Jython)!')

if __name__ == '__main__':
    App.runInUI('say hello', say_hello)

Hello from Jython

Relation to standard Python

As said above, Jython runs on the JVM. It implements a great part of the Python 2.7 standard library for the JVM. For example the following script will work when you set the file path to a valid path on your system:

import csv

with open('path/to/file.csv', 'w') as stream:
    writer = csv.writer(stream)
    writer.writerow(["data you", "may want", "to export",])

The Jython standard library is extracted to the python folder of the openLCA workspace which is by default located in your user directory ~/openLCA-data-1.4/python. This is also the location in which you can put your own Jython 2.7 compatible modules. For example, when you create a file tutorial.py with the following function in this folder:

# ~/openLCA-data-1.4/python/tutorial.py
def the_answer():
  f = lambda s, x: s + x if x % 2 == 0 else s
  return reduce(f, range(0, 14))

You can then load it in the openLCA script editor:

import tutorial
import org.openlca.app.util.MsgBox as MsgBox

MsgBox.info('The answer is %s!' % tutorial.the_answer())

An important thing to note is that Python modules that use C-extensions (like NumPy and friends) or parts of the standard library that are not implemented in Jython are not compatible with Jython. If you want to interact from standard CPython with openLCA (using Pandas, NumPy, etc.) you can use the openLCA-IPC Python API.

The openLCA API

As said above, with Jython you directly access the openLCA Java API. In Jython, you interact with a Java class in the same way as with a Python class. The openLCA API starts with a set of classes that describe the basic data model, like Flow, Process, ProductSystem. You can find these classes in the olca-module repository.

...

Content

License

This project is in the worldwide public domain, released under the CC0 1.0 Universal Public Domain Dedication.

Public Domain Dedication

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].