Docsity

Jython Programming: Control Flow, Collections, Scoping, and GUI - Prof. Warren Edwards, Study Guides, Projects, Research of Computer Science

Partial preview of the text

Download Jython Programming: Control Flow, Collections, Scoping, and GUI - Prof. Warren Edwards and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Week 2:

Quick and Dirty Jython Refresher

(Prelude to Asynchronous Programming)

CS

Connecting the Lo-Fi Prototype

with the Project

 (^) A few points about the IM assignment  (^) The IM protocol we’ll be using doesn’t support  (^) Authentication/login  (^) Sending messages to a user before that user joins the chat  (^) Named, persistent chat rooms  (^) Buddies  (^) Some of these you can implement in your own client, even without server support  (^) E.g., buffer messages sent to a user before he/she joins

Today’s Menu

 (^) Jython Refresher  (^) Basic Control Flow  (^) Collection Classes  (^) Scoping and Modules  (^) Classes and Objects  (^) GUI Programming  (^) Useful odds-and-ends that may come in handy for the next assignment

Basic Control Flow

 (^) All the usual, expected forms of control flow  (^) Conditionals: if if x == 1: print “X is one” elif x == 0: print “X is zero” else: print X is something else”  (^) Looping: while, for  (^) Exceptions: raise (we won’t talk about this until later in the class)

Basic Control Flow (cont’d)

• Iteration:^ for

for x in range(5): print x prints 0, 1, 2, 3, 4 for x in range(2, 5): print x prints 2, 3, 4 for x in [“spam”, “eggs”, “ham”]: print x prints spam, eggs, ham

• General form: the^ for^ statement iterates over a sequence of items,

such as a list

Jython Collection Classes

Variables and References

 (^) A variable is simply a name that contains a reference to some information  (^) foo = “Hello, world”  (^) Variables can be reassigned, and multiple variables can refer to the same thing.  (^) Stashing a reference in a variable gives you a way to name it, and get at it later. foo “Hello, world” String

The Need for More Complex

Data Structures

 (^) Some more complex structures are hard to represent by just a named variable though.  (^) Example: you want to keep track of all of the users in a chat.  (^) user1 = “Steven”  (^) user2 = “Amy”  (^) ...  (^) This is too static. Would you just create 1000 variables in case you ever had that many users? How would you do something to each one (can’t easily iterate)

A Quick Jython List Refresher

 (^) Lists are ordered collections of items

L=[0, ‘zero’, ‘one’, 1]  (^) Selecting items from a list (note indices start at 0!) print L[1] ‘zero’  (^) Getting the length of a list len(L) 4  (^) Modifying lists L.append(’two’) L.remove(’zero’) print L [0, ‘one’, 1, ‘two’]  (^) Iteration for item in L: print item

Tuples: Fixed Sequences

 (^) Like lists, only immutable  (^) The set of references in a tuple is fixed  (^) Generally used either when:  (^) You need a constant list  (^) daysOfWeek = ( “Monday,” “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday” )  (^) You need to group together a set of data whose structure is fixed:  (^) E.g., using tuples as quick-and-dirty records, such as address book entries:  (^) myContactInfo = ( “Keith Edwards”, “TSRB348”, “keith@cc” )  (^) All list operations work on tuples, except ones that modify the set of references within the tuple  (^) So, no append(), remove(), etc.

Associating Data Items With

Each Other (cont’d)

 (^) If you don’t know the associations you might have up front, you could use parallel lists:  (^) workDates = [ “1/29/05”, “1/30/05”, “2/1/05”, ... ]  (^) workHours = [ 4.5, 8, 5.5, ... ]  (^) Then, iterate through the first list to find the date you’re looking for, then look for the item with the corresponding index in the second list  (^) Too much work! Too error prone!  (^) Fortunately, Jython has a built-in data structure for creating associations: the dictionary

The Dictionary Data Structure

 (^) Dictionaries associate values with keys (you lookup a value given its key)  (^) Both are references to data items  (^) workRecord = { “1/29/05”: 4.5, “1/30/05”: 8, “2/2/05”: 5.5 }  (^) Dictionaries are the most commonly used Jython data type  (^) Virtually any Jython data type can be used as a key or as a value workRecord Dictionary “1/29/05” 4. “1/30/05” 8. “2/2/05” 5.

More Dictionary Goodness

 (^) Testing whether or not a dictionary has a given key

dict.has_key(“two”) 1 dict.has_key(“five”) 0  (^) Getting keys, values, and entire items dict.keys() [“two”, “three”, “four”] dict.values() [2222, 3, 4] dict.items() [(“two”, 2222), (“three”, 3), (“four”, 4)]

Scoping and Modules