Author(s): Daniel Arbuckle
Publisher: Packt Publishing
Year: 2017
Chapter 1. Python PrimerIn this chapter, we will be covering the basic syntax of Python, its built-in data structures, functions, classes, its standard library, and the new features in the latest versions of Python. If you need to get up to speed on the language, this is where we'll do that. We'll walk through, step by step, covering the following topics:Python basic syntax and block structureBuilt-in data structures and comprehensionsFirst-class functions and classesExtensive standard libraryWhat's new in Python
Chapter 1. Python Primer
Chapter 2. Setting UpIn the previous chapter, we took a brief tour of the Python programming language. In this chapter, we will look at various ways to download and install the correct version of Python, then we will see how to run Python code. In the last couple of sections of this chapter, we'll see how to begin taking advantage of the wide variety of publicly available Python code that can be found on the internet.The topics covered in this chapter are as follows:Downloading and installing PythonUsing the command line and the interactive shellInstalling packages with pipFinding packages in the Python package index
Chapter 2. Setting Up
Chapter 3. Making a PackageIn the previous chapter, we saw how to install Python and third-party packages of code that we can use with Python. In this chapter, we'll see how packages are represented on the computer's filesystem. We'll take a look at how to add code modules inside the package, how to make those code modules interact with each other within the package, and how to access data from non-code files that are incorporated into our package.By the end of this chapter, you'll have a pretty good idea about how to create your own packages of Python code. Packages will form the basis for programs and help you to make your code modular.In this chapter, we will cover the following topics:Creating an empty packageAdding modules to the packageAccessing code from other modulesAdding static data files to the package
Chapter 3. Making a Package
Chapter 4. Basic Best PracticesIn the previous chapter, we saw how to put together a Python package of code and data. In this chapter, we're going to look at some rather simple things we can do that will make our lives as Python programmers simpler overall. We'll switch gears and look at version control, which will help us to collaborate with other programmers and serve as an undo buffer for the whole lifetime of a project. We're going to look at Python's built-in virtual environment tool, venv, which allows us to keep our programs and dependencies separate from each other and the software installed on our overall system.You'll learn how to structure our docstrings for maximum utility, how to add Rich Text formatting to them, and how to export them into hyperlinked HTML documentation for viewing in a web browser. You'll also see one more cool advantage we can get from docstrings by actually executing the examples we include in our documentation and making sure they agree with what the
Chapter 4. Basic Best Practices
Chapter 5. Making a Command-Line UtilityIn the previous chapter, we visited some best practices that would help us in the long run when using Python. In this chapter, we're going to see how to make Python command-line programs and some features that make such programs easier and more useful. We're going to see how to create an entry point for code execution in a package and see how to run the package as a program.We're also going to see how to make the program read data from its command line and how to easily handle reading data from our program's command-line arguments. We'll also look at how to actually run other programs from inside our code.In this chapter, we will cover the following topics:Making a package executable via Python -mHandling command-line argument with argparsePython tools to interact with the userExecuting other programs with subprocessUsing shell script or batch files to run our programs
Chapter 5. Making a Command-Line Utility
Chapter 6. Parallel ProcessingIn the previous chapter, we created a text mode utility program and you learned about several of Python's built-in packages. In this chapter, we're going to see how to use both the high-level concurrent.futures package and the lower-level multiprocessing package to help us write parallel programs. Both are part of the Python standard library.We will cover the following two topics in detail:Using the concurrent.futures packageUsing multiprocessing packages
Chapter 6. Parallel Processing
Chapter 7. Coroutines and Asynchronous I/OIn the previous chapter, we looked at how to use multiple processes to increase the rate of data processing in our programs. This is great for CPU-bound programs because it allows them to use more than one CPU.In this chapter, we'll look at the inverse of this case; we'll use a single CPU to handle multiple data processing tasks at once within a single process, which is great for I/O-bound programs. We'll see some of the nuts and bolts of working with asyncio. We'll also discuss asyncio's future class and how it's used. Then we'll move on to synchronization and communication between asynchronous coroutine tasks. Lastly, we'll see how to use asyncio and coroutines to write a client-server program to communicate over a network.We will cover the following topics:The difference between asynchronous processing and parallel processingUsing the asyncio event loop and coroutine schedulerWaiting for data to become availableSynchronizing multiple tasksCo
Chapter 7. Coroutines and Asynchronous I/O
Chapter 8. MetaprogrammingIn the previous chapter, we discussed asynchronous I/O and coroutines. In this chapter, we turn our attention to metaprogramming and programmable syntax. We'll discuss various ways that Python allows us to control or alter the meaning of syntactic elements and use these features beneficially.We'll look at another programmable syntax feature of Python that meshes nicely with function decorators. We'll also discuss class decorators and how they're similar to, and different from, function decorators. Then we'll see a different way of programmatically modifying classes using metaclasses. We'll move on to a less esoteric topic and discuss context managers. Finally, we'll look at one more way of programming the semantics of basic Python operations when we look at descriptors.Metaprogramming is a blanket term for techniques where programs use program code or data structures constructed directly from program code as data to be manipulated. Python has a number of diffe
Chapter 8. Metaprogramming
Chapter 9. Unit TestingIn the previous chapter, we saw various approaches to metaprogramming and programmable syntax in Python. In this chapter, we're going to take a look at the ideas behind unit testing, then move on to several test automation tools we can use to make our testing easier and more useful. We'll focus on what unit testing is, and the ideas that motivate it. We'll also discuss Python's standard unittest package and how it works.Finally, you'll learn how to use unittest.mock to control the environment that your test code will run in so that the test will remain focused on making sure one thing works properly.In this chapter, we'll cover the following topics:Understanding the principle of unit testingUsing the unittest packageUsing unittest.mockUsing unittest's test discoveryUsing nose for unified test discovery and reporting
Chapter 9. Unit Testing
Chapter 10. Reactive ProgrammingIn the previous chapter, you learned about unit testing and the unittest.mock package. In this chapter, you will get a handle on the concepts of reactive programming and then take a look at the RxPY reactive programming framework. We'll work on solidifying your conceptual understanding of reactive programming and put together a very bare-bones reactive programming system from scratch.In this chapter, we will cover the following topics:What does reactive programming mean?Building a simple reactive programming frameworkUsing the Reactive Extensions for Python (RxPY)
Chapter 10. Reactive Programming
Chapter 11. MicroservicesIn the previous chapter, we looked at reactive programming and the ReactiveX framework. In this chapter, we're going to take a look at what a microservice is, why we might want to structure our programs as microservices, and how to use some common Python tools to create them. You'll learn how to use the Flask package to quickly and easily construct a microservice that uses HTTP and representational state transfer, (REST), to provide its interface. We're also going to look at using the nameko package to create microservices that communicate using remote procedure calls instead of HTTP methods.In this chapter, we will cover the following topics:Microservices and the advantages of process isolationBuilding high-level microservices with FlaskBuilding high-level microservices with nameko
Chapter 11. Microservices
Chapter 12. Extension Modules and Compiled CodeIn this chapter, we'll discuss how to integrate compiled code into Python programs. We'll take a look at the benefits and drawbacks of our compiled code and see two ways of making a connection between Python's managed environment and code that runs directly on the hardware.We'll see how to use the ctypes package and tie it to the interface of a C dynamic library, calling its functions and receiving their output from within our Python code. We'll also look at an easy way to write compiled code modules so that they could be imported and called directly from Python.We'll cover the following topics in detail:Advantages and disadvantages of compiled codeAccessing a dynamic library using ctypesInterfacing with C code using Cython
Chapter 12. Extension Modules and Compiled Code