PyjamaSamples

From IPRE Wiki

Jump to: navigation, search

This page provides some examples of using Pyjama.

Contents

Pyjama GUI

Enlarge

The Pyjama Window is divided into three sections:

  1. Editor
  2. Output
  3. Interactive Command Box


The Editor allows you to edit files. From the editor, you can select a section of code and press F5 to run part of your script interactively. If you don't have anything high-lighted, pressing F5 will run the entire file.

In the interactive command box, you can enter as much code as you like, using CONTROL+ENTER to give you a new line. ENTER will evaluate the code in the box.

Examples

Python and Ruby

Pyjama allows you to easily jump back and forth between languages. Enter this code in the command box:

def fib(n):
    if n <= 2: return 1
    return fib(n - 1) + fib(n - 2)

Make sure the bottom status line has Python checked, and press the Run! button (or simply hit <enter> when in the command box).

Now, enter this code in the command box:

class Hello_world
  def greet
    puts "Hello World"
  end
end
hw = Hello_world.new
hw.greet

Cross-language Access

In this example, you'll see that you can access code and variables defined in other languages.

Enter the following in the Interactive Command Box:

class PythonClass:
     def hello(self, value):
         print "Python says hello to", value

This defines a PythonClass. To test it out, enter the following in the command box:

pc = PythonClass()
pc.hello("Python")

In the Output Box, you'll of course see:

Python says hello to Python

Now, switch to Ruby mode (bottom line status bar, select Ruby) and enter this in the command box:

pc = python_class().new
pc.hello "Ruby"

This uses the PythonClass (now called python_class). In Ruby, you can create an object by placing the .new after it. You call the method .hello by placing the arguments after the method name (parens are not needed in Ruby).

You will see in the output box:

Python says hello to Ruby

which is Python code running inside Ruby!

While in Ruby Mode, enter:

class Ruby_class
   def hello caller
     puts "Ruby says hello to #{caller}"
   end
end

To see that work, try:

rc = Ruby_class.new
rc.hello "Ruby"

and you'll of course see:

Ruby says hello to Ruby

Finally, switch back to Python Mode, and enter:

import Ruby_class
rc = Ruby_class()
rc.hello("Python")

and you'll see:

Ruby says hello to Python

which is Ruby code running in Python!

Graphics

This example uses a graphics library written in Python. You'll need the file ipgraphics.py. Save it in a file of that name, and put it in the same directory as this file, named starburst.py:

# file: starburst.py
from ipgraphics import *
import random
win = Window(width=200, height=200)
win.autoflush = False
for radius in range(win.width/2, 0, -10):
    color = random.choice(colors)
    for i in range(radius * 2):
        p = Point(win.width/2 - radius + random.random() * 2 * radius, 
                  win.height/2 - radius + random.random() * 2 * radius)
        line = Line(Point(win.width/2, win.height/2), p)
        line.setOutline(color)
        line.draw(win)
win.update()

If you load this into Pyjama editor and press F5, it should produce something like the following picture:

Image:Pyjama-starburst.gif

Using IPGraphics from Ruby

This example combines the above examples: using ipgraphics with the cross-language capabilities.

In Python mode, run the ipgraphics.py program:

  1. Open the ipgrapics.py in Pyjama
  2. Press F5 (or click on the green triangle)

Now, go into Ruby mode (press Control+2) and enter:

win = self.Window.new
line = self.Line.new(self.Point.new(1,1),  self.Point.new(100, 100))
line.draw win

You should see as output the following text and window:

---- Ruby Mode ----
>>> win = self.Window.new
=> IronPython.NewTypes.System.Windows.Forms.Form_1$1, Text: Graphics Window

>>> line = self.Line.new(self.Point.new(1,1),  self.Point.new(100, 100))
=> IronPython.NewTypes.System.Object_2$2

>>> line.draw win
=> nil

Image:Ipgraphics.gif

Interacting with .NET

The languages of Pyjama can also interact with .NET/Mono. If you are already familiar with .NET/Mono programs (such as those written in C# or Visual Basic) then you will recognize some of these idioms and names.

As a simple example, let's use the interactive abilities to directly interact with some Windows Forms. In order to do this in Pyjama, will turn the threaded ability off, so as to talk to the GUI in the same thread as Pyjama is running in. Let's create a window (called a Form in .NET parlance):

# file: winform.py
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import *
import pyjama
pyjama.Threaded = False
win = Form()
win.Show()

Importing the special module named clr is the gateway to interacting with the underlying .NET/Mono subsystem. From the clr module, you can add a reference to any Dynamic Link Library (DLL) so that you can access library functions and objects. In this example, we add a reference to the System.Windows.Forms DLL which contains a library of Graphical User Interface (GUI) objects. We then can create a Form, save it in a variable named win, and call the form's Show() method.

If you'd like to give the window a name, enter this in Pyjama's interactive command box:

win.Text = "This is the Window Title"

These windows will go away when you close Pyjama, or you can close them manually.

Developing a Language for the DLR in the DLR

Python and Ruby are defined to interact as above by taking advantage of the Dynamic Language Runtime (DLR). To further show this off, here is an example of defining a new language, much like Lisp, called SymPL written in Python using the tools of the DLR:

  1. lexer.py - the lexer
  2. parser.py - the parser
  3. etgen.py - expression tree generators
  4. runtime.py - runtime helpers (e.g., lookup, import)
  5. test.py - the Read Eval Print Loop

Some sample SymPL programs:

  1. test.sympl - function tests
  2. ops.sympl - operator tests
  3. lists.sympl - list tests

Writing Fast Module Code Once

Perhaps the nicest aspect of the DLR is that you can develop code (in CSharp, for example) that is "compiled" and used directly. Such can be imported and used by any language in the DLR suite as if it were written in that language. For example, consider this simple example:

// file: Testing.cs
public class Point {
  private int _x;
  private int _y;
  public Point(int x, int y) {
	_x = x;
	_y = y;
  }
  public int x {
	get { return _x; }
	set { _x = value; }
  }
  public int y {
	get { return _y; }
	set { _y = value; }
  }
  public string __repr__() {
	return "<Point at (" + _x + "," + _y +")>";
  }
}

This can be compiled in Mono:

gmcs Testing.cs -target:library

or put into a Project in Visual Studio, and compiled into a library.

You should then put the resulting Testing.dll somewhere will Pyjama can find it (either put in the same directory with the pyjama.exe, or put it in a directory and add the path of the directory to Python's sys.path):

import sys
sys.path.append("/path/to/folder")

Finally, you can use it in Pyjama:

>>> import clr
>>> clr.AddReference("Testing")
>>> import Point
>>> Point
<type 'Point'>
>>> p = Point(1,2)
>>> p
<Point at (1,2)>
>>> p.x
1
>>> p.x = 9
>>> p
<Point at (9,2)>
>>> 

Creating Interactive Programs and Graphics for the Web

Once you have a working program in Pyjama, you can save the program to run on the Web in a manner very much like Flash applications. The resulting program can run in a browser using Silverlight/Moonlight. Example coming soon!

Personal tools