













































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














































CS
(^) 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
(^) 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
(^) 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)
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
such as a list
(^) 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
(^) 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)
(^) 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
(^) 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.
(^) 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
(^) 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.
(^) 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)]