Advanced Python COPY (PYT212 Version 1.1.0) Unauthorized Copyright Information
Total Page:16
File Type:pdf, Size:1020Kb
• EVALUATIONTRAINING MATERIALS FOR IT COPYPROFESSIONALS Unauthorized Reproduction or Distribution Prohibited EVALUATIONAdvanced Python COPY (PYT212 version 1.1.0) Unauthorized Copyright Information © Copyright 2019 Webucator. All rights reserved. The Authors Reproduction Nat Dunn Nat Dunn founded Webucator in 2003 to combine his passion for technical training with his business expertise and to help companies benefit from both. His previous experience was in sales, business and technical training, and management. Nat has an MBA from Harvard Business School and a BA in International Relations from Pomona College. or Roger Sakowski (Editor) Distribution Roger has over 35 years of experience in technical training, programming, data management, network administration, and technical writing for companies such as NASA, Sun Microsystems, Bell Labs, GTE, GE, and Lucent among other Fortune 100 companies. Stephen Withrow (Editor) Prohibited Stephen has over 30 years© experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University. EVALUATIONAccompanying Class FilesCOPY This manual comes with accompanying class files, which your instructor or sales representative will point out to you. Most code samples and exercise and solution Unauthorizedfiles found in the manual can also be found in the class files at the locations indicated at the top of the code listings. Due to space limitations, the code listings sometimes have line wrapping, where no line wrapping occurs in the actual code sample. This is indicated in the manual using three greater than signs: >>> at the beginning of each wrapped line. In other cases, the space limitations are such that we have inserted a forced line break in the middle of a word. When this occurs, we append the following symbol at the end of theReproduction line before the actual break: »» or Distribution Prohibited Table of Contents Table of Contents 1. Advanced Python Concepts.................................................................1 Lambda Functions............................................................................................1 EVALUATIONAdvanced List Comprehensions....................................................................... COPY2 Quick Review of Basic List Comprehensions............................................2 Multiple for Loops......................................................................................3 Exercise 1: Rolling Five Dice............................................................................5 UnauthorizedCollections Module...........................................................................................7 Named Tuples...........................................................................................7 Default Dictionaries (defaultdict)...............................................................7 Exercise 2: Creating a defaultdict...................................................................13 Ordered Dictionaries (OrderedDict)........................................................17 Exercise 3: Creating a OrderedDict................................................................20 Counters.................................................................................................23 Exercise 4: Creating a Counter.......................................................................28 Deques (deque)......................................................................................31 Exercise Reproduction5: Working with a deque................................................................... 34 Mapping and Filtering.....................................................................................38 map(function, iterable, ...).......................................................................38 filter(function, iterable).............................................................................38 Using Lambda Functions with map() and filter().....................................39 Mutable and Immutable Built-in Objects.........................................................42 Strings are Immutable.............................................................................42 Lists are Mutable.....................................................................................42 Sorting............................................................................................................43 Sorting Lists in Place..............................................................................43 The sorted() Function..............................................................................or 45 Exercise 6: Converting list.sort() to sorted(iterable)Distribution........................................ 47 Sorting Sequences of Sequences...........................................................50 Sorting Sequences of Dictionaries..........................................................51 Unpacking Sequences in Function Calls........................................................53 Exercise 7: Converting a String to a datetime.date Object.............................55 Modules and Packages...................................................................................57 Modules..................................................................................................57 Packages................................................................................................58 Search Path for Modules and Packages.................................................Prohibited58 Version: 1.1.0. Printed: 2019-04-02. i Table of Contents 2. Working with Data...............................................................................61 Relational Databases......................................................................................61 PEP 0249 -- Python Database API Specification v2.0............................61 PyMySQL................................................................................................64 Returning Dictionaries instead of Tuples.................................................66 EVALUATIONsqlite3...................................................................................................... COPY67 Exercise 8: Querying a SQLite Database.......................................................68 Passing Parameters................................................................................71 SQLite Database in Memory...................................................................71 UnauthorizedExecuting Multiple Queries at Once........................................................73 Exercise 9: Inserting File Data into a Database..............................................76 CSV................................................................................................................79 Reading from a CSV File........................................................................79 Finding Data in a CSV File......................................................................81 Exercise 10: Comparing Data in a CSV File...................................................83 Creating a New CSV File........................................................................85 CSV Dialects...........................................................................................86 Getting Data from the Web.............................................................................91 The ReproductionRequests Package........................................................................... 91 Beautiful Soup.........................................................................................94 XML.........................................................................................................96 Exercise 11: Requests and Beautiful Soup.....................................................97 JSON..............................................................................................................99 Exercise 12: Using JSON to print Course data.............................................106 3. Testing and Debugging.....................................................................111 Testing for Performance................................................................................111 time.perf_counter()................................................................................or 111 The timeit Module.................................................................................114 The unittest Module......................................................................................Distribution 120 Unittest Test Files..................................................................................122 Exercise 13: Fixing Functions.......................................................................129 Special unittest.TestCase Methods.......................................................132 Assert Methods.....................................................................................133 Prohibited ii © Copyright 2019 Webucator. All rights reserved. Table of Contents 4. Classes and Objects.........................................................................137 Attributes.......................................................................................................137