Daniel Arbuckle’s Mastering Python

Book description

Gain a thorough understanding of operating in a Python development environment, and some of the most important advanced topics with Daniel Arbuckle. This dynamic, concise book is full of real-world solutions for Python 3.6 problems, and advanced-level concepts such as reactive programming, microservices, ctypes and Cython.

About This Book

  • Covers the latest and advanced concepts of Python such as parallel processing with Python 3.6
  • Explore the Python language from its basic installation and setup to concepts such as reactive programming and microservices
  • Get introduced to the mechanism for rewriting code in a compiled language along with ctypes and Cython tools

Who This Book Is For

If you are a programmer and are familiar with the basics of Python, and you want to broaden your knowledge base to develop projects better and faster, this book is for you. Even if you are not familiar with Python, Daniel Arbuckle's Mastering Python starts with the basics and takes you on a journey to become an expert in the technology.

What You Will Learn

  • Get to grips with the basics of operating in a Python development environment
  • Build Python packages to efficiently create reusable code
  • Become proficient at creating tools and utility programs in Python
  • Use the Git version control system to protect your development environment from unwanted changes
  • Harness the power of Python to automate other software
  • Distribute computational tasks across multiple processors
  • Handle high I/O loads with asynchronous I/O to get a smoother performance
  • Take advantage of Python's metaprogramming and programmable syntax features
  • Get acquainted with the concepts behind reactive programming and RxPy

In Detail

Daniel Arbuckle's Mastering Python covers the basics of operating in a Python development environment, before moving on to more advanced topics. Daniel presents you with real-world solutions to Python 3.6 and advanced-level concepts, such as reactive programming, microservices, ctypes, and Cython tools.

You don't need to be familiar with the Python language to use this book, as Daniel starts with a Python primer. Throughout, Daniel highlights the major aspects of managing your Python development environment, shows you how to handle parallel computation, and helps you to master asynchronous I/O with Python 3.6 to improve performance. Finally, Daniel will teach you the secrets of metaprogramming and unit testing in Python, helping you acquire the perfect skillset to be a Python expert.

Daniel will get you up to speed on everything from basic programming practices to high-end tools and techniques, things that will help set you apart as a successful Python programmer.

Style and Approach

Daniel Arbuckle's Mastering Python covers basic to advanced-level concepts in computer science. If you are a beginner, then Daniel will help you get started. If you are experienced, he will expand your knowledge base.

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Python Primer
    1. Python basic syntax and block structure
      1. Basic building blocks
        1. Functions
        2. Variables
        3. Expressions
        4. Classes
        5. Flow control statements
      2. Indentation
    2. Python's built-in data structures and comprehensions
      1. Dictionaries
      2. List
      3. Tuple
      4. Set
      5. Comprehension
    3. First-class functions and classes
      1. The defaultdict class
      2. Attributes
    4. The standard library
      1. Different types of packages
    5. What's new in modern Python
      1. The changes in the syntactic
      2. Changes in packages
      3. Other changes in Python packages
    6. Summary
  3. Setting Up
    1. Downloading and installing Python
      1. Choosing a suitable version
      2. Installing Python
    2. Using the command line and the interactive shell
      1. Opening a command-line window
      2. Python interactive shell
    3. Installing packages with pip
      1. The pip tool for packages
      2. Managing installed packages
    4. Finding packages in the Python Package Index
      1. Using keywords
      2. Using Package Index
      3. Searching the Package Index with pip
      4. Legalities and licenses of the Python Package Index
    5. Summary
  4. Making a Package
    1. Creating an empty package
      1. Turning a regular folder into a package
        1. Importing all package modules
    2. Adding modules to the package
      1. Module loading with namespace packages
      2. The Package structure and interface
    3. Accessing code from other modules
      1. Importing a cyclic dependency
        1. Resolving attribute errors raised due to cyclic dependencies
    4. Adding static data files to the package
    5. Summary
  5. Basic Best Practices
    1. PEP 8 and writing readable code
      1. PEP 8 — guidelines for Python code
        1. Code indentation
        2. Formatting recommendations
        3. Naming conventions
    2. Using version control
      1. Initializing Git
        1. Committing the changes in Git
        2. Undoing the changes
      2. Branches
        1. Merging codes
        2. The mergetool command
        3. The pull command
    3. Using venv to create a stable and isolated work area
      1. Creating a virtual environment
      2. Activating a virtual environment
      3. pip in virtual environments
    4. Getting the most out of docstrings
      1. PEP 257 and docutils
      2. Sphinx
      3. Turning docstrings into HTML
      4. Using doctest to test documentation examples
      5. Testing examples using doctest
        1. What it means when a code example fails
    5. Summary
  6. Making a Command-Line Utility
    1. Making a package executable via Python -m
      1. Pipeline program
    2. Handling command-line arguments with argparse
      1. Creating an ArgumentParser object
      2. Setting the name of argument
      3. nargs
    3. Python tools to interact with the user
      1. Python's built-in functions - print and input
        1. The getpass package
        2. The pprint package
      2. The cmd class
      3. The Pipeline user interface
    4. Executing other programs with subprocess
      1. Subprocess and its variants
      2. Using the Popen subprocess
        1. The PIPE constant
        2. The wait method
      3. Finishing up our code example
    5. Setting up a shell script or batch file to launch the program
      1. Creating launches for our program
    6. Summary
  7. Parallel Processing
    1. Using the concurrent.futures package
      1. The concurrent.futures module
        1. Calling ProcessPoolExecutor
        2. Using the map method
        3. Using the submit method
          1. The done and result methods
          2. The wait and as_completed functions
          3. The add done callback function
          4. The cancel method
    2. Using the multiprocessing packages
      1. Process class in the multiprocessing module
        1. Queues
        2. Pipes
        3. Manager
          1. The lock object
          2. The event object
          3. The condition object
          4. The semaphore object
    3. Summary
  8. Coroutines and Asynchronous I/O
    1. The difference between asynchronous processing and parallel processing
      1. Multithreading is not good for servers
      2. Cooperative coroutine scheduler versus coroutine
        1. Python coroutines
          1. The coroutine scheduler
    2. Using the asyncio event loop and coroutine scheduler
      1. Creating a coroutine
      2. The asyncio scheduler - event_loop
        1. ensure_future
        2. The run_forever/run_until_complete methods
        3. Closing event_loop
    3. Awaiting data availability
      1. asyncio's future objects
      2. Asynchronous iterations
    4. Synchronizing multiple tasks
      1. Synchronization primitives
        1. The wait coroutine
        2. The wait_for coroutine
        3. The gather coroutine
      2. The asyncio Queue class
        1. Queue types
    5. Communicating across the network
      1. Creating a simple client in asyncio
      2. Creating a simple server in asyncio
      3. Handling client disconnections
    6. Summary
  9. Metaprogramming
    1. Using function decorators
      1. Using the @ syntax in a function decorator
      2. Global decorator - @staticmethod
      3. Attributes
      4. Enclosing the function in a wrapper
        1. The @wraps decorator
        2. The only function
    2. Function annotations
      1. Function annotation syntax
      2. Accessing annotation data
        1. The @no_type_check decorator
      3. Annotations as input to function decorators
        1. Keyword arguments
        2. Inspecting the package signature function
    3. Class decorators
      1. Modifying class attributes
      2. The factory function
        1. The factory_constructed function
        2. Class definitions
    4. Metaclasses
      1. What can we do with a metaclass?
        1. The __prepare__method
        2. The __new__ method
    5. Context managers
      1. Defining a context manager as a generator
      2. Adding context manager behavior to a class
        1. Synchronous-coroutine-based context managers
        2. Creating an asynchronous-coroutine-based context manager
    6. Descriptors
      1. Using @property to create a descriptor
      2. Writing descriptors as classes
    7. Summary
  10. Unit Testing
    1. Understanding the principle of unit testing
      1. What is a unit test?
    2. Using the unittest package
      1. Structuring a test file
      2. assert methods
      3. Comparing what happens to what should happen in unit tests
    3. Using unittest.mock
      1. What is a mock object?
        1. Preconfiguring mock objects
      2. assert methods of mock objects
      3. The unittest.mock patch function
    4. Using unittest's test discovery
      1. Unittest's discovery tool
        1. Command-line options in unit test discovery
    5. Using nose for unified test discovery and reporting
      1. Running our tests with nose
        1. The cover-package option
        2. Testing multiple worker processes
    6. Summary
  11. Reactive Programming
    1. The concept of reactive programming
    2. Building a simple reactive programming framework
      1. Observers
      2. Observables
        1. Emitting events
      3. Building the observable sequence
        1. Illustrating a stream of animal events
      4. Composing an observable sequence
    3. Using the reactive extensions for Python (RxPY)
      1. Translating our zoo demo into Rx
        1. Observable factory methods
        2. Explaining the observable sequence of events
          1. Creating an asyncio scheduler
          2. Combining and processing observable sequences
      2. Miscellaneous observable factory methods
        1. The Observable.create method
        2. The Observable.select_many method
        3. Empty, return_value, and from_iterable factory methods
        4. The where factory method
    4. Summary
  12. Microservices
    1. Microservices and the advantages of process isolation
      1. Advantages of the microservice architecture
      2. Applying the microservice architecture to web servers
    2. Building high-level microservices with Flask
      1. Installing Flask
      2. Creating endpoints for a RESTful API in Flask
        1. Building a microservice to maintain a database
        2. Making Flask handle a request
      3. Running and connecting to our microservice using Flask
        1. Test running the microservice
    3. Building high-level microservices with nameko
      1. Installing nameko
      2. Running and connecting a microservice using nameko
        1. Things to know before using nameko
        2. Interacting with our microservice
          1. Interacting with a microservice manually using the nameko shell
          2. Interacting with a microservice by creating another microservice
    4. Summary
  13. Extension Modules and Compiled Code
    1. Advantages and disadvantages of compiled code
      1. The downsides of compiled code
    2. Accessing a dynamic library using ctypes
      1. Locating and linking a dynamic library
      2. Accessing functions defined in the library
        1. Assigning attributes to a function
        2. Using a pointer as a parameter of a function
        3. Providing a function signature
        4. Providing data structure layouts
    3. Interfacing with C code using Cython
      1. Working with Cython
        1. Additional import methods in Cython
        2. Writing extension modules in Cython
      2. Methods to increase the execution speed of Python code
        1. Using cpdef in a Cython class
      3. Compiling an extension module in Python
    4. Summary

Product information

  • Title: Daniel Arbuckle’s Mastering Python
  • Author(s): Daniel Arbuckle
  • Release date: June 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781787283695