Online Book Reader

Home Category

Learning Python - Mark Lutz [5]

By Root 1353 0

Unified try/except/finally

Unified try Statement Syntax

Combining finally and except by Nesting

Unified try Example

The raise Statement

Propagating Exceptions with raise

Python 3.0 Exception Chaining: raise from

The assert Statement

Example: Trapping Constraints (but Not Errors!)

with/as Context Managers

Basic Usage

The Context Management Protocol

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

34. Exception Objects

Exceptions: Back to the Future

String Exceptions Are Right Out!

Class-Based Exceptions

Coding Exceptions Classes

Why Exception Hierarchies?

Built-in Exception Classes

Built-in Exception Categories

Default Printing and State

Custom Print Displays

Custom Data and Behavior

Providing Exception Details

Providing Exception Methods

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

35. Designing with Exceptions

Nesting Exception Handlers

Example: Control-Flow Nesting

Example: Syntactic Nesting

Exception Idioms

Exceptions Aren’t Always Errors

Functions Can Signal Conditions with raise

Closing Files and Server Connections

Debugging with Outer try Statements

Running In-Process Tests

More on sys.exc_info

Exception Design Tips and Gotchas

What Should Be Wrapped

Catching Too Much: Avoid Empty except and Exception

Catching Too Little: Use Class-Based Categories

Core Language Summary

The Python Toolset

Development Tools for Larger Projects

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

Test Your Knowledge: Part VII Exercises

VIII. Advanced Topics

36. Unicode and Byte Strings

String Changes in 3.0

String Basics

Character Encoding Schemes

Python’s String Types

Text and Binary Files

Python 3.0 Strings in Action

Literals and Basic Properties

Conversions

Coding Unicode Strings

Coding ASCII Text

Coding Non-ASCII Text

Encoding and Decoding Non-ASCII text

Other Unicode Coding Techniques

Converting Encodings

Coding Unicode Strings in Python 2.6

Source File Character Set Encoding Declarations

Using 3.0 Bytes Objects

Method Calls

Sequence Operations

Other Ways to Make bytes Objects

Mixing String Types

Using 3.0 (and 2.6) bytearray Objects

Using Text and Binary Files

Text File Basics

Text and Binary Modes in 3.0

Type and Content Mismatches

Using Unicode Files

Reading and Writing Unicode in 3.0

Handling the BOM in 3.0

Unicode Files in 2.6

Other String Tool Changes in 3.0

The re Pattern Matching Module

The struct Binary Data Module

The pickle Object Serialization Module

XML Parsing Tools

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

37. Managed Attributes

Why Manage Attributes?

Inserting Code to Run on Attribute Access

Properties

The Basics

A First Example

Computed Attributes

Coding Properties with Decorators

Descriptors

The Basics

A First Example

Computed Attributes

Using State Information in Descriptors

How Properties and Descriptors Relate

__getattr__ and __getattribute__

The Basics

A First Example

Computed Attributes

__getattr__ and __getattribute__ Compared

Management Techniques Compared

Intercepting Built-in Operation Attributes

Delegation-Based Managers Revisited

Example: Attribute Validations

Using Properties to Validate

Using Descriptors to Validate

Using __getattr__ to Validate

Using __getattribute__ to Validate

Chapter Summary

Test Your Knowledge: Quiz

Test Your Knowledge: Answers

38. Decorators

What’s a Decorator?

Managing Calls and Instances

Managing Functions and Classes

Using and Defining Decorators

Why Decorators?

The Basics

Function Decorators

Class Decorators

Decorator Nesting

Decorator Arguments

Decorators Manage Functions and Classes, Too

Coding Function Decorators

Tracing Calls

State Information Retention Options

Class Blunders I: Decorating Class Methods

Timing Calls

Adding Decorator Arguments

Coding Class Decorators

Singleton Classes

Tracing Object Interfaces

Class Blunders II: Retaining Multiple Instances

Decorators

Return Main Page Previous Page Next Page

®Online Book Reader