ebook img

Fast Lane to Python - Amazon Web Services PDF

170 Pages·2011·0.66 MB·English
by  
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 Fast Lane to Python - Amazon Web Services

Fast Lane to Python A quick, sensible route to the joys of Python coding Norm Matloff University of California, Davis This work is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States Li- cense. CopyrightisretainedbyN.Matloffinallnon-U.S.jurisdictions,butpermissiontousethesematerials inteachingisstillgranted,providedtheauthorshipandlicensinginformationhereisdisplayed. The author has striven to minimize the number of errors, but no guarantee is made as to accuracy of the contentsofthisbook. 2 Author’sBiographicalSketch Dr. Norm Matloff is a professor of computer science at the University of California at Davis, and was formerly a professor of mathematics and statistics at that university. He is a former database software developer in Silicon Valley, and has been a statistical consultant for firms such as the Kaiser Permanente HealthPlan. Dr. MatloffwasborninLosAngeles,andgrewupinEastLosAngelesandtheSanGabrielValley. Hehas aPhDinpuremathematicsfromUCLA,specializinginprobabilitytheoryandstatistics. Hehaspublished numerous papers in computer science and statistics, with current research interests in parallel processing, statisticalcomputing,andregressionmethodology. Prof. Matloff is a former appointed member of IFIP Working Group 11.3, an international committee concerned with database software security, established under UNESCO. He was a founding member of the UC Davis Department of Statistics, and participated in the formation of the UCD Computer Science Departmentaswell. HeisarecipientofthecampuswideDistinguishedTeachingAwardandDistinguished PublicServiceAwardatUCDavis. Dr. Matloffistheauthoroftwopublishedtextbooks,andofanumberofwidely-usedWebtutorialsoncom- puter topics, such as the Linux operating system and the Python programming language. He and Dr. Peter SalzmanareauthorsofTheArtofDebuggingwithGDB,DDD,andEclipse. Prof. Matloff’sbookontheR programminglanguage,TheArtofRProgramming,isduetobepublishedin2011. Heisalsotheauthorof several open-source textbooks, including From Algorithms to Z-Scores: Probabilistic and Statistical Mod- elinginComputerScience(http://heather.cs.ucdavis.edu/probstatbook), andProgram- mingonParallelMachines(http://heather.cs.ucdavis.edu/˜matloff/ParProcBook.pdf). Contents 1 Introduction 1 1.1 A5-MinuteIntroductoryExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.1 ExampleProgramCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.2 PythonLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.3 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.4 PythonBlockDefinition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.5 PythonAlsoOffersanInteractiveMode . . . . . . . . . . . . . . . . . . . . . . . . 5 1.1.6 PythonAsaCalculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.2 A10-MinuteIntroductoryExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.2.1 ExampleProgramCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.2.2 Command-LineArguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.2.3 IntroductiontoFileManipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.2.4 LackofDeclaration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.2.5 LocalsVs. Globals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.2.6 ACoupleofBuilt-InFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.3 TypesofVariables/Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.4 StringVersusNumericalValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.5 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.5.1 Lists(Quasi-Arrays) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 i ii CONTENTS 1.5.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.5.3 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.5.3.1 StringsAsTurbochargedTuples . . . . . . . . . . . . . . . . . . . . . . 15 1.5.3.2 FormattedStringManipulation . . . . . . . . . . . . . . . . . . . . . . . 16 1.5.4 SortingSequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.6 Dictionaries(Hashes) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.7 FunctionDefinition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.8 Useof name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.9 ExtendedExample: ComputingFinalGrades . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.10 Object-OrientedProgramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.10.1 ExampleProgramCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.10.2 TheObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.10.3 ConstructorsandDestructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.10.4 InstanceVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.10.5 ClassVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.10.6 InstanceMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.10.7 ClassMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.10.8 DerivedClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.10.9 AWordonClassImplementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.11 ImportanceofUnderstandingObjectReferences . . . . . . . . . . . . . . . . . . . . . . . . 28 1.12 ObjectDeletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1.13 ObjectComparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 1.14 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 1.14.1 ExampleProgramCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 1.14.2 HowimportWorks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 1.14.3 Usingreload()toRenewanImport . . . . . . . . . . . . . . . . . . . . . . . . . . 33 1.14.4 CompiledCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 CONTENTS iii 1.14.5 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 1.14.6 ANoteonGlobalVariablesWithinModules . . . . . . . . . . . . . . . . . . . . . 34 1.14.7 DataHiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 1.15 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.16 ExceptionHandling(NotJustforExceptions!). . . . . . . . . . . . . . . . . . . . . . . . . 37 1.17 Docstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 1.18 NamedArgumentsinFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1.19 TerminalI/OIssues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1.19.1 KeyboardInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1.19.2 PrintingWithoutaNewlineorBlanks . . . . . . . . . . . . . . . . . . . . . . . . . 40 1.20 ExtendedExample: CreatingLinkedDataStructuresinPython . . . . . . . . . . . . . . . . 40 1.21 MakingUseofPythonIdioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 1.22 FunctionalProgrammingFeatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 1.22.1 LambdaFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 1.22.2 Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 1.22.3 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 1.22.4 Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 1.22.5 ListComprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 1.22.6 Example: TextfileClassRevisited . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 1.23 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 1.24 OnlineDocumentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 1.24.1 Thedir()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 1.24.2 Thehelp()Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 1.24.3 PyDoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 1.25 PuttingAllGlobalsintoaClass. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 1.26 LookingatthePythonVirtualMachine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 1.27 RunningPythonScriptsWithoutExplicitlyInvokingtheInterpreter . . . . . . . . . . . . . 52 iv CONTENTS 2 FileandDirectoryAccessinPython 53 2.1 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.1.1 SomeBasicFileOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.2 Directories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2.2.1 SomeBasicDirectoryOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2.2.2 ThePowerfulwalk()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 2.3 Cross-PlatformIssues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 2.3.1 TheSmallStuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 2.3.2 HowIsItDone? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 2.3.3 PythonandSo-Called“Binary”Files . . . . . . . . . . . . . . . . . . . . . . . . . 59 3 PythonCursesProgramming 61 3.1 Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.2 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.3 RelevanceToday . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3.4 ExamplesofPythonCursesPrograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.4.1 UselessExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.4.2 UsefulExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3.4.3 AFewOtherShortExamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.5 WhatElseCanCursesDo? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.5.1 CursesbyItself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.6 LibrariesBuiltonTopofCurses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 3.7 IfYourTerminalWindowGetsMessedUp. . . . . . . . . . . . . . . . . . . . . . . . . . . 68 3.8 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4 PythonIteratorsandGenerators 69 4.1 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 CONTENTS v 4.1.1 WhatAreIterators? WhyUseThem? . . . . . . . . . . . . . . . . . . . . . . . . . 69 4.1.2 Example: FibonacciNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4.1.3 Theiter()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 4.1.4 ApplicationstoSituationswithanIndefiniteNumberofIterations . . . . . . . . . . 73 4.1.4.1 Client/ServerExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4.1.4.2 “Circular”ArrayExample . . . . . . . . . . . . . . . . . . . . . . . . . . 75 4.1.5 Overwritingthenext()Function: FileSubclassExample . . . . . . . . . . . . . . . 76 4.1.6 IteratorFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.1.6.1 GeneralFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.1.6.2 TheitertoolsModule . . . . . . . . . . . . . . . . . . . . . . . . . . 78 4.2 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 4.2.1 GeneralStructures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 4.2.2 Example: FibonacciNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 4.2.3 Example: WordFetcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 4.2.4 MutipleIteratorsfromtheSameGenerator . . . . . . . . . . . . . . . . . . . . . . 82 4.2.5 Theos.path.walk()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4.2.6 Don’tPutyieldinaSubfunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4.2.7 Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4.2.7.1 MythrdClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 4.2.7.2 TheSimPyDiscreteEventSimulationLibrary . . . . . . . . . . . . . . . 92 5 ParallelPythonThreadsandMultiprocessingModules 97 5.1 ThePythonThreadsandMultiprocessingModules . . . . . . . . . . . . . . . . . . . . . . 97 5.1.1 PythonThreadsModules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.1.1.1 ThethreadModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 5.1.1.2 ThethreadingModule . . . . . . . . . . . . . . . . . . . . . . . . . . 107 5.1.2 ConditionVariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 vi CONTENTS 5.1.2.1 GeneralIdeas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 5.1.2.2 OtherthreadingClasses . . . . . . . . . . . . . . . . . . . . . . . . . 111 5.1.3 ThreadsInternals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 5.1.3.1 Kernel-LevelThreadManagers . . . . . . . . . . . . . . . . . . . . . . . 112 5.1.3.2 User-LevelThreadManagers . . . . . . . . . . . . . . . . . . . . . . . . 112 5.1.3.3 Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 5.1.3.4 ThePythonThreadManager . . . . . . . . . . . . . . . . . . . . . . . . 113 5.1.3.5 TheGIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 5.1.3.6 ImplicationsforRandomnessandNeedforLocks . . . . . . . . . . . . . 114 5.1.4 ThemultiprocessingModule . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.1.5 TheQueueModuleforThreadsandMultiprocessing. . . . . . . . . . . . . . . . . 118 5.1.6 DebuggingThreadedandMultiprocessingPythonPrograms . . . . . . . . . . . . . 121 5.2 UsingPythonwithMPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 5.2.1 UsingPDBtoDebugThreadedPrograms . . . . . . . . . . . . . . . . . . . . . . . 123 5.2.2 RPDB2andWinpdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 6 NetworkProgrammingwithPython 125 6.1 OverviewofNetworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 6.1.1 NetworksandMACAddresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 6.1.2 TheInternetandIPAddresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 6.1.3 Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 6.1.4 ConnectionlessandConnection-OrientedCommunication . . . . . . . . . . . . . . 127 6.1.5 ClientsandServers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 6.2 OurExampleClient/ServerPair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 6.2.1 AnalysisoftheServerProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 6.2.2 AnalysisoftheClientProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 6.3 RoleoftheOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 CONTENTS vii 6.3.1 BasicOperation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 6.3.2 HowtheOSDistinguishesBetweenMultipleConnections . . . . . . . . . . . . . . 134 6.4 Thesendall()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 6.5 SendingLinesofText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 6.5.1 Remember,It’sJustOneBigByteStream,Not“Lines” . . . . . . . . . . . . . . . . 135 6.5.2 TheWonderfulmakefile()Function . . . . . . . . . . . . . . . . . . . . . . . . 135 6.5.3 GettingtheTailEndoftheData . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 6.6 DealingwithAsynchronousInputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6.6.1 NonblockingSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 6.6.2 AdvancedMethodsofPolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 6.7 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 6.8 OtherLibraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 6.9 WebOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 7 PythonDebugging 145 7.1 TheEssenceofDebugging: ThePrincipleofConfirmation . . . . . . . . . . . . . . . . . . 145 7.2 PlanforThisChapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 7.3 Python’sBuilt-InDebugger,PDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 7.3.1 TheBasics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 7.3.2 UsingPDBMacros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 7.3.3 Using dict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 7.3.4 Thetype()Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 7.3.5 UsingPDBwithEmacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 7.3.6 DebuggingwithXpdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7.4 DebuggingwithWinpdb(GUI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7.5 DebuggingwithEclipse(GUI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 7.6 DebuggingwithPUDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 viii CONTENTS 7.7 SomePythonInternalDebuggingAids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 7.7.1 Thestr()Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 7.7.2 Thelocals()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 7.7.3 The dict Attribute. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 7.7.4 Theid()Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

Description:
Fast Lane to Python A quick, sensible route to the joys of Python coding Norm Matloff University of California, Davis 3 Python Curses Programming 61
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.