登录 注册
当前位置:主页 > 资源下载 > 10 > Python Scripting for Computational Science, Third Edition (2008).pdf下载

Python Scripting for Computational Science, Third Edition (2008).pdf下载

  • 更新:2024-12-10 08:20:56
  • 大小:5.04MB
  • 推荐:★★★★★
  • 来源:网友上传分享
  • 类别:Python - 后端
  • 格式:PDF

资源介绍

TableofContents 1 Introduction............................................ 1 1.1 ScriptingversusTraditionalProgramming................. 1 1.1.1 WhyScriptingisUsefulinComputationalScience... 2 1.1.2 ClassificationofProgrammingLanguages.......... 4 1.1.3 ProductivePairsofProgrammingLanguages....... 5 1.1.4 GluingExistingApplications..................... 6 1.1.5 ScriptingYieldsShorterCode.................... 7 1.1.6 Efficiency...................................... 8 1.1.7 Type-Specification(Declaration)ofVariables....... 9 1.1.8 FlexibleFunctionInterfaces...................... 11 1.1.9 InteractiveComputing........................... 12 1.1.10 CreatingCodeatRunTime...................... 13 1.1.11 NestedHeterogeneousDataStructures............. 14 1.1.12 GUIProgramming.............................. 16 1.1.13 MixedLanguageProgramming................... 17 1.1.14 WhentoChooseaDynamicallyTypedLanguage... 19 1.1.15 WhyPython?.................................. 20 1.1.16 ScriptorProgram?.............................. 21 1.2 PreparationsforWorkingwithThisBook................. 22 2 GettingStartedwithPythonScripting............ 27 2.1 AScientificHelloWorldScript.......................... 27 2.1.1 ExecutingPythonScripts........................ 28 2.1.2 DissectionoftheScientificHelloWorldScript...... 29 2.2 WorkingwithFilesandData............................ 32 2.2.1 ProblemSpecification........................... 32 2.2.2 TheCompleteCode............................. 33 2.2.3 Dissection...................................... 33 2.2.4 WorkingwithFilesinMemory.................... 36 2.2.5 ArrayComputing............................... 37 2.2.6 InteractiveComputingandDebugging............. 39 2.2.7 EfficiencyMeasurements......................... 42 2.2.8 Exercises...................................... 43 2.3 GluingStand-AloneApplications........................ 46 2.3.1 TheSimulationCode............................ 47 2.3.2 UsingGnuplottoVisualizeCurves................ 49 2.3.3 FunctionalityoftheScript....................... 50 2.3.4 TheCompleteCode............................. 51 2.3.5 Dissection...................................... 53 2.3.6 Exercises...................................... 55 2.4 ConductingNumericalExperiments...................... 58 2.4.1 WrappingaLoopAroundAnotherScript.......... 59 XII TableofContents 2.4.2 GeneratinganHTMLReport..................... 60 2.4.3 MakingAnimations............................. 61 2.4.4 VaryingAnyParameter.......................... 63 2.5 FileFormatConversion................................. 66 2.5.1 ASimpleRead/WriteScript...................... 66 2.5.2 StoringDatainDictionariesandLists............. 68 2.5.3 MakingaModulewithFunctions................. 69 2.5.4 Exercises...................................... 71 3 BasicPython...........................................73 3.1 IntroductoryTopics.................................... 74 3.1.1 RecommendedPythonDocumentation............. 74 3.1.2 ControlStatements............................. 75 3.1.3 RunningApplications........................... 76 3.1.4 FileReadingandWriting........................ 78 3.1.5 OutputFormatting.............................. 79 3.2 VariablesofDifferentTypes............................. 81 3.2.1 BooleanTypes.................................. 81 3.2.2 TheNoneVariable.............................. 82 3.2.3 NumbersandNumericalExpressions.............. 82 3.2.4 ListsandTuples................................ 84 3.2.5 Dictionaries.................................... 90 3.2.6 SplittingandJoiningText....................... 94 3.2.7 StringOperations............................... 95 3.2.8 TextProcessing................................. 96 3.2.9 TheBasicsofaPythonClass..................... 98 3.2.10 CopyandAssignment...........................100 3.2.11 DeterminingaVariable’sType....................104 3.2.12 Exercises......................................106 3.3 Functions.............................................110 3.3.1 KeywordArguments............................111 3.3.2 DocStrings....................................112 3.3.3 VariableNumberofArguments...................112 3.3.4 CallbyReference...............................114 3.3.5 TreatmentofInputandOutputArguments........115 3.3.6 FunctionObjects...............................116 3.4 WorkingwithFilesandDirectories.......................117 3.4.1 ListingFilesinaDirectory.......................118 3.4.2 TestingFileTypes..............................118 3.4.3 RemovingFilesandDirectories...................119 3.4.4 CopyingandRenamingFiles.....................120 3.4.5 SplittingPathnames.............................121 3.4.6 CreatingandMovingtoDirectories...............122 3.4.7 TraversingDirectoryTrees.......................122 3.4.8 Exercises......................................125 TableofContents XIII 4 NumericalComputinginPython...................131 4.1 AQuickNumPyPrimer................................132 4.1.1 CreatingArrays................................132 4.1.2 ArrayIndexing.................................136 4.1.3 LoopsoverArrays..............................138 4.1.4 ArrayComputations............................139 4.1.5 MoreArrayFunctionality........................142 4.1.6 TypeTesting...................................144 4.1.7 MatrixObjects.................................145 4.1.8 Exercises......................................146 4.2 VectorizedAlgorithms..................................147 4.2.1 FromScalartoArrayinFunctionArguments.......147 4.2.2 Slicing.........................................149 4.2.3 Exercises......................................150 4.3 MoreAdvancedArrayComputing........................151 4.3.1 RandomNumbers...............................152 4.3.2 LinearAlgebra.................................153 4.3.3 Plotting.......................................154 4.3.4 Example:CurveFitting..........................157 4.3.5 ArraysonStructuredGrids......................159 4.3.6 FileI/OwithNumPyArrays.....................163 4.3.7 FunctionalityintheNumpyutilsModule...........165 4.3.8 Exercises......................................168 4.4 OtherToolsforNumericalComputations.................173 4.4.1 TheScientificPythonPackage....................173 4.4.2 TheSciPyPackage..............................178 4.4.3 ThePython–MatlabInterface....................183 4.4.4 SymbolicComputinginPython...................184 4.4.5 SomeUsefulPythonModules.....................186 5 CombiningPythonwithFortran,C,andC++....189 5.1 AboutMixedLanguageProgramming....................189 5.1.1 ApplicationsofMixedLanguageProgramming......190 5.1.2 CallingCfromPython..........................190 5.1.3 AutomaticGenerationofWrapperCode...........192 5.2 ScientificHelloWorldExamples.........................194 5.2.1 CombiningPythonandFortran...................195 5.2.2 CombiningPythonandC........................201 5.2.3 CombiningPythonandC++Functions............208 5.2.4 CombiningPythonandC++Classes..............210 5.2.5 Exercises......................................214 5.3 ASimpleComputationalSteeringExample................215 5.3.1 ModifiedTimeLoopforRepeatedSimulations......216 5.3.2 CreatingaPythonInterface......................217 5.3.3 TheSteeringPythonScript......................218 5.3.4 EquippingtheSteeringScriptwithaGUI..........222 5.4 ScriptingInterfacestoLargeLibraries....................223 XIV TableofContents 6 IntroductiontoGUIProgramming.................227 6.1 ScientificHelloWorldGUI..............................228 6.1.1 IntroductoryTopics.............................228 6.1.2 TheFirstPython/TkinterEncounter..............230 6.1.3 BindingEvents.................................233 6.1.4 ChangingtheLayout............................234 6.1.5 TheFinalScientificHelloWorldGUI..............238 6.1.6 AnAlternativetoTkinterVariables...............240 6.1.7 AboutthePackCommand.......................241 6.1.8 AnIntroductiontotheGridGeometryManager....243 6.1.9 ImplementingaGUIasaClass...................245 6.1.10 ASimpleGraphicalFunctionEvaluator............247 6.1.11 Exercises......................................248 6.2 AddingGUIstoScripts.................................250 6.2.1 ASimulationandVisualizationScriptwithaGUI..250 6.2.2 ImprovingtheLayout...........................253 6.2.3 Exercises......................................256 6.3 AListofCommonWidgetOperations....................257 6.3.1 Frame.........................................259 6.3.2 Label..........................................260 6.3.3 Button........................................262 6.3.4 TextEntry.....................................262 6.3.5 BalloonHelp...................................264 6.3.6 OptionMenu...................................265 6.3.7 Slider.........................................265 6.3.8 CheckButton..................................266 6.3.9 MakingaSimpleMegawidget.....................266 6.3.10 MenuBar......................................267 6.3.11 ListData......................................269 6.3.12 Listbox........................................269 6.3.13 RadioButton..................................272 6.3.14 ComboBox....................................274 6.3.15 MessageBox...................................275 6.3.16 User-DefinedDialogs............................277 6.3.17 Color-PickerDialogs.............................278 6.3.18 FileSelectionDialogs............................279 6.3.19 Toplevel.......................................280 6.3.20 SomeOtherTypesofWidgets....................281 6.3.21 AdaptingWidgetstotheUser’sResizeActions.....282 6.3.22 CustomizingFontsandColors....................284 6.3.23 WidgetOverview...............................286 6.3.24 Exercises......................................289 TableofContents XV 7 WebInterfacesandCGIProgramming.............295 7.1 IntroductoryCGIScripts...............................296 7.1.1 WebFormsandCGIScripts......................297 7.1.2 GeneratingFormsinCGIScripts.................299 7.1.3 DebuggingCGIScripts..........................301 7.1.4 AGeneralShellScriptWrapperforCGIScripts....302 7.1.5 SecurityIssues..................................304 7.2 AddingWebInterfacestoScripts........................306 7.2.1 AClassforFormParameters.....................306 7.2.2 CallingOtherPrograms.........................308 7.2.3 RunningSimulations............................309 7.2.4 GettingaCGIScripttoWork....................311 7.2.5 UsingWebApplicationsfromScripts..............313 7.2.6 Exercises......................................316 8 AdvancedPython.....................................319 8.1 MiscellaneousTopics...................................319 8.1.1 ParsingCommand-LineArguments................319 8.1.2 Platform-DependentOperations..................322 8.1.3 Run-TimeGenerationofCode....................323 8.1.4 Exercises......................................324 8.2 RegularExpressionsandTextProcessing.................326 8.2.1 Motivation.....................................326 8.2.2 SpecialCharacters..............................329 8.2.3 RegularExpressionsforRealNumbers.............331 8.2.4 UsingGroupstoExtractPartsofaText...........334 8.2.5 ExtractingIntervalLimits........................335 8.2.6 ExtractingMultipleMatches.....................339 8.2.7 SplittingText..................................344 8.2.8 Pattern-MatchingModifiers......................345 8.2.9 SubstitutionandBackreferences..................347 8.2.10 Example:SwappingArgumentsinFunctionCalls...348 8.2.11 AGeneralSubstitutionScript....................351 8.2.12 DebuggingRegularExpressions...................353 8.2.13 Exercises......................................354 8.3 ToolsforHandlingDatainFiles.........................362 8.3.1 WritingandReadingPythonDataStructures......362 8.3.2 PicklingObjects................................364 8.3.3 ShelvingObjects................................366 8.3.4 WritingandReadingZipandTarArchiveFiles.....366 8.3.5 DownloadingInternetFiles.......................367 8.3.6 BinaryInput/Output............................368 8.3.7 Exercises......................................371 8.4 ADatabaseforNumPyArrays..........................371 8.4.1 TheStructureoftheDatabase....................371 8.4.2 Pickling.......................................374 8.4.3 FormattedASCIIStorage........................375 XVI TableofContents 8.4.4 Shelving.......................................376 8.4.5 ComparingtheVariousTechniques................377 8.5 ScriptsInvolvingLocalandRemoteHosts.................378 8.5.1 SecureShellCommands.........................378 8.5.2 DistributedSimulationandVisualization..........380 8.5.3 Client/ServerProgramming......................382 8.5.4 Threads.......................................382 8.6 Classes...............................................384 8.6.1 ClassProgramming.............................384 8.6.2 CheckingtheClassType.........................388 8.6.3 PrivateData...................................389 8.6.4 StaticData....................................390 8.6.5 SpecialAttributes...............................390 8.6.6 SpecialMethods................................391 8.6.7 MultipleInheritance.............................392 8.6.8 UsingaClassasaC-likeStructure................393 8.6.9 AttributeAccessviaStringNames................394 8.6.10 New-StyleClasses...............................394 8.6.11 ImplementingGet/SetFunctionsviaProperties.....395 8.6.12 SubclassingBuilt-inTypes.......................396 8.6.13 BuildingClassInterfacesatRunTime.............399 8.6.14 BuildingFlexibleClassInterfaces.................403 8.6.15 Exercises......................................409 8.7 ScopeofVariables.....................................413 8.7.1 Global,Local,andClassVariables................413 8.7.2 NestedFunctions...............................415 8.7.3 DictionariesofVariablesinNamespaces............416 8.8 Exceptions............................................418 8.8.1 HandlingExceptions............................419 8.8.2 RaisingExceptions..............................420 8.9 Iterators..............................................421 8.9.1 ConstructinganIterator.........................421 8.9.2 APointwiseGridIterator........................423 8.9.3 AVectorizedGridIterator.......................427 8.9.4 Generators.....................................428 8.9.5 SomeAspectsofGenericProgramming............432 8.9.6 Exercises......................................436 8.10InvestigatingEfficiency.................................437 8.10.1 CPU-TimeMeasurements........................437 8.10.2 ProfilingPythonScripts.........................441 8.10.3 OptimizationofPythonCode....................442 8.10.4 CaseStudyonNumericalEfficiency...............445 TableofContents XVII 9 FortranProgrammingwithNumPyArrays.......451 9.1 ProblemDefinition.....................................451 9.2 FillinganArrayinFortran..............................453 9.2.1 TheFortranSubroutine..........................454 9.2.2 BuildingandInspectingtheExtensionModule......455 9.3 ArrayStorageIssues...................................457 9.3.1 GeneratinganErroneousInterface................457 9.3.2 ArrayStorageinCandFortran...................459 9.3.3 InputandOutputArraysasFunctionArguments...459 9.3.4 F2PYInterfaceFiles............................466 9.3.5 HidingWorkArrays.............................470 9.4 IncreasingCallbackEfficiency...........................470 9.4.1 CallbackstoVectorizedPythonFunctions..........471 9.4.2 AvoidingCallbackstoPython....................473 9.4.3 CompiledInlineCallbackFunctions...............474 9.5 Summary.............................................478 9.6 Exercises.............................................479 10CandC++ProgrammingwithNumPyArrays..483 10.1AutomaticInterfacingofC/C++Code...................484 10.1.1 UsingF2PY....................................485 10.1.2 UsingInstant...................................486 10.1.3 UsingWeave...................................487 10.2CProgrammingwithNumPyArrays.....................488 10.2.1 TheBasicsoftheNumPyCAPI..................489 10.2.2 TheHandwrittenExtensionCode.................491 10.2.3 SendingArgumentsfromPythontoC.............492 10.2.4 ConsistencyChecks.............................493 10.2.5 ComputingArrayValues.........................494 10.2.6 ReturninganOutputArray......................496 10.2.7 ConvenientMacros..............................497 10.2.8 ModuleInitialization............................499 10.2.9 ExtensionModuleTemplate......................500 10.2.10Compiling,Linking,andDebuggingtheModule.....502 10.2.11WritingaWrapperforaCFunction...............503 10.3C++ProgrammingwithNumPyArrays..................506 10.3.1 WrappingaNumPyArrayinaC++Object.......506 10.3.2 UsingSCXX...................................508 10.3.3 NumPy–C++ClassConversion...................511 10.4ComparisonoftheImplementations......................519 10.4.1 Efficiency......................................519 10.4.2 ErrorHandling.................................523 10.4.3 Summary......................................524 10.5Exercises.............................................525 XVIII TableofContents 11MoreAdvancedGUIProgramming.................529 11.1AddingPlotAreasinGUIs..............................529 11.1.1 TheBLTGraphWidget.........................530 11.1.2 AnimationofFunctionsinBLTGraphWidgets.....536 11.1.3 OtherToolsforMakingGUIswithPlots...........538 11.1.4 Exercises......................................539 11.2EventBindings........................................541 11.2.1 BindingEventstoFunctionswithArguments.......542 11.2.2 ATextWidgetwithTailoredKeyboardBindings...544 11.2.3 AFancyListWidget............................547 11.3AnimatedGraphicswithCanvasWidgets.................550 11.3.1 TheFirstCanvasEncounter......................551 11.3.2 CoordinateSystems.............................552 11.3.3 TheMathematicalModelClass...................556 11.3.4 ThePlanetClass...............................557 11.3.5 DrawingandMovingPlanets.....................559 11.3.6 DraggingPlanetstoNewPositions................560 11.3.7 UsingPmw’sScrolledCanvasWidget..............564 11.4SimulationandVisualizationScripts.....................566 11.4.1 RestructuringtheScript.........................567 11.4.2 RepresentingaParameterbyaClass..............569 11.4.3 ImprovedCommand-LineScript..................583 11.4.4 ImprovedGUIScript............................584 11.4.5 ImprovedCGIScript............................585 11.4.6 ParameterswithPhysicalDimensions.............586 11.4.7 AddingaCurvePlotArea.......................588 11.4.8 AutomaticGenerationofScripts..................589 11.4.9 ApplicationsoftheTools........................590 11.4.10AllowingPhysicalUnitsinInputFiles.............596 11.4.11ConvertingInputFilestoGUIs...................601 12ToolsandExamples...................................605 12.1RunningSeriesofComputerExperiments.................605 12.1.1 MultipleValuesofInputParameters..............606 12.1.2 ImplementationDetails..........................609 12.1.3 FurtherApplications............................614 12.2ToolsforRepresentingFunctions.........................618 12.2.1 FunctionsDefinedbyStringFormulas.............618 12.2.2 AUnifiedInterfacetoFunctions..................623 12.2.3 InteractiveDrawingofFunctions..................629 12.2.4 ANotebookforSelectingFunctions...............633 12.3SolvingPartialDifferentialEquations.....................640 12.3.1 NumericalMethodsfor1DWaveEquations........641 12.3.2 Implementationsof1DWaveEquations............644 12.3.3 ClassesforSolving1DWaveEquations............651 12.3.4 AProblemSolvingEnvironment..................657 12.3.5 NumericalMethodsfor2DWaveEquations........663 TableofContents XIX 12.3.6 Implementationsof2DWaveEquations............666 12.3.7 Exercises......................................675 ASettinguptheRequiredSoftwareEnvironment...677 A.1 InstallationonUnixSystems............................677 A.1.1 ASuggestedDirectoryStructure..................677 A.1.2 SettingSomeEnvironmentVariables..............678 A.1.3 InstallingTcl/TkandAdditionalModules.........679 A.1.4 InstallingPython...............................680 A.1.5 InstallingPythonModules.......................681 A.1.6 InstallingGnuplot..............................683 A.1.7 InstallingSWIG................................684 A.1.8 SummaryofEnvironmentVariables...............684 A.1.9 TestingtheInstallationofScriptingUtilities........685 A.2 InstallationonWindowsSystems........................685 BElementsofSoftwareEngineering...................689 B.1 BuildingandUsingModules.............................689 B.1.1 Single-FileModules.............................689 B.1.2 Multi-FileModules..............................693 B.1.3 DebuggingandTroubleshooting...................694 B.2 ToolsforDocumentingPythonSoftware..................696 B.2.1 DocStrings....................................696 B.2.2 ToolsforAutomaticDocumentation...............698 B.3 CodingStandards......................................702 B.3.1 StyleGuide....................................702 B.3.2 PythonicProgramming..........................706 B.4 VerificationofScripts...................................711 B.4.1 AutomatingRegressionTests.....................711 B.4.2 ImplementingaToolforRegressionTests..........715 B.4.3 WritingaTestScript............................719 B.4.4 VerifyingOutputfromNumericalComputations....720 B.4.5 AutomaticDocStringTesting....................724 B.4.6 UnitTesting...................................726 B.5 VersionControlManagement............................728 B.5.1 Mercurial......................................729 B.5.2 Subversion.....................................732 B.6 Exercises.............................................734 Bibliography...............................................739 Index........................................................741