ebook img

Advanced Computer Programming in Python PDF

313 Pages·2017·6.647 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Advanced Computer Programming in Python

Contents Contents 5 1 ObjectOrientedProgramming 9 1.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.2 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.3 AggregationandComposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.5 MultipleInheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.6 AbstractBaseClass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 1.7 ClassDiagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1.8 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2 DataStructures 45 2.1 Array-BasedDataStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 2.2 Node-basedDataStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 2.3 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 3 FunctionalProgramming 101 3.1 PythonFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 3.2 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 3.3 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 4 MetaClasses 135 4.1 Creatingclassesdynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 4.2 Metaclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 4.3 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 6 CONTENTS 5 Exceptions 147 5.1 ExceptionTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.2 Raisingexceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 5.3 Exceptionhandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 5.4 Creatingcustomizedexceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 5.5 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 6 Testing 163 6.1 Unittest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 6.2 Pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 6.3 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 7 Threading 185 7.1 Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 7.2 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 7.3 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 8 Simulation 209 8.1 SynchronousSimulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 8.2 DiscreteEventSimulation(DES) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 8.3 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 9 HandlingStringsandBytes 227 9.1 SomeBuilt-inMethodsforStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 9.2 BytesandI/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 9.3 bytearrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 9.4 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 10 I/OFiles 245 10.1 ContextManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 10.2 Emulatingfiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 11 Serialization 253 11.1 SerializingwebobjectswithJSON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 11.2 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 CONTENTS 7 12 Networking 263 12.1 Howtoidentifymachinesoninternet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 12.2 Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 12.3 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 12.4 Client-ServerArchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 12.5 SendingJSONdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 12.6 Sendingdatawithpickle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 12.7 Hands-OnActivities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 13 WebServices 277 13.1 HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 13.2 RESTarchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 13.3 Client-sideScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 13.4 Server-sideScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 13.5 Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 13.6 RequestData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 13.7 Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 13.8 OtherarchitecturesforWebServices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 14 GraphicalUserInterfaces 291 14.1 PyQt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 14.2 Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 14.3 EventsandSignals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 14.4 Sender . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 14.5 CreatingCustomSignals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 14.6 MouseandKeyboardEvents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 14.7 QTDesigner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 15 SolutionsforHands-OnActivities 315 15.1 Solutionforactivity1.1: Variablestars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 15.2 Solutionforactivity1.2: GeometricShapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 15.3 Solutionforactivity2.1: Productionlineofbottles . . . . . . . . . . . . . . . . . . . . . . . . 326 15.4 Solutionforactivity2.2: SubwayMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 15.5 Solutionforactivity3.1: PatientsinaHospital . . . . . . . . . . . . . . . . . . . . . . . . . . 332 8 CONTENTS 15.6 Solutionforactivity3.2: SoccerTeam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 15.7 Solutionforactivity3.3: HamburgerStore. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 15.8 Solutionforactivity4.1: MetaRobot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 15.9 Solutionforactivity5.1: Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 15.10 Solutionforactivity6.1: Testingtheencryptor . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 15.11 Solutionforactivity6.2: TestingATMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 15.12 Solutionforactivity7.1: Godzilla . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 15.13 Solutionforactivity7.2: MegaGodzilla . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 15.14 Solutionforactivity8.1: Clientqueues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 15.15 Solutionforactivity8.2: GoodZoo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 15.16 Solutionforactivity9.1: Fixingdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 15.17 Solutionforactivity9.2: Audiofiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 15.18 Solutionforactivity11.1: Cashiers’data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 Bibliography 389 KARIMPICHARA–CHRISTIANPIERINGER ADVANCED COMPUTER PROGRAMMING IN PYTHON ADVANCEDCOMPUTERPROGRAMMINGINPYTHON Copyright© 2017byKarimPicharaandChristianPieringer Allrightsreserved. Thisbookoranyportionthereofmaynotbereproducedorusedin anymannerwhatsoeverwithouttheexpresswrittenpermissionofthepublisherexcept fortheuseofbriefquotationsinabookreview. ISBN:9781521232385 Toourwivesandchildren Preface Thisbookcontainsmostoftherelevanttopicsnecessarytobeanadvancedcomputerprogrammer. Thelanguageused inthebookisPython3. Besidestheprogramminglanguage,thereaderwilllearnmostofthebackbonecontentsin computer programming, such as object-oriented modeling, data structures, functional programming, input/output, simulation,graphicalinterfaces,andmuchmore. Webelievethatthebestwaytolearncomputerprogrammingisto workinhands-onactivities. Practicalexercisesmaketheusergetfamiliarwiththemainchallengesthatprogramming brings,suchashowtomodelaparticularproblem;orhowtowritegoodcodeandefficientroutineimplementation, among others. Most of the chapters contain a set of hands-on activities (with proposed solutions) at the end. We encouragethereaderstosolveallthoseassignmentswithoutpreviouslycheckingthesolution. Challengesmaybehard forinitialprogrammers,butwhilegoingthroughthisbook,theactivitieswillbecomemoreachievableforthereader. ThisbookcontainsmostofthematerialusedfortheAdvancedPythonProgrammingcoursetaughtatPUCUniversity inChile,byprofessorsKarimPicharaandChristianPieringer. ThecourseisintendedforComputerSciencestudents aswellasanyotheraffinecareerthatcanbebenefitedbycomputerprogrammingknowledge. Ofcourse,thisbook is not enoughtobecome a SoftwareEngineer; thereareothernecessary courses that the reader musttaketo learn moreadvancedconceptsrelatedtothedevelopmentofbiggersoftwareprojects. Someoftherecommendedcourses are Database Systems, Data Structures, Operating Systems, Compilers, Software Engineering, Testing, Software Architecture,andSoftwareDesign,amongothers. Thecontentofthisbookwillpreparethereadertohavethenecessary backgroundforanyofthenextSoftwareEngineeringcourseslistedabove. Whileusingthisbook, readersshould followalongontheircomputerstobeabletotryalltheexamplesincludedinthechapters. Itwillbenecessarythat computershavealreadyinstalledtherequiredPythonlibraries. 3 Authors KarimPicharaBaksai Ph.D.inComputerScience, ResearchArea: MachineLearningandDataScienceappliedtoAstron- omy AssociateProfessor,ComputerScienceDepartment PontificiaUniversidadCatólicadeChile(PUC) ChristianPieringerBaeza Ph.D.inComputerScience ResearchArea: ComputerVisionandMachineLearning AdjuntProfessor,ComputerScienceDepartment PontificiaUniversidadCatólicadeChile(PUC) 4 Acknowledgments This book was not possible without the constant help of the teaching assistants; they gave us invaluable feedback, codeandtexteditionstoimprovethebook. ThemaincollaboratorswhohighlycontributedareBelénSaldías,Ivania Donoso,MarcoBucchi,PatricioLópez,andIgnacioBecker. Wewouldlikealsothanktheteamofassistantswhoworkedinthehands-onactivities: JaimeCastro,RodrigoGómez, BastiánMavrakis,VicenteDominguez,FelipeGarrido,JavieraAstudillo,AntonioGil,andJoséMaríaDeLaTorre. BelénSaldías IvaniaDonoso MarcoBucci PatricioLópez IgnacioBecker

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.