Download PDF
Download page Jython Scripting: Variables, Operators, and Statements.
Jython Scripting: Variables, Operators, and Statements
This workshop will explore the following topics:
- The ResSim Script Editor
- Data Types and Variables
- Numeric Operators
- Normal and Raw Strings.
- String Operators
- Statements
- Lists and Collections Sequence types and operations
- Mapping types and operations
- Nested collections.
Startup - Open the ResSim Scripting Editor
Standard Output from a script in ResSim goes to the console window and console log. Since keeping the console log viewer up to date is tedious, you might find it easier to use ResSim with an open console window. To accomplish this, first install the “personal.config” file so that you can modify the ResSim program configuration. To do so:
Open a file browser and to the ResSim 3.3 installation (we provided it in the class materials, c:\class\software\HEC-ResSim 3.3).
In the folder, you will find HEC-ResSim.exe among other files. One of those other files is named Install Personal Config and Definition Files.hta. That file is executable and does just what it says. Run it.
When it is done, it will open a browser window to the folder in your profile/appData area where the it copied the HEC-ResSim – Personal.config file. Right-click on that file and open it with a text editor.
Then, add a line, anywhere, with the command:
showConsole trueSave the file and close the editor.
Return to the ResSim installation folder and execute HEC-ResSim.exe. [Hint: before executing ResSim, consider making a shortcut and placing it on your desktop]
Create a New Watershed – Name it Workshop1
From the Tools menu, open the Script Editor
From the File menu, create a New
Give your script the name Variables and click Save.
How did you give your script a name?
I entered it in the label field at the top
What happened when you clicked Save?
The name of my script appeared in the Scripts tree and the edit panes of the Script Editor greyed out
Where did the Script Editor locate your script in the Scripts Tree? Why?
In the Watershed Setup folder of the Modules folder of my watershed.
Variables and Data Types
Create the following variables, then use the print statement to output them to the console log.
a = 20
print “a=”, a
b = 020
print “b=”, b
c = 0x20
print “c=”, c
d = 20L
print “d=”, d
e = 20.0
print “e=”, e
f = “abc” (with quotes)
print “f=”, f
g = None
print “g=”, g
h = 20000000000 (10 zeros)
print “h=”, h
a = 20
print “a=”, a
a= 20
a is of <type 'int'>
b = 020
print “b=”, b
b= 16
(input was octal)
020 is of <type 'int'>
c = 0x20
print “c=”, c
c= 32
(input was hex.)
c is of <type 'int'>
d = 20L
print “d=”, d
d=20
20L is of <type 'long'>
e = 20.0
print “e=”, e
e=20.0
e is of <type 'float'>
f = “abc” (with quotes)
print “f=”, f
f=abc
f is of <type 'str'>
g = None
print “g=”, g
g=None
g is of <type 'NoneType'>
h = 20000000000 (10 zeros)
print “h=”, h
h=20000000000
h is of <type 'long'>
- Click Save and Test to run your script
- Open the Console Log from the Tools menu, scroll to the bottom, then report what was printed for each variable in the first column.
- Python has a type(item) command that returns the data type of item in the form: <type ‘item data type’>.
Use the type command with some of the items above. You can use either the variable or the literal (constant) as the item argument. Re-run your script and report the results in the second column.
Valid and Invalid Variables and Literals (Constants)
Add each of the following to your script, one at a time, testing the script between each addition. Report what happens.
[Hint: For those statements that produce an error, either correct the statement or comment it out so you can proceed.]i = 0x20.0
j = abc (without quotes)
x2 = 5; print x2
2x = 5; print 2x
_2x = 5; print _2x
i = 0x20.0
Syntax Error
(hexadecimal integers don’t have decimals)j = abc (without quotes)
Name Error
(the name abc is not defined)x2 = 5; print x2
5
2x = 5; print 2x
Syntax Error
(a name cannot start with a digit)_2x = 5; print _2x
5
Numeric Operators
Try some numeric operations. Enter the following statements, then run your script and report the results:
x = 5.0; y=2.0print “x+y=”, x+y
print “x-y=”, x-y
print “x*y=”, x*y
print “x/y=”, x/y
print “x//y=”, x//y
print “x%y=”, x%y
print “x**y=”, x**y
print “x+y=”, x+y
x+y= 7.0
print “x-y=”, x-y
x-y= 3.0
print “x*y=”, x*y
X*y= 10.0
print “x/y=”, x/y
x/y= 2.5
print “x//y=”, x//y
x//y= 2.0
print “x%y=”, x%y
X%y= 1.0
print “x**y=”, x**y
X**y= 25.0
Strings
Get familiar with strings. Add the following statements to your script and answer the question.
s1 = “This is the first line.\nThis is the second line.”
s2 = r“This is the first line.\nThis is the second line.”
print “s1”
print s1
print “s2”
print s2
Are there any differences in the output of the print s1 and print s2 statements? If so, what?S1 prints on two lines
S2 prints on one line and it includes the \n
- Create variable s3 with the value of “Python”
- Create variable s4 with the value of “wonderful”.
- Create variable s5 using string concatenation (addition) as follows:
s5 = s3 + “ scripting is ” + s4 - Create the variable s6 using string interpolation as follows:
s6 = “%s scripting is %s” % (s3, s4) Print s5 and s6.
Are there any differences in the output of s5 and s6? If so, what?There are no differences
Collection Data Types
Tuples
Create a tuple named t with the following values:
1.23
"abc"
73
1024L
What is the result of each statement below?print "t=", t
print "t[1]=", t[1]
print "t[1:]=", t[1:]
print "t[-1]=", t[-1]
print "t[:-1]=", t[:-1]
print "t[::-1]=", t[::-1]
print "t+t=", t+t
print "t*2=", t*2
print "73 in t =", 73 in t
print "len(t)=", len(t)
print “t is of “, type(t)
t[1]="xyz”
print "t=", t
(1.23, 'abc', 73, 1024L)
print "t[1]=", t[1]
abc
print "t[1:]=", t[1:]
('abc', 73, 1024L)
print "t[-1]=", t[-1]
1024
print "t[:-1]=", t[:-1]
(1.23, 'abc', 73)
print "t[::-1]=", t[::-1]
(1024L, 73, 'abc', 1.23)
print "t+t=", t+t
(1.23, 'abc', 73, 1024L, 1.23, 'abc', 73, 1024L)
print "t*2=", t*2
(1.23, 'abc', 73, 1024L, 1.23, 'abc', 73, 1024L)
print "73 in t =", 73 in t
True
print "len(t)=", len(t)
4
print “t is of “, type(t)
<type 'tuple'>
t[1]="xyz”
How would you assign the four items of tuple t to the variables a, b, c, and d in a single statement?
a, b, c, d = t
or
a, b, c, d = 1.23, "abc", 73, 1024L
Lists
reate a list with the following values
8.4
“kittens”
13
64FCA
How did you enter the hexadecimal value 64FCA into the list?0x64FCA
What is the result of each statement below?
print "L=", L
print "L[1]=", L[1]
print "L[1:]=", L[1:]
print "L[-1]=", L[-1]]
print "L[:-1]=", L[:-1]
print "L[::-1]=", L[::-1]
print "L+L=", L+L
print "L*2=", L*2
print "13 in L =", 13 in L
print “L is of “, type(L)
print "len(L)=", len(L)
L[1]="puppy"; print "L=", L
print "L=", L
L= [8.4, 'kittens', 13, 413642]
print "L[1]=", L[1]
L[1]= kittens
print "L[1:]=", L[1:]
L[1:]= ['kittens', 13, 413642]
print "L[-1]=", L[-1]]
L[-1]= 413642
print "L[:-1]=", L[:-1]
L[:-1]= [8.4, 'kittens', 13]
print "L[::-1]=", L[::-1]
L[::-1]= [413642, 13, 'kittens', 8.4]
print "L+L=", L+L
L+L= [8.4, 'kittens', 13, 413642, 8.4, 'kittens', 13, 413642]
print "L*2=", L*2
L*2= [8.4, 'kittens', 13, 413642, 8.4, 'kittens', 13, 413642]
print "13 in L =", 13 in L
13 in L = False
print “L is of “, type(L)
L is of <type 'list'>
print "len(L)=", len(L)
len(L)= 4
L[1]="puppy"; print "L=", L
L= [8.4, 'puppies', 13, 413642]
How would you assign a reference of L to variable L2?
L2 = L
How would you assign a copy of L to variable L3?
L3 = L[:]
What would be the difference in behavior between L2 and L3?
L2 points to the same memory position as L. If you change something in L2, you change L.
L3 points to the memory position where the contents of L were copied. If you change something in L3, it would not affect L.
Strings
Yes, strings are collections. In fact, they are like tuples, but have additional methods for formatting and slicing them.
Create a string variable named s that contains “01234”.
Can you change the third character of s to “X”? Why or why not?No, Like tuples, strings are immutable.
What is the result of each statement below?
print “s=”, s
s += "56789"; print “s=”, s
print “s=”, s
s= 01234
s += "56789"; print “s=”, s
s= 0123456789
If strings are immutable, why does s += “56789” work?
Because s += “56789” is the same as s = s + “56789” which does not change the original string but creates a new string (s + “56789”) and assigns it to s.
Dictionaries
Create a dictionary variable named D with the following items:
key value
“a” 1
“b” “Two”
“c” 3.0
1 “A”
-2 “B”
3.0 “C”
What is the length of the dictionary?Len(D)=6
Reset the value associated with the key “c” to “three”.
What statement did you use?D["c"]="three"
Does your dictionary how have a key of “three”?
Yes. D= {'a': 1, 1: 'A', -2: 'B', 'b': 'two', 'c': 'three', 3.0: 'C'}
Nested Collections
Create a two dimensional list using the following code:
m = [[],[],[],[],[]]
for i in range(5) :
for j in range(5) :
m[i].append((i+1)*(j+1))
What is the type of m?
What is the length of m?
What is the type of m[0]?
What is the type of m[0][0]?
What is the value of m[3][3]?What is the type of m?
m is of <type 'list'>
What is the length of m?
len(m)= 5
What is the type of m[0]?
m[0] is of <type 'list'>
What is the type of m[0][0]?
m[0][0] is of <type 'int'>
What is the value of m[3][3]?
m[3,3]= 16