CIS 465 Multimedia JES (Jython Environment for Students) Tutorial -- I General Guide to Using JES 3

peanutunderwearSoftware and s/w Development

Nov 7, 2013 (3 years and 5 months ago)

63 views


JES Tutorial


I

1

CIS 465 Multimedia


JES (Jython Environment for Students)

Tutorial
--

I

General
Guide to Using

JES 3


More info on Python:
http://www.python.org/doc/tut/tut.html


JES Screen:




JES
interface
has
2
main areas
:




the
Program Area

(top part)



o

write your Jython programs here

o

provides
a text editor

o

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)

o

can
type in
J
ython
/Python

commands here at the

>>>


prompt, hit Enter

o

can type in names of loaded programs to be run

o

com
mands are interpreted

by Jython interpreter


Note
:

Load Program Button

>>>>>

IMPORTANT!


<<<<<
After saving a program created in the Program Area, u
se

JES Tutorial


I

2

the load button to load the file that is currently in the program area so it can be
recognized in the comm
and 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 prog
ram 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()


a.


displays list of directories an
d files

in a
window

(default shows file structure under
My Documents
)

b.


returns a string which is the name of file
that
you picked

c.

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()


JES Tutorial


I

3

'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


>>> pick
AFile()

'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



2.

makePicture(filename)

a.

argument should be name of so
me type of graphics file (
e.g.,
.

jpg
)

b.

returns

Picture
object

c.

displays

printout of

information about the Picture (filename, size

(height and width
in pixels)
, etc.)

d.

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
\
M
yPics
\
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,
sometimes

JE
S 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



JES Tutorial


I

4

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


3.

show
(
myPic
)

a.

argument is a Picture

b.

causes the Picture to be shown


E.G.


>>> show(makePicture(pickAFile() ) )



I picked butterfly.jpg
, see pic shown below






Working with sound

files
in JES


4.

makeSound(filename
)

a.

arg should be name of a sound file

b.

returns a Sound

object

c.

displays information about the Sound (filename, # samples, etc.)

d.

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
\
prea
mble.wav number of samples: 421110




5.

play(
mySound
)

a.

argument is a Sound

b.

causes the Sound to

be played





JES Tutorial


I

5


Using variable names and assignments in JES


E.G.s

>>> mynum = 10

>>> print mynum

10



>>> yournum = 12.8

>>> print mynum + yournum

22.8



Giving vari
able 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
Softwa
re
\
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)






JES Tutorial


I

6


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:

o

name

of the function you are defining

o

arguments

(
0 o
r more
) enclosed in parentheses

o

a
colon

E.g. def myfunction():




The body

o

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:

p
ickAndShow.py



Note the extension of py, for
Python

o

With my version of JES, a window opens up showing the file structure under
My Documents

on my computer. But, i
t appears you can save
the file

anywhere, but be organized



Click on Load Program button


DO
N’T FORGET THIS!!



Click in the Command Area and type the command pickAndShow()



name of

function you defined

o

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



JES Tutorial


I

7


E.g. 2



Hardcoding a file n
ame 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



Creat
e

a function that takes an argument
, and us
es

comments


Here, the input to the function i
s 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











JES Tutorial


I

8


E.g.
4



Create a function that takes the name of a sound file as an
argument







In the Command Area I typed:


>>>
play
Named("c:
\
MyMedia
\
My
Sounds
\
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 i
s 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











JES Tutorial


I

9


Operators and Useful Functions
in Python


+,
-
, *, /, **


Arithmetic operators; normal precedence

<. >, ==. <=. >=

Relational

operators

<>, !=

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 Py
t
hon


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


E.g.
s

>>> print max(
-
231,
-
500)

-
231


>>> print len("Jython")

6

>>>




St
r
ing Methods in Py
thon


More String info:
http://www.python.or
g/doc/tut/tut.html


Assume

s

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




s.count(sub):

returns the nu
mber of times sub appears in s


JES Tutorial


I

10



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 poin
t

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 s
eparated 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 la
st
-
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'


JES Tutorial


I

11

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 inde
x 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 = "123anyw
hERE"

>>> 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']






JES Tutorial


I

12

Programming Constructs
in Python

No
te:

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

a.

for

command



use
s

an index variable
, t
he keyword “in”

and a list



note use of colon after the list



it signals that what comes next is a block



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

1.

one arg: range(3) →

range is from 0 to arg
-
1

2.

two args; range(1,4) → range starts at 1 ends at 3

3.

three args; range(1,4,2) →starts with 1, steps of 2, ends at 3

E.g.


JES Tutorial


I

13


>>> myloopc()

0

1

***

1

2

***

1

3

5

***

1

>>>





can use an array for the list

E.g
.

for sample in getSamples(mySound):


b.

while

command



takes a logical expression and executes its block as long as the logical
expression is true



note use of colon after the list after logical expression


E.g.


JES Tutorial


I

14



>>> mywhile()

1

2

3

4


2.

Selection (branching)


a.

if command



else
is
optional



can nest the if commands



E.g.

1



no else part





>>> myifa()

Good grade



E.g. 2



else part present




JES Tutorial


I

15



>>> 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 determine
s

in what block a statement is




>>> myifd()

75 :is OK

75 :could be any grade < 80