domingo, 3 de abril de 2016

Integrating Python with other languages

The normal Python distribution uses the C language and is often referred to as CPython. But there are other implementations of the Python language in other languages. These non‐C based interpreters facilitate integration of the other language with Python. Two of the best known of these alternate Python versions are Jython, written in Java, and IronPython, an implementation for Microsoft’s .NET environment. A third alternative is Cython, which is not strictly an implementation of Python but a very closely related subset that can be compiled into C to provide very fast performance while still providing the speed of development of a Python‐like language.

Jython
The Java implementation of Python offers many advantages to Java programmers looking for an interactive environment in which to test their Java classes or to build prototype solutions that can, if necessary, be converted to full Java later. The distribution includes both an interpreter and a compiler. The interpreter comes with the familiar interactive prompt, as well as the ability to run scripts directly. In addition to importing Python modules (including many of the regular Python standard library modules), Jython can import Java libraries, making the classes available to the Python interpreter as if they were regular Python classes. This makes it possible to exercise and test new Java classes interactively at the Jython prompt. Jython also enables dynamic prototyping of solutions by mixing Java and Python code together. The interpreter can also be used to run script files with all of these same features for bigger projects or prototypes. The compiler takes Jython code (either pure Python or a mixture of Java classes and Python code) and compiles it into a '.java' file. This is a powerful tool for prototyping new classes because they can be developed and written in Python, compiled, and included in Java code. Once proven, the Python version can be seamlessly replaced with a pure Java version. The downside of Jython is that it tends to produce slower code than pure Java and is also more memory hungry. This is largely due to the fact that the compiler effectively embeds a Python interpreter in the output files.

IronPython
IronPython is a version of Python written for the Microsoft .NET framework. 
.NET is not a single language system; rather, it depends on a common bytecode to which several languages can be compiled. The modules so produced can then be shared between languages. Thus, code written in IronPython can import modules written in C#, C++, Visual Basic, and several other .NET compatible languages. Similarly, IronPython modules can be imported by any of those other .NET languages. IronPython is an extremely appealing prospect for developers working on the .NET platform. 
Better still is the fact that an open source variant of .NET called Mono has been produced that can run under Linux and Mac OS X and many others, including mainframe computers and games consoles. This is achieved while maintaining binary‐level compatibility with the Microsoft .NET implementation. As .NET becomes the de facto standard for building applications on Microsoft Windows, the availability of Python within that framework is a major boon for Python programmers. The IronPython implementation supports most of the standard Python library as well as the .NET module system. Modules in .NET are called assemblies, but they are imported into IronPython in exactly the same way that ordinary Python modules are imported. Some issues exist due to the dynamic typing used by Python and the .NET type system, which is more static in nature. However, once understood these can be worked around using some helper features built into IronPython. Full documentation is provided on the IronPython in documentation.

Cython
Cython is significantly different from the other language integration options discussed here. It is, in effect, a separate language from Python but is highly compatible with it, describing itself as a super‐set of Python. This means Python programmers can easily learn Cython and take advantage of its special features. So what are these features that would make you want to use Cython? In short, speed. Cython is a compiler that produces C code that, in turn, can be compiled to native machine code and thereby has the potential to run much faster than its Python equivalent. This compiled code can then be imported back into regular Python just like any other module to provide the best of both worlds — easy Python development combined with C‐level speed of execution.

Oracle Data Integration

What's ?
Oracle Data Integrator is a comprehensive data integration platform that covers all data integration requirements: from high-volume, high-performance batch loads, to event-driven, trickle-feed integration processes, to SOA-enabled data services. This tool is used in BI and Big Data projects for extract-transformation-load (ETL). For more information, learn in this documentation.

Why Jython?
As mentioned above Jython (Java version of Python, or better, Python under Java) is an object-oriented scripting language. Jython scripts run on any platform that has a java virtual machine (JVM). Oracle Data Integrator includes the Jython Interpreter within the execution agent. The agent is able to run script written in Jython.

Oracle Data Integrator users may write procedures or knowledge modules using Jython, and may mix Jython code with SQL, PL/SQL, OS Calls, etc. Thanks to Jython, the programming capabilities of Oracle Data Integrator are dramatically increased. It is possible to perform complex processing with strings, lists, "dictionaries", call FTP modules, manage files, integrate external Java classes, etc. 

Note: To use Jython code in KM (Knowledge Management), procedure commands or procedures commands, you must systematically set the technology to Jython.

How to do it? (Using the Jython interpreter)
Jython programs can be interpreted for test purposes outside of Data Integrator, using the standard Jython interpreter. 

>> Start the Jython interpreter:
1. Start an OS prompt (console)
2. Go to the /bin directory.
3. Key in: jython
4. The interpreter is launched

>> Exiting the Jython interpreter:
1. Hit Ctrl+Z (^Z), then Enter
2. You exit the interpreter

>> Running Jython scripts:
1. Go to the /bin directory.
2. Type in: jython <script_path.py>
3. The script is executed

>> Using Jython in the procedures:
All Jython programs can be called from a procedure or a Knowledge Module.

>> Create a procedure that calls Jython:
1. In Designer, select a Folder in your Project and insert a new Procedure
2. Type the Name of the procedure
3. Add a command line in the Detail tab
4. In the command window, type the Name for this command
5. In the Command on Target tab, choose the Jython Technology from the list
6. In the Command text, type the Jython program to be executed, or use the expression editor
7. Click OK to apply the changes
8. Click Apply to apply the changes in the procedure window
9. In the Execution tab, click the Execute button and follow the execution results in the execution log.

The procedure that was created with this process can be added to a Package like any other
procedure.

Some Examples

>> Using FTP:
In some environments, it can be useful to use FTP (File Transfer Protocol) to transfer files between heterogeneous systems. Oracle Data Integrator provides an additional Jython module to further integrate FTP. The following example show how to use this module. Pull the '*.txt' files from '/home/odi' of the server ftp.myserver.com into the local directory 'c:\temp'.



>> Using JDBC:
It can be convenient to use JDBC (Java DataBase Connectivity) to connect to a database from Jython. All Java classes in the CLASSPATH can be directly used in Jython. The following example shows how to use the JDBC API to connect to a database, to run a SQL query and write the result into a file. The reference documentation for Java is available at http://java.sun.com



Others code samples about it, are available on my GitHub.

Thank you, see you in the next post!

Nenhum comentário:

Postar um comentário