Learning Python Application Development

Book description

Take Python beyond scripting to build robust, reusable, and efficient applications

About This Book

  • Get to grips with Python techniques that address commonly encountered problems in general application development.
  • Develop, package, and deploy efficient applications in a fun way.
  • All-practical coverage of the major areas of application development, including best practices, exception handling, testing, refactoring, design patterns, performance, and GUI application development.

Who This Book Is For

Do you know the basics of Python and object oriented programming? Do you want to go an extra mile and learn techniques to make your Python application robust, extensible, and efficient? Then this book is for you.

What You Will Learn

  • Build a robust application by handling exceptions.
  • Modularize, package, and release the source distribution.
  • Document the code and implement coding standards.
  • Create automated tests to catch bugs in the early development stage.
  • Identify and re-factor badly written code to improve application life.
  • Detect recurring problems in the code and apply design patterns.
  • Improve code efficiency by identifying performance bottlenecks and fixing them.
  • Develop simple GUI applications using Python.

In Detail

Python is one of the most widely used dynamic programming languages, supported by a rich set of libraries and frameworks that enable rapid development. But fast paced development often comes with its own baggage that could bring down the quality, performance, and extensibility of an application. This book will show you ways to handle such problems and write better Python applications.

From the basics of simple command-line applications, develop your skills all the way to designing efficient and advanced Python apps. Guided by a light-hearted fantasy learning theme, overcome the real-world problems of complex Python development with practical solutions. Beginning with a focus on robustness, packaging, and releasing application code, you'll move on to focus on improving application lifetime by making code extensible, reusable, and readable. Get to grips with Python refactoring, design patterns and best practices. Techniques to identify the bottlenecks and improve performance are covered in a series of chapters devoted to performance, before closing with a look at developing Python GUIs.

Style and approach

The book uses a fantasy game theme as a medium to explain various topics. Specific aspects of application development are explained in different chapters. In each chapter the reader is presented with an interesting problem which is then tackled using hands-on examples with easy-to-follow instructions.

Table of contents

  1. Learning Python Application Development
    1. Table of Contents
    2. Learning Python Application Development
    3. Credits
    4. Disclaimers
    5. About the Author
    6. About the Reviewer
    7. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    8. Preface
      1. Important things to note
      2. Very important note for e-book readers
      3. What this book covers
      4. What you need for this book
      5. Who this book is for
      6. Conventions
      7. Reader feedback
      8. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    9. 1. Developing Simple Applications
      1. Important housekeeping notes
      2. Installation prerequisites
        1. Installing Python
          1. Option 1 – official distribution
          2. Option 2 – bundled distribution
          3. Python install location
            1. Unix-like operating systems
            2. Windows OS
          4. Verifying Python installation
          5. Installing pip
          6. Installing IPython
          7. Choosing an IDE
      3. The theme of the book
        1. Meet the characters
      4. Simple script – Attack of the Orcs v0.0.1
        1. The game – Attack of the Orcs v0.0.1
        2. Problem statement
        3. Pseudo code – version 0.0.1
        4. Reviewing the code
        5. Running Attack of the Orcs v0.0.1
      5. Using functions – Attack of the Orcs v0.0.5
        1. Revisiting the previous version
        2. Pseudo code with attack feature – Version 0.0.5
        3. Reviewing the code
        4. Running Attack of the Orcs v0.0.5
      6. Using OOP – Attack of the Orcs v1.0.0
        1. Prioritize the feature requests
        2. Problem statement
        3. Redesigning the code
        4. Painting the big picture
          1. Pseudo UML representation
          2. Understanding the pseudo UML diagram
        5. Reviewing the code
        6. Running Attack of the Orcs v1.0.0
      7. Abstract base classes in Python
        1. Exercise
      8. Summary
        1. Very important note for e-book readers
    10. 2. Dealing with Exceptions
      1. Revisiting Attack of the Orcs v1.0.0
        1. Debugging the problem
        2. Fixing the bugs…
      2. Exceptions
        1. What is an exception?
        2. Most common exceptions
        3. Exception handling
        4. Raising and re-raising an exception
        5. The else block of try…except
        6. finally...clean it up!
      3. Back to the game – Attack of the Orcs v1.1.0
        1. Preparatory work
        2. Adding the exception handling code
        3. Running Attack of the Orcs v1.1.0
      4. Defining custom exceptions
        1. Preparatory work
        2. Custom exception – The problem
        3. Writing a new exception class
        4. Expanding the exception class
        5. Inheriting from the exception class
      5. Exercise
      6. Summary
    11. 3. Modularize, Package, Deploy!
      1. Selecting a versioning convention
        1. Serial increments
        2. Using a date format
        3. Semantic versioning scheme
      2. Modularizing the code
        1. Attack of the Orcs v2.0.0
      3. Creating a package
        1. Importing from the package
      4. Releasing the package on PyPI
        1. Prepare the distribution
          1. Step 1 – Setting up the package directory
          2. Step 2 – Writing the setup.py file
          3. Step 3 – Updating the README and LICENSE.txt files
          4. Step 4 – Updating the MANIFEST.in file
          5. Step 5 – Build a deployment-ready distribution
        2. Uploading the distribution
          1. Step 1 – Creating an account on PyPI test website
          2. Step 2 – Creating a .pypirc file
          3. Step 3 – Register your project
          4. Step 4 – Uploading the package
        3. A single command to do it all
        4. Installing your own distribution
      5. Using a private PyPI repository
        1. Step 1 – Installing pypiserver
        2. Step 2 – Building a new source distribution
        3. Step 3 – Starting a local server
        4. Step 4 – Installing the private distribution
      6. Making an incremental release
        1. Packaging and uploading the new version
        2. Upgrading the installed version
      7. Version controlling the code
        1. Git resources
        2. Installing Git
        3. Configuring your identity
        4. Basic Git terminology
        5. Creating and using a Git repository
          1. Creating a bare remote repository
          2. Clone the repository
          3. Copying the code to the cloned repository
          4. Staging the code and committing
          5. Pushing the changes to the central repository
      8. Using GUI clients for Git
      9. Exercise
      10. Summary
    12. 4. Documentation and Best Practices
      1. Documenting the code
        1. Docstrings
        2. Introduction to reStructuredText
          1. Section headings
          2. Paragraphs
          3. Text styles
          4. Code snippets
          5. Mathematical equations
          6. Bullets and numbering
        3. Dosctrings using RST
        4. Docstring formatting styles
        5. Automatically creating docstring stubs
        6. Generating documentation with Sphinx
          1. Step 1 – Installing Sphinx using pip
          2. Step 2 – cd to the source directory
          3. Step 3 – Running sphinx-quickstart
          4. Step 4 – Updating conf.py
          5. Step 5 – Running sphinx-apidoc
          6. Step 6 – Building the documentation
            1. Using sphinx-build
            2. Using Makefile
      2. Python coding standards
      3. Code analysis – How well are we doing?
        1. Code analysis using IDE
        2. Pylint
          1. Pylint in action
          2. PEP8 and AutoPEP8
      4. Exercise
      5. Summary
    13. 5. Unit Testing and Refactoring
      1. This is how the chapter is organized
        1. Important housekeeping notes
      2. Why test?
        1. A new feature was requested
        2. You implemented this feature
        3. But something wasn't right...
        4. It required thorough testing
      3. Unit testing
        1. Python unittest framework
          1. Basic terminology
          2. Creating tests with unittest.TestCase
          3. Controlling test execution
          4. Using unittest.TestSuite
      4. Writing unit tests for the application
        1. Setting up a test package
        2. Creating a new class for unit testing
        3. First unit test – Injured unit selection
        4. Running the first unit test
        5. Second unit test – Acquiring the hut
          1. Running only the second test
        6. Creating individual test modules
        7. Batch executing unit tests
      5. Unit tests using mock library
        1. Quick introduction to mock
        2. Let's mock!
        3. Using Mock objects in a unit test
        4. Working with patches
          1. Using patch in a unit test
        5. Third unit test – The play method
      6. Is your code covered?
        1. Resolving import errors, if any
      7. Other unit testing tools
        1. Doctest
        2. Nose
        3. Pytest
      8. Refactoring preamble
        1. Take a detour – Refactor for testability
      9. Refactoring
        1. What is refactoring?
        2. Why refactor?
        3. When to refactor?
        4. How to refactor?
          1. Renaming
          2. Extracting
        5. Moving
          1. Pushing down
          2. Pulling up
          3. Refactoring tools for Python
      10. Unit testing revisited
        1. Refactoring for testability
        2. Fourth unit test – setup_game_scenario
      11. Exercise
        1. Refactoring and redesign exercise
      12. Summary
    14. 6. Design Patterns
      1. Introduction to design patterns
        1. Classification of patterns
          1. Behavioral patterns
          2. Creational patterns
          3. Structural patterns
          4. Concurrency patterns
      2. Python language and design patterns
        1. First-class functions
        2. Classes as first-class objects
        3. Closures
        4. Miscellaneous features
          1. Class method
          2. Abstract method
          3. The __getattr__ method
        5. Duck typing
      3. Structure of the rest of the chapter
      4. Fast forward – Attack of the Orcs v6.0.0
      5. Strategy pattern
        1. Strategy scenario – The jump feature
        2. Strategy – The problem
        3. Strategy – Attempted solution
        4. Strategy – Rethinking the design
        5. Strategy solution 1 – Traditional approach
        6. Strategy solution 2 – Pythonic approach
      6. Simple factory
        1. Simple factory scenario – The recruit feature
        2. Simple factory – The problem
        3. Simple factory – Rethinking the design
        4. Simple factory solution 1 – Traditional approach
        5. Simple factory solution 2 – Pythonic approach
      7. Abstract factory pattern
        1. Abstract factory scenario – An accessory store
        2. Abstract factory – The problem
        3. Abstract factory – Rethinking the design
          1. Simplifying the design further
        4. Abstract factory solution – Pythonic approach
          1. Advanced topic – enforcing an interface
      8. Adapter pattern
        1. Adapter scenario – Elf's distant cousin
        2. Adapter – The problem
        3. Adapter – Attempted solution
        4. Adapter solution – Pythonic approach
          1. Adapter – Multiple adapter methods
      9. Summary
    15. 7. Performance – Identifying Bottlenecks
      1. Overview of three performance chapters
        1. More focus on the runtime performance
        2. The first performance chapter
        3. The second performance chapter
        4. The third performance chapter
        5. Sneak peek at the upcoming application speedup
      2. Scenario – The Gold Hunt
        1. High-level algorithm
        2. Reviewing the initial code
        3. Running the code
      3. The problem
      4. Identifying the bottlenecks
        1. Measuring the execution time
        2. Measuring the runtime of small code snippets
        3. Code profiling
          1. The cProfile module
          2. The pstats module
          3. The line_profiler package
      5. Memory profiling
        1. The memory_profiler package
      6. Algorithm efficiency and complexity
        1. Algorithm efficiency
        2. Algorithm complexity
      7. Big O notation
        1. Big O complexity classes
          1. O(1) – constant time
          2. O(log n) – logarithmic
          3. O(n) – Linear time
          4. O(n log n) – Log linear
          5. O(n2) – Quadratic
          6. O(n3) – cubic
          7. Upper bound of the complexity
          8. Complexity for common data structures and algorithms
          9. Wrapping up the big O discussion
      8. Summary
    16. 8. Improving Performance – Part One
      1. Prerequisite for the chapter
      2. This is how the chapter is organized
      3. Revisiting the Gold Hunt scenario
        1. Selecting a problem size
        2. Profiling the initial code
      4. Optimizing Gold Hunt – Part one
        1. Tweaking the algorithm – The square root
          1. Gold Hunt optimization – Pass one
        2. Skipping the dots
          1. Gold Hunt optimization – Pass two
        3. Using local scope
          1. Gold Hunt optimization – Pass three
      5. Performance improvement goodies
        1. List comprehension
        2. Recording execution time
        3. Dictionary comprehension
        4. Swapping conditional block and for loops
        5. 'try' it out in a loop
        6. Choosing the right data structures
        7. The collections module
          1. The deque class
          2. The defaultdict class
        8. Generators and generator expressions
          1. Generator expressions
          2. Comparing the memory efficiency
          3. Generator expressions or list comprehensions?
        9. The itertools module
          1. The itertools.chain iterator
      6. Exercises
      7. Summary
    17. 9. Improving Performance – Part Two, NumPy and Parallelization
      1. Prerequisites for this chapter
      2. This is how the chapter is organized
      3. Introduction to NumPy
        1. Installing NumPy
        2. Creating array objects
        3. Simple array operations
        4. Array slicing and indexing
          1. Indexing
          2. Slicing
        5. Broadcasting
        6. Miscellaneous functions
          1. numpy.ndarray.tolist
          2. numpy.reshape
          3. numpy.random
          4. numpy.dstack
          5. numpy.einsum
          6. Computing distance square with einsum
        7. Where to get more information on NumPy?
      4. Optimizing Gold Hunt – Part two
        1. Gold Hunt optimization – pass four
        2. Gold Hunt optimization – pass five
      5. Parallelization with the multiprocessing module
        1. Introduction to parallelization
          1. Shared memory parallelization
          2. Distributed memory parallelization
        2. Global interpreter lock
        3. The multiprocessing module
          1. The Pool class
      6. Parallelizing the Gold Hunt program
        1. Revisiting the gold field
        2. Gold Hunt optimization – Pass six, parallelization
          1. Other methods for parallelization
      7. Further reading
        1. JIT compilers
          1. GPU accelerated computing
      8. Summary
    18. 10. Simple GUI Applications
      1. Overview of GUI frameworks
        1. Tkinter
        2. PyQt
        3. PySide
        4. Kivy
        5. wxPython
      2. GUI programming design considerations
        1. Understanding user requirements
        2. Developing a user story
        3. Simplicity and accessibility
        4. Consistency
        5. Predictability and familiarity
        6. Miscellaneous design considerations
      3. Event-driven programming
        1. Event
        2. Event handling
        3. Event loop
      4. GUI programming with Tkinter
        1. Tkinter documentation links
        2. The mainloop() in Tkinter
        3. Simple GUI application – Take 1
        4. Simple GUI application – Take 2
        5. GUI Widgets in Tkinter
        6. Geometry management
          1. Grid geometry manager
          2. Pack geometry manager
          3. Place geometry manager
        7. Events in Tkinter
          1. Event types
          2. Event descriptors
          3. Event object attributes
        8. Event handling in Tkinter
          1. Command callback (Button widget)
          2. The bind() method
          3. The bind_class() method
          4. The bind_all() method
      5. Project-1 – Attack of the Orcs V10.0.0
        1. Background scenario
        2. Problem statement
        3. Writing the code
          1. Overview of the class HutGame
          2. The __init__ method
          3. The occupy_huts method
          4. The create_widgets method
          5. The setup_layout method
          6. The radio_btn_pressed and enter_hut methods
          7. The announce_winner method
        4. Running the application
      6. MVC architecture
        1. Model
        2. View
        3. Controller
        4. Advantages of MVC
      7. Project 2 – Attack of the Orcs v10.1.0
        1. Revisiting the HutGame class
        2. Creating MVC classes
        3. Communication between MVC objects
          1. Controller to Model or View communication
          2. Model to Controller communication
            1. Using method assignment
            2. Using a publish-subscribe pattern
              1. PyPubSub package
          3. View to Controller communication
          4. Communication between View and Model
        4. Reviewing the code
          1. The Controller class
        5. The Model class
        6. The View class
        7. Running the application
      8. Testing GUI applications
        1. Testing considerations
          1. Unit testing and MVC
          2. Manual testing
          3. Automated GUI testing
      9. Exercises
      10. Further reading
      11. Summary
    19. Index

Product information

  • Title: Learning Python Application Development
  • Author(s): Ninad Sathaye
  • Release date: September 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781785889196