PyNSource - Java and Delphi code generation

The pynsource command line tool can generate java and delphi skeleton code from python source code, for the purpose of importing (e.g. Java source code) into other UML tools - which might have better layout and other features.


PyNSource can convert your python source into java files (just the class declarations of course, not the meat of the code) and thus you can import the resulting java files into a more sophisticated UML modelling tool that understands java.  There you can auto-layout your  UML and you can print out.or take screen shots of the resulting diagrams.

You could even write a script to automate the java code generation.  Thus when you fire up you Java based UML modelling tool and do an import, you only import the changes to your diagram and you don't lose the custom way you have laid out your diagrams.

PyNSource can also generate Delphi (object pascal) code.


Here we will convert a single python file into a number of java files.  Java likes to have one class per file, whereas python doesn't mind if you have multiple classes in one file.

1. Input: python code

Assuming the following python code

class ParseMeTest:
def __init__(self):
self.a = 10 # class attribute
self.b = Blah() # class attribute COMPOSITE
self.a.c = 20 # skip
self.__class__.d = 30 # static class attribute
self.e.append(10) # class attribute (list/vector)
self.f.append(Blah()) # class attribute (list/vector) COMPOSITE


    def IsInBattle(self):
if not self.tileinfo:
return 0
return self.tileinfo.battletriggered


    def DoA(self):


class ParseMeTest2(ParseMeTest):
def DoB(self):
self._secretinfo = 2

lives in a file called on my desktop in a folder called c:\stuff, it can be turned into java code with the following command:

2. Run the conversion

CMD> \python22\python.exe \wherever\ -j c:\stuff c:\stuff\

3. Output: java source files

The resulting files will be:


The latter files (dict, list, unittest, variant and object) are always generated as a matter of course, just in case you need to compile your java code and your generated java code refers to these classes.  For example, if your python code has a class inheriting from class object, then the above file will satisfy the java compiler wanting to find that class.

TIP on Missing or Built-in Java Classes: If there are other missing java classes you may need to add them yourself - just copy and edit it to match the class that is missing.
Ideally pyNSource should generate java equivalents to all python classes referred to by your source code - at the moment it only generates the obvious ones e.g. to coresspond to python's dict class.  Note however, that when using the GUI front end to PyNSource, any needed classes are generated on the fly and a UML picture of those classes are included in the final diagram.

4. Output: java source files - contents of which are...

Java code generated (one class per file):

// Generated by PyNSource  


public class ParseMeTest {
public variant a;
public Blah b = new Blah();
public static variant d;
public variant e;
public Blah f = new Blah();
public void __init__() {
public void IsInBattle() {
public void DoA() {


// Generated by PyNSource 


public class ParseMeTest2 extends ParseMeTest {
public variant _secretinfo;
public void DoB() {


// Generated by PyNSource 

public class Blah {
public void __init__() {


public class variant {




Importing the generated java code into a UML tool

Just import the resulting .java files into something like Enterprise Architect (commercial) or ESS-Model (free).

Enterprise Architect 

Enterprise Architect  importing java files (see above, which was once Python code)

note in the above diagram the phrase 'Blah b = new Blah()' is not strictly UML and this seems to be a fault with the version of Enterprise Architect.  Please contact me with any free UML tools that I can list here for people to use.

LATEST TIP for big images in Enterprise Architect.

Export the entire UML image as EMF format, then open in Windows XP 'picture and fax viewer' (usually the default viewer for most documents.  This will create a reasonable sized huge image which you can zoom into.

Additional step:  If you want a small size, zoomable pdf, and have Adobe Acrobat installed, print to Adobe pdfWriter from within the Windows XP 'picture and fax viewer', selecting "Full page fax print' within the print wizard.

and you will end up with a pdf file of the entire, huge image, which you can zoom cleanly (vector graphics, no chunkiness) and pan over at your lesiure.  Or put it in a web page and let all your developers have easy access to it.


ESS-Model is free and very fast.  It has a pretty good layout algorithm too, plus an overview window fo large scale navigation of your models.  It can also export as XMI.


So the idea with a tool like this is to convert your python source into java files and import them into this UML tool so that it can auto-layout the UML and you can print out.or take screen shots of the resulting diagrams.

Of course you can instead use the supplied free tool which does the same thing, though the layout algorithm is not as nice and there is no overview window.

Jbuilder - Analysing Jbuilder Projects

Imagine you want to process a bunch of python code and emit java source code, then run jbuilder X Enterprise and use the UML reverse engineering features to visualise your python code.

Consider some folder e.g. .../Tests/ and create a jbuilder project based off there called PythonToJavaTest01 which will actually create a folder called PythonToJavaTest01 plus subfolders called src and classes etc. 

The Borland project file will also live in .../Tests/PythonToJavaTest01/ with a name PythonToJavaTest01.jpx

Run pynsource so that it dumps the output into the src folder  e.g. assuming the batch file is in the PythonToJavaTest01 folder and the python source is in   .../Tests/PythonToJavaTest01/pythoninput01 then the command is

 pynsource -j src pythoninput01\*.py

Jbuilder X Enterprise 

Borland's Jbuilder enterprise has a UML reverse engineering feature, though it doesn't combine multiple classes onto the one diagram (though it does show refrences to other classes on the one diagram).  You can use it free for 30 days or purrchase it.

PyNSource - batch file usage examples

You can automate the java code generation by putting often used the invocations of pynsource into a batch file, for each of my commonly used projects. e.g.

@echo OFF

set REL="\python22\Lib\site-packages\pynsource\"
set MAPEDITOR=c:\cc\Devel\MapEditor\*.py c:\cc\Devel\MapEditor\Model\*.py c:\cc\Devel\MapEditor\Gui\*.py c:\cc\Devel\MapEditor\Reactor\*.py

%REL% -v -m -j "C:/CC/Devel/UML diagrams/Feb2004_pynSource_1_1/MapEditor/src" %MAPEDITOR%

the above example creates java files out of all the python code found in the directories referred to by MAPEDITOR and places the resulting java source code into the single folder "C:/CC/Devel/UML diagrams/Feb2004_pynSource_1_1/MapEditor/src".

Delphi code generation

You can generate Delphi pascal files using the -d option instead of the -j (for java) option.  Importing into the fantastic tool Modelmaker works well.