1

JES Tutorial – I

CIS 465 Multimedia

JES (Jython Environment for Students) Tutorial -- I

General Guide to Using JES 3

More info on Python:

JES Screen:

JES interface has 2 main areas:

  • the Program Area (top part) –
  • write your Jython programs here
  • provides a text editor
  • after typing in your program, go to File > Save Program As, using a filename with extension .py (for Python) – on my version of JES (3.0.6), a window opened up displaying the file structure under My Documents on the computer—however, you can save it anywhere
  • the Command Area (bottom part)
  • can type in Jython/Python commands here at the “” prompt, hit Enter
  • can type in names of loaded programs to be run
  • commands are interpreted by Jython interpreter

Note:

Load Program Button
IMPORTANT! After saving a program created in the Program Area, use the load button to load the file that is currently in the program area so it can be recognized in the command area.

When you write or open a program in the program area, you must load it before you can call it from the command area. If you forget to load your program, JES either will not recognize your program's name, or will call the last version of your program that was loaded, without any changes you might have made since then.

Use Help Menu for more information about various topics.

Some JES commands& Python features:

Adding and string concatenation

> print 3 + 5

8

> print 2 - 9

-7

> print "Hello" + " There!"

Hello There!

Division in Python:

> print 1.0/2.0

0.5

> print 1/2

0

Some Functions in Python

> print ord("A")

65

> print abs(-5)

5

Some JES functions (NOT Python)

Picking a file in JES

  1. pickAFile() –
  2. displays list of directories and files in a window (default shows file structure under My Documents)
  3. returns a string which is the name of file that you picked
  4. this does NOT load the file into memory

E.G.s

> pickAFile()

'C:\\Documents and Settings\\Barb\\My Documents\\AAA_Student_Labs\\songs.txt'

> pickAFile()

'C:\\Documents and Settings\\Barb\\My Documents\\My Pictures\\turkey.gif' picture file

> pickAFile()

'C:\\Documents and Settings\\Barb\\My Documents\\My Pictures\\Luanna.jpg'

picture file

> pickAFile()

'C:\\Documents and Settings\\Barb\\My Documents\\Teaching\\465 Multimedia\\JES_Course Software\\MediaSources\\MediaSources\\ducks-thames.wav' sound file

Working with picture files in JES

  1. makePicture(filename)
  2. argument should be name of some type of graphics file (e.g.,. jpg)
  3. returns Picture object
  4. displays printout of information about the Picture (filename, size (height and width in pixels), etc.)
  5. does NOT display the picture

Note: can say print makePicture(filename) -- displays info as in c above.

E.G.s

> makePicture(pickAFile())

Picture, filename C:\Documents and Settings\Barb\My Documents\Teaching\465 Multimedia\JES_Course Software\MediaSources\MediaSources\butterfly.jpg height 66 width 52

> print makePicture("c:\MyMedia\MyPics\garden.jpg")

Picture, filename c:\MyMedia\MyPics\garden.jpg height 480 width 360

************************************************************************

---- IMPORTANT ----

If you hard code the name of a file as a string in a command, sometimesJES will require that you preface the string with the character ‘r’. (Strange, but sometimes you will not have to do this. )

E.G.

> sound = makeSound( r"c:\MyMedia\MySounds\aah.wav") see use of ‘r’

> play sound

*************************************************************************

  1. show(myPic)
  2. argument is a Picture
  3. causes the Picture to be shown

E.G.

> show(makePicture(pickAFile() ) )  I picked butterfly.jpg, see pic shown below

Working with soundfiles in JES

  1. makeSound(filename)
  2. arg should be name of a sound file
  3. returns a Sound object
  4. displays information about the Sound (filename, # samples, etc.)
  5. does NOT play the sound

Note: can say print makeSound(filename) -- displays info as in c above.

E.G.s

> makeSound(pickAFile())

Sound file: C:\Documents and Settings\Barb\My Documents\Teaching\465 Multimedia\JES_Course Software\MediaSources\MediaSources\c4.wav number of samples: 55125

> print ( makeSound("c:\MyMedia\MySounds\preamble.wav"))

Sound file: c:\MyMedia\MySounds\preamble.wav number of samples: 421110

  1. play(mySound)
  2. argument is a Sound
  3. causes the Sound tobe played

Using variable names and assignments in JES

E.G.s

> mynum = 10

> print mynum

10

> yournum = 12.8

> print mynum + yournum

22.8

Giving variable names to results returned from commands

> myfile = pickAFile()  using variable name “myfile”

> print myfile can use that var name in subsequent commands

C:\Documents and Settings\Barb\My Documents\Teaching\465 Multimedia\JES_Course Software\MediaSources\MediaSources\arch.jpg

______

> myfile = pickAFile()

> mypic = makePicture(myfile)  using var name “mypic” for a Picture object

> print mypic

Picture, filename C:\Documents and Settings\Barb\My Documents\Teaching\465 Multimedia\JES_Course Software\MediaSources\MediaSources\arch.jpg height 480 width 360

______

> mysound = makeSound(pickAFile())  using var name mysound for a Sound obj

> print mysound

Sound file: C:\Documents and Settings\Barb\My Documents\Teaching\465 Multimedia\JES_Course Software\MediaSources\MediaSources\croak.wav number of samples: 8808

> play(mysound)

Writing a program in Python

A program in Python is a named collection of one or more functions.

Use “def” command to define a new function

Syntax of a function:

  • The signature -- Following the key word “def” is:
  • name of the function you are defining
  • arguments (0 or more) enclosed in parentheses
  • a colon

E.g. def myfunction():

  • The body
  • all commands to be included in this function, one after the other – this defines the block of the function

IMPORTANT >

All commands belonging to the block must be indented – the use of indentation serves as definition of the scope of the block.

E.g. 1

In the top part of the JES screen, i.e., in the Program Area, type in the definition of a function:

Now,

  • go to File > Save Program As
  • Save the program, supply a name:pickAndShow.py Note the extension of py, for Python
  • With my version of JES, a window opens up showing the file structure under My Documents on my computer. But, it appears you can save the file anywhere, but be organized
  • Click on Load Program button DON’T FORGET THIS!!
  • Click in the Command Area and type the command pickAndShow() name of

function you defined

  • this causes all commands in your program to be executed, and ultimately, the picture you picked will be displayed

E.g. 2 – Hardcoding a file name as an argument in a command in your function

  • Here we hardcoded the names of the two files, one a picture file, and one a sound file.

E.g. 3 – Create a function that takes an argument, and uses comments

Here, the input to the function is a file name of a picture file

  • In the Command Area I typed:

> showNamed("c:\MyMedia\MyPics\garden.jpg")  note the filename arg used

the garden picture was displayed

E.g. 4 – Create a function that takes the name of a sound file as an argument

  • In the Command Area I typed:

playNamed("c:\MyMedia\MySounds\preamble.wav”) note the filename arg used

 the preamble was played

E.g. 5 – Create a function that takes a Picture object as argument

Here, the input to the function is a Picture object;

  • In the Command Area I typed:

> myPict = makePicture("c:\MyMedia\MyPics\garden.jpg")

> showPic(myPict)  Note the Picture arg

 the garden picture was displayed

Operators and Useful Functions in Python

+, -, *, /, ** / Arithmetic operators; normal precedence
<. >, ==. <=. >= / Relationaloperators
>, != / More logical operators – not-equal (equivalent)
and, or, not / Logical operators
int(arg) / returns the integer part of the arg
e.g. int(-8.7) is -8
float(arg) / returns the floating point version of the arg
e.g. float(3) is 3.0
e.g. float(2.7) is 2.7
str(arg) / returns the string rep of the arg
e.g. > print str(8.9)
8.9
ord(arg) / returns the ASCII rep of the char arg
e.g. > print ord('A')
65

Numeric Functions in Python

abs(), sin(), cos(), max(a1, a2), min(a1, a1), len(arg)

E.g.s

> print max(-231, -500)

-231

> print len("Jython")

6

String Methods in Python

More String info:

Assumes is a reference to a String object. The following methods can be called on s, as shown:

  • s.count(sub): returns the number of times sub appears in s
  • s.find(sub): returns the index where sub appears in the string str, or retuns -1, if not found. This method find can take an optional starting point and an optional ending point. Method rfind takes the same inputs but works right to left.
  • s.upper(), s.lower(): converts the string to all upper, lower cases, respectively
  • s.isalpha(), s.isdigit(): returns true if all the characters in the string are alphabetic or all numeric, respectively
  • s.replace(k,r): replaces all instances of “k” with “r” in the string
  • s.split(d): returns a list of subslists where the character d is the split point

Strings can be subscripted (indexed); like in C, the first character of a string has subscript (index) 0. There is no separate character type; a character is simply a string of size one. Substrings can be specified with the slice notation: two indices separated by a colon.

Assume word=”HelpA”

> word[4]

'A'

> word[0:2]

'He'

> word[2:4]

'lp'

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

> word[:2] # The first two characters

'He'

> word[2:] # Everything except the first two characters

'lpA'

Indices may be negative numbers, to start counting from the right. For example:

> word[-1] # The last character

'A'

> word[-2] # The last-but-one character

'p'

> word[-2:] # The last two characters

'pA'

> word[:-2] # Everything except the last two characters

'Hel'

But note that -0 is really the same as 0, so it does not count from the right!

> word[-0] # (since -0 equals 0)

'H'

Out-of-range negative slice indices are truncated, but don't try this for single-element (non-slice) indices:

> word[-100:]

'HelpA'

> word[-10] # error

Traceback (most recent call last):

File "<stdin>", line 1, in ?

IndexError: string index out of range

E.g.s

> s = "Lalapalooza"

> print s.count("la")

1

> print s.find("L")  Note: chars in a string start at index 0!

0

> print s.find("pal")

4

> print s.rfind("zoo") not found

-1

> print s.rfind("La")

0

> s = "123anywhERE"

> print s

123anywhERE

> s.isdigit()

0

> s.isalpha()

0

> s = "OK"

> print s.isalpha()

1

> s = "Roses are red Violets are yellow"

> s.split(" ")

['Roses', 'are', 'red', 'Violets', 'are', 'yellow']

Programming Constructs in Python

Note:

Comment using “#”.

Some examples:

# this is the first comment

SPAM = 1 # and this is the second comment

# ... and now a third!

STRING = "# This is not a comment."

  1. Looping
  2. for command
  3. uses an index variable, the keyword “in” and a list
  4. note use of colon after the list – it signals that what comes next is a block
  5. commands in a block must be indented at least 2 spaces to become part of the block

E.g.

> myloopa()

1

2

3

  • can use range() function to generate the list
  • one arg: range(3) → range is from 0 to arg-1
  • two args; range(1,4) → range starts at 1 ends at 3
  • three args; range(1,4,2) →starts with 1, steps of 2, ends at 3

E.g.

> myloopc()

0

1

***

1

2

***

1

3

5

***

1

  • can use an array for the list

E.g.

for sample in getSamples(mySound):

  1. while command
  2. takes a logical expression and executes its block as long as the logical expression is true
  3. note use of colon after the list after logical expression

E.g.

> mywhile()

1

2

3

4

  1. Selection (branching)
  1. if command
  2. else is optional
  3. can nest the if commands

E.g. 1 – no else part

> myifa()

Good grade

E.g. 2 – else part present

> myifb()

Could be ok, we'll see

E.g. 3 – Nested if/else

> myifc()

68 :should be improved

E.g.4

Note:

  • true or false task could be more than one statement
  • indentation determines in what block a statement is

> myifd()

75 :is OK

75 :could be any grade < 80