Modern Python Cookbook - Steven F. Lott - E-Book

Modern Python Cookbook E-Book

Steven F. Lott

0,0
37,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Python is the preferred choice of developers, engineers, data scientists, and hobbyists everywhere. It is a great scripting 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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 913

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Modern Python Cookbook

 

 

 

 

 

 

 

 

 

The latest in modern Python recipes for the busy modern programmer

 

 

 

 

 

 

 

 

 

 

 

 

Steven F. Lott

 

 

 

 

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Modern Python Cookbook

 

Copyright © 2016 Packt Publishing

 

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: November 2016

Production reference: 1200918

 

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham 
B3 2PB, UK.

 

ISBN 978-1-78646-925-0

www.packtpub.com

Credits

Author

 

Steven F. Lott

Copy Editor

 

Safis Editing

Reviewers

 

Sanjeev Kumar Jaiswal

Dr. Vahid Mirjalili

Project Coordinator

 

Suzanne Coutinho

Commissioning Editor

 

Kunal Parikh

Proofreader

 

Safis Editing

Acquisition Editor

 

Sonali Vernekar

Indexer

 

Tejal Daruwale Soni

ContentDevelopmentEditor

 

Zeeyan Pinheiro

Graphics

 

Kirk D'Penha

Technical Editors

 

Pratish Shetty

Abhishek Sharma

Production Coordinator

 

Aparna Bhagat

About the Author

Steven F. Lott has been programming since the 70s, when computers were large, expensive, and rare. As a contract software developer and architect, he has worked on hundreds of projects, from very small to very large. He's been using Python to solve business problems for over 10 years.

He’s currently leveraging Python to implement microservices and ETL pipelines. His other titles with Packt Publishing include Python Essentials, Mastering Object-Oriented Python, Functional Python Programming, and Python for Secret Agents.

Steven is currently a technomad who lives in various places on the east coast of the U.S. His technology blog is http://slott-softwarearchitect.blogspot.com  and his LinkedIn address is https://www.linkedin.com/in/steven-lott-029835.

About the Reviewers

Sanjeev Jaiswal is a computer graduate with 7 years of industrial experience in web development and cyber security. He basically uses Perl, Python, and GNU/Linux for his day-to-day activities. He is currently working on projects involving penetration testing, source code review, and security design and implementations.

He is very much interested in web and cloud security. You can follow him on Twitter at @aliencoders and on GitHub at https://github.com/jassics.

He has written Instant PageSpeed Optimization and co-authored Learning Django Web Development for Packt Publishing. He has reviewed more than 5 books for Packt Publishing and looks forward to authoring or reviewing more books for Packt Publishing and other publishers.

 

 

Vahid Mirjalili is a software engineer and data scientist, currently working towards his PhD study in Computer Science at Michigan State University. His research at the i-PRoBE (integrated pattern recognition and biometrics) lab involves attribute classification of face images from large image datasets.

Furthermore, he teaches Python programming as well as computing concepts for data analysis and databases. Owing to his specialty in data mining, he is very interested in predictive modeling and getting insights from data. He is also a Python developer and likes to contribute to the open source community.

Moreover, he enjoys making tutorials for different directions of data science and computer algorithms, which can be found in his GitHub repository at http://github.com/mirjalil/DataScience.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

Table of Contents

Title Page

Copyright

Modern Python Cookbook

Credits

About the Author

About the Reviewers

www.PacktPub.com

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

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...

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

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

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

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

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

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

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

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

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

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

Preface

Python is the preferred choice of developers, engineers, data scientists, and hobbyists everywhere. It is a great scripting 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 insights 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 takes a recipe-based approach, where each recipe addresses specific problems and issues.

What this book covers

Chapter 1, Numbers, Strings, and Tuples, will look at the different kinds of numbers, work with strings, use tuples, and use the essential built-in types in Python. We will also exploit the full power of the Unicode character set.

Chapter 2, Statements and Syntax, will cover some basics of creating script files first. Then we’ll move on to looking at some of the complex statements, including if, while, for, try, with, and raise.

Chapter 3, Function Definitions, will look at a number of function definition techniques. We’ll also look at the Python 3.5 typing module and see how we can create more formal annotations for our functions.

Chapter 4, Built-in Data Structures – list, set, dict, will look at an overview of the various structures that are available and what problems they solve. From there, we can look at lists, dictionaries, and sets in detail, and also look at some more advanced topics related to how Python handles references to objects.

Chapter 5, User Inputs and Outputs, will explain how to use the different features of the print() function. We'll also look at the different functions used to provide user input.

Chapter 6, Basics of Classes and Objects, will create classes that implement a number of statistical formulae.

Chapter 7, More Advanced Class Design, will dive a little more deeply into Python classes. We will combine some features we have previously learned about to create more sophisticated objects.

Chapter 8, Input/Output, Physical Format, Logical Layout, will work with different file formats such as JSON, XML, and HTML.

Chapter 9, Testing, will give us a detailed description of the different testing frameworks used in Python.

Chapter 10, Web Services, will look at a number of recipes for creating RESTful web services and also serving static or dynamic content.

Chapter 11, Application Integration, will look at ways that we can design applications that can be composed to create larger, more sophisticated composite applications. We’ll also look at the complications that can arise from composite applications and the need to centralize some features, such as command-line parsing.

Chapter 12, Functional and Reactive Programming Features, provides us with methods to writing small, expressive functions that perform the required data transformations. Moving ahead, you will learn about the idea of reactive programming, that is, having processing rules that are evaluated when the inputs become available or change. This chapter is not present in the book, it is available for download at https://www.packtpub.com/sites/default/files/downloads/Functional_and_Reactive_Programming_Features.pdf.

Chapter 13, Statistical Programming and Linear Regression, will look at some basic statistical calculations that we can do with Python’s built-in libraries and data structures. We’ll look at the questions of correlation, randomness, and the null hypothesis. This chapter is not present in the book, it is available for download at https://www.packtpub.com/sites/default/files/downloads/Statistical_Programming_and_Linear_Regression.pdf.

What you need for this book

All you need to follow through the examples in this book is a computer running any recent version of Python. While the examples all use Python 3, they can be adapted to work with Python 2 only a few changes.

Who this book is for

The book is for web developers, programmers, enterprise programmers, engineers, and big data scientists. 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.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book 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 files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.

Hover the mouse pointer on the SUPPORT tab at the top.

Click on Code Downloads & Errata.

Enter the name of the book in the Search box.

Select the book for which you're looking to download the code files.

Choose from the drop-down menu where you purchased this book from.

Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for Windows

Zipeg / iZip / UnRarX for Mac

7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Modern-Python-Cookbook. We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Numbers, Strings, and Tuples

We'll cover these recipes to introduce basic Python data types:

Creating meaningful names and using variables

Working with large and small integers

Choosing between float, decimal, and fraction

Choosing between true division and floor division

Rewriting an immutable string

String parsing with regular expressions

Building complex strings with "template".format()

Building complex strings from lists of characters

Using the Unicode characters that aren't on our keyboards

Encoding strings – creating ASCII and UTF-8 bytes

Decoding bytes – how to get proper characters from some bytes

Using tuples of items

Introduction

This chapter will look at some central types of Python objects. We'll look at the different kinds of numbers, working with strings, and using tuples. We'll look at these first because they're the simplest kinds of data Python works with. In later chapters, we'll look at data collections.

Most of these recipes assume a beginner's level of understanding of Python 3. We'll be looking at how we use the essential built-in types available in Python—numbers, strings, and tuples. Python has a rich variety of numbers, and two different division operators, so we'll need to look closely at the choices available to us.

When working with strings, there are several common operations that are important. We'll explore some of the differences between bytes—as used by our OS files, and Strings—as used by Python. We'll look at how we can exploit the full power of the Unicode character set.

In this chapter, we'll show the recipes as if we're working from the >>> prompt in interactive Python. This is sometimes called the read-eval-print loop (REPL). In later chapters, we'll look more closely at writing script files. The goal is to encourage interactive exploration because it's a great way to learn the language.

Creating meaningful names and using variables

How can we be sure our programs make sense? One of the core elements of making expressive code is to use meaningful names. But what counts as meaningful? In this recipe, we'll review some common rules for creating meaningful Python names.

We'll also look at some of Python's assignment statement variations. We can, for example, assign more than one variable in a single statement.

Getting ready

The core issue when creating a name is to ask ourselves the question what is this thing? For software, we'd like a name that's descriptive of the object being named. Clearly, a name like x is not very descriptive, it doesn't seem to refer to an actual thing.

Vague, non-descriptive names are distressingly common in some programming. It's not helpful to others when we use them. A descriptive name helps everyone.

When naming things, it's also important to separate the problem domain—what we're really trying to accomplish—from the solution domain. The solution domain consists of the technical details of Python, OS, and Internet. Anyone who reads the code can see the solution; it doesn't require deep explanation. The problem domain, however, can be obscured by technical details. It's our job to make the problem clearly visible. Well-chosen names will help.

How to do it...

We'll look at names first. Then we'll move on to assignment.

Choosing names wisely

On a purely technical level, Python names must begin with a letter. They can include any number of letters, digits, and the _ character. Python 3 is based on Unicode, so a letter is not limited to the Latin alphabet. While the A-Z Latin alphabet is commonly used, it's not required.

When creating a descriptive variable, we want to create names that are both specific and articulate the relationships among things in our programs. One widely used technique is to create longer names in a style that moves from particular to general.

The steps to choosing a name are as follows:

The last part of the name is a very broad summary of the thing. In a few cases, this may be all we need; context will supply the rest. We'll suggest some typical broad summary categories later.

Use a prefix to narrow this name around your application or problem domain.

If needed, put more narrow and specialized prefixes on this name to clarify how it's distinct from other classes, modules, packages, functions, and other objects. When in doubt about prefixing, remember how domain names work. Think of

mail.google.com

—the name flows from particular to general. There's no magic about the three levels of naming, but it often happens to work out that way.

Format the name depending on how it's used in Python. There are three broad classes of things we'll put names on, which are shown as follows:

Classes

: A class has a name that summarizes the objects that are part of the class. These names will (often) use

CapitalizedCamelCase

. The first letter of a class name is capitalized to emphasize that it's a class, not an instance of the class. A class is often a generic concept, rarely a description of a tangible thing.

Objects

: A name for an object usually uses

snake_case

- all lowercase with multiple

_

characters between words. In Python, this includes variables, functions, modules, packages, parameters, attributes of objects, methods of classes, and almost everything else.

Script and module files

: These are really the OS resources, as seen by Python. Therefore, a filename should follow the conventions for Python objects, using letters, the

_

characters and ending with the

.py

extension. It's technically possible to have pretty wild and free filenames. Filenames that don't follow Python rules can be difficult to use as a module or package.

How do we choose the broad category part of a name? The general category depends on whether we're talking about a thing or a property of a thing. While the world is full of things, we can create some board groupings that are helpful. Some of the examples are Document, Enterprise, Place, Program, Product, Process, Person, Asset, Rule, Condition, Plant, Animal, Mineral, and so on.

We can then narrow these with qualifiers:

FinalStatusDocument ReceivedInventoryItemName

The first example is a class called Document. We've narrowed it slightly by adding a prefix to call it a StatusDocument. We narrowed it even further by calling it a FinalStatusDocument. The second example is a Name that we narrowed by specifying that it's a ReceivedInventoryItemName. This example required a four-level name to clarify the class.

An object often has properties or attributes. These have a decomposition based in the kind of information that's being represented. Some examples of terms that should be part of a complete name are amount, code, identifier, name, text, date, time, datetime, picture, video, sound, graphic, value, rate, percent, measure, and so on.

The idea is to put the narrow, more detailed description first, and the broad kind of information last:

measured_height_value estimated_weight_value scheduled_delivery_date location_code

In the first example, height narrows a more general representation term value. And measured_height_value further narrows this. Given this name, we can expect to see other variations on height. Similar thinking applies to weight_value, delivery_date and location_code. Each of these has a narrowing prefix or two.

Some things to avoid: Don't include detailed technical type information using coded prefixes or suffixes. This is often called Hungarian Notation; we don't use f_measured_height_value where the f is supposed to mean a floating-point. A variable like measured_height_value can be any numeric type and Python will do all the necessary conversions. The technical decoration doesn't offer much help to someone reading our code, because the type specification can be misleading or even incorrect. Don't waste a lot of effort forcing names to look like they belong together. We don't need to make SpadesCardSuit, ClubsCardSuit, and so on. Python has many different kinds of namespaces, including packages, modules, and classes, as well as namespace objects to gather related names together. If you combine these names in a CardSuit class, you can use CardSuit.Spades, which uses the class as namespace to separate these names from other, similar names.

How it works...

This approach to creating names follows the pattern of using narrow, more specific qualifiers first and the wider, less-specific category last. This follows the common convention used for domain names and e-mail addresses.

For example, a domain name like mail.google.com has a specific service, a more general enterprise, and finally a very general domain. This follows the principle of narrow-to-wider.

As another example, [email protected] starts with a specific destination name, has a more general enterprise, and finally a very general domain. Even the name of destination (PacktPub) is a two-part name with a narrow enterprise name (Packt) followed by a wider industry (Pub, short for publishing). (We don't agree with those who suggest it stands for Public House.)

The assignment statement is the only way to put a name on an object. We noted that we can have two names for the same underlying object. This isn't too useful right now. But in Chapter 4, Built-in Data Structures – list, set, dict we'll see some interesting consequences of multiple names for a single object.

There's more...

We'll try to show descriptive names in all of the recipes.

We have to grant exceptions to existing software which doesn't follow this pattern. It's often better to be consistent with legacy software than impose new rules even if the new rules are better.

Almost every example will involve assignment to variables. It's central to stateful object-oriented programming.

We'll look at classes and class names in Chapter 6, Basics of Classes and Objects; we'll look at modules in Chapter 11,Application Integration.

See also

The subject of descriptive naming is a source of ongoing research and discussion. There are two aspects—syntax and semantics. The starting point for thoughts on Python syntax is the famous Python Enhancement Proposal number 8 (PEP-8). This leads to use of CamelCase, and snake_case names.

Also, be sure to do this:

>>> import this

This will provide more insight into Python ideals.

For information on semantics, look at the legacy UDEF and NIEM Naming and Design Rules standards (http://www.opengroup.org/udefinfo/AboutTheUDEF.pdf). Additional details are in ISO11179 (https://en.wikipedia.org/wiki/ISO/IEC_11179), which talks in detail about meta-data and naming.

Working with large and small integers

Many programming languages make a distinction between integers, bytes, and long integers. Some languages include distinctions for signed versusunsigned integers. How do we map these concepts to Python?

The easy answer is that we don't. Python handles integers of all sizes in a uniform way. From bytes to immense numbers with hundreds of digits, it's all just integers to Python.

How to do it...

Don't worry. Really. Python behaves as if it has one universal type of integer, and this covers all of the bases from bytes to numbers that fill all of the memory. Here are the steps to using integers properly:

Write the numbers you need. Here are some smallish numbers: 355, 113. There’s no practical upper limit.

Creating a very small value—a single byte—looks like this:

>>> 2

2

Or perhaps this, if you want to use base 16:

>>> 0xff

255

In later recipes, we'll look at a sequence of bytes that has only a single value in it:

>>> b'\xfe'

b'\xfe'

This isn't—technically—an integer. It has a prefix of b' that shows us it's a 1-byte sequence.

Creating a much, much bigger number with a calculation might look like this:

>>> 2**2048

323...656

This number has 617 digits. We didn't show all of them.

See also

We'll look at the two division operators in the

Choosing between true division and floor division

recipe

We'll look at other kinds of numbers in the

Choosing between float, decimal, and fraction

recipe

For details on integer processing, see

https://www.python.org/dev/peps/pep-0237/

Choosing between float, decimal, and fraction

Python offers us several ways to work with rational numbers and approximations of irrational numbers. We have three basic choices:

Float

Decimal

Fraction

With so many choices, when do we use each of these?

Getting ready

It's important to be sure about our core mathematical expectations. If we're not sure what kind of data we have, or what kinds of results we want to get, we really shouldn't be coding. We need to take a step back and review things with pencil and paper.

There are three general cases for math that involve numbers beyond integers, which are:

Currency

: Dollars, cents, or euros. Currency generally has a fixed number of decimal places. There are rounding rules used to determine what 7.25% of $2.95 is.

Rational Numbers or Fractions

: When we're working with American units for feet and inches, or cooking measurements in cups and fluid ounces, we often need to work in fractions. When we scale a recipe that serves eight, for example, down to five people, we're doing fractional math using a scaling factor of 5/8 . How do we apply this to 2/3 cup of rice and still get a measurement that fits an American kitchen gadget?

Irrational Numbers

: This includes all other kinds of calculations. It's important to note that digital computers can only approximate these numbers, and we'll occasionally see odd little artifacts of this approximation. The float approximations are very fast, but sometimes suffer from truncation issues.

When we have one of the first two cases, we should avoid floating-point numbers.

How to do it...

We'll look at each of the three cases separately. First, we'll look at computing with currency. Then we'll look at rational numbers, and finally irrational or floating-point numbers. Finally, we'll look at making explicit conversions among these various types.

Converting numbers from one type to another

We can use the float() function to create a float value from another value. It looks like this:

>>> float(total_amount)

3.163875

>>> float(sugar_cups * scale_factor)

1.5625

In the first example, we converted a Decimal value to float. In the second example, we converted a Fraction value to float.

As we just saw, we're never happy trying to convert float to Decimal or Fraction:

>>> Fraction(19/155)

Fraction(8832866365939553, 72057594037927936)

>>> Decimal(19/155)

Decimal('0.12258064516129031640279123394066118635237216949462890625')

In the first example, we did a calculation among integers to create a float value that has a known truncation problem. When we created a Fraction from that truncated float value, we got some terrible looking numbers that exposed the details of the truncation.

Similarly, the second example tried to create a Decimal value from a float.

How it works...

For these numeric types, Python offers us a variety of operators: +, -, *, /, //, %, and **. These are for addition, subtraction, multiplication, true division, truncated division, modulus, and raising to a power. We'll look at the two division operators in the Choosing between true division and floor division recipe.

Python is adept at converting numbers between the various types. We can mix int and float