Expert Python Programming Second Edition Michal Jaworski Tarek Ziade all chapter instant download
Expert Python Programming Second Edition Michal Jaworski Tarek Ziade all chapter instant download
com
https://ebookmeta.com/product/expert-python-programming-
second-edition-michal-jaworski-tarek-ziade/
OR CLICK BUTTON
DOWNLOAD NOW
https://ebookmeta.com/product/expert-python-programming-second-
edition-michal-jaworski-tarek-ziade-ziade-jaworski/
ebookmeta.com
https://ebookmeta.com/product/expert-python-programming-second-
edition-michal-jaworski-tarek-ziade-ziade-jaworski-2/
ebookmeta.com
https://ebookmeta.com/product/expert-python-programming-2nd-edition-
michal-jaworski-tarek-ziade/
ebookmeta.com
https://ebookmeta.com/product/into-andhera-1st-edition-roland-
carlsson/
ebookmeta.com
Teaching Sprints How Overloaded Educators Can Keep Getting
Better 1st Edition Simon Breakspear
https://ebookmeta.com/product/teaching-sprints-how-overloaded-
educators-can-keep-getting-better-1st-edition-simon-breakspear/
ebookmeta.com
https://ebookmeta.com/product/dungeons-of-drakkenheim-5th-edition-
monty-martin-and-kelly-mclaughlin/
ebookmeta.com
https://ebookmeta.com/product/experimental-marine-biology-1st-edition-
susannah-nix/
ebookmeta.com
https://ebookmeta.com/product/my-two-mates-double-desert-
shifters-6-1st-edition-mia-wolf/
ebookmeta.com
Peptide Nucleic Acids Peter E Nielsen Ed Daniel H Appella
Ed
https://ebookmeta.com/product/peptide-nucleic-acids-peter-e-nielsen-
ed-daniel-h-appella-ed/
ebookmeta.com
Table of Contents
Expert Python ProgrammingSecond Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
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. Current Status of Python
Where are we now and where we are going?
Why and how does Python change?
Getting up to date with changes – PEP documents
Python 3 adoption at the time of writing this book
The main differences between Python 3 and Python 2
Why should I care?
The main syntax differences and common pitfalls
Syntax changes
Changes in the standard library
Changes in datatypes and collections
The popular tools and techniques used for maintaining cross-
version compatibility
Not only CPython
Why should I care?
Stackless Python
Jython
IronPython
PyPy
Modern approaches to Python development
Application-level isolation of Python environments
Why isolation?
Popular solutions
virtualenv
venv
buildout
Which one to choose?
System-level environment isolation
Virtual development environments using Vagrant
Containerization versus virtualization
Popular productivity tools
Custom Python shells – IPython, bpython, ptpython, and so on
Setting up the PYTHONSTARTUP environment variable
IPython
bpython
ptpython
Interactive debuggers
Useful resources
Summary
2. Syntax Best Practices – below the Class Level
Python's built-in types
Strings and bytes
Implementation details
String concatenation
Collections
Lists and tuples
Implementation details
List comprehensions
Other idioms
Dictionaries
Implementation details
Weaknesses and alternatives
Sets
Implementation details
Beyond basic collections – the collections module
Advanced syntax
Iterators
The yield statement
Decorators
General syntax and possible implementations
As a function
As a class
Parametrizing decorators
Introspection preserving decorators
Usage and useful examples
Argument checking
Caching
Proxy
Context provider
Context managers – the with statement
General syntax and possible implementations
As a class
As a function – the contextlib module
Other syntax elements you may not know yet
The for … else … statement
Function annotations
The general syntax
The possible uses
Summary
3. Syntax Best Practices – above the Class Level
Subclassing built-in types
Accessing methods from superclasses
Old-style classes and super in Python 2
Understanding Python's Method Resolution Order
super pitfalls
Mixing super and explicit class calls
Heterogeneous arguments
Best practices
Advanced attribute access patterns
Descriptors
Real-life example – lazily evaluated attributes
Properties
Slots
Metaprogramming
Decorators – a method of metaprogramming
Class decorators
Using the __new__() method to override instance creation
process
Metaclasses
The general syntax
New Python 3 syntax for metaclasses
Metaclass usage
Metaclass pitfalls
Some tips on code generation
exec, eval, and compile
Abstract Syntax Tree
Import hooks
Projects using code generation patterns
Falcon's compiled router
Hy
Summary
4. Choosing Good Names
PEP 8 and naming best practices
Why and when to follow PEP 8?
Beyond PEP 8 – team-specific style guidelines
Naming styles
Variables
Constants
Naming and usage
Public and private variables
Functions and methods
The private controversy
Special methods
Arguments
Properties
Classes
Modules and packages
The naming guide
Using the has or is prefix for Boolean elements
Using plurals for variables that are collections
Using explicit names for dictionaries
Avoiding generic names
Avoiding existing names
Best practices for arguments
Building arguments by iterative design
Trust the arguments and your tests
Using *args and **kwargs magic arguments carefully
Class names
Module and package names
Useful tools
Pylint
pep8 and flake8
Summary
5. Writing a Package
Creating a package
The confusing state of Python packaging tools
The current landscape of Python packaging thanks to PyPA
Tool recommendations
Project configuration
setup.py
setup.cfg
MANIFEST.in
Most important metadata
Trove classifiers
Common patterns
Automated inclusion of version string from package
README file
Managing dependencies
The custom setup command
Working with packages during development
setup.py install
Uninstalling packages
setup.py develop or pip -e
Namespace packages
Why is it useful?
PEP 420 – implicit namespace packages
Namespace packages in previous Python versions
Uploading a package
PyPI – Python Package Index
Uploading to PyPI – or other package index
.pypirc
Source packages versus built packages
sdist
bdist and wheels
Standalone executables
When are standalone executables useful?
Popular tools
PyInstaller
cx_Freeze
py2exe and py2app
Security of Python code in executable packages
Making decompilation harder
Summary
6. Deploying Code
The Twelve-Factor App
Deployment automation using Fabric
Your own package index or index mirror
PyPI mirroring
Deployment using a package
Common conventions and practices
The filesystem hierarchy
Isolation
Using process supervision tools
Application code should be run in user space
Using reverse HTTP proxies
Reloading processes gracefully
Code instrumentation and monitoring
Logging errors – sentry/raven
Monitoring system and application metrics
Dealing with application logs
Basic low-level log practices
Tools for log processing
Summary
7. Python Extensions in Other Languages
Different language means – C or C++
How do extensions in C or C++ work
Why you might want to use extensions
Improving performance in critical code sections
Integrating existing code written in different languages
Integrating third-party dynamic libraries
Creating custom datatypes
Writing extensions
Pure C extensions
A closer look at Python/C API
Calling and binding conventions
Exception handling
Releasing GIL
Reference counting
Cython
Cython as a source to source compiler
Cython as a language
Challenges
Additional complexity
Debugging
Interfacing with dynamic libraries without extensions
ctypes
Loading libraries
Calling C functions using ctypes
Passing Python functions as C callbacks
CFFI
Summary
8. Managing Code
Version control systems
Centralized systems
Distributed systems
Distributed strategies
Centralized or distributed?
Use Git if you can
Git flow and GitHub flow
Continuous development processes
Continuous integration
Testing every commit
Merge testing through CI
Matrix testing
Continuous delivery
Continuous deployment
Popular tools for continuous integration
Jenkins
Buildbot
Travis CI
GitLab CI
Choosing the right tool and common pitfalls
Problem 1 – too complex build strategies
Problem 2 – too long building time
Problem 3 – external job definitions
Problem 4 – lack of isolation
Summary
9. Documenting Your Project
The seven rules of technical writing
Write in two steps
Target the readership
Use a simple style
Limit the scope of information
Use realistic code examples
Use a light but sufficient approach
Use templates
A reStructuredText primer
Section structure
Lists
Inline markup
Literal block
Links
Building the documentation
Building the portfolio
Design
Usage
Recipe
Tutorial
Module helper
Operations
Making your own portfolio
Building the landscape
Producer's layout
Consumer's layout
Working on the index pages
Registering module helpers
Adding index markers
Cross-references
Documentation building and continuous integration
Summary
10. Test-Driven Development
I don't test
Test-driven development principles
Preventing software regression
Improving code quality
Providing the best developer documentation
Producing robust code faster
What kind of tests?
Acceptance tests
Unit tests
Functional tests
Integration tests
Load and performance testing
Code quality testing
Python standard test tools
unittest
doctest
I do test
unittest pitfalls
unittest alternatives
nose
Test runner
Writing tests
Writing test fixtures
Integration with setuptools and a plug-in system
Wrap-up
py.test
Writing test fixtures
Disabling test functions and classes
Automated distributed tests
Wrap-up
Testing coverage
Fakes and mocks
Building a fake
Using mocks
Testing environment and dependency compatibility
Dependency matrix testing
Document-driven development
Writing a story
Summary
11. Optimization – General Principles and Profiling Techniques
The three rules of optimization
Make it work first
Work from the user's point of view
Keep the code readable and maintainable
Optimization strategy
Find another culprit
Scale the hardware
Writing a speed test
Finding bottlenecks
Profiling CPU usage
Macro-profiling
Micro-profiling
Measuring Pystones
Profiling memory usage
How Python deals with memory
Profiling memory
objgraph
C code memory leaks
Profiling network usage
Summary
12. Optimization – Some Powerful Techniques
Reducing the complexity
Cyclomatic complexity
The big O notation
Simplifying
Searching in a list
Using a set instead of a list
Cut the external calls, reduce the workload
Using collections
deque
defaultdict
namedtuple
Using architectural trade-offs
Using heuristics and approximation algorithms
Using task queues and delayed processing
Using probabilistic data structures
Caching
Deterministic caching
Nondeterministic caching
Cache services
Memcached
Summary
13. Concurrency
Why concurrency?
Multithreading
What is multithreading?
How Python deals with threads
When should threading be used?
Building responsive interfaces
Delegating work
Multiuser applications
An example of a threaded application
Using one thread per item
Using a thread pool
Using two-way queues
Dealing with errors and rate limiting
Multiprocessing
The built-in multiprocessing module
Using process pools
Using multiprocessing.dummy as a multithreading interface
Asynchronous programming
Cooperative multitasking and asynchronous I/O
Python async and await keywords
asyncio in older versions of Python
A practical example of asynchronous programming
Integrating nonasynchronous code with async using futures
Executors and futures
Using executors in an event loop
Summary
14. Useful Design Patterns
Creational patterns
Singleton
Structural patterns
Adapter
Interfaces
Using zope.interface
Using function annotations and abstract base classes
Using collections.abc
Proxy
Facade
Behavioral patterns
Observer
Visitor
Template
Summary
Index
Expert Python
ProgrammingSecond Edition
Expert Python
ProgrammingSecond Edition
Copyright © 2016 Packt Publishing
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 authors, 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.
Livery Place
35 Livery Street
CHAPTER XV
The Trap
The man of the thin features produced the revolver from its holster
and sat down on a snow-covered rock near the top of the shaft. He
held the weapon negligently, but there was no doubt that he could
level it at the Hardy boys in a second if they attempted to escape.
"You can sit down if you want," he said. His partner still retained a
tight grasp on Frank. "Let him go, Shorty. I've got this gun here and
I guess they won't try to get away. We may as well be comfortable."
The fellow addressed as "Shorty" moved away from Frank and sat
down by his companion. The Hardy boys found a heap of rocks near
by and seated themselves. They knew there was no use of
attempting to escape as long as that ugly-looking revolver was in the
hand of their captor.
"Say, Slim," remarked Shorty, "do you think Black Pepper is at the
camp?"
The other man nodded.
"Yeah! He came back this morning."
Slim looked up at the Hardy boys.
"What were you guys lookin' for in that mine, anyway?"
"Oysters," replied Frank, with a grin.
"None of your funny stuff," rapped out Slim. "We'll make you talk
soon enough. We know what you're after."
"What did you ask us for, then?" asked Joe.
The outlaws were silent. They saw that nothing was to be gained by
seeking information from the lads. They were content to await the
return of Black Pepper and their companion Jack.
Frank and Joe Hardy sat on the snow-covered rocks in silence.
Slowly Frank put his hand behind his back and began to grope about
among the rocks. He knew that they were loose and that they were
of various sizes. The idea had occurred to him that if he could but
use one of them as a weapon he might be able to disarm Slim and
perhaps effect his escape and that of his brother.
Bit by bit he groped about. One rock was too large for him to grasp.
Another was too small to be of any use. Finally his hands closed
about a good-sized stone that came from the rest of the pile without
much difficulty.
He calculated the distance and eyed the revolver warily. Frank had
been pitcher on the Bayport high school nine and the accuracy of his
aim had often been the despair of opposing batsmen. Now he called
on all his skill.
Without moving from his position he suddenly brought up the rock
and flung it with all his strength directly at the revolver in Slim's
hand. The outlaw's grip on the weapon had relaxed in his
indifference, and when the stone struck its mark, full and true, the
gun went flying into the deep snow.
"Come on, Joe!" shouted Frank scrambling to his feet. He had
noticed a path leading through the snow in the direction of the road
that went to Hank Shale's cabin and he ran toward this path with all
the speed at his command. Joe had not been slow to grasp the
situation, and he too came racing through the snow but a few paces
behind.
The outlaws were taken off their guard. Slim instinctively reached for
his revolver, but it had disappeared in the snow and he wasted many
precious seconds hunting for it. Shorty had leaped after the boys,
then, seeing that his companion did not follow, he hesitated, ran
back, and then turned around again. He did not know what to do.
"After them!" roared Slim, and Shorty took up the pursuit. But his
indecision had given the Hardy boys the opportunity they needed.
They had a good start on their pursuer and Shorty was but a clumsy
runner at best. Frank gained the path and there his progress was
swifter because he was not handicapped by the impeding snow. Slim
finally abandoned his search for the weapon and also took up the
chase, but by this time he was far behind.
The boys gained the main road, with Shorty ploughing along in
pursuit. Even yet they were not safe, but chance came to their aid in
the shape of a stage that ran from Lucky Bottom to one of the
neighboring camps. It rattled along, with sleighbells jingling, the
driver muffled to the ears, and when Shorty and Slim caught sight of
it they slowed up and abandoned the chase. The open road was a
dangerous place. They did not wish any interference from the stage
driver or his passengers.
When Frank and Joe saw that their pursuers had turned back they
slowed down to a walk. Hank Shale's cabin was already in sight.
"We gave them the slip, all right," declared Frank jubilantly.
"I'll tell the world we did. Black Pepper and the other fellow will be
hopping mad when they come back and find that we've escaped."
"We'll have to be on the lookout for them from now on. They won't
stop until they do lay their hands on us."
"Perhaps it's just as well. We can be on our guard. If we weren't
expecting anything wrong we'd be liable to walk right into their
arms."
When the boys reached the cabin they found their father and Hank
Shale greatly worried by their prolonged absence. They told of their
descent into the abandoned mine, of the cave-in, and of their
subsequent escape, of their capture by Black Pepper's men and of
their get-away. Mr. Hardy looked grave.
"I think we'd better drop the case," he said finally. "It's too big a risk
to take."
"Why?" asked the boys, in surprise.
"You might have been buried alive in that mine, in the first place. I
would never have forgiven myself. And now that you have run up
against Black Pepper's gang they'll be out to get you. I don't want to
be responsible for making you run those risks."
"We won't drop the case," laughed Frank. "It's just getting
interesting now. We'll find that gold for you, Dad."
"Don't worry about us," chimed in Joe. "We can look after ourselves.
We probably won't be up against any worse dangers than the ones
we faced to-day."
"Well," said Mr. Hardy, reluctantly, "you've come all the way out
here, and I suppose you'll be disappointed if I don't let you go
ahead; but I don't want you to take any unnecessary risks."
"I'm thinkin' they'll pull through all right," said Hank Shale solemnly.
"Let the lads be, Mr. Hardy."
So, with this encouragement, Mr. Hardy consented to let his sons
continue their activities on the case. Both Frank and Joe promised to
take all due precautions and next morning they resumed their search
for the missing gold.
During the days that followed they explored several abandoned
workings, but the hunt was fruitless. They succeeded only in getting
themselves well covered with dirt and grime and would return to the
cabin hungry and weary. There had been no sign of any members of
Black Pepper's gang. But finally Hank Shale, who had been down to
the general store at Lucky Bottom one day, had news for them.
"They be sayin' down town," declared the old miner, "that Black
Pepper and his gang have broke up camp."
"Have they left Lucky Bottom?" asked Mr. Hardy quickly.
Hank Shale shook his head. "Nobody knows. They had a camp
somewheres back in the mountain, but they've all cleared away from
it. Maybe the two lads here scared 'em."
"They've likely just moved to a new camping place," remarked
Frank.
"I hope so," said Mr. Hardy. "If they've gone away it means that the
gold has gone with them. If they're still around we have a chance
yet."
Frank and Joe said nothing, but when they went to bed that night
they talked in whispers in the darkness.
"What's the program for to-morrow?" asked Joe.
"We're going to find out if any of that gang are still around."
"Do you mean we'll go out looking for them?"
"Sure! It's just as dad says—if they've gone away the gold has gone
with them. If they're still hanging around we'll know there's still a
good chance of finding it ourselves."
"Where shall we look?"
"Up in the mountains. We can look around for trails in the snow."
"Suits me, as long as they don't catch us."
"That's a chance we have to take."
So next morning, without revealing their plans to any one, the boys
started out into the mountains. It was a gloomy day and the sky was
overcast. The lowering, snow-covered crags loomed high above
them as they headed toward a narrow defile not far from the
abandoned mine where they had been captured by Black Pepper's
men some days previous. It was toward this defile that the man
called Jack had gone on his way to summon Black Pepper, and the
boys judged that the outlaws' abandoned camp was probably
somewhere in that direction.
They discovered a narrow trail through the snow. It was a trail that
had evidently been much used, for the snow was packed hard by the
tramp of many feet.
"I think we're on the right track, all right," said Frank. "Even if we
only find the deserted camp we may get some clues that will help
us."
The boys went higher up into the mountain and at last they came to
a protected spot beneath an overhanging crag, where the snow had
not penetrated. Here the trail ended in a long platform of bare rock.
They went across it, but were unable to pick up the trail again,
although they searched about in every direction.
Suddenly Frank said to his brother in a low voice:
"Don't look around. Keep straight ahead."
"What's the matter?"
"There's some one following us. I just caught a glimpse of him out
of the corner of my eye. He's hiding behind the rocks back there."
"Let's tackle him."
"There may be others with him. Let him follow, and if he's alone
we'll grab him."
Without giving any indication that they had seen their pursuer, the
Hardy boys cut down into a narrow ravine where huge masses of
boulders made progress difficult. They came to a place where rocks
rose on either side so close together that there was room for only
one person to pass at a time. As soon as they had gone through the
opening Frank leaped to one side, motioning to his brother to take
the opposite side of the boulders. They were now completely hidden
from the man who followed.
"We'll get him when he comes through," whispered Frank.
They waited expectantly.
At last they heard the crunch of snow that indicated the
unsuspecting man was approaching. Cautiously he drew nearer, step
by step. The boys prepared themselves.
The man drew nearer. He was just entering the passage between
the boulders. Frank and Joe pressed themselves against the rocks.
They saw a head appear in view, then the shoulders of the man. He
stepped forward and, at the same moment, they sprang at him.
Frank launched himself full on the fellow's shoulders and he gave a
cry of surprise. At the same time Joe flung his arms about the man's
waist and all three came tumbling to the ground. There was a flurry
of snow as they struggled, but the fight was short-lived. Taken
completely by surprise, the man was quickly overcome. He had
reached for a revolver at his waist, but Frank had seen it in the nick
of time and had struck it from his grasp. He seized the weapon
himself and pressed the barrel of it to the fellow's temple.
"All right! All right!" he gasped. "I give in."
There was something familiar about the voice. The man turned his
head about and they saw that it was the man known as Slim, the
thin-faced fellow who had been among their captors several days
before.
CHAPTER XVI
Information
CHAPTER XVII
The Outlaw's Notebook
CHAPTER XIX
The Lone Tree