Glue It All Together With Python

    Glue It All Together With Python

    Guido van Rossum
    CNRI
    1895 Preston White Drive
    Reston, VA 20191
    Email:
    guido@cnri.reston.va.us,guido@python.org

    Position paper for theOMG-DARPA-MCCWorkshop on Compositional Software Architecture inMonterey, California, January 6-8, 1998.

    Introduction

    Python is an advanced scripting language that is being usedsuccessfully to glue together large software components. It spansmultiple platforms, middleware products, and application domains.Python is an object-oriented language with high-level data structures,dynamic typing, and dynamic binding. Python has been around since1991, and has a very active user community. For more information, seethe Python websitehttp://www.python.org.

    Like Tcl, Python is easily extensible with C/C++/Java code, andeasily embeddable in applications. Python even uses Tk, the Tcl GUItoolkit, for a de-facto standard portable GUI toolkit. Unlike Tcl,however, Python supports object-oriented programming. Pythonprogrammers can create classes, use multiple inheritance, definemethods, overload operators, and so on.

    Python's Strengths

    Syntactically, Python code looks like executable pseudo code.Program development using Python is 5-10 times faster than usingC/C++, and 3-5 times faster than using Java. In many cases, aprototype of an application can be written in Python without writingany C/C++/Java code. Often, the prototype is sufficiently functionaland performs well enough to be delivered as the final product, savingconsiderable development time. Other times, the prototype can betranslated in part or in whole to C++ or Java -- Python'sobject-oriented nature makes the translation a straightforwardprocess.

    The best approach is often to write only the performance-criticalparts of the application in C++ or Java, and use Python for allhigher-level control and customization. There are several anecdotesabout applications that started out as pure C++ code to which Pythonwas added as an extension language, where in each new version thepercentage of the application written in Python increased, while alsoincreasing the overall performance, functionality and reliability ofthe application. (E.g. Case Study: Python in a CommercialEnvironment, by Greg Stein, Microsoft, inProceedingsof the 6th International Python Conference, and theAlice VR project at UvA and CMU.)

    Python has a strong presence on the web. It is suitable for CGIprogramming (on all platforms: Unix, Windows and Mac); there areinterfaces to all major commercial databases. Python has a librarythat interfaces to the main Internet and web protocols, and has HTMLparsing and generation toolkits. Python was a major implementationlanguage forInfoseek when theywere smaller. At least one company (Digital Creations) is selling a suite of server side tools usingPython. And finally, Python has been used to implement a web browser(Grail).

    Python is also well represented in the distributed systems world.It is one of the main languages supported by Xerox PARC'sILU(Inter-Language Unification; a CORBA compatible distributed objectsystem), and many distributed applications have been built in Pythonusing ILU. Python is also used by theHector project at theUniversity of Queensland, Australia.

    Finally, Python is well integrated with the Windows platforms.Python programs can interact with COM and DCOM services, and can evenimplement new COM and DCOM services (which is not possible usingVisual Basic!). Python can also be used as a scripting engine inMicrosoft's Active Scripting architecture.

    Using Python as an Integration Language

    Relevant to the topic of this workshop, Python is in use at manyplaces as an integration language, used to glue together ("steer")existing components. The strategy here is to create Python extensionmodules (written in C/C++) that make the functionality of largecomponents written in C/C++ available to the Python programmer. Theextension ("glue") modules are required because Python cannot callC/C++ functions directly; the glue extensions handle conversionbetween Python data types and C/C++ data types and error checking,translation error return values into Python exception.

    Creation of glue extensions is simplified by the existence ofSWIG, which readsheader files containing function and method prototypes andautomatically generates the necessary type conversion and errorchecking code. In situations where the underlying code (usually Ccode) doesn't use an object-oriented model, the glue extension can inturn be wrapped in a Python module that defines a proper classhierarchy, while delegating the performance critical operations to theC code.

    Using Python, better applications can be developed becausedifferent kinds of programmers can work together on a project. Forexample, when building a scientific application, C/C++ programmers canimplement efficient numerical algorithms, while scientists on the sameproject can write Python programs that test and use those algorithms.The scientist doesn't have to learn a low-level programming language,and the C/C++ programmer doesn't need to understand the scienceinvolved.

    Without Python, large amounts of C/C++ code often have to be writtenjust to provide a flexible enough input mechanism so that scientistscan feed the program its data, in all the variantions that arerequired for reasons of experimental setup (for instance). WithPython, Python can be used to wrote a much more flexible inputmechanism in a much shorter time, or Python itself can be the ultimateflexible input mechanism. As an extreme example, Lawrence LivermoreNational Laboratories is using Python to eventually replace ascripting language (BASIS) that was developed in house for the samepurpose; BASIS started out as a simple input mechanism for Fortranprograms, and gradually acquired many features of scripting languages(variables, conditionals, loops, procedures and so on) with increasingawkwardness.

    Because Python has existing interfaces to so many differentcomponents in very different application domains, Python is ideal foroddball integration tasks. It can link a commercial database tonumber-crunching code; it can add a graphical user interface to anetwork management tool; it can send email from a virtual realityapplication.

    Conclusion

    Python can fulfill an important integration role in the design oflarge applications with a long life expectancy. It allows a fastresponse to changes in user requirements that require adapting thehigher-level application logic without changing the fundamentalunderlying components. It also allows quick adaptation of theapplication to changes in the underlying components.


    Epilogue: Python and Java Integration

    A new Python implementation written in 100% Pure Java, dubbedJPython, is currently underdevelopment; alpha releases are available for evaluation. JPythonoffers seamless scripting for Java. It is a full implementation ofthe Python language and standard library, adding direct access to theuniverse of Java classes. Java code can also use Python classes --this is important for callbacks, for instance.

    The main thrust for JPython is that it does for Java what Pythonalready does for C and C++: to present programmers with more optionsin the trade-off between development time and execution time, byproviding a more dynamic, more expressive alternative. JPython'sintegration with Java is superior to Python's integration with C/C++:due to Java's Reflection API, JPython can use arbitrary Java classeswithout the help of a wrapper generator such as SWIG. (C/C++ codemust first be made available to Java through the Java native codeinterface; once it is callable from Java it is callable from JPython.)