Python: Master the Art of Design Patterns

Book description

Ensure your code is sleek, efficient and elegant by mastering powerful Python design patterns

About This Book
  • Learn all about abstract design patterns and how to implement them in Python 3

  • Understand the structural, creational, and behavioral Python design patterns

  • Get to know the context and application of design patterns to solve real-world problems in software architecture, design, and application development

  • Discover how to simplify Design Pattern implementation using the power of Python 3

  • Who This Book Is For

    If you have basic Python skills and wish to learn in depth how to correctly apply appropriate design patterns, this course is tailor made for you.

    What You Will Learn
  • Discover what design patterns are and how to apply them to writing Python

  • Implement objects in Python by creating classes and defining methods

  • Separate related objects into a taxonomy of classes and describe the properties and behaviors of those objects via the class interface

  • Understand when to use object-oriented features, and more importantly when not to use them

  • Get to know proven solutions to common design issues

  • Explore the design principles that form the basis of software design, such as loose coupling, the Hollywood principle, and the Open Close principle, among others

  • Use Structural Design Patterns and find out how objects and classes interact to build larger applications

  • Improve the productivity and code base of your application using Python design patterns

  • Secure an interface using the Proxy pattern

  • In Detail

    Python is an object-oriented scripting language that is used in everything from data science to web development. Known for its simplicity, Python increases productivity and minimizes development time. Through applying essential software engineering design patterns to Python, Python code becomes even more efficient and reusable from project to project.

    This learning path takes you through every traditional and advanced design pattern best applied to Python code, building your skills in writing exceptional Python. Divided into three distinct modules, you’ll go from foundational to advanced concepts by following a series of practical tutorials.

    Start with the bedrock of Python programming – the object-oriented paradigm. Rethink the way you work with Python as you work through the Python data structures and object-oriented techniques essential to modern Python programming. Build your confidence as you learn Python syntax, and how to use OOP principles with Python tools such as Django and Kivy.

    In the second module, run through the most common and most useful design patterns from a Python perspective. Progress through Singleton patterns, Factory patterns, Façade patterns and more all with detailed hands-on guidance. Enhance your professional abilities in in software architecture, design, and development.

    In the final module, run through the more complex and less common design patterns, discovering how to apply them to Python coding with the help of real-world examples. Get to grips with the best practices of writing Python, as well as creating systems architecture and troubleshooting issues.

    This Learning Path combines some of the best that Packt has to offer in one complete, curated package. It includes content from the following Packt products:

  • Python 3 Object-Oriented Programming - Second Edition by Dusty Phillips

  • Learning Python Design Patterns - Second Edition by Chetan Giridhar

  • Mastering Python Design Patterns by Sakis Kasampalis

  • Style and approach

    Advance your Python code through three distinct modules that each build on preceding content. Get the complete coverage of Python design patterns you need to write elegant and efficient code that’s reusable and powerful.

    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. Python: Master the Art of Design Patterns
      1. Table of Contents
      2. Python: Master the Art of Design Patterns
      3. Python: Master the Art of Design Patterns
      4. Credits
      5. Preface
        1. What this learning path covers
        2. What you need for this learning path
        3. Who this learning path is for
        4. Reader feedback
        5. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      6. 1. Module 1
        1. 1. Object-oriented Design
          1. Introducing object-oriented
          2. Objects and classes
          3. Specifying attributes and behaviors
            1. Data describes objects
            2. Behaviors are actions
          4. Hiding details and creating the public interface
          5. Composition
          6. Inheritance
            1. Inheritance provides abstraction
            2. Multiple inheritance
          7. Case study
          8. Exercises
          9. Summary
        2. 2. Objects in Python
          1. Creating Python classes
            1. Adding attributes
            2. Making it do something
              1. Talking to yourself
              2. More arguments
            3. Initializing the object
            4. Explaining yourself
          2. Modules and packages
            1. Organizing the modules
              1. Absolute imports
              2. Relative imports
          3. Organizing module contents
          4. Who can access my data?
          5. Third-party libraries
          6. Case study
          7. Exercises
          8. Summary
        3. 3. When Objects Are Alike
          1. Basic inheritance
            1. Extending built-ins
            2. Overriding and super
          2. Multiple inheritance
            1. The diamond problem
            2. Different sets of arguments
          3. Polymorphism
          4. Abstract base classes
            1. Using an abstract base class
            2. Creating an abstract base class
            3. Demystifying the magic
          5. Case study
          6. Exercises
          7. Summary
        4. 4. Expecting the Unexpected
          1. Raising exceptions
            1. Raising an exception
            2. The effects of an exception
            3. Handling exceptions
            4. The exception hierarchy
            5. Defining our own exceptions
          2. Case study
          3. Exercises
          4. Summary
        5. 5. When to Use Object-oriented Programming
          1. Treat objects as objects
          2. Adding behavior to class data with properties
            1. Properties in detail
            2. Decorators – another way to create properties
            3. Deciding when to use properties
          3. Manager objects
            1. Removing duplicate code
            2. In practice
          4. Case study
          5. Exercises
          6. Summary
        6. 6. Python Data Structures
          1. Empty objects
          2. Tuples and named tuples
            1. Named tuples
          3. Dictionaries
            1. Dictionary use cases
            2. Using defaultdict
              1. Counter
          4. Lists
            1. Sorting lists
          5. Sets
          6. Extending built-ins
          7. Queues
            1. FIFO queues
            2. LIFO queues
            3. Priority queues
          8. Case study
          9. Exercises
          10. Summary
        7. 7. Python Object-oriented Shortcuts
          1. Python built-in functions
            1. The len() function
            2. Reversed
            3. Enumerate
            4. File I/O
            5. Placing it in context
          2. An alternative to method overloading
            1. Default arguments
            2. Variable argument lists
            3. Unpacking arguments
          3. Functions are objects too
            1. Using functions as attributes
            2. Callable objects
          4. Case study
          5. Exercises
          6. Summary
        8. 8. Strings and Serialization
          1. Strings
            1. String manipulation
            2. String formatting
              1. Escaping braces
              2. Keyword arguments
              3. Container lookups
              4. Object lookups
              5. Making it look right
            3. Strings are Unicode
              1. Converting bytes to text
              2. Converting text to bytes
            4. Mutable byte strings
          2. Regular expressions
            1. Matching patterns
              1. Matching a selection of characters
              2. Escaping characters
              3. Matching multiple characters
              4. Grouping patterns together
            2. Getting information from regular expressions
              1. Making repeated regular expressions efficient
          3. Serializing objects
            1. Customizing pickles
            2. Serializing web objects
          4. Case study
          5. Exercises
          6. Summary
        9. 9. The Iterator Pattern
          1. Design patterns in brief
          2. Iterators
            1. The iterator protocol
          3. Comprehensions
            1. List comprehensions
            2. Set and dictionary comprehensions
            3. Generator expressions
          4. Generators
            1. Yield items from another iterable
          5. Coroutines
            1. Back to log parsing
            2. Closing coroutines and throwing exceptions
            3. The relationship between coroutines, generators, and functions
          6. Case study
          7. Exercises
          8. Summary
        10. 10. Python Design Patterns I
          1. The decorator pattern
            1. A decorator example
            2. Decorators in Python
          2. The observer pattern
            1. An observer example
          3. The strategy pattern
            1. A strategy example
            2. Strategy in Python
          4. The state pattern
            1. A state example
            2. State versus strategy
            3. State transition as coroutines
          5. The singleton pattern
            1. Singleton implementation
          6. The template pattern
            1. A template example
          7. Exercises
          8. Summary
        11. 11. Python Design Patterns II
          1. The adapter pattern
          2. The facade pattern
          3. The flyweight pattern
          4. The command pattern
          5. The abstract factory pattern
          6. The composite pattern
          7. Exercises
          8. Summary
        12. 12. Testing Object-oriented Programs
          1. Why test?
            1. Test-driven development
          2. Unit testing
            1. Assertion methods
            2. Reducing boilerplate and cleaning up
            3. Organizing and running tests
            4. Ignoring broken tests
          3. Testing with py.test
            1. One way to do setup and cleanup
            2. A completely different way to set up variables
            3. Skipping tests with py.test
          4. Imitating expensive objects
          5. How much testing is enough?
          6. Case study
            1. Implementing it
          7. Exercises
          8. Summary
        13. 13. Concurrency
          1. Threads
            1. The many problems with threads
              1. Shared memory
              2. The global interpreter lock
            2. Thread overhead
          2. Multiprocessing
            1. Multiprocessing pools
            2. Queues
            3. The problems with multiprocessing
          3. Futures
          4. AsyncIO
            1. AsyncIO in action
            2. Reading an AsyncIO future
            3. AsyncIO for networking
            4. Using executors to wrap blocking code
            5. Streams
              1. Executors
          5. Case study
          6. Exercises
          7. Summary
      7. 2. Module 2
        1. 1. Introduction to Design Patterns
          1. Understanding object-oriented programming
            1. Objects
            2. Classes
            3. Methods
          2. Major aspects of object-oriented programming
            1. Encapsulation
            2. Polymorphism
            3. Inheritance
            4. Abstraction
            5. Composition
          3. Object-oriented design principles
            1. The open/close principle
            2. The inversion of control principle
            3. The interface segregation principle
            4. The single responsibility principle
            5. The substitution principle
          4. The concept of design patterns
            1. Advantages of design patterns
            2. Taxonomy of design patterns
            3. Context – the applicability of design patterns
          5. Patterns for dynamic languages
          6. Classifying patterns
            1. Creational patterns:
            2. Structural patterns
            3. Behavioral patterns
          7. Summary
        2. 2. The Singleton Design Pattern
          1. Understanding the Singleton design pattern
            1. Implementing a classical Singleton in Python
          2. Lazy instantiation in the Singleton pattern
          3. Module-level Singletons
          4. The Monostate Singleton pattern
          5. Singletons and metaclasses
          6. A real-world scenario – the Singleton pattern, part 1
          7. A real-world scenario – the Singleton pattern, part 2
          8. Drawbacks of the Singleton pattern
          9. Summary
        3. 3. The Factory Pattern – Building Factories to Create Objects
          1. Understanding the Factory pattern
          2. The Simple Factory pattern
          3. The Factory Method pattern
            1. Implementing the Factory Method
            2. Advantages of the Factory method pattern
          4. The Abstract Factory pattern
            1. Implementing the Abstract Factory pattern
          5. The Factory method versus Abstract Factory method
          6. Summary
        4. 4. The Façade Pattern – Being Adaptive with Façade
          1. Understanding Structural design patterns
          2. Understanding the Façade design pattern
          3. A UML class diagram
            1. Façade
            2. System
            3. Client
          4. Implementing the Façade pattern in the real world
          5. The principle of least knowledge
          6. Frequently asked questions
          7. Summary
        5. 5. The Proxy Pattern – Controlling Object Access
          1. Understanding the Proxy design pattern
          2. A UML class diagram for the Proxy pattern
          3. Understanding different types of Proxies
            1. A virtual proxy
            2. A remote proxy
            3. A protective proxy
            4. A smart proxy
          4. The Proxy pattern in the real world
          5. Advantages of the Proxy pattern
          6. Comparing the Façade and Proxy patterns
          7. Frequently asked questions
          8. Summary
        6. 6. The Observer Pattern – Keeping Objects in the Know
          1. Introducing Behavioral patterns
          2. Understanding the Observer design pattern
            1. A UML class diagram for the Observer pattern
          3. The Observer pattern in the real world
          4. The Observer pattern methods
            1. The pull model
            2. The push model
          5. Loose coupling and the Observer pattern
          6. The Observer pattern – advantages and disadvantages
          7. Frequently asked questions
          8. Summary
        7. 7. The Command Pattern – Encapsulating Invocation
          1. Introducing the Command pattern
          2. Understanding the Command design pattern
            1. A UML class diagram for the Command pattern
          3. Implementing the Command pattern in the real world
            1. Design considerations
          4. Advantages and disadvantages of Command patterns
          5. Frequently asked questions
          6. Summary
        8. 8. The Template Method Pattern – Encapsulating Algorithm
          1. Defining the Template Method pattern
            1. Understanding the Template Method design pattern
            2. A UML class diagram for the Template Method pattern
          2. The Template Method pattern in the real world
          3. The Template Method pattern – hooks
          4. The Hollywood principle and the Template Method
          5. The advantages and disadvantages of the Template Method pattern
          6. Frequently asked questions
          7. Summary
        9. 9. Model-View-Controller – Compound Patterns
          1. An introduction to Compound patterns
          2. The Model-View-Controller pattern
            1. Model – knowledge of the application
            2. View – the appearance
            3. Controller – the glue
          3. A UML class diagram for the MVC design pattern
          4. The MVC pattern in the real world
            1. Modules
          5. Benefits of the MVC pattern
          6. Frequently asked questions
          7. Summary
        10. 10. The State Design Pattern
          1. Defining the State design pattern
            1. Understanding the State design pattern
            2. Understanding the State design pattern with a UML diagram
          2. A simple example of the State design pattern
            1. The State design pattern with v3.5 implementation
          3. Advantages/disadvantages of the State pattern
          4. Summary
        11. 11. AntiPatterns
          1. An introduction to AntiPatterns
          2. Software development AntiPatterns
            1. Spaghetti code
            2. Golden Hammer
            3. Lava Flow
            4. Copy-and-paste or cut-and-paste programming
          3. Software architecture AntiPatterns
            1. Reinventing the wheel
            2. Vendor lock-in
            3. Design by committee
          4. Summary
      8. 3. Module 3
        1. 1. The Factory Pattern
          1. Factory Method
            1. A real-life example
            2. A software example
            3. Use cases
            4. Implementation
          2. Abstract Factory
            1. A real-life example
            2. A software example
            3. Use cases
            4. Implementation
          3. Summary
        2. 2. The Builder Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        3. 3. The Prototype Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        4. 4. The Adapter Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        5. 5. The Decorator Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        6. 6. The Facade Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        7. 7. The Flyweight Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        8. 8. The Model-View-Controller Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        9. 9. The Proxy Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        10. 10. The Chain of Responsibility Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        11. 11. The Command Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        12. 12. The Interpreter Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        13. 13. The Observer Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        14. 14. The State Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        15. 15. The Strategy Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
        16. 16. The Template Pattern
          1. A real-life example
          2. A software example
          3. Use cases
          4. Implementation
          5. Summary
      9. A. Bibliography
      10. Index

    Product information

    • Title: Python: Master the Art of Design Patterns
    • Author(s): Dusty Phillips, Chetan Giridhar, Sakis Kasampalis
    • Release date: September 2016
    • Publisher(s): Packt Publishing
    • ISBN: 9781787125186