You are previewing Modular Programming with Python.
O'Reilly logo
Modular Programming with Python

Book Description

Introducing modular techniques for building sophisticated programs using Python

About This Book

  • The book would help you develop succinct, expressive programs using modular deign

  • The book would explain best practices and common idioms through carefully explained and structured examples

  • It will have broad appeal as far as target audience is concerned and there would be take away for all beginners to Python

  • Who This Book Is For

    This book is intended for beginner to intermediate level Python programmers who wish to learn how to use modules and packages within their programs. While readers must understand the basics of Python programming, no knowledge of modular programming techniques is required.

    What You Will Learn

  • Learn how to use modules and packages to organize your Python code

  • Understand how to use the import statement to load modules and packages into your program

  • Use common module patterns such as abstraction and encapsulation to write better programs

  • Discover how to create self-testing Python packages

  • Create reusable modules that other programmers can use

  • Learn how to use GitHub and the Python Package Index to share your code with other people

  • Make use of modules and packages that others have written

  • Use modular techniques to build robust systems that can handle complexity and changing requirements over time

  • In Detail

    Python has evolved over the years and has become the primary choice of developers in various fields. The purpose of this book is to help readers develop readable, reliable, and maintainable programs in Python.

    Starting with an introduction to the concept of modules and packages, this book shows how you can use these building blocks to organize a complex program into logical parts and make sure those parts are working correctly together.

    Using clearly written, real-world examples, this book demonstrates how you can use modular techniques to build better programs. A number of common modular programming patterns are covered, including divide-and-conquer, abstraction, encapsulation, wrappers and extensibility. You will also learn how to test your modules and packages, how to prepare your code for sharing with other people, and how to publish your modules and packages on GitHub and the Python Package Index so that other people can use them. Finally, you will learn how to use modular design techniques to be a more effective programmer.

    Style and approach

    This book will be simple and straightforward, focusing on imparting learning through a wide array of examples that the readers can put into use as they read through the book. They should not only be able to understand the way modules help in improving development, but they should also be able to improvise on their techniques of writing concise and effective code.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Modular Programming with Python
      1. Table of Contents
      2. Modular Programming with Python
      3. Credits
      4. About the Author
      5. About the Reviewer
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why subscribe?
      7. 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
      8. 1. Introducing Modular Programming
        1. Introducing Python modules
        2. Introducing Python packages
        3. Using modules and packages to organize a program
        4. Why use modular programming techniques?
        5. Programming as a process
        6. The Python Standard Library
        7. Creating your first module
          1. Caching
          2. Writing a cache module
          3. Using the cache
        8. Summary
      9. 2. Writing Your First Modular Program
        1. The inventory control system
        2. Designing the inventory control system
          1. The data storage module
          2. The user interface module
          3. The report generator module
          4. The main program
        3. Implementing the inventory control system
          1. Implementing the data storage module
          2. Implementing the user interface module
          3. Implementing the report generator module
          4. Implementing the main program
        4. Summary
      10. 3. Using Modules and Packages
        1. Modules and packages
        2. Packages within packages
        3. Initializing a module
          1. Initialization functions
        4. Initializing a package
        5. How to import anything
          1. What does the import statement actually do?
          2. Using the import statement
          3. Relative imports
        6. Controlling what gets imported
        7. Circular dependencies
        8. Running modules from the command line
        9. Summary
      11. 4. Using Modules for Real-World Programming
        1. Introducing Charter
        2. Designing Charter
        3. Implementing Charter
          1. Implementing the chart.py module
          2. Implementing the generator.py module
            1. The Pillow library
            2. Renderers
            3. Testing the code
            4. Rendering the title
            5. Rendering the x axis
            6. The remaining renderers
            7. Testing Charter
        4. The fly in the ointment – changing requirements
        5. Redesigning Charter
        6. Refactoring the code
          1. Implementing the PDF renderer modules
          2. Testing the code
        7. Lessons learned
        8. Summary
      12. 5. Working with Module Patterns
        1. Divide and conquer
        2. Abstraction
        3. Encapsulation
        4. Wrappers
        5. Extensible modules
          1. Dynamic imports
          2. Plugins
          3. Hooks
        6. Summary
      13. 6. Creating Reusable Modules
        1. Using modules and packages to share your code
        2. What makes a module reusable?
          1. Functioning as a standalone unit
          2. Using relative imports
          3. Noting external dependencies
        3. What makes a good reusable module?
          1. Solving a general problem
          2. Following standard conventions
          3. Having clear documentation
        4. Examples of reusable modules
          1. requests
          2. python-dateutil
          3. lxml
        5. Designing a reusable package
        6. Implementing a reusable package
        7. Testing our reusable package
        8. Summary
      14. 7. Advanced Module Techniques
        1. Optional imports
        2. Local imports
        3. Tweaking imports using sys.path
        4. Import gotchas
          1. Using an existing name for your module or package
          2. Naming a Python script after a module or package
          3. Adding package directories to sys.path
          4. Executing and importing the same module
        5. Using modules and packages with the Python interactive interpreter
        6. Dealing with global variables
        7. Package configuration
        8. Package data
        9. Summary
      15. 8. Testing and Deploying Modules
        1. Testing modules and packages
          1. Testing with the unittest Standard Library module
          2. Designing your unit tests
          3. Code coverage
          4. Test-driven development
          5. Mocking
          6. Writing unit tests for your modules and packages
        2. Preparing a module or package for publication
        3. Uploading your work to GitHub
        4. Submitting to the Python Package Index
        5. Using pip to download and install modules and packages
        6. Summary
      16. 9. Modular Programming as a Foundation for Good Programming Technique
        1. The process of programming
          1. The inevitable changes
          2. Change management
        2. Dealing with complexity
        3. Being an effective programmer
        4. Summary
      17. Index