Download as pdf
Download as pdf
You are on page 1of 395
oe ID: 6689691 ()G)-Pabe 8 Instructor Notes nd about 2 years ago by| (U/FOUO) Instructor notes for COMP 3321 UNCLASSIFIEDHFOR- GFFIGIAL BSE-ONEY (U) So, youre teaching the Python class. What have you gotten yourself into? You should probably take a few moments (or possibly a few days) to reconsider the life choices that have put you in this position, (U) Course Structure (U) As mentioned inthe involution, this course is designed for fexcbilty. When aught in a classroom setting, single lesson or module can be ‘covered ina session that lasts between 45 and 90 minutes, depending on the topics to be covered. The standard way to structure the course is as fulltime, two week block. During the frst week, the ten lessons are covered with moming and aftemioon lectures. During the second week, up to ‘ten modules are covered ina similar manner, as needed or requested by the students in the class. (the class needs are not known, take @ vote), During the fist few days of class, students should choose a project to work on. On the lat day, students should report back onthetr progress and, if possible, demonstrate ther work. Instructors should be available outside of lactures to assist students with exercises and projects. {(U) The two week block isnot the only way of teaching the course. The material could be presented at a more leisurely pace, for instance during a ‘weekly brown bag lunch that continues for several months. Alternatively, f students are already prepared (or wiling to do some ofthe intial lessons. ina selstudy manner), a great deal can be accomplished in a two oF three day workshop. For instance, (all students already have a basic ‘knowledge of Python, they might well start with the lessons on iooling and wiling.masiules anc packages, then move on to cover various modules of interest. Approved for Ri 36 oc ID: 6689691 (U) Instructional Style (U) When teaching mathematics, the common practice ofthe instructor wring solutions on the chalkboard is a moderating method that helps students keep up. Writing on a chalkboard isnot usualy helpful when teaching programming, but the same principle applies; as the instructor, you should adopt practices that help you slow down. We recommend that you have alive, interactive session displayed a the front ofthe room, large ‘enough fo ll the students to soe. This session can either be a terminal session ora Jupyter notebook. The important detallis that in most cases the commands should not be pre-populated; eg. you should not just execute cll from an existing Jupyler notebook. The materials are present to help you prepare, and as a reference for the students as they work on exercises; they are not an acceptable substtue forthe shared experience of teaching and learning. You will make unexpected mistakes as you write code lve In font ofthe lass. Dont worry, relax, and let the students help ‘you-it wil help them eam the principles and figure out how to solve their own problems, This Is not a substitute for proper preparation; too ‘many mistakes and fumbles will cause your students o lone interest nthe course and trust in you (U) Ongoing Development ae (U#FOUE} The developers of this course believe thatthe current materials are sufficiently welldevetpied tobe an effective ad forthe course. However, improvements, extensions, and refinements are aways welcome, To that ert we have attempted to make i easy to contribute to the Drie. Th docmartan bao on te Mack offal COMP 2921 mete, you weno neko charge for our ‘own purposes, fel tree to clone that repository or fork any ‘on the Juptyer Gallery. Please submit @ change request onthe Gallery if you'd like to make a one-off contribution, including new or improved exercises, additions to lessons or modules, or entrely new lessons or ‘modules. Ifyou woul ke to be a collaborator onal ofthe COMP 3321 notebooks, contact the COMP3321 GlobalMe group and ask o be added. (U) A possible icebreaker (U) To got the students interacting with each other as wel as thinking about code at an abstract level, consider the following icebreaker. Instructor becomes human compile to interpret written instructions to get out ofthe room. (U) Phase I~ discussion with whole class (U) Invent a programming language together, one sufcient fr ths task. Clearly explain the task, showing where inthe room the instructor wil begin and how big a steps, (U) Take suggestions from students about what instructions they will want to use. Write each instruction on the board. Be clear that everything ‘written on the paper must come from syntax onthe board, No other syntax allowed, (U) Make sure the instructor and students agree about precisely what each instruction means. AS the instructor, be certain thelist onthe board is. ‘enough for you to solve the problem. Give hints unt i's complete (U) Let the students come up with the syntax. But here are some syntax examples they may come up with + step(n) takes in an integer and causes instructor to taken steps, Doe 6589691 um(d) ~ takes in a number in degrees and caused instructor to tum clockwise that many degrees. Students may abuse this and putin numbers that lead to dizzyness, e.g. tun(1440) + obstacle ~ returns boolean indicating rather an obstacle is directly in font of instructor. Variations for checking tothe left or right may be Sesirable as wel, + If-<> then < > else < > ~ fst blank takes boolean (make sure boolean functions exist), Second blanks take instructions, + while <>: <> ~ takes boolean function and any expression not ~ expression to reverse a boolean any integer (U) Phase It break into teams (U) Teams of 35 students tend to be appropriat (U) Each team produces a piece of paper wih computer instructions forthe human compiler to get out ofthe room. Only allowable syntaxis what is writen on the board. Should take no more than 15 minutes. (U) Phase I~ demonstrations (U) One ata time, the instructor takes a team's solution and follows the instructions iterally and fairy. Does the instructor get out ofthe room? (U) Introductory e-mail (U) Tere area couple things it woud be nice ifthe students could nave done in advance, n pacar having GITLAB accounts and INHERE Agreements. | have the COMP3321 learning facitatorssond te folowing eal to enrolled students (U) Aone (U) You are receiving this e-mail because you are registered for COMP3321 beginning <.>. (U) fatal possible, we could use you do a few setup things in advance to make class go smoothly onthe frst day. Really, two simple things that ill take @ minute of your time and save us hours on the fst day of class. (U) 1) GO iagree and find, read and agree to the INHERE user agreement (U)2)60 gitab. (U) in more deta: (U) 1) The course wil be run-vtaLABBENCH and NBGALLERY. Before using this, you will need to agree,to'the terms of service and| knowledge ths Instuctons can be found ber: noallarsa acid (GO NBGALLERY) by cleking GET A BENCH. We ony TOTS foto theft, uta below. Be careful going fre because LABBENCH modi ea dest wo weeks ar creation 50 Yeu want aes one the fret ay of lags : (U) GO iagree. Search "inhere”. Read the INHERE user agreement and agree toi co eventually acknowledge this agreement. That's what we need Doe ID: 6 {85.0 ota. 2y ana tee one an acount il be eel. Thats al we need bet (Utyouwantto go turer and ue gta he command ine[_——Jtownstctons har snide (U) Tre are several opto presented. Whatever you carga won Ws, (U)3) Optpna. Th lao bt 3'CABBENCH. But some people retro use[_——JoachiThs wl ake goths wrk. nial frwconda oy fekowng bese isbn: ioscan rade 8 Nan 22673 (U) you ave any issues wih he instuctons, please contact he insigor: > a (U) Submitting projects - (Aone mr api onc css [Trt an oer Gagan costo Poney epee mand ft BST SET nm VY. Stes wl aut ace © Ges. an bo ean aSumplan fresh he web epleaton by nee" rand copying and esi ere. The web option owt outa tar Steno te Suse ta aed bt can ee Bun comune a rye UNCLASSIFIEDHFOR-OFFIGIAL USE-ONLY oc ID: 6489692 Python Programming (U#2YO} Course introduction and syllabus for COMP 3321, Python Programming. UNCLASSIFIED//FOR-OFFIGIAL USE-ONLY— (U) History .-in December, 1989, | was looking for a "hobby" programming project that would keep me occupied during the week around Christmas. My office ... would be closed, but | had a home computer, and not much else on my hands. | decided to write an interpreter for the new scripting language | had been thinking about lately: a descendent of ABC that would appeal to Unix/C hackers. | chose Python as a working ttle for the project, being ina slightly irreverent mood (and a big fan of Monty Python's Flying Circus) Guido van Rossum, Foreword for Programming Python, 1st Edition. (U) Motivation (U) Python was designed to be easy and inttive without sacrificing power, open source, and suitable for everyday tasks, with quick development times. I makes the layers between programming and problem solvingseem as thin as possible. Its suitable for- ‘+ Opening an interactive session to solve the Dally Piz, + Writing a script that automates a tedious and time-ceinhith Do ID: 6689492, Creating a quick web service or an extensive web application, and + Doing advanced matnematica esearch. (U) Ifyou dont know any programming languages yet, Python isa good place to start. It you already know a diferent language, its easy to pick Python up on the side. Python isnt entirely free o frustration and confusion, but hopefully you can avoid those pars until ong after you get some ‘900d use out of Python, (U) Programming is nota spectator sport! The more you practice programming, the more you will eam in this class, both in breadth and depth. Python practically teaches itsetf-the goal of your instructor is fo guide you to the good parts and help you move just ait bt more quickly than you would otherwise. Happy Programming! Betas reen (U) Objective (U) The goal ofthis cass is to help students accomplish work tasks more easly and robustly by programming in Python. To pass the course, each ‘student must wrte atleast one Python program that has substantial personal ult ors of significant personal interest. When choosing a project, ‘students are encouraged to fit think of work-related tasks. For students who need help geting stared, several suggestions for possible projects ‘are found atthe bottom ofthis page. On the fist day, instructors wil lead a discussion where project ideas are discussed, {(U) This class is designed for students of varying backgrounds and levels of experience, from complete navi to competent programmer. Asking ‘each student to design and implement their own project allows everyone to leam and progress at an individual pace. (U) Logistics (U#F946) This course is designed to be suitable for set1eaming. Even ifno formal offerings are available for your schedule, you may access and work on the modules of his course at anytime. Even if you dont have access toa recent version of Python on a workstation or vital machine, you ‘can access a personalized Jupyter notebook avaliable on | ABBENCH. Foran individual pursuing this self-study option itis recommended to fist ‘over the Python Basics roughiy in order, then select as many ofthe Useful Modules as seem appropriate. You can also use this Jupyter ‘notebook to experiment and wrie solutons to exercises. (U##O4E) One possibilty fora group of potential students who start out with a diferent amounts of programming experience is to use Jupyter as a sel-study tool until everyone has a basic understanding of programming, then folow up with an abbreviated instructor-led course (anywhere fom two days toa week or more, depending on needs). (U) In the Classroom (U) For a two week course: there wll be a morning lecture and an afternoon lecture every day. The moming lecture wil ast between an hour and hinety minutes; the aftemoon lecture wil be somewhat shorter. Ha lecture Is going too fast, please ask questions to slow us down! Its going ‘00 stow feo! fee to work ahead on your own, Please Enter Footer Text. oc ID: 6499692 (Gi#SL0} You wil ether use Python within jronment or within LABBENCH, SSS ews] ‘ut some diferences between the Python 3x and 2x les, Tis Course wil focus on Python ‘Zryou need oF wantTo rn Pyihon oh Linux, probably withing achineShon VM, well work wih you. To the extent possible, we wil write code in platform-agnostc manner and pont out fetes that are unique specif versions of Python. : {(U)Atnough lectures wil ony take up two o three FoUs.each day, encourage you to spend the remainder of yur day programming in Python, either on your own orn groups, butin the classroom f possible. AL least ofe instructor willbe available in the classfoom during normal business hours. i (U) This course is a work in progress; we welcome all suggestions. There afe'mgre Ueful Modules than we can Kispe to cover ina two-week class; instructors wil ake a vote to determine which modules to cover. I there is anbihe topic that you would lke {9 have covered, especialy along the lines of "How would | do x, v oF 2 in Python?" please ask-if there's enough interest; wel covert ina lecture. Weld even be happy to have you contribute tothe course documentation! Tak to an instructor to find out how. (U) Table of Contents (U) Part |: Python Basics (Week 1) + (U) Lesson 01: Introduction: Your First Python Program + (U) Lesson 02: Variables and Functions © (U) Optional: Vasable Exercises © (U) Optional: Function Exercises + (U) Lesson 03: Flow Control © (U) Optional Fw Control Exercises (U) Lesson 04: Container Data Tynes ‘| + (U) Lesson 08: Modules. Namespaces and Packages © (U) Supplement: Modules and Packaoes + (U) Lesson 08: Exceptions, Profling and Testing + (U) Lesson 10: teraors. Generators and Duck Typing © (U) Supplement: Pioeining wih Generalors + (U) Lesson 11: Sting Formatting =“) Part I: Useful Modules (Week 2) + U) EERE Module: Cotectons and tertoois @ (U) Supplement: Functional Programming + (U) Supplement: Recursion Examales una ‘© (U) Supplement: Maninulating Microsoft Office Documents wih win’2com + (U) Module: Threading and Subprocesses + (U#FOweYDstdbutig a Python Package at NSA + (U) Modula: Machine Learning ntoduction (U) Homework + (U)Day1 Homework Please Enter Footer Text... ‘Doe ID: 6689692, * {Uy Day.2Homewors (U) Exercises (with Solutions) (U) Class Projects (U) Glick nore to get to @ notebook containing instructions for password checker and password generator projects. (U) Project Ideas + (U) White a currency conversion script + (U) Write a web application + (U)DoProject Euler problems + (U)Eind Anomalous Acivily onthe BigCorm Network + (U)BSA Encrymtion Module, Par 2 + (U) Pick project rom one of the Safari books below (U) General Resources (U) Python Language Documentation (UHFEYE}NSA Course Materials (©) Dive into Python (U) Exe ython Programming (Head First Python (U)HHion Performance Pytnon (advanced) ()Leaming Python {(U) Leaming Python Programming (videos) + (U)Prooramming Python + (U)2ytnon Crash Course (includes 3 sample projects) + (U)2ytion Piayoround (more sample projects) (U) 2ython Packet Reference (consider geting a print copy) (U) 2ython Programming fr the Absolute Beginner (even more sample projects, games & quizzes) * (U) and More Python Programming forthe Absolute Beginner + (U)Thing Python + (U) Safar Books (General Query) (U) Other + (U)Einal Project Schedule Generator + (U) Justa ite notebook fr randomly generating a schedule for students to present their nal projects for COMPS321, (U#FOUE) The Pyinon coun on NSA Gitkat (U) The Hitchhiker's Guide to Python! (U#FOVO) Pyinon on Wisinio (U) Python on StackOveriow (U) Addtional targeted resources (often excerps from the'sbove)aredinkédn eactriesson and module. ere 86 Doe UNCLASSIFIEDHFOR OFFICIAL USE-ONLY- oe ID; 6689693 Lesson 01: Introduction: Your First Python program a (U) Covers Anaconda installation, the python interpreter, basic data types, running code, and some built-ins. UNCLASSIFIED//FOR-GFRHIGIALUSE-ONEY (U) Welcome To Class! (U) Lets get to know each other. Stand up and wai for instruction, (U) Who has a specie project in mind? (U) Method 1: Anaconda Setup (U) Aternately, flow tis tradecrat hub arte, ios production \radecrafproj.nsa ie. govlentry/29475 (U) We will be using version 4.4.0 ofthe Anaconda Python dstrbution, availabe from Git Software. Anaconda includes many packages for large~ ‘scale data processing, predictive analytics, and scientific computing (U) Installation Instructions ‘Approved for Release by NSA on 12-02-2019, FOIA Case # 108165 es EU) Click onthe lnk above 2.{U) Glick on the “Download Now” button 53. (U) Accep the agreement and click "Next 4.(U) Click Next 5. (U) Download "Anaconda3-4.8.0-Windows-x86_64 exe 6-(U) Open the folder containing the download (pica ths is your “Downloads” folder) 7 (U) Doublecick the Anacondas-4.4.0-Windows-186_64.exe le 8 (U) Click "Nex?" to Start the installer 8. (U) Glick the" Agree” button to accept the icense agreement 10.(U) Choose to install for “Just Me" and cick "Next™ 11, (UHFOUS) Select a destination folder on your U:crive (such as U\prvatelanacondad) 4. (U#EOUO} Cick the "Browse." butlon, Gck on "Computer" and selec the U: drive 2 (UAFOUO} Select "My Documents" and press "OK" [Note: DO NOT Make a folder named anaconda3) '3. (UAFOUE} n the Destination Folder input area add “anacondad" asthe folder name 4. (UMPUE}Clck Next 12.(U) Cick instal to begin the install eave checkboxes a fs) 13 (U) Wait about 30 minutes for the instal complete 5 (U) Running python deceeeeeee? (U) You can run python directly on yourfET Pisskop and immediately interact with it 4. (U) Open a Windows command window (Type “cma” in the Windows Programs search bar) 2 (U) Type “python” inthe command window (U) Running Jupyter (U) Aterately, you can run python in a browser from a web-enabled python called a jupyter notebook, Tit Wc Gory: "or Wis Cass, however, we each ‘Sar up aur own ndiiGual Jupyler web: portal ofan Our Glass novebooks. 4. (U) From the Windows Start menu, search for "upyter 2 (U) Right-click on Jupyter Notebook inthe resulis and select Properties 3. (UHFOUS) in the “Target field, add" uprivat” atthe end (ater "notebook’) [Note: don't forget the space before u:prvate] 4 (U) Chick Apply and then OK 5. (U) Search for jupyter again in the star menu and click on Jupyter Notebook to run it 6. (U) Wait a few moments...This should launch Jupyter in your browser at ili. localhost 8888 (U) Method 2: LABBENCH Setup oc ID: 6589693 (UHFEUSE} Step 1: Access to LABBENCH + (U#FOUO) 90 inoree and read and accept the INHERE User Agreement. (U#FEUO} This s a prerequisite for access to LABBENCH, a VM system where we willbe working. It may take a few hours forthe approval to propagate through the system. (U) Step 2: Visit Jupyter Gallery 41. (UAFOUO} 90 junviee 2 (U) Glick on the Jupyter Gallory logo to get to the Gallery 3. (U) Click on Tour the Gallery for quick demo. 4. (UFOS) To find the course notebooks, either search for “Syllabus” or choose Notebooks > Learning > COMP 3321 and sor by title to find the Syllabus. (U#FOUG} Step 3: Set up Jupyter on LABBENCH (U#FOUO) At the Jupyter Gallery, click “Jupyter on LABBENCH™ for a tutorial on howto get set up. (U) Basic Basics: Data and Operations (U) The most basic data types in Python are: + Numbers © Integer (these are “arbitrary precision"; no need to worry whether i's 92 bits or 64 bits, etc) © Float (using 1) forthe imaginary number) + Stings © No iflerence between single and double quotes © Escape special characters (e.g. quotation marks) © Raw sting r'raw string” prevents need for some escapes, © Thiple-quotes allow mutiple line strings © Unicode u'aert \x26 Ernie’ ctype ‘unicode'> + Booleans: true and False (U) We operate on data using + operators, e.g. mathematical operators +, - ; the Keyword in, and others DeclD: 62 functions, which are operations that ake one or more pieces of data as arguments, ©, ‘ype("heLlo") , Ien("worls") , and + methods, which are atached toa piece of data and called from itusing a. to separate the data from the method, e.g. "Hello World”. sp1ie() , oF *abe’ -upper() (U) Deep in the guts of Python, these are all essentially the same thing, but syntactically and pedagogically it makes sense to separate them, (U) Pisces of basic data can be stored inside containers, including + Late + Dictionaries + Sets ‘but well ntroduce those later. (U) The Interactive Interpreter \with that basic background, let's try some things in your Windows command window. Us\privaterpython Python 3.5.1 [Anacondo 2.5.0 (68-b{t)| (default, Jan 29 2016, 15:01:46) [KSC v.1900 64 bit (ANDSA)] on win32 Type “help”, “copyright”, “credits” or “license” for more inforsation. ‘typet5e7) sype(7-1 - 2.2) as 4 this changed tn pythons aB//s nelle” + * wort nelle * + 20 (U) Executing code in a file eo I: 699603 Open the fle Fiest-progran. (U) Ifyou dont have a favorite editor do this: 41. (U) Go to your Jupytr porta at hit. /localnost 8888!roe 2 (U) Pull down the “New” button menu and choose “Text Fle" 3. (U) Click on the "Untitled! et" name and enter the new fle name as “ist-program py" (or anything ending with .y )n your favorite editor (Iuse enacs , but you can use whatever you want), {(U) Type some Python statements int so {(U) Don't forget to save it (File>Save from Jupyte). (U) Torun it, give the fle name as an argument to Python: ‘Make sure the command window is referencing the same folder as the fle. That is U\prvate for most. Hf your command window isnot referencing U:\pivate, do tis: 1 Enter "U 2. Enter “cd private* U:\privatespython Fist-progran.py (U) Nothing appears to happen, because auto-prnting ofthe output ofa function only happens in the interpreter. Fix it up: print(5e7) print(9r43) printcarsi2) (U) Built-in functions and methods (U) Some functions work on almost any arguments that you supply: ‘+ elp(x): shows interactive help ‘+ dir(x): gives the directory of the object, Le. al the methods available + typa(x) tls you the type of x —a type's almost the same as any other object + Ssinstance(a,0) tel fobject a is an instance of & , which must be a type ; something like type(s) = + print ‘+ hasater(a,b): tells whether a has something by the name ; something like & in din(a) + getatr oc ID: 6559693, sa + snput (U) Constructor functions usually tr to do their best with the argments you give, and retum the appropriate data ofthe requested type: ‘+ str tums numbers (and other things) into their sting representations ‘+ Ant: tuncates float , parses str ings containing a single integer, with optional radix (Le. base), eror on complex + lost: parses strings, qlves oat representation of int , eroF on complex ‘+ complex: takes (real, imag) numeric arguments, or parses a str fora single number (U) Other functions only work with ane or two types of data + Numbers: ‘© Functions: abs, round, float , max, min, pow (modula), chr dimes, ete © Operators: Standard math, bitwise: <<, >>, ‘> Methods: Numeric classes don't have methods + Stings: © Functions: ten, ain, max, ord © Operators: +, * (with a number), 9 © Methods: strip, split, startswith, upper. find (U) Exercises: 1. Make a shopping list of ive things you need at the grocery store. Put each item on its own line in a cel. Remember to use quotes! Use print() so that each of your tems displays (ty it frst without). 2. Your groceries ring up as 9.42, 5 67, 3.25, 13.40, and 7.50 respectively. Use python as a handy calculator to add up these amounts 3. But wait You decide you to buy five of th last tom, Re-calculato your total, 4 Using the 1en() function, determine the number of characters inthe string "blood-oxygenation level dependent functional magnetic resonance imaging" (Fun fact: this string isthe longest entry in WordNet 1 index). 5. Pick your favorite snack. Use the * operator to print 100 copies of i. Modify your code to have them print with spaces between them. 6. Challenge: Run >> IsDivisibleBy7(21) Tne >>> isDivisibleBy7(25) False (U) White a function sspivisibiesy(num, divisor) to check if num is evenly divisible by divisor. >>> eDiveibleBy(35,7) True >>> isDivsibleBy(35,4) False (U)Make a function snoue(wora) that accepts a sting and returns tha >>> shout("bananas") ‘BANANAS? ting in capital letters with an exclamation mark. (U) Make a function introduce) to ask the user forthe name and shout it back to them. Call your function shout to make this happen. >>> What's your name? >>> Bob HIBo8! Dec ID: 6589693 Lesson 2 - Variable Exercises (U) Variable Exercises for COMP3321 Lesson 2 (U) Lesson 2 - Variables Exercises (U) Identity the type of each ofthe folowing variables, and add the type after each variable in a comment. > (U) What vale is in variable my_var at the end ofthese assignments? [Add a comparison after the last statement in the form of my_val = oc ID: 6689683, myvar = 99 yvar saa ry_var = ste(ay_var) myvar “= 2 sy_var = Len(ey_var) myvar *= 6 oc ID: 6589693 Lesson Mase Flow Control peer gy voor e00 orf Ti Ge saat = (U) Python flow control wth conditionals and loops (i, wile, for, range, et). UNCLASSIFIED (U) Introduction (U) you have ever programmed before, you know one of the core building blocks of algorithms is flow contro. It tells your program what to do next based on the stat tis currently in (U) Comparisons (U) First. let's lok at how to compare values. The comparison operators are >, >=, <, <=, I=,and == ..When working with numbers, they do ‘what you think: tum True oF False depending on whether the statement is true oF fale. Doe ID: 6689493 (G)Byinon 2x wilt you try to compare any two objects, no matter how ifferent. The results may not be what you expect. Python 3.x only ‘compares types where a comparison operation has been defined. ‘apple’ > “orange” # cose-sensitive alphabetical ‘apple’ > ‘Orange’ ‘apple’ > (‘orange’] ‘apple’ > (‘orange’) (U) We wi leave more discussion of comparisons for later, including how to inteligently compare objects that you create (U) Exercises 1. Write @ you son function that randomly picks a number from your price ist (9.42, 5.67, 3.25, 12.40, and 7.50) and prints. Tese or False depending on whether the random number is greater than 10. 2 White a function snack check that takes string snack and retums ‘True Or False depending on whether ornotitis your favorite snack (U) Conditional Execution: The if Statement {(U) The i statement is an important and useful tol. basically says, "Ifa condition Is rue, do the requested operations" ‘def even(n): AF (982 == 0): ‘rint(*T an even!*) even(2) even(s) ‘even( hello") # tat wos silly (U) What if we want to be abe to say we are not even? Or the user submitted a bad type? We use else and elif clauses ec ID: 6689693 ef even(n): AF (typen) I= Ant) print("T only talk about integers") alte (0 2 == 0): print(*T an even!") se: print(*T an oat") ceven(2) ceven(3) ceven(‘hel1o") (U) Exercises 1. Re-wnite the snack check to take a sting snack and prints an apporpriate response depending on whether the input is your favorite snack or not 2 Wiite an in_grocery_1ist function that takes in a grocery_sten prints a diferent message depending on whether grocery_iten isin your grocery list. 3. Modify in_grocery. list to testi grocery_tten isa sting. Print a message warring the user fits not 4 Challange: Re-write the you won function fo randomly choose a number from your pies list and print appropriale message depending on whether you won (the number was greater than 10) oF not. Also include the amount of change you willbe receiving in your message. (Recall you are winning the amount change you would have owed... 5. Advanced challonge: Write a function that imports datetine and uses it to determine the current time. This function should print appropriate message based onthe time exif the curent tie is between 0900 and 1000, print the message "Morning Lecture time! (U) Looping Behavior (U) The while Loop (U) The while is used for repeated operations that continue as long as an expression is tue. (U) The famous infinite loop: while (2 +2 == 4): prine(“forever") (U) Armistake that may ead to an infinite lop: ac ID: 6649693 ino wtde (1 <= 20): print(i) (U) The below is probably a more sensible thing to type. 1-0 wotte (1 rine 1) ed 2): (U) break and continue (U) For more contro, we can use break and continue (they work just a in C). The break command will break out ofthe ‘smallest while oF for loop: tee wetle(trve): ded int (i) reel ‘break 22) (U) The continue command wid halt the current iteration ofthe loop and continue tothe next value iso wide(trve): tea Af (== 10): eint("T an 101") continue print (i) AF (1 => 20): ‘break oe ID: 6559693 (U) The else clause (U) You can also have an else statement atthe end ofa loop. It will be run only if the loop completes normally, that is, when the conditional ‘expression results in False .A break wil skip it. i-0 nite (1 <2): print) ted aise: print("This executes after the condition becones false.") print("Oone!") wntte (4 <2): print) Sf True: ‘break tea ease: Print("This won't print because the loop was exited early.) prine¢"Done!") (U) Exercises Hint you will not need continue oF break for these exercises. 1. Previously we printed out many copies ofa string using the * operator, Use a whe Toop to printout 10 copies of your favorite snack. Each ‘copy can be on it's own line, tha’ fe. 2 Mocand match! Write @ white loop that uses the to print multiple copies of your favorite snack per line. Print out 10 lines with the number ‘of copes per line corresponding tothe line number (your fist line wil have one copy and your last ne will have 10). 3. Challenge: Write a white loop that prints 100 copies of your favorite snack on one single (wrapped) ine. Hint use = (U) The for loop (U) The for loops probably the most used contol low element asi has the most functionality. It basically says, “forthe following expict items, {do something,” We are going to use the listtype here, More interesting properties of tis type wil flow in another lesson. oe ID: 6589688, for 4 im [2,2,344,55'3" print 1) (U) The variable 4 "becomes" each value ofthe ist and then the folowing code is executed for An (2,243,455, °8°,"b°,"e") print(s, type(s) for € Sn “ovat print c) (U) Exercises 1. Wite a for loop that pnts out each character inthe sting "blood oxygenation lave dependent functonal magnetic resonance imaging” {Fun fact ts ting i the lengest ety in WordNet Index). 2 Te your erocor a of ve ems (or cote ene), Writ opt cto the message "Nott sl, buy.“ and then tho grocery em. 3 Write @ for loop that prints outa numbered Ist of your grocery items, 4. Clearly your favorite snack is more important than the other items on your list. Modify your for loop from Exercise 310 use break sto printing once you have found your favorite snack in your list. Question: Could you have achieved the same result without using a break ? Bonus: if your snack isnt inthe lst, have your code print a warning at the end. 5 Challenge: use the string method split to write a. for loop tha pints aut each word inthe string "blood-oxygenation level dependent functional magnetic resonance imaging” Hint: run help(str- 18's just on object! for 4 in range( 10020): (Ln 2 = 0): print 1) b= range(2, 1090000, 100) » be) on be otal (U) Exercises oF se cane to write a for loop to printout a numbered grocery list. 2 Use enumerate to print outa numbered grocery Ist. Youve now done this three ways. What are some pros and cons to each technique’? ‘There are often several different ways to get the same output! However, usually one is more elegant than the other. 3.Use range 10 wile @ for loop that prints out 10 copies of your favorite snack. How does this compare to useing a while loop? ‘4. Challenge: White a "Guess my number” game that generates a random number and gives your user a fixed number of guesses Use input to get the user's guesses. Think about wat loop type you might use and how you might provide feedback based on the user's uesses. Hint: what type does. input return? You might need fo conver this to a more useful type... However, now what happens if your User inputs something that srt a number? UNCLASSIFIED oe ID: 6689653 Lesson 3 - Flow Control Exercises (U) Flow Control Exercises for COMP3321 Lesson 3 (U) Lesson 3 - Flow Control Exercises (U) Change the loop below so that i prints numbers from 1 to 10. for & An range(s) print (1) (U) Using a for loop and enumerate, write a function getindex(string, character) to recreate the string method .index “skyscraper” index(°c") etindex( “skyscraper, "c") ) Using the shout function from the fist set of basic exercises, wite a shout_words(sentence) function that takes a sting argument and "shouts* f2ach word on its own ne. shout_words("Everybody 1ikes bananas”) # eveRve00"! sans! Do ID: 659603 (G)Whte an exeract_longer(ength, sentence function that takes a sentence and word length, then retums alist ofthe sentence's words that ‘exceed the given length. I no words match the length, return False. extract_longer(S, “Try not to interrupt the speaker.”) # ["interrupt', 'Speoker."] lextract_longer(7, “Sorry about the mess.*) false Doe ID: 6589693 Lesson 04: Container Data Types a] ss . | oo (U) Lesson 04: Container Data Types UNCLASSIFIED (U) Introduction (U) Now that we've worked with stings and numbers, we tum out attention to the next logical thing: data containers that allow us to buld up ‘complicated structures. There are diferent ways of puting data into containers, depending on what we need to do with, and Python has several builtin containers to support the most common use cases. Python's builtin container types include: 4. ust 2 wole 3 atet 4 set 5. frozenset (U)Ofthese, tuple and frozenset are immutable, which means that they can not be changed aftr they are created, whether that's by addition, removal, or Some other means. Numbers and stings are also immutable, which should make the following statement more sensible: the variable that names an immutable object can be reassigned, but the immutable object itself cart be changed. (U) To create an instance of any container, we cal ts name as a function (sometimes known as a constructor. With no arguments, we get an empty Instance, which isnt very useful for immutable types. Shortcuts for creating non-emply List 6, tuple 6, dict s, and even set s willbe covered in the folowing sections. ust() oo ID: 6689693 act) tuple() set) (U) Many bulvin functions and even some operators work with container types, where it makes sense. Later on weil see the behind:-the-scenes. "mechanism that makes this work; for now, well enumerate how this works as pat of the discussion of each separate type. (U) Lists (U)A ist is an ordered sequence of zero or more objects, which are often of cifferent types. I is commonly created by putting square brackets. [ } around a comma-separated isto is iia values: ‘spam’, “eggs, 5, 3.2, [100, 200, 300]] fruit = ['Apple’, ‘Orange’, "Pear, ‘Line (U) Values can be added to or removed from the lst in efferent ways: frult.append( Banana") frutt-insert(3, “eherry") frutt-append({ "Kiet", “watermeton’]) fruttcextend({* Cherry's Banana") fruit renove( Banana") ruse ‘rute-por() ‘rute-pop(2) Frutt (U) THe + operator works tke the extend method, except that it retums a new list a frust oc ID: 689693 Feutt (U) Other operators and methods tell how long a sis, whether an element is inthe Ist, andi so, where or how often itis found. ren(Frutt) fruit. append(‘Apple") ‘apple’ An frutt “Cranbercy’ not in fruse srutt.count( Apple") frutt.index<"Apple") _# Careful--con cause an error fruit.index(apple", 1) (U) List Comprehension (U) Great effort has been to make lists easy to work with. One of the most common uses of als is to iterate over its elements with a for loop, storing ofthe results ofeach iteration in a new ist. Pytion removes the repetitive boilerplate code from this type of procedure with list ‘comprehensions. They're best learned by example: [3 for $ sn range(s0)) [3002 for An range(29)] Ua, a2, for 4 in range(0)) (1a, a2, 1099) for 4 am range(ie) #4 x 2]. # conditionalst [{ej for 4 tn “abede"] for j in ‘ay2"] ——# nesting! (U) Sorting and Reordering oc ID: 6589693 {8} Soting is another extremely common operation on lists. Wall covert in greater detail later, but here we cover the most basic builtin ways of sorting. The sorted function works on more than just List, but aways retums a new ist wih the same contents asthe orginal in sorted order. ‘There's also. sort method on Lists that performs an in-place sot. frult.renove({'Kiwi’, ‘Watermelon']) # can’t compare List with str sorted fruit = sorted(fruit) sorted fruit == fruit Frutt.sore() sorted fruit —~ fruit (U) Reversing the order ofa lists similar, with abuit-in reversed function and an in-place reverse method for ist s. The reversed function retums an iterator, which must be converted back into a list expbcily To sor something in reverse, you could combine the reversed and the sorted methods, but you should use the optional reverse argument on the sorted and sort functions. fruit = List(reversed¢Fruit)) fruit reverse() rfruit == frust sorted(r_frutt, reverse-True) (U) Tuples (U)Much tke a 14st, tuple is an ordered sequence of zero or more objects of any ype. They can be constructed by puting a comma- separated list of tems inside parentheses ( ) , or even by assigning a comma-separated list o a variable with no delimiters at al. Parentheses are heavily overloaded-they also indicate function calls and mathematical order of operations-so defning @ one-element tuple i tricky: the one element must be followed by a comma. Because @ tuple is immutable, it won't have any ofthe methods that change lists, ke append oF sort 2 (3, 2, “first and second") rena sorted(a) a index(2) ‘count (2) oc ID: 6559603 bert, (2, "3 type(b) tuple = 12", cra» «tuple dra © C8") d.ewple = (ds) draw tuple (U) Interlude: Index and Slice Notation (U) For the ordered containers 1st and tuple, as wel as for other ordered types lke st ings, it's offen useful to retrieve or change just one clement ora subset ofthe elements. Infexand slice notation are available to help with ths. Indexes in Python always start at 0. Wel start outwith 2 ew ist and work by example: aninals = ['tiger’, ‘monkey’, ‘cat", ‘dog', ‘horse’, ‘elephant*] aninals(2) ‘aninals(1] = “chimpanzee! ‘aninals[1:3] ‘aninals[3] in aniaals{2:5) animals[:3]—# starts at beginning animals[s:] goes to the end aninals[-2:] aninals(2:6:2] _# uses the optional step parameter animals[::-1] == List(reversed(aninals)) Do ID: 6559603 {0} Because slicing retums a new lst and not just @ view on the list, Itcan be used to make a copy (technically a shallow copy}: sne_aninals = animals 4iferent_animals = aninals(:] sane_aninals[0] = ‘Lion’ aninals(0) ifferent_animals[2] = ‘Leopard’ Ai¢ferent_aninals[0) =~ aninals{2] (U) Dictionaries (U)A dict isa container that associates keys with values. The keys of a can be any type. (U) The dictionary construction shortcut uses curly braces ( ) with a colon : between keys and values (e.g. my_dict = (key: value, Keyl values) ) Alternate constructors are availabe using the dict Keyword. Values can be added, changed, or retrieved using index notation ‘with keys instead of index numbers, Some of the operators, functions, and methods that work on sequences also work wit dictionaries. bugs = {"ant": 10, “praying mantis": 0) {ct must be unique, and only immutable objects can be keys. Values bugst fly") = 5 ugs-update({"spider': 1}) Like extend et bugs spider") ‘ray’ in tugs 5 dn bugs bugs "Aly" (U) Dictionaries have several addtional methods speciicto ther structure, Methods that retum ists, Ike itens keys , and values , are not {Quaranteed to do so in any particular order, but may be in consistent order if no modifications ae made to the dictionary in between the calls. ‘The get method is often preferable to index notation because it does not raise an error when the requested key isnot found; instead, it retums None by default, ora defaut value that Is passed as a second argument. oe ID: 689603 bugs. ttens() # List of tuples bugs keys) bugs values) bugs. get") bugs. get( spider") bugs. getC"spier", 4) bugs-clear() ues (U) Sets and Frozensets (U)A set isa container that can only hold unique objects. Adding something that's already there will do nothing (but cause no error). Elements of ‘a sel must be immutable ike keys ina dictionary). The set and frazenset constructors take any iterable as an argument, whether it's ‘a List , tuple, or otherwise. Cuty braces around a ist of comma-separated values can be used in Python 2:7 and later asa shortcut constructor, but that could cause confusion wth the ict shortcut, Two sets are equalf they contain the same items, regardless of order. manbers = et( (1015 101413130303431202,203,344]) letters = set("ThequickBrownFoxtunpedoverTheL.azyDog" Lower()) 2 aice sre umbers = {05 25 34 4s Sh set runbers. 262) runbers. d6(3) roners. pdate((3, 4, 72) rnbers. 990) 1 could be anything runbers.renove(?) Bo ID; 6689693. ronbers.discard(7) 4 no error (U)A tozen setis constructed in a similar way; the ony ference isin the mutability. This makes frozen sets sultable as dictionary keys, but frozen ‘sets are uncommon, 2 Frozenset((1,1,21,2,3,3,3,3,32,2,2,3,3,4]) {(U) Sets adopt the notation of bitwise operators for set operations lke union intersection, and symmetric diference, This is similar to how the + operator is used for concatenating 1sst s and tuple s. house pets = {"dog', “cat, "fish") farmaninals = {cow's ‘sheep’, ‘pig's “dog's ‘eat"} hovse_pets & farmaninals # intersection house pets | faraaninals union house_pets * faraninals # symetric aifference house_pets - farmaninals # osymetric difference (U) There are verbose set methods that do the same ting, but wth two important iflerence: they accept List 6, tuple , and other terables as rugmens, and can be used to update the set n place. though there are methods corresponding to all the set operators, we give only a few examples. farm_aninal_List = List(farm animals) * 2 house_pets.intersection( fare aninal_ist) house pets. union( farm aninal_list) house_pets. intersection update(farm_animal_list) (U) Comparison of ses is similar: operators can be used to compare two sets, while methods can be used to compare sets with other iterabes, Unlike numbers or strings, sets are often incomparable. house pets = ("dog’, "cat", “Fish') farm_aninals > house pets house pets < farm_aninals oc ID: 6689693, house pets. intersection update (farm aninals) Farm_aninals > house pets house pets. ‘ssubset(fare_aninal_list) (U) Coda: More Built-In Functions (U) We've seen how some built-in functions operate on one or two ofthese container types, bu all ofthe following can be applied to any container, ‘although they probably wort always wrk; that depends on the contents ofthe container. There are some caveats: + (U) When passed a dictionary as an argument, these functions look tthe keys ofthe dictionary, not the values. + (U) The any and 311 functions use the boolean context of the valves of the container, @.g. © is False and non-zero numbers are True , land all stings are Tove except forthe empty string", whichis False + (U) The sum function only works when the contents of the container are numbers. generic container = farm animals or bugs, annals, ete ali generic_container) any(generic_container) “pig? in genertc_container rot An generic container Len(generic_container) snax(generic_container) snin(generic_container) sum(L1, 2, 3, 4, 51) Lesson Exercises Exercise 1 (Euler's multiples of 3 and 5 problem) ee ID: 4689693, Ifve ist all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5, 6 and 9. The sum of these multiples is 2. Find the sum of athe muliples of 3 or 5 below 1000. Exercise 2 ‘Wote a function that takes a ist as a parameter and retums a second lst composed of any objects that appear more than once in the orginal list + duplicates(1,2.36,73,4,56)) should return [3,6] + what should dupicates(Tcow pig goat: hose] return? Exercise 3 \Wrto a function thet takes a portion mark as input and retums the ful classification + convert_lassification(U/FOUO) should return 'UNCLASSIFIED/FOR OFICIAL USE ONLY" + convert_classification(S/REL TO USA, FVEY' should return SECRETIREL TO USA, FEV" UNCLASSIFIED oe ID: 689693 Lesson 05: File Input and Output pene upate almost 2 years ago by [Tin chi sz ated ee (U) Lesson 05: File Input and Output UNCLASSIFIED (U) Introduction: Getting Dangerous (U) As you probably already know, input and output isa core to! in algorithm development and reading ftom and wring to files is one ofthe most ‘common forms. Lo’ jump right in justo Soe how easy itis to write a fe, myfile = open(“data.txt", ‘w") myfile.write("I an writing data to ny file") yfile.close() (U) And there you have it You can write data to fles in Python. By the way, the variables you put into that open command are the filename (asa sring-do not forget the path) and the fle mode. Here we are wring the fle, as indicated by the x’ as the second argument 1 the open function. (U)Lettear apart what we actually did ‘open(data.tet', "w") (U) This actully returs something caled a fle object. Let's name it (U) EEE) Opening a fie that already esas for wring will erase the original fle. myfile = open(‘data-tet", ‘w") oo ID: s6s9693, (U) Now we have a variable to this fle object, which was opened in write made. Let try to wre to the fe: ryflle.write("r an writing data to my File") ryfile.read() # 0ops...notice the error nyfile.close() * Guess what thot ata (U) There are only a few file modes which we need to use. You have seen "x (wring), The others ‘are 'r* (reading), "3° (eppencing), ‘r+* (reading and wring), and s* (binary mode). myfile = open(‘data-txt', ‘") myfile.read() mflle.wrtte("r am writing more data to my file") Oops again. .check our mode data ~ myfile.read() iydata 1 HEY! Mere did the dota 90. myfite.close() # don’t be 0 piggy (U) Aco! way o use contents of a fi in a black is withthe ith command, Formally, this is called a context manager. Informally, it ensures that the fle is closed when the block ends. saith open(*data.txt") as F: print(.read()) (U) Using sien is a good idea buts usualy not absolutly necessary. Python tries to close files once they are no longer needed. Having fles open | not usually a problem, unless you try to open a large number al at once (@.9. inside a oop). (U) Reading Lines From Files (U) Here are some of the other useful methods for le objects: Lines _File ~ open(fewlines.txt", ‘w') Lnes_file.writelines(“First\n") Lines_File.writelines({"second\n", “thtrd\n"]) Lines_file.close() ‘open(*Fewlines.txt", 0") 1ines_File.read2ine() 1ines_file. read2ine() nes_Fie.read2ine() nes_File.readzine() (U) And make sure the fe is closed before opening it up again in the next cell ines_File. close() (U)Aternatety ines = open("Feulines.txt", ‘r*).readlines() # Note the plurality ines (UE both reas and resaine(s) have optional size arguments that iit how much s read. For resdline(s) this may return incomplete lines. (U) But what ithe file is very long and | dont need or want to read all of them at once. ie objects behave as ther own iterator. Lines_File = open( "Fevines.txt", 'r") for line in 1ines File: rine 2ine) ‘The below syntax is avery common formula for reading through files. Use the with keyword to make sure everything goes smoothly. Loop through the file one line ata time, because often our les have one record to a line. And do something with each line. with open("feulines.txt*) as my_flle: for Line in my file: print(Line.strip()) # The strip function removes newlines and whitespace fran the start and fintsh “The fle was closed upon exiting the with block. (U) Moving Around With tell and seek oe ID; 6649603 (0) The tei method returns the current posiion ofthe cursor within the file. The seek command ses the current postion of the cursor within the fle, Inputfile = open(‘data.txt", 'r") Anputfie.t011() Anputfite.read(s) Anputfie.te110) Anputfite. seek(0) input file. read() (U) File-Like objects (U) There are other times when you really need to have data in a fie (because another function requires it be read from a fle perhaps). But why waste ime and disk space if you already have the data in memory? (U) A very useful module to make a sting into a fle-ike object is called Stringro . This wil ake a sting and give it fle methods lke read and write snport 0 systeingfile = f0.string10() 4 For handing bytes, use to.Bytesto systringftle.welte("Ts 4s ay datal) # he Just wrote f0 the object, not a Fllehandle systringttte.read() 4 cursor {s at the end ystringtite.seek(0) systringtite.reaa() nevstringfile = Jo.stringlo("wy data") # The cursor will autonaticatly be set to @ (U) Now les pretend we have a function that expects to read data from a file before it operates oni. This sometimes happens when using ibrary functions. def iprintaatac) rint(F.read()) torintdata(‘mdata’) rrr "y 40 « f0.Stringio( ‘nydata*) Sprintdata(ey so) # vay Lesson Exercises Get the data ‘Copy sonnet from htios.Jur.nsa.ic.qavlvixGam and paste into sonnet. txt. Exercise 1 Write a function called 1 input file with only the ie_capitalize() that takes an input fle name and an output fle name, then writes each word from the input fi Wy ‘rst letter capitalized tothe output file. Remove all punctuation except apostrophe. capitelize("sonnet txt’, ‘sonnet_caps.txt") => capitalized words written to sonnet cap Exercise 2 ‘counts for each word. Remove all punctuation {il @ function called fle_word_ count) that takes a fle name and retums a dictionary containing the counts for ‘except apostrophe. Lowercase all words. File_word_count(“sonnet txt") => { "at's 4, tne: 2, oa} Extra Credit Wie the counts dictionary toa file, one key-value per line. UNCLASSIFIED oc ID: 6689693 Lesson 06: Development Environment and Tooling Created over 3 years ago by [Tin cove saat sya OO Oo es (U) Lesson 06: Development Environment and Tooting (U) Package Management (U) The Problem: Python ha leaves something to be desired + Python doesn'thave a classpath and unless you are root , you cant intall new packages forthe whole system, + How do you share a script with someone else when you dont know what packages are installed on ther systom? () G)rP.b, 86=3E atteres included" philosophy-it has a comprehensive standard library, but by default, using other packages + Sometimes you have to use Project A, which relies on a package that requires awesome-package v.11, but youre writing Project B and want fo use some features that are new in awesome-package v.20? + The bestin-class package manager isn in the Python standard ibrar. (U) The Solution: virtualenv (U) The vintuaieny package creates virtual environments, i. isolated spaces containing ther own Python instances. It provides a uly script ‘that manipulates your environment to activate your environment of choice. (U) Its already installed and available on the class VM. The -p flag indicates which Python executable to use asthe base for the vitual ‘environment Doe 1D: 6659693 (3.35 wh ctaatony neweny p_Jusr/locat/bin/nython new python executable Installing Setuptoots Installing Pip.» Sta use[iocatoineython — = NEMENY/n/ pyERon C—O i (U) The virtuateny package can be cowoloadeand fin gs a Sip o create a vitual environment based on any recent Python installation. A Viual environment has the package manager hip preinstalled, which can be hooked ito the internal mor of the Python Package Index ()2!) by exporting the correct agdress'to the, 0" INDEX URL environment variable: (32s cc srt ne Um Ieap/Ttbrantiacep area. 1.40/97 (F238 pytnon Python 2-7-5 (default, Nov 6 2013, 10:23:48) [Gee 4.8.7 20120313 (Red Hat 4.4.7-3)] on Linux? Type "help", "copyright", “credits” or “License” for nore information. Amport requests Traceback (ost recent call last): File “estdin>", Line 1, in Inportrror: No module naned requests ext, oe 1D: 6589593 (323s spurge nenenv/oinyactivate (Fis spires WORN LEESEE: Downloading/onpacking requests Downloading requests-2.0.0.tar.gr (362K8): 362K8 downloaded Running setup.py ega_info for package requests Installing collected packages: requests Running setup.py install for requests ce sucessfully installed rests + Cleaning vp. (0) 2 yon Python 2.775 (Gefwolt, Nov. 6 2013, 10:23:48) [ce 4.4.7 20120913 (Red Hat 4.4.7-3)] 09 nun? Type helps “copyright, “ered” or “License” for sore infizsion. Amport requests vrequests.__version_ "2.0.0" Amport sys sys.path C', ‘none —Sieweue7io/pytnon27.249", */mone{——ueuenv/si0/oytnonz.7°, * ‘mone newenv/1sb/pytiona.7/pat- Linixa', “home f———NEWENV/11b/pythok2.7/14b-tk", "7none/[——/NEWENY/14b/python2.7/14b-084", ‘hone /[—__NENENV/14 b/python2.7/1ib-dynlbad', */usr/lecalylib/python2.7", '/usr/local/1ib/python2.7/plat-Linux2", ‘/usr/local/1ib/python2.7/11b-t 1)" Pnoe/P NEMO Ab python. 7/site-packages} ext) (W000 =F “5 tp Frere woetrefeces? oc ID: 6559693 ‘Now we have a place to install custom code and a way to share it! + Develop code inside ~/neven/1ib/python2.7/site-packages + Capture installed packages with pip freeze >> requirenents.txt and install them to anew virtualenv with pip snstall requirenents.xt (U) The Ultimate Package (U) 8ython is an alternative interactive shell for Python with los of cool features, among which are: ‘a web-based notebook interface with easy-o-share fies, and + distributed computing (dont ask about tis) (U) To get state: oe 1: 6685698 (nese ——, Domnloading Spython-12.6 Successfully installed ipython Python 2.7.5 (Gefault, Nov 6 2013, 10:23:48) Type “copyright”, “credits” or “License” for more information Python 1.1.0 == An enhanced Interactive Python 2 ~> Introduction and overview of Python's features. quickret -> Quick reference help -> Python's own help syste. object? => Details about ‘object", use ‘object??* for extra details. an (1): Is BASE3/ Hello Moré. html Hello World. ‘pyn NEMENV/ na}: hist as In[3}: smport 08 Inf}: os.path #press tab foes os.path os-pathconf os,pjtiicont_paes os.pathsep In (4): os.path eae (U) Touse the web interface, you hadg 1 install sjplemental packages: (WEVENY =F 245 in inetd pyena tornado Jinja2 pygnents (Nee) ————T = py notebook --no-nathjex (uss mo mae potages a ret o et awasoe nin rahies I (NEMENV) [LJ ~1 pip install matplotlib oe ID: 6559693 Lesson 07: Object Orienteering: Using Classes Updated. 9 months ago by 23 me (U) Introduction to classes, objects, and inheritance in Python UNCLASSIFIED (U) Introduction (U) From the name oft you can see that object-oriented programming is oczing with abstraction and complication. Take heart: there's no need to ‘ear or avoid object-oriented programming in Python! Is just another easy-to-use, flexible, and dynamic tool in the deep toolbox that Python makes avaiable. In fac, we've been using objects and object oriented concepts ever since the fist ine of Python code that we wrote, so ifs already {amili. In this lesson, well think more deeply about what itis that we've been doing all along, and how we can take advantage ofthese ideas. (U) Consider, for example, the diference between a funetion and a method: name = hark™ en(nane) function rame-upper() # method (U) Inthis example, ane is an instance of the st type. nother words, mane is an object ofthat ype. An objects just a convenient wrapper ‘around a combination of some data and functionality related to that data, embodied in methods. Until now, youve probably thought of ‘every str justin terms of its data, ie. the literal string “hark that was used to assign the variable. The methods that work with ane were defined just once, ina class definition, and apply to every string thatis ever created. Methods are actually the same thing as functions that lve inside a cass instead of outside it. (Tis paragraph probably sill seems really confusing, Ty rereading it atthe end ofthe lesson!) a ID: 659693 (U) Your First class (U) Just asthe keyword def is used to define functions, the Keyword class is used to define a type object that will generate a new kind of objec, hich you get to name! As an ongoing example, well work wih a class thal wel choose to name. Person = ‘lass Person(object) pass ‘ype(Person) *ype(Person) == typecint) nebedy ~ Person) ‘ype(nabody) (U) Atfist, the Person class doesn't do much, because i's totaly empty! This isnt as useless as it seems, because, just Ike everything else in Python, classes and their objects are dynamic. The (sbject) afer Person is not a function call; here it names the parent class. Even though the Person class looks boring, the fundamentals are there: + the Person clas is ust as much ofa class as int or any other bullin, + we can make an instance by using the class name as a constructor function, and + the type of the instance nobody Is Person just ike type(2) I int (U) Since that's about all we can do, le’ start over, and wrap some data and functionality into the Person class Person(object): Species = “Hono sapiens” ef tatk(sel®): ‘return "Hello there, how are you?” nobody = Person() nobody. species nobody. talk() (U) Its very important to give any method (:. function defined inthe class) atleast one argument, which is almost always called self. This is because internally Python transates nobody. talk) into something ike Person.talk(nobady) (U) Lets experiment with the Person class and its objects and do things like re-assigning other data atrbutes. sonebody = Person() Bo ID: 689693 sonebody.species = ‘Homo internetus’ sonebody name = “Mark” robody. species Person. species = "Unknown" nobody. spectes somebody. species erson.nane = “Unknoxn” nobody.nane sonebody rane ‘det soncbody.nane sonebody name {(U) Attrough we could add a nase to each instance just after creating i, one ata time, wouldnt tbe nice to assign instance-specifc attributes tke that when the object is frst constructed? The __init_ function lets us do that. Except fr the funny underscores in the name, i just an ordinary function; we can even give it default arguments. lass Person(object): Species = “Hono sapiens” def _init_(selF, nane-"Unknom", age-18) Self name = rane self.age - age def talk(seif): return “Hello, my nane is ().*format (self nane) mark = Person("Mark”, 33) everic_voter = Person() generic worker = Person(age-<1) seneric worker. age oc ID; 6559693 generic worker. name (U) In Python, it int unusual to access attributes of an object directly, unike some languages (e.9 Java), where that is considered poor form and ‘everything is done tough getter and seer methods. This is because in Python, attributes can be added and removed at any time, so the getters and setters might be useless by the time that you want to use ther, mark, favorite color » “green del generic_worker rane generic worker, name (U) One potential downside is that Python has no real equivalent of private data and methods; everyone can see everything. There is a polite convention: other developers are supposed to treat an atrbute as private fits name stats with a single underscore (_). And there is also 8 trick: names that start with two underscores ( _) are mangled to make them harder to access. (U)The __init__ method is just one of many that can help your class behave lke a fullledged built-in Python object. To control how your object 's printed, implement __str__, and to control how itlooks as an output from the interactive intorprter, implement _repr__ Ths ime, we wont lar rom scratch; weil add these dynamically ef person_str(self): return "Mane: {8}, Age: {2)".format(self.nane, self. age) Person._str__ = person_str ‘def person_repr (self) return "Person({@) {2))* format(self.nane, self age) Person.__repr__ = person_repr print (mark) # which special method does print use? snark which special method does Jupyter use to auto-print? (U) Take a minute to think about what just happened: ‘+ We added methods toa class after making a bunch of object, but every object in that class was immediately able to use that method. + Because they were special methods, we could immediately use builtin Python functions (Ike str )on those objects. (U)Be careful when implementing special methods. For instance, you might want the default sort of the Person class tobe based on age. The special method __1¢_(self,other) willbe used by Python in place ofthe builtin 1¢ function, even for sorting. (Python 2 uses cep instead.) Even though its easy, this is problematic because it makes objects appear tobe equal when they are just ofthe same age! Doe ID: 6589693 ‘def person_ea(self, other): return self.age == other. age Person._eq_= person_ca bob = Person("t 33) bob == mark (U) na situation ike ths, it might be beter to implement a subset ofthe rich comparison methods, maybe just it _ and _ gt more complicated __eq__ function that is capable of uniquely dentying all the objects you wil ever creat. (U) Wile we've shown examples of adding methods to class after the fact, note that itis rarely actually done that way in practice. Here we did ‘that just for convenience of not having fo re-define the class every lime we wanted to create a new method. Normally you would just define all lass ‘methods under the class itself If we were to do so withthe _str_, _+ 1nd _eq_ methods for the Person class above, the class would lke the below: lass Person(object): species = “Hono sapiens” def _init_(self, name-"Unknown", age=18): Gelf.nane = name self age ~ age ef talk(sel#) return “Hello, ay nane 1s ().".format (self. name) def _str_(self)) ‘return "Name: {0}, Age: {1)".format(self.nane, self.age) ef _repr_(self): Feturn "Person( "(0)", (1))*format(self.nane, self.age) def _eq_(self, other): ‘eturn self. age == other.age (U) Inheritance (U) There are many types of people, and each type could be represented by is own class. It would be a pain ifwe had to reimplement the fundamental Person traits in each new class. Thankfuly, inheritance gives us a way o avoid that. We've already seen how it Works: Person inherits from (ois a subclass of) the object class. However, any lass can be inherited from (0. have descendants), Doc ID: 6689693 class student (Person): bedtime = "Midnight” (def do_honework (self): Anport tine print ("T need to work.) ine. s1e¢p(5) print("Oid 1 Just fall asleep?) tyler = student(“Tyler", 19) tyler. species tyler-talk() tyler. do_honework() (U) An objec from the subclass has all the properties ofthe parent cass, along with any additions from its own class definition, You can sil easy to ‘override behavior fom the parent clas easily-just create a method with the same name inthe subclass. Using the parent class's behavior inthe child clas is ticy, but fun, because you have to use the super function, ‘class Employee(Person): ef talk(sel¥): talk_str = super(Eaployee, self).talk() return talk str +" T work for {}*.format (self. eaployer) fred = Employee("Fred Flintstone”, 55) fred-enployer = "Slate Rock and Gravel Company” fred. talk() (U) The syntax heres strange a frst. The super function takes a class (Le. a type ) 8s frst argument, and an object descended from that lass as its second argument. The object has a chain of ancestor classes. For fred that chain is Employee, Person, object] ‘The super function goes through that chain and returns the lass thats afferthe one passed asthe functions fist argument. ‘Therefore, super can be used to skip up the chain, passing modifications made in intermediate classes, (U)As a second, more common (but more complicated) example, i's often useful to add addtional properties to subclass objects in the constructor. oc ID: 6589693 class Employee(Person) def _init_(self, nane, age, enployer): Super(Employee, self). init_(nane, age) self.enployer ~ exployer ef talk(sel#): talk_str = super(Eaployee, self).talk() return talk.ste +" T work for {)*.Format(self. employer) fred = Eaployee("Fred Flintstone", 55, "Slate Rock and Gravel Company") fred. talk() (U)A class in Python can have more than one listed ancestor (which is sometimes called polymorphism). We wont go into great detail here, aside from pointing ou that exsis and is powerful ut complicate. ‘lass Studenttaployee(student, Employee): pass ann = studentEmployee("ann", 58, "Family Services”) ann.talk() bLLL = Studentemployee("bi1I", 20) # what happens here? why? (U) Lesson Exercises (U) Exercise 1 (U) White a Query class that has the folowing attributes: + classification + justiteation + selector (U) Provide default value foreach attribute (consider using one). Make itso that when you print, you can display al ofthe attributes and their ‘values nicely. # your class definition here (U) Afterwards, something ike this should work: oc ID 6689693 query! = Query("SSASEHREL IGG SISK, “Primary email address of Zendian diplonat”, “ileonafstato.gov.24") print (query) (U) Exercise 2 (U) Make a RangedQuery class that inerits from Query and has the adltional attributes: + begin date + end date (U) For now, just make the dates ofthe form YYYY-MIM.DD. Dont worry about date formating or eror checking for now. Wel tak about the satetine module and exception handling iter. (U) Provide defaults fr these atibutes, Make sure you incorporate the Query class's ializer into the RangedQueryinalzer. Ensure the new ‘lass can also be printed nicely. 1 your class definition here (0) Atgewards this shoul woe: query2 = RangedQuery(SeiiGEtiRetHe-Noh FHM, “Primary IP address of Zendian diplonat", "18.254.18.162", "2016-12-01", "261 print (query2) (U) Exercise 3 (U) Change the Query class to accept alist of selectors rather than a single selector. Make sure you can sil rit everthing OK. UNCLASSIFIED ee: 6589603 Lesson 07: Supplement est ‘months ago by[————Tia CONE 3824 ce (U) Supplement to lesson 07 based on exercises from previous lectures. ‘You may have written function lke this to check ifn tem is in your grocery stand print something snarky if ts not: ef Sn my ist (Aten): ay list = ['apples* AF item in my List: return "ot St!" else: ‘return ‘Nope! * ‘nt2k*, "butter', ‘orange Juice’) in_ny_Ase( apples") in_ay_List("ehocel: ) ‘But what if really wanted chocolate tobe on my ist? | would have to rewrite my function. I had writen a class instead ofa function, | would be ‘able to change my list. class ny List (object): ny list = ['apples' ,’milk',"butter’, ‘orange Jutce"] def in_ny List (self, item): AF item in self ay List: return ‘Got itt” ese: return ‘Nope! * oo ID: 6689693 ecenber = Hy 1ist() december. in my list(‘chocolate") ecenber.ay list decenber my list +['chocolate"] ecenber.in my List(‘chocolate") Now Ihave a nice template for grocery ists and grocery list behavior jan = my tist( decenber-ay list jon.my ist ‘This ont helpful: print (decenber) So.we overwrite the _tr__function we inherted from object: lass Wy List(object): ry list = [‘apples", ‘milk’, ‘butter’, ‘orange juice’) def _str_(eelf): ‘return "hy List: ()° format(*, *Join(self.ay_1ist)) def _repr_(self): ‘return self._str_() def in_ay_List (self, tem): Af Stem An self ay List: return “Got iti” else: return “Nope! " ecenber = Hy_List() print (decenber) december Maybe | also want to be more easly testify favorite snack ison thelist... oe ID: 6689653, lass My. list(object): ay list = [‘apples', ’at2k*, ‘butter’, ‘orange Juice”) ef _init_(selF, snack-' chocolate’): Belt snack = snack def _str_(self): return "Wy List: ()° format(", *-Join(self.ay_list)) (def Sn_my_1ist (self, stem) AF Stem in self my List: ‘return ‘Got Sti" ase: ‘return ‘Nope! * (def snack check(sel#): return self.snack dn self.9y list ‘my favorite snack ts chocolate... But tn Jonuary I'm going to pretend it's oranges jan = My_List(‘apples") an. snack check() ‘s8ut in February, I'm back to the defoult feb = My 1st) ‘Feb. snack_check() ‘About that object. ‘ir(object) These are all the things you inher by subclassing object. lass caps_List hy list): ‘def in-my_List(self, item): response = super(caps_list, return response.upper() AF) Anny AAst( item) shouty = caps 1ist() shouty-in_my_List("chocolate’) ir(caps aust) You can also call the super class directly, ike so: oe ID: 6589693 lass caps_List (my list): def in_ny_List(self, item): "© but you stilt have to pass self response = Hy_list.in_ny_list(self, item) ‘return response.upper() shouty = caps 1ist() shouty.in_ay_list( chocolate’) ‘Super actually assumes the correct things... Most of the time. lass caps_List (my_list): def in ay List(self, stem): ‘response ~ super().in_ay_list(item) ‘return response.upper() shouty = caps_14st() shouty.in_ay_list( chocolate’) hetp¢super) Doc ID: 6689653 Lesson 08: Modules, Namespaces, and Packages Upaigd rr 2 er apo bp — ici aera = (U#FUO) A lesson on Python modules, namespaces, and packages for COMP3321. UNCLASSIFIEDHFOR-OFFIGIAL USE-ONEY (U) Modules, Namespaces, and Packages (U) We have already been using modules quite a bit — every te we've run Inport, in fact. But what is a module, exactly? (U) Motivation (U) When working in Jupyer, you don't have to worry about your code disappearing when you exit. You can save the notebook and share it with ‘others. A Jupyter notebook kind of behaves like a python seript: a tex fe containing Python source code. You can give that fle tothe python Interpreter on the command line and execute al the code inthe fle (kind of like "Run Alin a Jupyter notebook}: $ python awesome.py (U) There are a few significant liitations to sharing code in Jupyter notebooks, though: 4. what if you want to share with somebody who has python installed but not Jupyter? 2 what ityou want o share part f the code with others (or reuse part of it yourself}? oc ID: 689603 "Swati you're writing a large, complex program? (U) Alot these do have native solutions in Jupyter: 4. convert the notebook o a script (Fle > Download as > Python) 2 copy-paste..? 3. make'a big, messy notebook... (U) but they got unwieldy fast. This is where modules come in, (U) Modules (U)Atits most basic, a module in Python is really just another name fora scrip. I's just a fle containing Python defritions and statements. The flename is the module's name followed by a py extension. Typically, though, we don't run modules directly ~ we import their defnions into our ‘own code and use them thee. Modules enable us to write modular code by organizing our program into logical units and putting those units in ‘separate files. We can then share and reuse those fle indvidualy as pars of other programs. (U) Standard Modules (U) Python ships with a library of standard modules, so you can get prety far without writing your own. We've Seen some ofthese modules already, ‘and much of next week willbe devoted to learning more about useful oes. They are documented in full detain the Pyition Standard Liorary ‘ference. (U) An awesome example (U) To understand modules better let's make our own. This wil put some Python code in a fle called avesone.py inthe current directory clase Avesome object): def _init_(self, avesone_thing) Self.thing = avesone_thing def _str_(self): return "{@.thing) 4s avesone!!-foraat(self) a = fwesone( Everything”) print(a) with open(‘awesone.py', "w") as #2 F.weite(contents) 1: 6689685, {(G)Now you can run python awesone.py on the command line as a Python script. (U) Using modules: import (U) You can also import swesone.py here as a module: Anport avesone (U) Note that you leave out the file extension when you impor it. Python knows to lok fora le in your path called awesome. py (U) The frst time you impor the module, Python executes the code inside it. Any defined functions, classes, ec. willbe availabe for use. But notice ‘hat happens when you try to impor it again: Anport avesone (U) ts assumed thatthe other statements (e.g variable assignments, print) are there to help initialize the module. That's why the module is only run once. Ifyou try to import the same module twice, Python will not re-run the code ~ it wil refer back to the already-imported version. This is helpful when you import multiple modules that in turn impor the same module. (U) However, what the module changed since you last imported it and you really want to do want to resimport i? contents = class Avesone object): def _init_(self, awesone_thing): Self. thing = avesome thing ef _str_(self): return “{@.thing) 45 awesonel!!*format(sel#) ef cool(group): return “Everything is cool when you're part of (@)".Format(group) 1 = Awesone( Everything”) prine(a) vith open(‘awesone.py', 'W") a5 £2 F.wrive(contents) (U) You can bring in the new version with the help ofthe nport1sb- module import inportlib {nportiib.reload(awesone) Doc ID: 689603 (UY'calling the module's code (U) The main point of importing @ module isso you can use its defined functions, classes, constants, etc. By default, we access things defined in ‘the awesone module by prefixng them with the module's name. print (awesone.Anesone("A Nobel prize") mesome cool" tean") print (awesone. a) (U) What ifwe get ted of wing avesone all the time? We have a few options. (U) Using modules: import __ as (U) Fist, we can pick a nickname forthe module: import avesone as ave print (owe. Awesone("A book of Greek antiquities”)) awe.cool("the Python developer comunity”) print (awe. a) (U) Using modules: from ___ import ___ (U) Second, we can import specific things from the. suesone module into the curent namespace: from avesone import coo} cool("this class") print(Awesone("A piece of string")) # will this work? print(a) # will thes work? (U) Get everything: from ___ import * (U) Final, if you really want to import everything from the module into the curent namespace, you can do this Doc ID: 6589693 ‘fron awesone import * 7 BE CAREFUL (U) Now you can re-run the cals above and get them to work (U) Why might you need to be careful with this method? 1 what {F you had defined thts prion to import? ef 001) return "Something important 4s pretty cool” impart «the ‘arabes are imported into your current namespace. Think ofa namespace as a super-variable that contains references to lots of eer variables, or ‘8 a superclass that can contain dat, functons, and classes. (U)Ater import, a modules dynamic ike any Python objec: for example, the oeloss function takes @ module as an argument, and you can add data and methods tothe module afer you've Imports i (but they woa' perit beyond the Iftime of your script or session). Amport my_funcs def si1ly_fune(x): return "Silly ()!*format(x) re sflly_fune = stlly_fune we stlly_fune(“hark™) Silly Markt (U)Incontrast, the from cmodule> ‘eport Command adds the function tothe current namespace, (U) Preventing Excess Output: The Magic of __main__ (0) Suppose you havea script that does something awesome, called avesonepy class Aesone object) def _ init__(self, awesone_thing): Self. thing = awesone_thing def _str_(seif) Feturn “{@.thing) 4s AWESOME.” Format( self) 12 = Awesome( "BASE Jumping”) print(a) (U) This can be executed from the command line oroported:* ns Cooma re (vw) bython oc ID; 6689693 mort avesone BASE Dumping 15 AWESOME Traceback (gost recent call last) File “estéin>", Line 1, in enodule> NaneError: mane “a {5 not defined ‘awesome. twesome object at @X7fa222a8b420> print (awesome. 2) BASE Jumping 1s AVESONE (U) You don't want that print statement to exeoute every time you import. Of equal importance, avesone.s Is probably extraneous within an Import. Lot's fix io get rid of those when you import the module, but keep them when you execute the script class Awesome object): def _init_(self, awesone_thing): Self thing = avesone thing def _str_(seif): Feturn "{@.thing} 4s AWESOHE.~.Format(self) 4 _name_ == *_ ain 1 = Avesome("BASE Jumping”) print(a) (U) We can do even beter. There are some situation, e.g. profiling or testing, where we would want to impor the module, then look at what would happen if we run its a script To enable that, move the main functionalty into a function called main() oc ID: 6689695 lass Avesone object): def _init_(self, avesone thing) Selfthing = avesone thing def _str_(self): Feturn "{@.thing) 4s ANESOHE.”.Format(sel#) def main): 1 = Auesome(“BASE Jumping") prine(a) $F names *_nain_ ain() (U) From Modules to Packages (U)A single Pytnon module corresponds oa file. Its not hard to imagine a situation where you have several related modules that you want to group together in the equivalent ofa folder; the Python term for this concept isa package. We make a package by + creating a folder + puting sripts/modules inside it + ding some Python Magic (which obviously willinvolve __ in some way, shape, or form) (U) For example, wel put ouesone.py in a package called feelings-later on, well add terrible.py and totally_rad.py The directory structure ie feelings/ J awesone.py fF _inte_py Tain py (U) The _snit_.py fle is REQUIRED; without it, Python won't identily this folder as a package. However, __main_py is optional but nice; if you have i, you can ype python feelings and the contents of main _.py willbe executed as a sript. (NB: Now you can postulate on what F Lane == "ain": i really doing (U) The _init_.py fle can contain commands. Much ke the _init_() function ofa class , the _init_py is executed immediately ‘after importing the package. One common use is to expose modules as package attributes; allthis takes is sport «nodule nane> in the package's init__.py Me, oe ID: 6589693 (U) Onward to the Whole World Pretty soon, youll want to share the feelings packages witha wider audience. There are thousands of people who want to do Auesone stuff, but " Mine 2 ‘symtaxérror: Snvalid syntax (U) Python could not parse what we were trying to do here (because we forgot our colon). I di, however, let us know where things stopped making ‘sense, Note the printed line with an ny arrow (“pointing to where Python thinks there isan issue (U) The statement syntaxtrror: invalid syntax Is an example ofa special exception called a SyntaxError. Its fay easy to Soe what happened here, and there is not much to do besides fing your typo. Other exceptions can be much more interesting, (U) There are many types of exceptions: Anport butttins # This will display @ Lot of output. 4 To wake it scrollable, select this cell and choose 4 Cell > Current Output > Toggle SeroLLing help(buittins) 4 Python 2 used to have this info in the “exceptions” module f python 2 moved it into “builtins” for consistency 48 50 for python 2, try this instead Anport exceptions ‘ir(exceptions) (U) bet we can make some ofthese happen. Infact, you probably already have recent 10 ef £0: ve import chris fie open( ‘data’, Fite. read() Do ID: 6589693 (U) Exception Handling (U) When exceptions might occur, the best course of action to to handle them and do something more useful than exit and print something to the soteen. Infact, sometimes exceptions can be very useful tools (e.g. Keybosrdinterrupt ). In Python, we handle exeptions with the try and except commands. (U) Here is how it works: 41. (U) Everything between the try and excest commands is executed, 2 (U) that produces no exception, the except block Is skipped and the program continues. 43 (U) fan exception occurs, te rest ofthe try Block ie skipped! 44 (U) tthe type of exception is named after the except Keyword, the code after the except command is executed. 5 (U) Otherwise, the execution stops and you have an unhandled exception. (U) Everything makes more sense with an example: ef F00: ty: ‘print (°I am going to convert the input to an integer") print (int(x)) except ValueError: ‘Print ("Sorry, Twas not able to convert that") Fo) #2) FC") (U) You can add mutiple Exception types tothe except command: except (Typetrror, Valuetrrer) (U) The keyword as Jets us grab the message from the error: Do ID: 6589693 ‘def be_careful(a, b): wy: print(Float(a)/#loat(b)) except (Valuetrror, TypeError, ZeroDivistonérror) as detail: print("Mandled Exception: “, detail) except: ‘Print “Unexpected error!*) ‘finaly: ‘rd (“THIS WELL ALWAYS RUN") be_careful(s0) be_careful(i (2421) be_careful(1,"two") be _careful(15"*400,1) ‘Aloat(. 128) (U) Weve also added the Finally command. twill always be executed, regardless of whether there was an exception or not, 80 t should be used {8a place to clean up anything let over from the try and except clauses, e., closing files that might stil be open. (U) Raising Exceptions (U) Sometimes, you wil want to cause an exception and let someone else handle it. This can be done wih the aise command. raise TypeError("You submitted the wrong type") (U) no builtin exception is suitable for what you want to raise, defining a new type of exception is as easy as creating @ new cass tha inherits from the exception type. ‘lass nypersonalerror( Exception pass raise MyPersonalérror("I am mighty. Hear my roar!) oe ID; 6589693 def tocater (ayLocation) AF (nylocation» factortal(s) xe ef Factorial(n): sr'neturn the factorial of n, an exact integer >= >>> [factortal(n) for # An range(6)] [4 1, 2 6 24, 120) >>> factorsal(30) 265252459812191058636208480000000 >>> factorsal(-4) Traceback (nost recent call last Valuetrror: must be >= @ Factorials of Floats are OK, but the float must be an exact integer: >>> factordal(30.1) ‘Traceback (nost recent call last): Valuaérror: n must be exact integer do» factorsal(3@.0) 265252859812191058636308¢80000000 Tt mist also not be ridiculously Lange: >>> factorial(1¢100) Traceback (nost recent call last): overFlovError: n too larg import math Af not n= raise ValueError(" must be >= AF nath.Floor(n) |= 1: oe ID: 6559693 raise Valuetrron(“n must be exact integer”) Af mri == ni # catch 2 volue Like 10200 raise OverflovError(n too large") result = 2 factor = 2 whkle factor 200: raise StopIteration for y in self.avoid multiples: Af self.x Ky = 8: self x break ores else: return self. ef _iter_(self): ‘return self stlent_Fizz_buzz ~ NonFactortterable(3, 5) [x for x in stlent_fz2_bur2] rnostly_prine = NonFactortterable(2, 3, 5, 7% 12, 13, 17, 19) partial_sun - @ oc ID: 6559583 for x in nostly_prine: partial sum x partial_sum mostly prine = NonFactortterable(2, 3, 5, 7 11, 13, 17, 19) print (un(aostly_prine)) (U) tmay seem strange thatthe __‘eer__ method doesnt appear todo anything. This is because in some cases the terator for an objec should not be the same as the object itself. Covering such usage is beyond the scope of the course, (U) There is another way of implementing custom iterator: the __getiten_ method. This allows you to use the square bracket {] notation for ‘gottng data out of the object. However, you stil must remember fo raise a Stopiteration exception fort to work properly in for loops and ist comprehensions. Another iterator example Inthe below example, we create an iterator that retums the squares of numbers. Note that in the __next_ method, all we're doing is iterating our counter ( sei.» ) and returning the square ofthat counter number, as long as the counters not greater than the pre-defined limit (self.imit ) The while loop inthe previous example was speci o that use-case; we don’ actually need to implement any looping at alin _next._, a8 that's simply the method called for each iteration through a loop on our iterator. Here we're also implementing the _getiven__ method, which allows us to retrieve a value from the erator at a certain index location. This one. simply calls the terator using self. next untlit aries atthe desired index location, then retums that value. oc 1D: 6689693 class Squares (object def _init_(self, Linit-200): Seif tinit = Limit self.x= 0 ef __next_(sel4): welt 3 Af self.x > self.tinit: raise Stoptteration return (self.x-1)"*2 def __petitem_(self, 16x): Fintetottze counter to 0 self.x = 0 4€ not Asinstance(idx, int): raise Exception("Only integer index arguments are accepted!") Mitte selfx ¢ ddx: self._next_() return self x ef _iter_(sel): ‘return self Squares(1init=20) sy sauares (x for x An ay_squares} vy_squares{5} # since we set o Lintt of 20, we can’t access an index Location higher than that y_squares[25) (U) Benefits of Custom Iterators 4. (U) Cleaner code 2 (U) Abily to work wth inirite sequences 3. (U) Abily to use built-in functions ike sun that work with terables 4. (U) Possibilty of saving memory (0.8. range ) Doc ID: 6589693 (U) Generators (U) Generators are iterators with a much lighter syntax. Very simple generators look ust ke list comprehensions, except they're surrounded with parentheses () instead of square brackets {] . More complicated generators are defined lke functions, withthe one difference being that they Use the yield keyword instead ofthe return Keyword. A generator maintains state in between times when itis called; execution resumes stating immediately ater the yield statement and continues untl the next yield is encountered. y= (xx For x So range(0)) print(y) # hen ef xsquared(): for 4 in range(se): yield $1 ef xsquared_int(): wile True: yleld xx squares = [x for x dn xsquared()) print(squares) {(U) Another example..days of te week! ef day_of week() 1-0 days ~ ["Ronday", "Tuesday", "Wednesday" "Thursday", "Friday", "Saturday", "Sunday"] while True: yield days[127) fed ay oF weok() Anmport randon f4eFsnosday(prob-.01): r= random. randoe() 46 6 © prob: return “snowday!™ se: ‘return “regular day." Do ID: 6559693 for x An day_of week(): ‘today ~ snonday() print(x +" 4s a” + today) 4 today = “snowday!” ‘break weekday = (day for day in day_of week()) ext (weekeay) (U) Pipelining {(U) One powerful use of generators isto connect them together nto a pipeline, where each generator is used by the next. Since Python evaluates (onerators "lazily" Le. 26 needed, this can increase the speed and potential alow steps to run concurrenly. This is especialy useful fone or two stops can take a longtime (e.9. database query). Without generators, the long-unning steps will become a bottleneck for execution, but ‘generators alow other steps to proceed while waling fr the long-unning steps to finish. Amport random # Get the fractional port of @ string representation of @ float ef Frac_part(v): v= str(v) 4, f= vsplit(.") return # 4 tradtttonat approach results = (1 for {in range(20): = randon-randoa() °18 # generate a ronfon maber ristr = str(r) 1 convert {¢ t0 9 string rofrac = frac_part(rstr) # get the fractional part Flout = float("®." + r_frac) # convert it back to @ float results. append(r_out) results ec ID: 689693 1 generator pipetine rand_gen = ( randon.randon() * 208 for 4 in range(20) ) ste_gen = ( str(r) for r in rand_gen ) frac_gen = ( frac_part(e) for r i str_gen ) fout.gen = ( flest("®."sr) for r in frac_gen ) results = Mist(out_gen) results (U) Sorting (U) In Python 2, anything iterable can be sorted, and Python will happily sort it for you, even if the data is of mixed types--by default, it uses the Satin tmp uncon wich almost aye doos someting (oxcpt wth complex nubs) However, he ecu maya pe what you expect (Un Pte 3, terable objets must have the _3¢__ (1 = less han) funcon expt defined inorder to be sortable (U) The builtin function sorted) returns a new list withthe data from x in Sorted order. The sort. method (for Lists only sorts alist in-place ‘and returns None snt_data = (20, 1, 5, 6 2] sorted(int data) snt_data int_data.sort() snt_data (U) To specify how the sorting takes place, both sorted and sort take an optional argument called key. key specifies a function of one ‘argument that i used to extract a comparison key rom each list element (e.g. key=str. Lower ). The default value is None (compare the elements rect). users = [‘hackert", ‘"TheBoss", “botman', ‘turingTest"] sorted(users) sorted(users, key-str. lower) (U) The __1+__ function takes two arguments: se1f and another object, normally ofthe same type, oe ID: 6559693 class. conparablecnp (complex) def _it_(self, other ‘Feturn abs(Self) < abs(other) aeay sea aco a1 = conparablecnp(a) bt = conparablecep() arch (ba, at] sorted(e) (U) Here's how it works: 4. the argument given to key, must bea function that takes a single argument; 2 intemal, sortea creates function calls key(iten) on each tem in the list and then 4. sors the orginal list by using —_t__ on the results ofthe Key(iten) function (U) Another way to do the comparison is to use ey ‘def magnitude key(a): return (a°a.conjugate()) real ragnitude.key(2043) sorted( (5025, 15, -24, 35005], keyomagnitude key) (U) In many cases, we must sort list of dictionaries, lists, or even objects. We could define our own key function or even several key functions for ifn! keting mato List_to_sort = [{"Inane":"Jones*, “fnane':'Sally"), {°Inane dones', “fnane’ "Jerry", {Iname*:"Saith', “frane’:John"}, (le return List stent Iname") oc ID: 6689693, ef Fane sorten(1ist_stem): return List_stem{ fname”) ef inane_then_frane_sorter(List_iten) return (1ist_iten{"Inane'], list_ite['fane']) sorted(List_to_sort, key-lnane_sorter) sorted(List_to_sort, key-frane_sorter) sorted(ist_to_s rt, key-lnane_then_frane_sorter) (U) While i's good to know how this works, this pattem common enough that there isa method inthe standard library operator package o doit ‘even more concisely, {import operator Inane_sorter = operator. stengetter(“Inane") # sone of previous nose sorter (U) The application ofthe Ltengetter: method retums a function thats eqivalent tothe nane_sorter function above. Even better, when passed ‘muliple arguments, it retums a tuple containing those tems inthe given order. Moreover, we dont even need to give ita name fst, ine to do this sorted(List_to_sort, key-operator. itengetter("Inane")) sorted(List_to sort, key-operator.itengetter("Iname", "frame")) # same as using Lnome_then fnane_sorter (U) Touse operator.stengetter with List oF tuple 8, give it integer indices as arguments. The equivalent function for objects Is operator attrgetter (U) Since we know so much about Python now, its not hard to figure out how simple operstor.stemgetter actually is; the following function is essentially equivalent: ef itongetter_clone( args): et F(iten) return tuple(iten{x] for x in args) return # (U) Obviously, operator.stengetter and itengetter_clone are not actually simples just that most ofthe complet is hidden inside the Python intemals and arses out ofthe fundamental data mod (U) Duck Typing Doe ID: 6503 {GVA ne magic methods we've discussed are examples ofthe fundamental Python principle of duck typing: "fit walks lke a duck and quacks lke a duck, it must be a duck" Even though Python has isinseance and type methods, i's considered poor form to use them to validate input Inside a function or method. I verfcation needs to take place, it should be restricted to verifying required behavior using hasattr. The benefit of this approach can be seen inthe builtin sun function help(sun) (U) Any sequence of numbers regardless of whether i's a List , tuple, set , generator, or custom erable, can be passed to sum (U) The folowing is @ comparison of bed and good examples of how to write @ product function: ef List_prod(to_multiply): AF Asinstance(to multiply, List): # don't do this! ‘accumulator = 1 for 4 in to.multiply: accumulator *- § return accumulator else: raise Typetrron("Argunent to sultiply must be list") ef generic_prod(to_multiply Af hasattr(to_multiply, "_tter_') or hasattr(to_aultiply, "_getiten__ ‘accumulator = © for 4 in to_multiply: accumulator "= § ‘return accumulator aise Typetrror("Argunent to_sultiply must be a sequence") List_prod({3,2,2)) List_pred((2,2,3)) sgeneric_pred((22,3)) (U) Having given that example, testing for iterabilly is one ofa few special cases where isinstance might be the right function to use, but notin ‘the obvious way. The collections package provides abstract base classes which have the express purpose of helping to determine when an ‘object implements a common interface. (U) Final, effective use of duck typing goes hand in hand with robust error handling, based on the principle that “its easier to ak fo forgiveness than permission." DoeDe69603 Exercises 1. Ata meta to your RangedQuey class to alow instances fhe cass tobe sorte by ‘start dt! 2 vito an trata aos Revereoter at thes atl ad erates fom he revere recon 3. Wite a generator which wil iterate over every day in a year. For example, the ist output would be ‘Monday, January 1. ‘4 Mody the generator from exercise 2 50 the user can specify the year and ital day ofthe week UNCLASSIFIED Do ID: 6589693 Pipelining with Generators Created over 3 years gag by[ TT” L:Pytnons thumbnail (U) Defining processing pipelines with generators in Python. I's simply awesome. Pipelining with Generators Imagine you'e doing your laundry. Think about the stages involved. Roughly speaking, the stages are sorting, washing, drying, and folding. The ‘beauty though is that eventhough these stages are sequential, they can be performed in paral. Ths is called pipelining, Python generators make pipelining easy and can even dary your code quite a bit. By breaking your processing into distinct stages, the Python Interpreter can make bettor use of your computer's resources, and even break the stages out into separate threads behind the scenes. Memory is. also conserved because values are automatically generated as needed, and discarded as soon as possible. {A prime example of his is processing results from a database query. Oten, before we can use the results ofa database query, we need to clean thom up by running them through a series of changes or ransformations. Pipelined generators are peroct fr this. ‘fron pprint import pprint import randon A Silly Example Here we're going to take 200 randomly generated numbers and extract ther fractional parts (the part after the decimal point). There are probably ‘more efcient ways to do ths, but we're doing to do itby sling out the string ito two pars, Here we have a function that simply returns the integer part and the fractional pat of an input oat as two strings in a tuple Doe ID: 6589693 ef split_Aloat(v): ‘Takes a Float or string of 2 Flost land returns a tuple containing the Anteger part and the fractional part ff the number, as strings, respectively. v= str(v) 4, f= vesplitc.") return (1, °8."+6) The Pipeline Here we have a pipeline of four generators, each feeding the one below it. We pprint ou the final resulting star all the stages have comple See the comments after each ine for further explanation, rand_gen = ( randon.randon() * 160 for i in range(200) ) # generate 200 random floats between @ and 108, one at o tine results = ( split float(r) for rin randgen ) # call our split floot() function which will generate the correspoding tuples results - ( r{1] for r An results ) # we only care about the fractional part, s0 only heep that part of the tuple results = ( float(r) for r in results ) # convert our fractional value fron @ string back nto o float porint(List(results)) # print the Final results Why not a for-loop? We could have put al the steps of our pipeline into a single foroop, but we get a couple advantages by breaking the stages out into separate ‘onerators: + There's some clay gained by having distinct stages specied as a pipeline. People reading the code can clearly see the transforms. + Ina for-Joop, Python simply computes the values sequential; there's no chance for automatic optimization or mult-hreading. By breaking ‘the stages out, each stage can execute in paral, just Ike your washer and dyer. Another (Pseudo-)Example Hore’ a pseudo-example querying a database that returns JSON that we need to conver to lists. Bo ID: 6589693 mort json results = ( json-loads(result) for result in b_cursor.execute(my_query) ) results = ( r{'results'} for r in results ) results = ( [ r{'nane'], r{'type'], r['count'], e['source'] ] for v in results ) Filters \We can even fitr our data in our generator pipeline. results = (r for rin results if r[2] > @) remove results with a count of zere foo(results) # do something else with your results oe ID: 6589693 Lesson 11: String Formatting Upisied gmat ao [Th st a (U) Lesson 11: String Formatting (©) G)-P-L, 86-36 Fy) 2: UNCLASSIFIED (U) Intro to String Formatting (U) Sting formating isa very powerful way to display information to your users and yourself. We have used it through many of our examples, such as ts: ‘This As a formatted String ()° format(*---oht I'm a formatted String argumenté- (U) This is probably the easiest example to demonstrate. The empty curly brackets {) take the argument passed into. format (U) Here's a more complicated example: (2) {4} and (0) format (‘Heney", 1", “Bob (U) Arguments can be positional, as illustrated above, or named like the example below. Order does matter, but names can help. {ho} 45 really {what}!" format (who-"Tony’, what="awesone") {(U) You can also format ist: oc ID: 6689653 cities = ["Dallas', ‘Baltimore’, ‘0C", ‘Austin’, “New York") {o[41) 4s a really big city.” format(cities) (U) And dictionaries: over to_upper = {" “This 45 a big letter {0[a])*format(lower_to_upper) # notice no quotes around o "This 45 a big letter {Lookup[a]}*.format(Lookup-lower_to_upper) # can be noned for 1stele, big in lower_to_upper.stens(): print([--0{@:10} -- (1:10}<--]" format(little, big)) {(U) Ifyou actually want to include curly brackets in your printed statement, use double brackets lke this: (( }) “({0)) (0)" format(“imere do I get printed?*) {(U) You can also store the format sting ina variable ahoad of ime and use it lator: ‘the way_1_want_it = "{@:>6) = {@:>#16b) = (0:406x)" for 4 in 1, 25, 458, 7890: rint(the say {/want_st-format(4)) (U) Format Field Names (U) Here are some examples of field names you can use in cuty brackets within a format string. {fled name>) + (U)1: the second positional argument + (U) name : keyword argument + (U) Oar: attribute named var of the frst positional argument + (U) 310]: element 0 ofthe fourth positional argument + (U) me_¢atalkey]: element associated withthe specic key string key’ of me_data (U) Format Specification oo ID: 6680693 (G) Won using a format specification, it flows the feld name within the cuty brackets, and its elements must be in a certain order. This is only for reference; fora full description, see the Python documentation on sing ormatig. {efield names :cformat spee>) 1. (U) Padding and Alignment > align right «< :align let += only for numeric types +» seanter 4.(U) Sign ‘+= pref negative numbers with a minus sign + + slke ~ but aiso pref postive numbers with a + +" lke = but also prefix positive numbers with a space 1. (U) Base Indicator (precede with a hash & lke above) + 0» = binary + 00 : octal + 0x :hexadecimal 4. (U) Digt Separator +, use a comma to separate thousands 1.(U) Fels wien + leading © : pad with zer09s atthe front 1. (U) Fila Type (eter ong which type of value should be formatted) string (the detaut) binary ‘decimal: base 10, octal hex uses lower case laters hex uses upper case lke < , use locale settings to determine decimal point and thousands seperator + no code integer: tke + = exponential with salle + s exponential wit big E + fixed point, nan for nota number and nf for infinity F same as F but uppercase aN and NF ee rt + 6 slike « but uppercase +n cale settings tke + times 100, displays as F with a x ‘+ no code decimal: ike «precision of twelve and always one spot ater decimal point 4.) Vaile with (U) New in Python 3.6: f-strings 4 had 'f* before the string to create an f-string 1 expression added dtrectly inside the “(}° brackets rather than after the format stotenent Fra = OO ry_rane = ‘Bob! My name As {my_name}* (U) Examples “40:(1)2)0)" foent(o076.5832, 18, 3) “ *¢9:4010.4#)° format(-123.456) 20.48)" Format(-123.456) for 4 An range(2, 6): print("{0:10.(1}4)" format(123.456, 1)) “value :276.542, ‘width':15, ‘preciston':5) (0{ value] :(0[width]}.{0[preciston}}}*.format(v) data = [('steve', 59, 202), ('Smantha’, 49, 156), ("Dave", 61, 135)] ‘for nane, print weight in dati 12s) (1:44) (2:44)" format(nane, age, weight)) oc ID: 6589693 # some as above but with f-strings data = [('Steve', 59, 202), ("Samantha’, 49, 156), Dave", 61, 335)] for nane, age, weight in data: rine (#°(nane:<12s) {3 {weight:ad)") UNCLASSIFIED oe ID: 6589693 COMP3321 Day01 Homework - GroceryList Upate aa S er ciara pie (U) Homework for Day0t of COMP3321. Task is to sort items into bins. (U) COMP3321 Day01 Homework GroceryList oo ID: 6689653 nyorocerylist jpples", “bananas”, “ailk", “eggs”, “bread”, “Shanburgers", "hotdogs", "ketchup", “grapes”, “tilapia”, “sweet potatoes", “cereal”, “paper plates”, “napkins”, “cookies”, “Hee creas”, “cherries”, “shampoo") 18 Teens by category vegetables = ["sweet potatoes", "carrots", "broccoli", "spinach", “onions”, "mushroons", "peppers] fruit = ["bananas", "apples", "grapes", "plunbs", “cherries”, “pineapple"] cold itens = ["eges", "milk", “orange juice", “cheese, “ice crean"] proteins = ["turkey", "tilapia", "hamburgers", "hotdogs", "pork chops", "han", “neatballs"] boxed items = ["pasta", “cereal”, “oatmeal”, "cookies", "ketchup", “bread”] paper_products = ["totlet paper, “paper plates", "napkins", "paper towels] ‘tolletry_itens ~ ["toothbrush", “toothpaste”, “deodorant, "shampoo", "soap" #8 Wy items by category ny_vegetables = [] my fruit = y_cold_ttens = () myproteins = (] sy toxed items sy_paper_products. ry tolletry items a a (U)Filin your code below. Sort the items in myGroceryList by ype ino appropriate my category lists using looping and decision making print("My vegetable List: ", my_vegetables) print (My fruit List: *, ay fruit) print("My cold Sten List: ", my_cold_ items) Dprint("My protein list: “, ay_proteins) print (My boxed iten List: ", my boxed ttens) print ("My paper product List: *, my paper_products) print ("My tofletry item list: *, mtoiletry_itens) oc ID: 6689604 Dictionary and File Exercises Updated over 2 years ago by in ci’ Bees OOO CUO (U) Dictionary and file exercises for COMP3321. Lists and Dictionary Exercises Exercise 1 (Euler's multiples of 3 and 5 problem) we ist all the natural numbers below 10 that are multiples of 3 or 5, we get 3 6, 6 and 8. The sum of these multiples is 2. Find the sum ofall the muliples of 3 or 5 below 1000. multiples 3 = (1 for £ in range(3,1020,3)] multiples 5 = [1 for 1 in range(5,1020,5)] multiples = set((multiples 3 + multiples_)) # set will renove duplicate numbers sum(multiples) # odd oll the numbers together you can also do this in one line: sum([1 for 4 in range(3,1000) 4 1 X 3 == © or 4x 5 == 0]) Exercise 2 Write @ function that takes a ist as a parameter and retums a second list composed of any objects that appear more than once inthe orignal ist aeproved for Relel FOIA Case 108165 4.5.6) should retum (3,6) + what should duplicates((cow’pi'goat’ "horse pig) return? # you can use a dictionary to Keep track of the number of tines seen ef duplicates(x): ‘up(i} = dup. get(aye)s result = (] for i in dup-keys(): A dupl) > 1 result. append(1) return result X= 11,24356470304,545] Gupticates() ‘you con also just use Lists. def auplicates2(x) on = 0) for An x: Af x-count(4) » 2 and 4 not Sn dup: ‘dup. append(i) return dup ¥ = [cow "pig" "goat, "horse dupticates2(y) 2 = ['2016", "2615", "2016"] ‘dupticates(2) Exercise 3 \Wote a function that takes a potion mark as input and returns the ful classification + convert_classiiation(U/FOUO) should rtum 'UNCLASSIFIEDI/FOR OFICIAL USE ONLY’ + conver_classiication(S/IREL TO USA, FVEY) should return ‘SECRETI/REL TO USA, FVEY" Dec ID: 689604 1 just create 0 “Lookup table” for potentol portion marks full_classifications = {'U//FOUD":UNCLASSIFIED//FOR OFFICIAL USE ONLY", 'C//REL TO USA, FVEY':'CONFIDENTIAL/ REL TO USA, FVEY" '5//MEL TO USA, FVEY':SECRET//REL TO USA, FVEV', S//ST//REL TO USA, FVEY': 'SECRET//SI//REL TO USA, FVEY" "TS//REL. TO USA, FVEY": "TOP SECRET//REL TO USA, FVEY", "TS//S1//REL TO USA, FVEY": "TOP SECRET/SI//REL TO USA, FVEY"} def convert_classification(x) return full_classifications.get(x, UMKNOMN") # Look up the value for the portion mark convert_classtfication(*U//F00') convert_classification(S//REL TO USA, FVEY") comvert_classification(¢//S=") File Input/Output Exercises “These exercises build on concepts in Lesson 3 (Flow Control, 9. for loops) and Lesson 4 (Container Data Type, 9, dictionaries). You wil use all these concepts together with reading and wating from fles First, Get the Data ‘Copy the sonnet from hiins://urn.nsa.ic.gov/t/ix6am and paste it into a new text file named sonnet.txt. Exercise 1 rite function called file_capitalize( that takes an input fle name and an output fle name, then writes each word from the input fle with only the first letter capitalized to the output fle, Remove all punctuation except apostrophe, File capitalize("somet.tet", “sonnet_caps.txt") => capitalized words written to sonnet_caps.txt oc ID: 6589654 # use help("") to see what each of these string methods are doing ef capitalize( sentence): words = sentence.split() # use split to split the string by spaces (1-e., words) new_words = [ word.strip().capitalize() fer word in words } # coptiaLize each word return * *Join(new words) # create and return one string by combing words with ‘def renove_punct (sentence): 4 since replace) method returns @ new string, you can choin calls to the replace() 4 ethod in order to renove alt punctuation in one Line of code return sentence.replace(".’, "').replace(',", '*).replace(':', '*).replace(';", ") def Fle copitalize(infilenane, outflle nae): Anfile = open(infile.nane, 'r*) # open the input File outfile = open(outfiienane, ‘w") # open the outpu Fite for Line in infile: # loop through each Line of input outFile weite(capitalize(renove punct(Iine)) + *\n') # unite the copttalized version to the output file Anfile.close() # finally, close the files ovtfile.close() FAle_capitalize("sonnet.txt", ‘somet_caps.txt!) Exercise 2 “Make function called fle_word_count() that takes a file name and returns a dictionary containing the counts for each word. Remove all punctuation except apostrophe. Lowercase all words. File_word_count("sonnet.txt") => ("it's 4, ‘ne ec ID: 6689654 ef #L1e word count (inFile_nane): sword_counts = (} with openinfilenane, ‘r') as infile: # using ‘with’ so we don't have to close the File ‘for Line in infile: # loop over each Line in the file words = remove punct(Line) # se can use the renove.punct fron exercise above ords = words.split() # =pLit the Line into words for word in words: # Loop over each word ord = word. strip()-Lower() "add one to the current count for the word (start at @ {f not there) word_counts{word) ~ word.counts.get(word, ©) + 3 ‘retuen word.counts # return the whole dictionary of word counts counts ~ Fle word_count( sonnet. txt") Extra Credit Write the counts dictionary oa fle, one key-value per in. ‘def write_counts(outfile name, counts} with open(outfilenane, "w', encoding-"utf-8") as outfile: 4 to Loop over a dictionary, use the ttens() method 1 {tens() will return 2-eLement tuple contetning @ hey and a value 4 below we pull out the values from the tuple into their own vartables, word and count for word, count An counts. itens(): outfile.write(vord + ':* + str(count) + "\nt) # write out in he value format wrlte_counts(‘sonnet_counts.txt", counts) # use the counts dictionary fron Exercise 2 above oc ID; 6649694 Structured Data and Dates Exercise ptt ge 9 year 200 fT momma OoOoOc (U) COMP3321 exercise for working with structured data and dates. Structured Data and Dates Exercise ‘Save the Apple stock data from hiios/um.nsa c-9o¥/VO9r to aapLsy Use DictReader to read the records. Take the daly stock data and compute the average adjusted close (Adj Close”) per week. Hint: Use Isocalencar() for your datetime object to get the week number. For each week, print he year, month, and average adjusted cose to two decimal places. Year 2015, beck 23, Average Close 107.48 Year 2015, Week 22, Average Close 105.10 oc ID: 6559694 ‘fron csv import DictReader fron datetine import datetine ‘def average(nunbers): Af Len(nunbers return 9.0 return sun(runbers) / float len(rumbers)) ef get_year_veok(record): ‘dt = datetine.strptine(record[ Oate"], "2¥-tm-xd') return (at.year, at. ts0calendar()(1]) def get_averages(data): aves = 0 for year_week, closes in data. ttens(): ‘avgs[year_week] = average(closes) return aves ‘det weokly_surmary reader): weekly data = () for record in reader: year_weck = get_year_veok(record) Af year_week not in wookly. data: weekly_dataLyear_veet] = [] weekly_data[year_week] append(Float(record Adj Close"})) ‘return get_averages (weekly data) ef F.Le_weekly_surmary(infile_name): with open(infile mane, ‘r') as infile: return weekly, summary OictReader(infile)) def print_weekly_summary (weekly_dats for year_week in reversed(sorted(weekly_data.keys())) oc ID: 689604 year = year woek(0) week = year_week{ 1] avg = weekly datalyear_week) Drint(Year {year}, Week (week), Average Close {avg:.2f}' format(year-year, week-veek, avg-2v@)) ata = File woekdy_summary("aplcs¥") print weekly_suenary(data) Extra Use csv DictWriter to write this week data out toa new CSV fle ‘from csv Anport Dictwriter def write weokly.sunmary(weekly data, outfile_nane) headers = [ "Year", ‘week’, "Avg" ] ith open(outfile_nane, 'w', newline-"') as outfite: weiter = Dictweiter(outfile, headers ) weiter wrsteheader() a-keys())): vyear_week(], for year_week in reversed(sorted(weekl) rec = { "Year": year_week[o], “week” writer weiterow(rec) weekly datalyear_week] } data = File weekly sumary(‘aapl.csv') write weekly, summary (data, ‘aapl_sumary.csv") Extra Extra Use son dumps( to write a JSON entry for each week on a new line. Amport json def urite_json_weekly_summary (weekly data, outfile nane): with open(outfilenane, 'w") as outfile: ‘for year_week in reversed(sorted(weekly data. keys())): ree = { ‘year’: year_week{0}, "week': year_week[ i], ‘ave loutfie.write(json.dumps(rec) » "\n") oc ID: 6559604 data ~ File wookdy_sumary(aapl.csv") write_json weekly_sumary(data, ‘29p1.Json’) Dec ID: 6559604 Datetime Exercise Solutions Getta eee i ii 32 * pee se ovo (U) Solutions forthe Datetime exercises (U) Datetime Exercises (U) How tong before Christmas? Anport datetine, tine print(datetine. date(2017, 12, 25) - datetine.date.todey()) (W) Or if youre counting the microseconds: print (datetine.datetine(2017, 12, 25) - datetine.datetine.today()) (U) How many seconds since you were born? birthdate = datetine, datetine( 1985, 3, 23) ‘tine since birth ~ datetine. datetine.today() - birthdate print("(:,)" format (tine_since birth. total_seconds())) (U) What is the average numberof days between Easter and Christmas forthe years 2000 - 29887 oc ID: 6689694 fron dateutil.easter taport easter total = & span = range(2000, 3020) for year in span: otal + (datetine.date(year, 12, 25) ~ easter(year)) days ‘average = total / len(span) print ("(:6.4°)' Format (average)) (U) What day ofthe week does Christmas fll on this yoar? datetine.date(2015, 12, 25).steftime("XA") (U) You get a intercepted emai wth a POSIX timestamp of 1495074325. The ema s from the leader of a Zendian extremist group and says that ‘there will be an attack on the Zendian capitol in 14 hours. In Zendian focal time, when wil the attack occur? (Assume Zendia sin the same time ‘one as Kabul) Amport pytz lute tz = pytz.timezone("Ete/UTC') ‘onail_tine_ute = datetine.datetine.fromtinestarp(1635078525, tz-ute_t2) attack tine_ute ~ enail_tine utc + datetine.tinedelta(hours@14) Zzendia_tz = pytz.tinezone(Asia/Kabul') attack tine_zendia = attack tine_ute.astinezone(zendia tz) print enati_tine_ute) print (attack tine ute) print (attack tine_zendia) oe ID; 6589694 Object Oriented Programming and Exceptions Exercise grees a yeas af Ti Se ate (U) COMP3321 exercise for object oriented programming and exceptions. Object Oriented Programming and Exceptions Exercise Mak a ass cle Symbol at hots data fora sock synbo wih th flowing proper: self.nane self.daily_data It should leo have the following functions: def _init_(self, nave, input_file) def data for date(self, éate_str) Init(sef, name, input fle) should open the input fle and read it with DictReader, puting each entry in self. daily_data, using the date strings as the keys. Make sure to open the dally data fie within a trylexcept block in case the fle does not exist the fle does not exist, sot sof daily data to an cempy dictionary. ta_for_dato(sol,date_str) should take a date sting and retum the dctionary containing that days data. If there is no entry for that date, return an ‘empiy dietonary oe ID: 689694 Tests “Make sure the folowing execute as speciid in each comment. You can get the aaplcsv fle from hios:/un.nsa.i.cov)uOari. The apple.csv fle should not exis. 51 = Symbol("ARPL*, ‘aaplesv") print(s1.data_for_date("2615-08-10")) print(st-data_for_date("2615-08-09")) 52 = symbol("AAPL', ‘apple.csv") print(s2.data_for_date("26i5-08-10")) Drint(s2.data_for_date(*2635-08-09")) 4 should return a dictionary for that date 4 should return an eapty dictionary 4 should not raise an exception! 4 should return an eapty dictionary 4 should return an empty dictionary Doe 1D: 6589695 Module: Collections and Itertools wr ated amost 2 yeers ago by[ Jin Coie 32s oc (U) Module: Collections and ttertools (U) Any programming language has to strike a balance between the number of basic elements it exposes, ke control structures, datatypes, and so ‘orth, and the utity of each one. For example, Python could do without tuple s entrely, and could replace the ict witha list of 1ist sor even ‘single st where even-numbered indices contain keys and odd-numbered indices contain valves. Often, there are situations that happen so ‘commonly that they warrant inclusion, bu inusion inthe builtin library isnot quite justified. Such is the case with ‘the collections and itrtools modules. Many programs could be simplified wih a. defauitsict , and having one availabe witha single fron collection import defaultaict fs much better than reinventing the wheel everytime its needed. (U) Value Added Containers with collections (U) Suppose we want to buld an index for a poem, so that we can ook up the lines where each word occurs. To do this, we plan to construct 2 ")) 4 -update(na John", action="ran") oe ID: 6559698 “{@{nane]} {@{action}} to (O{obsect]}" Format) 4 # “object” added to 4 (UA counter I ike a defoultsict(int) with additional features If given a 2st or other iterable when constructed, it will create counts of ll the unique elements it ses. It can also be constructed from a dictionary with numeric values. Ithas a custom implementation of update and some specialized methods, ike most _conmon and subtract ‘fron collections import Counter vord_counts = counter(poem.split()) word_counts.most_common(2) ord_counts.update("Ianb Lanb lanb stew" .split()) word_counts.ost_comon(3) € = counter(ar3, bei) 4 » counter(art, b-2) cod ed 1 Dia you get the output you expected? (Oe cad ela (U) An ordereabict is a dictionary that remembers the order in which keys were originally inserted, which determines the order fr its iteration, ‘Aside fom that, thas a popitem method that can pop from ether the beginning or end ofthe ordering. (U) nanectople Is used to create lightweight objects that are somewhat ike tuples, n that they ae immutable and atributes can be accessed with [) notation. As the name indicates, attributes are named, and can also be accessed with the . notation. tis most often used as an ‘optimization, when speed or memory requirements dictate thal a cic or custom object isn't good enough. Construction of a nanectuple is Somewhat indirect, as nanestuple takes field specications as strings and returns @ type , which s then used to create the named tuples. named tuples can also enhance code readabilty. ‘from collections import nanedtuple oc ID: 6689685 Person = nanedtuple("Person", bob = Person(nane="Bob print ( "Xs 1s a 2d year-old Xs" x bob) # 2.x style string formatting rint( "{) 1s a {) year 0" Format(“bob) ) Print( ‘25 48 9 2d year-old Xs" X (bob.name, bob.2g6, bob. gender) ) print( "{) As 2 Q year-old {)'.Fformat(bob.nane, bob.age, bob. gender) ) veo} boot mane") 1 Typetrror bob nane PrAnt( "R(nane)s As a X(age)d year-old X(gender)s” X bob) # Doesn't work print( "{name) is a (age) year-old {gender} format(*bob) )# Doesn't work print mane) 1s 2 {0.age) year-old {8.gender}'.format(bob) ) # Works! (U)Finaly, deqve provides queue operations. rom collections import deque = deque("ghi") __# make 0 new deque with three ‘tens .append('3") 4 add a new entry t0 the right side A.appendlefe('#) ——# odd 0 new entry to the Left side 4.poplert() ‘+ return ond remove the Leftmost tem a.rotate(s) 4 ight rotation Gentendlefe(‘abe") —# extendLeft() reverses the input order (U) The collections module also provides Abstract Base classes for common Python interfaces. Their purpose and use is curently beyond the ‘cope ofthis course, but the documentation is reasonably good oc ID: 6589588 (U) Slicing and Dicing with itertools Given one or more List, iterators, or other itrable objects, there are many ways to sce and dice the constituent elements. ‘The itertools module tries to expose building block methods to make this easy, but also tes to make sure that ts methods are useful situations, so the documentation contains @ cooktonk of common use cases. We only have time to cover a small subset of ‘the itertoots functionality. Methods from itertools usually return an iterator, which is great for use in loops and lst comprehensions, but not so good {or inspection; inthe cade blocks that folow, we often call List on these things to unwrap them, (U) The chain method combines iterabes into one supersterable. The groupby method separates one iterator into groups of adjacent objects, possibly as determined by an optional argument-his can be tricky, especially because there's no look back to see Ifa new key has been ‘encountered previously Amport itertools 2 variety of Lst(stertoots. ehain(range(s)+{545])) => [8114243444545] size_groups = itertools. groupby((1,1,2,2,2,'B"s"B,3,4,2,3,21) [ekey, List(vals)) for key, vals in size groups] (U) A deeply nested for loop or lst comprehension might be beter served by some ofthe combinatoric generators ike product , permutations, (or combinations ter_product = itertools.produet([1,2,3],1'2", en List(iter_product) Ater_conbi = itertools. combinations ("abet List combi = List(iter_ combi) Aist_conbi ‘ter_permutations = itertools.permutations("abcd”,3) List(iter_permutations) (U) stertoots can also be used to create generators: counter = Stertools.count(®, 5) next (counter) print(list(next(counter) for c in range(s))) ose oe (U) Be careful... What's going on here?!? counter = Stertools.count(0.2,0.1) for ¢ in counter: print (c) Ae > 15: break cycle = itertools.cycle(*ABCDE") for 4 in range(10): print (next (cyele)) repeat = itertools. repeat ‘again! ") for 4 in range(s): rine (next (repeat) ) repeat = itertools.repeat(*again!", 3) for i in range(s): Drint (next (repeat) ums = range(19,0,-1) ay.2ip = zip(nuns, itertools.repeat(‘p')) ‘for thing in ay_zip: print (thing) Doc ID; 6589695 Functional Programming we Created over 3 years ago by[ Jin GoM 325, cad Sse : Ommome (U#FOUO) A short adaptation of FA practical introduction to funetional programming® in Python to supplement COMP 3321 material Aso Gacusses lambda. UNCLASSIFIED (U) Introduction (U) Ata basic evel, there are two fundamental programming styles or paradigms: + imperative or procedural programing and + declarative or functional programming, (U) Imperative programming focuses on teling a computer how to change a program's stalo-its stored infrmation-step by slep. Most programmers start out learning and using tis style. I's @ natural outgrowth ofthe way the computer actualy works. These instructions can be ‘organized ino functions/procedures (racedural programming) and objects (object-orientedprogramming), bu those stylistic improvements remain imperative at heart. (U) Deciaratve programming, on the other hand, focuses on expressing what the program should do, not necessarily how it should be one. Functional programming isthe most common favor ofthat. I treats a program as if itis made up of mathomaticastye functions: fora given input x, running trough function £ wil aways give you the same output f(x) , and x itself will remain unchanged afterwards. (Note tha this isnot necessariy the same as a procedural-stye function, which may have access to global variables or other inputs" and which may be able to ‘modify those inputs directly.)

You might also like