USING JYTHON TO PROTOTYPE AND EXTEND JAVA

2y ago
4 Views
3 Downloads
306.68 KB
8 Pages
Last View : 5m ago
Last Download : 3m ago
Upload by : Harley Spears
Transcription

USING JYTHON TO PROTOTYPE AND EXTEND JAVA-BASED SYSTEMSDale Parson, Dylan Schwesinger and Thea SteeleDepartment of Computer Science, Kutztown Universityparson@kutztown.edu, on is an implementation of the interpretiveprogramming language Python written in Java anddesigned to generate Java Virtual Machine byte codes. Itprovides easy access to compiled Java classes includingsubstantial Java libraries. It also provides many of thelibraries that are popular with programmers of C-basedPython. Python‟s object-oriented language constructssupport creation of executable specifications for objectoriented Java systems. Python‟s high-level sourcelanguage mechanisms include generic container types,meta-classes, first-class functions, closures, generators,list comprehensions, source-level reflection and run-timeinterpretation of generated source code. Consequently,Jython can serve as a powerful rapid prototyping andextension environment for Java applications andframeworks. Its integration into the Java Virtual Machineallows construction of layered systems composed ofJython and Java layers. Example systems discussed in thepaper include a prototype computer game that uses Java‟sgraphical user interface libraries, a computer musicperformance system that allows users to write instrumentcontrol code as part of a performance, a Jython-Javalayered system for analyzing audio data streams, andmultiprocessor performance benchmarks written in Jythonand Java.KEY WORDSExtension language, object-oriented software, Python,Java, Jython, rapid prototype.1. IntroductionThis report on using the Jython [1,2] implementation ofthe Python programming language [3,4] to prototype andextend object-oriented Java systems [5] grows out of fourprojects that the authors have undertaken recently. Thefirst project is a graphical game for a course in advancedobject-oriented system design that illustrates the power ofusing Jython to prototype Java applications while1Portions of this work were made possible by a PASSHE (PA StateSystem of Higher Education) Faculty Professional Development Grantfor the summer of 2010, and by an Instructional Materials Grant fromthe Kutztown University Professional Development Committee.leveraging Java‟s graphical user interface (GUI) classesand other classes from Java‟s substantial class library [6].The second project is a graphical computer musicperformance application that includes support for livecoding, an established means of electronic instrumentcontrol, that in this application relies on Jython‟s ability tocompile and run extension code at execution time. Thethird project is a benchmark program that comparesperformance of a CPU intensive task on twomultiprocessor server systems in several multithreadedprogramming languages including Java and Jython. Thefourth project is an investigation into a Java streamingaudio library that uses a Java application layer forperformance-critical audio analysis tasks and a Jythonlayer for user interface construction as well as commandline exploration of the audio library. Jython, Java and Cbased Python are open source languages with freelyavailable implementations of compilers, run-timesystems, code libraries, documentation and supportcommunities.2. Related WorkPython, Tcl and Perl are representative scriptinglanguages [7] originating in the late 1980‟s that remainpopular. Interpreted languages such as LISP, BASIC andthe assorted UNIX Shell languages exerted stronginfluences on the creation of these languageenvironments. Emphases in using these languages includeinterpretation and interactive coding, dynamic typing,high-level container types such as associative arrays, andavailability of an eval function that can interpretgenerated code at run time. They provide mechanisms foreasily invoking and coordinating (a.k.a. “gluingtogether”) compiled executable programs written insystem programming languages such as C, C and Java.Python and Tcl go further in serving as extensionlanguages for application frameworks. While all scriptinglanguages provide means for dispatching and coordinatingother programs, an extension language provides softwareinterfaces whereby a developer can load compiledlibraries into the extension language interpreter at runtime and create application-specific commands for theoperations of those plug-in libraries. Scripting languagessupport program-level granularity in integrating system

programs and other scripts into aggregate programs;extension languages go further in supporting commandlevel extension of the language itself via an extensibleinterpreter and dynamic loading of compiled procedures.An extension language caters to the incremental creationof a domain-specific language via the loading of domainspecific command libraries into the language interpreter.Extensionlanguagescript plicationlibraries ncallbacks (3)Figure 1: Extension language extension mechanismsFigure 1 shows three general types of extension supportedby extension languages; arrows show direction ofprocedure invocation. With category 1 the programmercreates extension language procedures that invoke built-inlanguage mechanisms. With category 2 the programmerextends the language‟s set of built-in commands viadynamic loading of compiled, plug-in extensions.Category 3 consists of callbacks from built-in or plug-inprimitives to code written in the extension language. Forexample, handlers for events triggered in the compiledsystem programming language can be written in theextension language. An example usage of a category 3mechanism is the coding of event handlers for graphicaluser interface (GUI) events in the extension language.3. Jython in Java-based systems3.1 The Jython language and librariesJython is an implementation of the Python languagespecification written in Java. Python is a dynamicallytyped, interpreted language. The Jython interpretercompiles Python source code into Java byte code atruntime. The Java Virtual Machine (JVM) sees the sameresult from compilation of either Java or Jython – JVMbyte code. However, the code generated from the Jythoninterpreter will in most cases contain more instructionsthan a semantically equivalent block of code compiledfrom Java. This is because dynamically typed languagesput off the type decisions until runtime. Statically typedlanguages handle type checking at compile time,eliminating some instructions that are otherwise necessaryto handle type checking at runtime.The Jython distribution includes the core Python libraries.This inclusion allows for most existing Python code torun on the Jython interpreter with no modification. Thereal power of Jython over the official C basedimplementation is the ability not only to leverage existingPython libraries, but also existing Java libraries as well.The mechanism that achieves this integration of Pythonand Java code is the import statement. Unlike Java'simport statement, which is a compiler directive, Jython'simport statement is an expression that imports at runtime.Any compiled Java class is available to Jython. Duringthe import process Java reflection accesses classinformation. Reflection is the examination of objects atruntime to determine all pertinent information, such asdata members and methods. As Jython imports Javaclasses, it converts Java data types into their equivalentJython types. Writing custom Java code for a Jythonframework is far less painful than writing C or C codefor C-based Python. While the latter activity requireswriting low level adapter functions in the systemprogramming language, integrating custom Java code intoa Jython framework is accomplished via a simple importstatement.Moreover, the integration infrastructure between Java andJython also enables utilizing Python modules from withinJava code. The most common method to accomplish thisis to create a factory method in the Java code that createsan instance of the Jython interpreter, which is itself a Javaclass, to load the Python module. The module can then beconverted from a Python object to a Java object. Thisintegration requires a small amount of effort, but again itis not nearly as troublesome as writing the convolutedwrapper code that is necessary for other extensionlanguage implementations in accomplishing similar tasks.3.2 Rapid prototyping for Java-based systemsThe key for building a prototype rapidly is the ability totranslate high level abstractions into code as effortlesslyas possible. The denser the code can be, i.e. the fewerlines of code required to express a concept, the easier it isto create a prototype quickly. A language that does notrequire much boiler plate code and has high levellanguage abstractions is an ideal candidate for rapidprototyping.Python is such a language. The syntax of Python issimilar to pseudo code, and it is a fairly concise sourcelanguage. Python is an object-oriented language in thesense that every data type is an object and all of thestandard object-oriented abstractions are present,including inheritance and polymorphism. In the case ofJython, these mechanisms offer an easy way to mirrorJava class hierarchies. This makes it fairly easy to create aprototype in Jython, and once the high level designdecisions are ironed out, the classes can easily betranslated into Java equivalents.

Python offers more than just the object-orientatedabstractions. It also has implicitly polymorphic built-incomplex data types, primarily container objects that cancontain heterogeneous data, primitive and aggregate alike.Container types include sequences, associative arrays, andsets. Having these aggregate types built into the languagewith a compact syntax increases the density of the sourcecode. For example, the following line of Python code setsthe variable a to refer to a list that contains a string, aninteger, and an associative array that maps a string to astring.a ["Don't panic!", 42, {'key' : 'value'}]While the preceding code could be duplicated in Java, itwould require much more effort than the single line ofPython code.Python also supports powerful functional programmingfeatures that increase the conciseness of the sourcelanguage and ability to abstract at a high level. The corefeature that supports functional programming is thatfunctions and closures are first-class objects. This meansthat a function can be passed as an argument to anotherfunction, a function can be returned from a function, and afunction can be stored in a variable. A closure is a firstclass function that has one or more free variables boundwithin its lexical environment. Closures can be used asstateful objects. A full discussion of closures is beyondthe scope of this paper. The language also supportslambda expressions and has higher-order functionsincluding map, filter, and reduce, for building andapplying composite functions, which are typical higherorder functions in functional programming languages.Another advantage of using Jython is the fact that thecode base is compact and readable, making it easy forsomeone new to the project to use. Had we asked foroutside advice on some aspect of the game play, anyonewith the ability to read pseudocode could have understoodwhat was going on. Likewise, there is very little overheadin bringing someone new to the project up to speed.Many of the benefits above would have been apparent inpure Python as well. One advantage to using Jython isthat we were able to add a GUI by leveraging the JavaSwing library without rewriting any of the code in Java.In that sense, our prototype was not just a proof-ofconcept – we were able to use it to complete a finishedlooking game. The syntax for using Java objects in Jythonis actually more concise than it is in Java. Thus, the GUIcode looks a lot cleaner in Jython.Normally, the trade-off for using a dynamically typedlanguage is a hit to performance due to runtime typechecking. In this particular application performance wasreally not an issue because most of time the game is justwaiting for player input. There is another trade-off in thetesting phase as well. A statically typed language willcatch most type errors at compile time. Programs writtenin dynamically typed languages need to be morethoroughly tested to ensure that all expressions areproviding the correct types. The game is a relatively smallamount of code, so in this case the additional testing wasalso small.Most interpreted languages have the ability to invoke theinterpreter during runtime. Python is no exception. APython program can call the interpreter via eval and exec.When eval is called on a string that is a valid Pythonexpression, the code is evaluated and the result of theevaluation is returned. The exec statement is similarexcept any arbitrary string of Python code is executed asif it actually appeared in place of the exec statement. Execcan compile Python class and function definitions for laterexecution as byte code. Eval and exec allow for powerfulmetaprogramming capabilities.3.3 Example prototype: successes and pitfallsIn designing a graphical game, we considered a number ofpossibilities for game play that we could not effectivelyevaluate without playing the game. Rapid prototyping inJython let us quickly get to the point of testing the gameto make sure that we were on the right track. Had we notused Jython, it is possible that we would have needed tomock up a physical game first. But with Jython, we wereable to develop the code and the game play mechanicssimultaneously.Figure 2: A Java-based graphical, interactive game in JythonThe main pitfall when designing the game wasremembering that certain Java types are converted intoJython types. In most cases a given Java object is usedlike any Jython object, for example using dot notation tocall methods. But in the case of strings, the conversion

results in a Python string. So, instead of callingstring.length() as is done if the object behaved like a Javaobject, it must be called as len(string) as in the Pythonsyntax.3.4 Jython as an application-specific languageThis section outlines a project that includes support forlive coding, an established means of electronic musicalinstrument control, that in this application relies onthe previous game example, this application uses JavaGUI library classes to create the display. It also providesevent-handling classes written in Jython that extendJava‟s event-handling interfaces. These Jython subclassesof Java interfaces react to user actions and periodic timerevents.Each row in the GUI configures performance data for oneof 16 MIDI channels in the sound synthesizers. Eachchannel controls one instrument voice. Graphical controlsFigure 3: Graphical User Interface for a Jython / Java Just-Intonation KeyboardJython‟s ability to compile and run extension code atexecution time. The need for this application grew out ofa desire to perform synthesized computer music using justintonation, an approach to musical scale construction thatdiffers from the twelve-note equal-tempered scale used inmost modern Western music [8]. The primary nongraphical mechanisms of this program are construction oftables of per-note frequency information from scaleparameters, followed by translation of these frequencytable entries into ordered pairs of note number and pitchbend values for the Musical Instrument Digital Interface(MIDI) protocol [9] used to control music synthesizers.Table construction and translation occur at start-up timeusing the functional programming mechanisms of Pythonto convert custom scale parameters to frequency values,and then to convert these frequencies to MIDI values.Java‟s extensive code library contains packages for thecontrol of MIDI-based synthesizers, including a largearray of built-in instrument voice generators. Java‟s MIDIlibrary can also control software synthesizers from thirdparty vendors as well as external hardware synthesizers.Figure 3 is a screenshot of the application‟s graphical userinterface, constructed after data table completion. As withinclude buttons that play notes when pressed, spinnersthat adjust the octave, volume and instrument voice of arow‟s notes, and combo boxes for several parametersincluding change of key and target MIDI synthesizer.MIDI design is based on the Western equal-temperedscale, so that each channel can play only one note at atime in a non-equal-tempered scale, doing so by adjustingthe pitch of an equal-tempered note using MIDI pitchbend messages. This note-at-a-time constraint, known asmonophonic synthesis, makes it impossible to play chordsand multi-voice parts using standard musical keyboardtechniques. Also, a computer mouse supports pressingonly one button at a time. Partial solutions to thismonophonic constraint include the checkboxes to the leftof the note buttons for latching key presses, therebysustaining notes, along with a 16 x 4 crossbar ofcheckboxes that allow a button press on a single row to beused in all other rows connected to that row via a crossbarcolumn. The crossbar enables the simultaneous soundingof chords by multiple synthesized instruments, i.e.,polyphony.

The most important aspect of this musical interface forthe current discussion is the use of live coding, wherebyperformers can write and modify snippets of stylizedPython code at performance time to control a row of GUIcontrols in Figure 3 in a way that is reminiscent of aplayer piano.def genfunc(s): # „player piano‟ for MIDI channelimport randomdef r(lower, upper):return random.randint(lower,upper)dur 1.0de 1s.p 80s.o -1while True:s.b2 duryield deif r(0,3) 0: # do this 25% of the times.b3 duryield des.b1 duryield des.b0 duryield deperiodically without losing the bindings of its localvariables. The program also supports regular Pythonfunctions that return the delay until next invocation as areturn value at the end of each invocation. The GUI eventthread invokes execution of any channel‟s activated codeat its scheduled time under the scheduling control of aJava GUI Timer object. Buttons at the bottom of theeditor window of Figure 3 allow a performer to compileand run code after an edit, to cancel an edit or todeactivate the performance code on a channel. The GUIcontrols on a row remain active for performer interactioneven when live code is scheduled for that row.class PJCheckBox(JCheckBox):def getValue(self):return self.isSelected()def setValue(self, value):issel self.isSelected()if value and not issel:self.doClick()elif issel and not value:self.doClick()v property(getValue, setValue, None)Listing 2: Java checkbox class adapted in a Jython subclassListing 1: Live Python code for controlling the GUI keyboardListing 1 shows the code for one MIDI channel thatappears in the pop-up code editor window of Figure 3when a performer clicks that row‟s “Code” button. Thiscode is pure Python, which the application interpretsperiodically under the scheduling control of the code itselfwithin the run-time context of the GUI. Listing 1illustrates that live coding can import any Python librarysuch as the random module used here. Mnemonic namesthat appear across the top row of Figure 3, such as “b2”for note button 2 or “o” for the octave spinner on the GUI,provide the symbols through which a performer controlsthe GUI via live coding. While a name such as “b2” doesnot appear to be mnemonic, the use of terse names allowsfor minimal typing when using the pop-up text editorwindow during a performance. The appearance of theseterse names above their respective columns in the mainGUI window makes memorizing them unnecessary.The code in Listing 1 sets values for local variables durfor duration and de for delay in temporal units scaledaccording to a tempo initialization parameter. It sets thechannel‟s instrument voice spinner to MIDI “patch”number 80 via “s.p 80” and sets the octave spinnerusing the “s.o -1” assignment. The “s” symbolrepresents the sc

Python also supports powerful functional programming features that increase the conciseness of the source language and ability to abstract at a high level. The core feature that supports functional programming is that functions and closures are first-class objects. This mean

Related Documents:

programming language and the basic concepts of computer programming, including command-line processing. To fully utilize Jython's features you should also be familiar with the basic concepts of object-oriented programming. It will also be helpful to have a working knowledge of the Java platform, as Jython runs on a JVM;

Programming Languages Groovy, JavaScript, JRuby (Ruby), Jython (Python), and Kotlin Floid R. Gilbert & David B. Dahl Abstract The R package jsr223 is a high-level integration for five programming languages in the Java platform: Groovy, JavaScript, JRuby, Jython, and Kotlin. Each of these languages can use Java objects in their own syntax.

asynchronous application framework –Floodlight incorporates a threading model that allows modules to share threads with other modules –Floodlight is Java/Jython centric Jython: Pyt

prototypes using three different tools: Microsoft Visual Basic, Macromedia Flash, and Macromedia Dreamweaver. It begins by giving a quick introduction to building a prototype, discusses selecting the correct tool, and then proceeds to give a step-by-step guide to creating a prototype using each of the tools. The prototype being built will be a

work/products (Beading, Candles, Carving, Food Products, Soap, Weaving, etc.) ⃝I understand that if my work contains Indigenous visual representation that it is a reflection of the Indigenous culture of my native region. ⃝To the best of my knowledge, my work/products fall within Craft Council standards and expectations with respect to

action matching in long video sequences. During training, an action prototype tree is learned in a joint shape and motion space via hierarchical K-means clustering and each training sequence is represented as a labeled prototype sequence; then a look-up table of prototype-to-prototype distances is generated.

Plaintiffs, Prototype Productions, Inc. and Prototype Productions Incorporated Ventures Two, LLC (collectively "Prototype"), brought a claim against Defendant Reset, Inc. ("Reset") alleging patent infringement under 35 U.S.C. § 271. On May 31, 2011, Defendant filed a Motion to Dismiss for Lack of Jurisdiction or Transfer.

ager. Even the term "prototype" is likely to be ambiguous on such a team. Everyone has a different expectation of what a prototype is. Industrial designers call a molded foam model a prototype. Interaction designers refer to a simula-tion of on-screen appearance and behavior as a pro-totype. Programmers call a test program a proto-type.