万本电子书0元读

万本电子书0元读

顶部广告

Modern Python Cookbook电子书

售       价:¥

40人正在读 | 0人评论 6.2

作       者:Steven F. Lott

出  版  社:Packt Publishing

出版时间:2016-11-01

字       数:769.2万

所属分类: 进口书 > 外文原版书 > 电脑/网络

温馨提示:数字商品不支持退换货,不提供源文件,不支持导出打印

为你推荐

  • 读书简介
  • 目录
  • 累计评论(0条)
  • 读书简介
  • 目录
  • 累计评论(0条)
The latest in modern Python recipes for the busy modern programmer About This Book Develop succinct, expressive programs in Python Learn the best practices and common idioms through carefully explained and structured recipes Discover new ways to apply Python for the new age of development Who This Book Is For The book is for web developers, programmers, enterprise programmers, engineers, big data scientist, and so on. If you are a beginner, Python Cookbook will get you started. If you are experienced, it will expand your knowledge base. A basic knowledge of programming would help. What You Will Learn See the intricate details of the Python syntax and how to use it to your advantage Improve your code readability through functions in Python Manipulate data effectively using built-in data structures Get acquainted with advanced programming techniques in Python Equip yourself with functional and statistical programming features Write proper tests to be sure a program works as advertised Integrate application software using Python In Detail Python is the preferred choice of developers, engineers, data scientists, and hobbyists everywhere. It is a great *ing language that can power your applications and provide great speed, safety, and scalability. By exposing Python as a series of simple recipes, you can gain insight into specific language features in a particular context. Having a tangible context helps make the language or standard library feature easier to understand. This book comes with over 100 recipes on the latest version of Python. The recipes will benefit everyone ranging from beginner to an expert. The book is broken down into 13 chapters that build from simple language concepts to more complex applications of the language. The recipes will touch upon all the necessary Python concepts related to data structures, OOP, functional programming, as well as statistical programming. You will get acquainted with the nuances of Python syntax and how to effectively use the advantages that it offers. You will end the book equipped with the knowledge of testing, web services, and configuration and application integration tips and tricks. The recipes take a problem-solution approach to resolve issues commonly faced by Python programmers across the globe. You will be armed with the knowledge of creating applications with flexible logging, powerful configuration, and command-line options, automated unit tests, and good documentation. Style and approach This book takes a recipe-based approach, where each recipe addresses specific problems and issues. The recipes provide discussions and insights and an explanation of the problems.
目录展开

Modern Python Cookbook

Modern Python Cookbook

Credits

About the Author

About the Reviewers

www.PacktPub.com

Why subscribe?

Preface

What this book covers

What you need for this book

Who this book is for

Conventions

Reader feedback

Customer support

Downloading the example code

Errata

Piracy

Questions

1. Numbers, Strings, and Tuples

Introduction

Creating meaningful names and using variables

Getting ready

How to do it...

Choosing names wisely

Assigning names to objects

How it works...

There's more...

See also

Working with large and small integers

Getting ready

How to do it...

How it works...

There's more...

See also

Choosing between float, decimal, and fraction

Getting ready

How to do it...

Doing currency calculations

Fraction calculations

Floating-point approximations

Converting numbers from one type to another

How it works...

There's more...

See also

Choosing between true division and floor division

Getting ready

How to do it...

Doing floor division

Doing true division

Rational fraction calculations

How it works...

See also

Rewriting an immutable string

Getting ready

How to do it...

Slicing a piece of a string

Updating a string with a replacement

Making a string all lowercase

Removing extra punctuation marks

How it works...

There's more...

See also

String parsing with regular expressions

Getting ready

How to do it...

How it works...

There's more...

See also

Building complex strings with "template".format()

Getting ready

How to do it...

How it works...

There's more...

See also

Building complex strings from lists of characters

Getting ready

How to do it...

How it works...

There's more

See also

Using the Unicode characters that aren't on our keyboards

Getting ready

How to do it...

How it works...

See also

Encoding strings – creating ASCII and UTF-8 bytes

Getting ready

How to do it...

How it works...

See also

Decoding bytes – how to get proper characters from some bytes

Getting ready

How to do it..

How it works...

See also

Using tuples of items

Getting ready

How to do it...

Creating tuples

Extracting items from a tuple

How it works...

There's more

See also...

2. Statements and Syntax

Introduction

Writing Python script and module files – syntax basics

Getting ready

How to do it...

How it works...

There's more...

See also

Writing long lines of code

Getting ready

How to do it...

Using backslash to break a long statement into logical lines

Using the () characters to break a long statement into sensible pieces

Using string literal concatenation

Assigning intermediate results to separate variables

How it works...

There's more...

See also

Including descriptions and documentation

Getting ready

How to do it...

Writing docstrings for scripts

Writing docstrings for library modules

How it works...

There's more...

See also

Writing better RST markup in docstrings

Getting ready

How to do it...

How it works...

There's more...

Using directives

Using inline markup

See also

Designing complex if...elif chains

Getting ready

How to do it...

How it works...

There's more...

See also

Designing a while statement which terminates properly

Getting ready

How to do it...

How it works...

See also

Avoiding a potential problem with break statements

Getting ready

How to do it...

How it works...

There's more...

See also

Leveraging the exception matching rules

Getting ready

How to do it...

How it works...

There's more...

See also

Avoiding a potential problem with an except: clause

Getting ready

How to do it...

How it works...

See also

Chaining exceptions with the raise from statement

Getting ready

How to do it...

How it works...

There's more...

See also

Managing a context using the with statement

Getting ready

How to do it...

How it works...

There's more...

See also

3. Function Definitions

Introduction

Designing functions with optional parameters

Getting ready

How to do it...

Particular to General Design

General to Particular design

How it works...

There's more...

See also

Using super flexible keyword parameters

Getting ready

How to do it...

How it works...

There's more...

See also

Forcing keyword-only arguments with the * separator

Getting ready

How to do it...

How it works...

There's more...

See also

Writing explicit types on function parameters

Getting ready

How to do it...

How it works...

There's more...

See also

Picking an order for parameters based on partial functions

Getting ready

How to do it...

Wrapping a function

Creating a partial function with keyword parameters

Creating a partial function with positional parameters

How it works...

There's more...

See also

Writing clear documentation strings with RST markup

Getting ready

How to do it...

How it works...

There's more...

See also

Designing recursive functions around Python's stack limits

Getting ready

How to do it...

How it works...

There's more...

See also

Writing reusable scripts with the script library switch

Getting ready

How to do it...

How it works...

There's more...

See also

4. Built-in Data Structures – list, set, dict

Introduction

Choosing a data structure

Getting ready

How to do it...

How it works...

There's more...

See also

Building lists – literals, appending, and comprehensions

Getting ready

How to do it...

Building a list with the append() method

Writing a list comprehension

Using the list function on a generator expression

How it works...

There's more...

Other ways to extend a list

See also

Slicing and dicing a list

Getting ready

How to do it...

How it works...

There's more...

See also

Deleting from a list – deleting, removing, popping, and filtering

Getting ready

How to do it...

Deleting items from a list

The remove() method

The pop() method

The filter() function

How it works...

There's more...

See also

Reversing a copy of a list

Getting ready

How to do it...

How it works...

See also

Using set methods and operators

Getting ready

How to do it...

How it works...

There's more...

See also

Removing items from a set – remove(), pop(), and difference

Getting ready

How to do it...

How it works...

There's more...

See also

Creating dictionaries – inserting and updating

Getting ready

How to do it...

How it works...

There's more...

See also

Removing from dictionaries – the pop() method and the del statement

Getting ready

How to do it...

How it works...

There's more...

See also

Controlling the order of dict keys

Getting ready

How to do it...

How it works...

There's more...

See also

Handling dictionaries and sets in doctest examples

Getting ready

How to do it...

How it works...

There's more...

Understanding variables, references, and assignment

How to do it...

How it works...

There's more...

See also

Making shallow and deep copies of objects

Getting ready

How to do it...

How it works...

See also

Avoiding mutable default values for function parameters

Getting ready

How to do it...

How it works...

There's more...

See also

5. User Inputs and Outputs

Introduction

Using features of the print() function

Getting ready

How to do it...

How it works...

There's more...

See also

Using input() and getpass() for user input

Getting ready

How to do it...

How it works...

There's more...

Input string parsing

Interaction via the cmd module

See also

Debugging with "format".format_map(vars())

Getting ready

How to do it...

How it works...

There's more...

See also

Using argparse to get command-line input

Getting ready

How to do it...

How it works...

There's more...

See also

Using cmd for creating command-line applications

Getting ready

How to do it...

How it works...

There's more...

See also

Using the OS environment settings

Getting ready

How to do it...

How it works...

There's more...

See also

6. Basics of Classes and Objects

Introduction

Using a class to encapsulate data and processing

Getting ready

How to do it...

How it works...

There's more...

See also

Designing classes with lots of processing

Getting ready

How to do it...

How it works...

There's more...

See also

Designing classes with little unique processing

Getting ready

How to do it...

Stateless objects

Stateful objects with a new class

Stateful objects using an existing class

How it works...

There's more...

See also

Optimizing small objects with __slots__

Getting ready

How to do it...

How it works...

There's more...

See also

Using more sophisticated collections

Getting ready

How to do it...

How it works...

There's more...

See also

Extending a collection – a list that does statistics

Getting ready

How to do it...

How it works...

There's more...

See also

Using properties for lazy attributes

Getting ready...

How to do it...

How it works...

There's more...

See also...

Using settable properties to update eager attributes

Getting ready

How to do it...

How it works...

There's more...

Initialization

Calculation

See also

7. More Advanced Class Design

Introduction

Choosing between inheritance and extension – the is-a question

Getting ready

How to do it...

Wrapping – aggregation and composition

Extending - inheritance

How it works...

There's more...

See also

Separating concerns via multiple inheritance

Getting ready

How to do it...

How it works...

There's more...

See also

Leveraging Python's duck typing

Getting ready

How to do it...

How it works...

There's more...

See also

Managing global and singleton objects

Getting ready

How to do it...

Module global variable

Class-level static variable

How it works...

There's more...

Using more complex structures – maps of lists

Getting ready

How to do it...

How it works...

There's more...

See also

Creating a class that has orderable objects

Getting ready

How to do it...

How it works...

There's more...

See also

Defining an ordered collection

Getting ready

How to do it...

How it works...

There's more...

See also

Deleting from a list of mappings

Getting ready

How to do it...

How it works...

There's more...

See also

8. Functional and Reactive Programming Features

Introduction

Writing generator functions with the yield statement

Getting ready

How to do it...

How it works...

There's more...

See also

Using stacked generator expressions

Getting ready

How to do it...

How it works...

There's more...

Namespace instead of list

See also

Applying transformations to a collection

Getting ready...

How to do it...

How it works...

There's more...

See also...

Picking a subset - three ways to filter

Getting ready...

How to do it...

How it works...

There's more...

See also...

Summarizing a collection – how to reduce

Getting ready

How to do it...

How it works...

There's more...

Maxima and minima

Potential for abuse

Combining map and reduce transformations

Getting ready

How to do it...

How it works...

There's more...

See also

Implementing "there exists" processing

Getting ready

How to do it...

How it works...

There's more...

The itertools module

Creating a partial function

Getting ready

How to do it...

Using functools.partial()

Creating a lambda object

How it works...

There's more...

Simplifying complex algorithms with immutable data structures

Getting ready

How to do it...

How it works...

There's more...

Writing recursive generator functions with the yield from statement

Getting ready

How to do it...

How it works...

There's more...

See also

9. Input/Output, Physical Format, and Logical Layout

Introduction

Using pathlib to work with filenames

Getting ready

How to do it...

Making the output filename by changing the input suffix

Making a number of sibling output files with distinct names

Creating a directory and a number of files

Comparing file dates to see which is newer

Removing a file

Finding all files that match a given pattern

How it works...

There's more...

See also

Reading and writing files with context managers

Getting ready

How to do it...

How it works...

There's more...

See also

Replacing a file while preserving the previous version

Getting ready

How to do it...

How it works...

There's more...

See also

Reading delimited files with the CSV module

Getting ready

How to do it...

How it works...

There's more...

See also

Reading complex formats using regular expressions

Getting ready

How to do it...

Defining the parse function

Using the parse function

How it works...

There's more...

See also

Reading JSON documents

Getting ready

How to do it...

How it works...

There's more...

Serializing a complex data structure

Deserializing a complex data structure

See also

Reading XML documents

Getting ready

How to do it...

How it works...

There's more...

See also

Reading HTML documents

Getting ready

How to do it...

How it works...

There's more...

See also

Upgrading CSV from DictReader to namedtuple reader

Getting ready

How to do it...

How it works...

There's more...

See also

Upgrading CSV from a DictReader to a namespace reader

Getting ready

How to do it...

How it works...

There's more...

See also

Using multiple contexts for reading and writing files

Getting ready

How to do it...

How it works...

There's more...

See also

10. Statistical Programming and Linear Regression

Introduction

Using the built-in statistics library

Getting ready

How to do it...

How it works...

There's more...

Average of values in a Counter

Getting ready

How to do it...

How it works...

There's more...

See also

Computing the coefficient of a correlation

Getting ready

How to do it...

How it works...

There's more...

Computing regression parameters

Getting ready

How to do it...

How it works...

There's more...

Computing an autocorrelation

Getting ready

How to do it...

How it works...

There's more...

Long-term model

See also

Confirming that the data is random – the null hypothesis

Getting ready

How to do it...

How it works...

There's more...

See also

Locating outliers

Getting ready

How to do it...

How it works...

There's more...

See also

Analyzing many variables in one pass

Getting ready

How to do it...

How it works...

There's more...

Using map()

See also

11. Testing

Introduction

Using docstrings for testing

Getting ready

How to do it...

Writing examples for stateless functions

Writing examples for stateful objects

How it works...

There's more...

See also

Testing functions that raise exceptions

Getting ready

How to do it...

How it works...

There's more...

See also

Handling common doctest issues

Getting ready

How to do it...

Writing doctest examples for mapping or set values

Writing doctest examples for floating-point values

How it works...

There's more...

See also

Creating separate test modules and packages

Getting ready

How to do it...

How it works...

There's more...

Some other assertions

Separate tests directory

See also

Combining unittest and doctest tests

Getting ready

How to do it...

How it works...

There's more...

See also

Testing things that involve dates or times

Getting ready

How to do it...

How it works...

There's more...

See also

Testing things that involve randomness

Getting ready

How to do it...

How it works...

There's more...

See also

Mocking external resources

Getting ready

Creating an entry document in the entrylog collection

Seeing a typical response

Client class for database access

How to do it...

How it works...

Creating a context manager

Creating a dynamic, stateful test

Mocking a complex object

Using the load_tests protocol

There's more...

See also

12. Web Services

Introduction

Implementing web services with WSGI

Getting ready

How to do it...

How it works...

There's more...

See also

Using the Flask framework for RESTful APIs

Getting ready

How to do it...

How it works...

There's more...

See also

Parsing the query string in a request

Getting ready

How to do it...

How it works...

There's more...

See also

Making REST requests with urllib

Getting ready

How to do it...

How it works...

There's more...

The OpenAPI (Swagger) specification

Adding Swagger to the server

See also

Parsing the URL path

Getting ready

How to do it...

Server

Client

How it works...

Deck slicing

Client side

There's more...

Providing a Swagger specification

Using a Swagger specification

See also

Parsing a JSON request

Getting ready

How to do it...

Swagger specification

Server

Client

How it works...

There's more...

Location header

Additional resources

Query for a specific player

Exception handling

See also

Implementing authentication for web services

Getting ready

Configuring SSL

Users and credentials

Flask view function decorator

How to do it...

Defining the User class

Defining a view decorator

Creating the server

Creating an example client

How it works...

There's more...

Creating a command-line interface

Building the Authentication header

See also

13. Application Integration

Introduction

Finding configuration files

Getting ready

Why so many choices?

How to do it...

How it works...

There's more...

See also

Using YAML for configuration files

Getting ready

How to do it...

How it works...

There's more...

See also

Using Python for configuration files

Getting ready

How to do it...

How it works...

There's more...

See also

Using class-as-namespace for configuration

Getting ready

How to do it...

How it works...

There's more...

Configuration representation

See also

Designing scripts for composition

Getting ready

How to do it...

How it works...

There's more...

Designing as a class hierarchy

See also

Using logging for control and audit output

Getting ready

How to do it...

How it works...

There's more...

Combining two applications into one

Getting ready

How to do it...

How it works...

There's more...

Refactoring

Concurrency

Logging

See also

Combining many applications using the Command design pattern

Getting ready

How to do it...

How it works...

There's more...

See also

Managing arguments and configuration in composite applications

Getting ready

How to do it...

How it works...

The Command design pattern

There's more...

See also

Wrapping and combining CLI applications

Getting ready

How to do it...

How it works...

There's more...

Unit test

See also

Wrapping a program and checking the output

Getting ready

How to do it...

How it works...

There's more...

See also

Controlling complex sequences of steps

Getting ready

How to do it...

How it works...

There's more...

Building conditional processing

See also

累计评论(0条) 2个书友正在讨论这本书 发表评论

发表评论

发表评论,分享你的想法吧!

买过这本书的人还买过

读了这本书的人还在读

回顶部