𝔖 Scriptorium
✦   LIBER   ✦

📁

Supercharged Python: Take Your Code to the Next Level

✍ Scribed by Brian Overland; John Bennett


Publisher
Addison-Wesley Professional
Year
2019
Tongue
English
Leaves
667
Category
Library

⬇  Acquire This Volume

No coin nor oath required. For personal study only.

✦ Synopsis


Tapping into the full power of Python doesn’t have to be difficult. Supercharged Python is written for people who’ve learned the fundamentals of the language but want to take their skills to the next level.

Using the example-driven approach featured in Python Without Fear, Brian Overland and John Bennett start with simple examples and carefully build more complex applications. After briefly reviewing the fundamental concepts of Python, the authors carefully explain more advanced topics, including text formatting, regular expressions, and chart plotting.

The book reviews advanced techniques for list and string classes, and then covers all the ways of handling text and binary files; decimal, money, and other special classes; advanced techniques for writing classes; generators and decorators; and the random, math, and Numpy (Numeric Python) packages, which can supercharge your applications.

- Use profilers and “magic methods” to code like a pro
- Harness the power of regular expressions to process text quickly with a single statement
- Take advantage of 22 coding shortcuts, along with performance tips to save time and optimize your code
- Create really useful classes and objects, for games, simulations, money, mathematics, and more
- Use multiple modules to build powerful apps while avoiding the “gotchas”
- Import packages to dramatically speed up statistical operations—by as much as 100 times!
- Refer to the five-part language reference to look up fine points of the language

Supercharged Python demonstrates techniques that allow you to write faster and more powerful code, whether you’re manipulating large amounts of data or building sophisticated applications.

✦ Table of Contents


Cover
Title Page
Copyright Page
Contents
Preface
What Makes Python Special?
Paths to Learning: Where Do I Start?
Clarity and Examples Are Everything
Learning Aids: Icons
What You’ll Learn
Have Fun
Acknowledgments
About the Authors

Chapter 1 Review of the Fundamentals
_1.1 Python Quick Start
_1.2 Variables and Naming Names
_1.3 Combined Assignment Operators
_1.4 Summary of Python Arithmetic Operators
_1.5 Elementary Data Types: Integer and Floating Point
_1.6 Basic Input and Output
_1.7 Function Definitions
_1.8 The Python “if” Statement
_1.9 The Python “while” Statement
_1.10 A Couple of Cool Little Apps
_1.11 Summary of Python Boolean Operators
_1.12 Function Arguments and Return Values
_1.13 The Forward Reference Problem
_1.14 Python Strings
_1.15 Python Lists (and a Cool Sorting App)
_1.16 The “for” Statement and Ranges
_1.17 Tuples
_1.18 Dictionaries
_1.19 Sets
_1.20 Global and Local Variables
_Summary
_Review Questions
____Suggested Problems

Chapter 2 Advanced String Capabilities
_2.1 Strings Are Immutable
_2.2 Numeric Conversions, Including Binary
_2.3 String Operators (+, =, *, >, etc.)
_2.4 Indexing and Slicing
_2.5 Single-Character Functions (Character Codes)
_2.6 Building Strings Using “join”
_2.7 Important String Functions
_2.8 Binary, Hex, and Octal Conversion Functions
_2.9 Simple Boolean (“is”) Methods
_2.10 Case Conversion Methods
_2.11 Search-and-Replace Methods
_2.12 Breaking Up Input Using “split”
_2.13 Stripping
_2.14 Justification Methods
_Summary
_Review Questions
____Suggested Problems

Chapter 3 Advanced List Capabilities
_3.1 Creating and Using Python Lists
_3.2 Copying Lists Versus Copying List Variables
_3.3 Indexing
_3.3.1 Positive Indexes
__3.3.2 Negative Indexes
__3.3.3 Generating Index Numbers Using “enumerate”
__3.4 Getting Data from Slices
_3.5 Assigning into Slices
_3.6 List Operators
_3.7 Shallow Versus Deep Copying
_3.8 List Functions
_3.9 List Methods: Modifying a List
_3.10 List Methods: Getting Information on Contents
_3.11 List Methods: Reorganizing
_3.12 Lists as Stacks: RPN Application
_3.13 The “reduce” Function
_3.14 Lambda Functions
_3.15 List Comprehension
_3.16 Dictionary and Set Comprehension
_3.17 Passing Arguments Through a List
_3.18 Multidimensional Lists
__3.18.1 Unbalanced Matrixes
__3.18.2 Creating Arbitrarily Large Matrixes
_Summary
_Review Questions
____Suggested Problems

Chapter 4 Shortcuts, Command Line, and Packages
_4.1 Overview
_4.2 Twenty-Two Programming Shortcuts
__4.2.1 Use Python Line Continuation as Needed
__4.2.2 Use “for” Loops Intelligently
__4.2.3 Understand Combined Operator Assignment (+= etc.)
__4.2.4 Use Multiple Assignment
__4.2.5 Use Tuple Assignment
__4.2.6 Use Advanced Tuple Assignment
__4.2.7 Use List and String “Multiplication”
__4.2.8 Return Multiple Values
__4.2.9 Use Loops and the “else” Keyword
__4.2.10 Take Advantage of Boolean Values and “not”
__4.2.11 Treat Strings as Lists of Characters
__4.2.12 Eliminate Characters by Using “replace”
__4.2.13 Don’t Write Unnecessary Loops
__4.2.14 Use Chained Comparisons (n < x< m)
__4.2.15 Simulate “switch” with a Table of Functions
__4.2.16 Use the “is” Operator Correctly
__4.2.17 Use One-Line “for” Loops
__4.2.18 Squeeze Multiple Statements onto a Line
__4.2.19 Write One-Line if/then/else Statements
__4.2.20 Create Enum Values with “range”
__4.2.21 Reduce the Inefficiency of the “print” Function Within IDLE
__4.2.22 Place Underscores Inside Large Numbers
_4.3 Running Python from the Command Line
_4.3.1 Running on a Windows-Based System
__4.3.2 Running on a Macintosh System
__4.3.3 Using pip or pip3 to Download Packages
__4.4 Writing and Using Doc Strings
_4.5 Importing Packages
_4.6 A Guided Tour of Python Packages
_4.7 Functions as First-Class Objects
_4.8 Variable-Length Argument Lists
__4.8.1 The args List
__4.8.2 The “
*kwargs” List
_4.9 Decorators and Function Profilers
_4.10 Generators
__4.10.1 What’s an Iterator?
__4.10.2 Introducing Generators
_4.11 Accessing Command-Line Arguments
_Summary
_Questions for Review
_Suggested Problems

Chapter 5 Formatting Text Precisely
_5.1 Formatting with the Percent Sign Operator (%)
_5.2 Percent Sign (%) Format Specifiers
_5.3 Percent Sign (%) Variable-Length Print Fields
_5.4 The Global “format” Function
_5.5 Introduction to the “format” Method
_5.6 Ordering by Position (Name or Number)
_5.7 “Repr” Versus String Conversion
_5.8 The “spec” Field of the “format” Function and Method
__5.8.1 Print-Field Width
__5.8.2 Text Justification: “fill” and “align” Characters
__5.8.3 The “sign” Character
__5.8.4 The Leading-Zero Character (0)
__5.8.5 Thousands Place Separator
__5.8.6 Controlling Precision
__5.8.7 “Precision” Used with Strings (Truncation)
__5.8.8 “Type” Specifiers
__5.8.9 Displaying in Binary Radix
__5.8.10 Displaying in Octal and Hex Radix
__5.8.11 Displaying Percentages
__5.8.12 Binary Radix Example
_5.9 Variable-Size Fields
_Summary
_Review Questions
_Suggested Problems

Chapter 6 Regular Expressions, Part I
_6.1 Introduction to Regular Expressions
_6.2 A Practical Example: Phone Numbers
_6.3 Refining Matches
_6.4 How Regular Expressions Work: Compiling Versus Running
_6.5 Ignoring Case, and Other Function Flags
_6.6 Regular Expressions: Basic Syntax Summary
__6.6.1 Meta Characters
__6.6.2 Character Sets
__6.6.3 Pattern Quantifiers
__6.6.4 Backtracking, Greedy, and Non-Greedy
_6.7 A Practical Regular-Expression Example
_6.8 Using the Match Object
_6.9 Searching a String for Patterns
_6.10 Iterative Searching (“findall”)
_6.11 The “findall” Method and the Grouping Problem
_6.12 Searching for Repeated Patterns
_6.13 Replacing Text
_Summary
_Review Questions
_Suggested Problems

Chapter 7 Regular Expressions, Part II
_7.1 Summary of Advanced RegEx Grammar
_7.2 Noncapture Groups
__7.2.1 The Canonical Number Example
__7.2.2 Fixing the Tagging Problem
_7.3 Greedy Versus Non-Greedy Matching
_7.4 The Look-Ahead Feature
_7.5 Checking Multiple Patterns (Look-Ahead)
_7.6 Negative Look-Ahead
_7.7 Named Groups
_7.8 The “re.split” Function
_7.9 The Scanner Class and the RPN Project
_7.10 RPN: Doing Even More with Scanner
_Summary
_Review Questions
____Suggested Problems

Chapter 8 Text and Binary Files
_8.1 Two Kinds of Files: Text and Binary
_8.1.1 Text Files
__8.1.2 Binary Files
__8.2 Approaches to Binary Files: A Summary
_8.3 The File/Directory System
_8.4 Handling File-Opening Exceptions
_8.5 Using the “with” Keyword
_8.6 Summary of Read/Write Operations
_8.7 Text File Operations in Depth
_8.8 Using the File Pointer (“seek”)
_8.9 Reading Text into the RPN Project
_8.9.1 The RPN Interpreter to Date
__8.9.2 Reading RPN from a Text File
__8.9.3 Adding an Assignment Operator to RPN
__8.10 Direct Binary Read/Write
_8.11 Converting Data to Fixed-Length Fields (“struct”)
_8.11.1 Writing and Reading One Number at a Time
__8.11.2 Writing and Reading Several Numbers at a Time
__8.11.3 Writing and Reading a Fixed-Length String
__8.11.4 Writing and Reading a Variable-Length String
__8.11.5 Writing and Reading Strings and Numerics Together
__8.11.6 Low-Level Details: Big Endian Versus Little Endian
__8.12 Using the Pickling Package
_8.13 Using the “shelve” Package
_Summary
_Review Questions
_Suggested Problems

Chapter 9 Classes and Magic Methods
_9.1 Classes and Objects: Basic Syntax
9.2 More About Instance Variables
____9.3 The “
init ” and “ new ” Methods
____9.4 Classes and the Forward Reference Problem
____9.5 Methods Generally
____9.6 Public and Private Variables and Methods
____9.7 Inheritance
____9.8 Multiple Inheritance
____9.9 Magic Methods, Summarized
____9.10 Magic Methods in Detail
__9.10.1 String Representation in Python Classes
__9.10.2 The Object Representation Methods
__9.10.3 Comparison Methods
__9.10.4 Arithmetic Operator Methods
__9.10.5 Unary Arithmetic Methods
__9.10.6 Reflection (Reverse-Order) Methods
__9.10.7 In-Place Operator Methods
__9.10.8 Conversion Methods
__9.10.9 Collection Class Methods
__9.10.10 Implementing “
iter ” and “ next _”
_9.11 Supporting Multiple Argument Types
_9.12 Setting and Getting Attributes Dynamically
_Summary
_Review Questions
____Suggested Problems

Chapter 10 Decimal, Money, and Other Classes
_10.1 Overview of Numeric Classes
10.2 Limitations of Floating-Point Format
____10.3 Introducing the Decimal Class
____10.4 Special Operations on Decimal Objects
____10.5 A Decimal Class Application
____10.6 Designing a Money Class
____10.7 Writing the Basic Money Class (Containment)
____10.8 Displaying Money Objects (“
str ”, “ repr _”)
_10.9 Other Monetary Operations
_10.10 Demo: A Money Calculator
_10.11 Setting the Default Currency
_10.12 Money and Inheritance
_10.13 The Fraction Class
_10.14 The Complex Class
_Summary
_Review Questions
____Suggested Problems

Chapter 11 The Random and Math Packages
_11.1 Overview of the Random Package
_11.2 A Tour of Random Functions
_11.3 Testing Random Behavior
_11.4 A Random-Integer Game
_11.5 Creating a Deck Object
_11.6 Adding Pictograms to the Deck
_11.7 Charting a Normal Distribution
_11.8 Writing Your Own Random-Number Generator
__11.8.1 Principles of Generating Random Numbers
__11.8.2 A Sample Generator
_11.9 Overview of the Math Package
_11.10 A Tour of Math Package Functions
_11.11 Using Special Values (pi)
_11.12 Trig Functions: Height of a Tree
_11.13 Logarithms: Number Guessing Revisited
_11.13.1 How Logarithms Work
__11.13.2 Applying a Logarithm to a Practical Problem
__Summary
_Review Questions
_Suggested Problems

Chapter 12 The “numpy” (Numeric Python) Package
_12.1 Overview of the “array,” “numpy,” and “matplotlib” Packages
_12.1.1 The “array” Package
__12.1.2 The “numpy” Package
__12.1.3 The “numpy.random” Package
__12.1.4 The “matplotlib” Package
__1 2.2 Using the “array” Package
_12.3 Downloading and Importing “numpy”
_12.4 Introduction to “numpy”: Sum 1 to 1 Million
_12.5 Creating “numpy” Arrays
_12.5.1 The “array” Function (Conversion to an Array)
__12.5.2 The “arange” Function
__12.5.3 The “linspace” Function
__12.5.4 The “empty” Function
__12.5.5 The “eye” Function
__12.5.6 The “ones” Function
__12.5.7 The “zeros” Function
__12.5.8 The “full” Function
__12.5.9 The “copy” Function
__12.5.10 The “fromfunction” Function
__12.6 Example: Creating a Multiplication Table
_12.7 Batch Operations on “numpy” Arrays
_12.8 Ordering a Slice of “numpy”
_12.9 Multidimensional Slicing
_12.10 Boolean Arrays: Mask Out That “numpy”!
_12.11 “numpy” and the Sieve of Eratosthenes
_12.12 Getting “numpy” Stats (Standard Deviation)
_1 2.13 Getting Data on “numpy” Rows and Columns
_Summary
_Review Questions
_Suggested Problems

Chapter 13 Advanced Uses of “numpy”
_13.1 Advanced Math Operations with “numpy”
_13.2 Downloading “mat plotlib”
_13.3 Plotting Lines with “numpy” and “matplotlib”
_13.4 Plotting More Than One Line
_13.5 Plotting Compound Interest
_13.6 Creating Histograms with “matplotlib”
_13.7 Circles and the Aspect Ratio
_13.8 Creating Pie Charts
_13.9 Doing Linear Algebra with “numpy”
_13.9.1 The Dot Product
__13.9.2 The Outer-Product Function
__13.9.3 Other Linear Algebra Functions
__13.10 Three-Dimensional Plotting
_13.11 “numpy” Financial Applications
_13.12 Adjusting Axes with “xticks” and “yticks”
_13.13 “numpy” Mixed-Data Records
_13.14 Reading and Writing “numpy” Data from Files
_Summary
_Review Questions
____Suggested Problems

Chapter 14 Multiple Modules and the RPN Example
_14.1 Overview of Modules in Python
14.2 Simple Two-Module Example
____14.3 Variations on the “import” Statement
____14.4 Using the “
all ” Symbol
____14.5 Public and Private Module Variables
____14.6 The Main Module and “
main _”
_14.7 Gotcha! Problems with Mutual Importing
_14.8 RPN Example: Breaking into Two Modules
_14.9 RPN Example: Adding I/O Directives
_14.10 Further Changes to the RPN Example
__14.10.1 Adding Line-Number Checking
__14.10.2 Adding Jump-If-Not-Zero
__14.10.3 Greater-Than (>) and Get-Random-Number (!)
_14.11 RPN: Putting It All Together
_Summary
_Review Questions
_Suggested Problems

Chapter 15 Getting Financial Data off the Internet
_15.1 Plan of This Chapter
_15.2 Introducing the Pandas Package
_15.3 “stockload”: A Simple Data Reader
__15.4 Producing a Simple Stock Chart
_15.5 Adding a Title and Legend
_15.6 Writing a “makeplot” Function (Refactoring)
_15.7 Graphing Two Stocks Together
_15.8 Variations: Graphing Other Data
_15.9 Limiting the Time Period
_15.10 Split Charts: Subplot the Volume
_15.11 Adding a Moving-Average Line
_15.12 Giving Choices to the User
_Summary
_Review Questions
____Suggested Problems

Appendix A: Python Operator Precedence Table
Appendix B: Built-In Python Functions
_abs(x)
_all(iterable)
_any(iterable)
_ascii(obj)
_bin(n)
_bool(obj)
_bytes(source, encoding)
_callable(obj)
_chr(n)
_compile(cmd_str, filename, mode_str, flags=0, dont_inherit=False, optimize=–1)
_complex(real=0, imag=0)
_complex(complex_str)
_delattr(obj, namestr)
__dir([obj])
_divmod(a, b)
_enumerate(iterable, start=0)
_eval(exprstr [, globals [,locals]] )
__exec(object [, global [,locals]])
_filter(function, iterable)
_float([x])
_format(obj, [formatspec])
__frozenset([iterable])
_getattr(obj, namestr [,default])
__globals()
_hasattr(obj, namestr)
__hash(obj)
_help([obj])
_hex(n)
_id(obj)
_input([prompt_str])
_int(x, base=10)
_int()
_isinstance(obj, class)
_issubclass(class1, class2)
_iter(obj)
_len(sequence)
_list([iterable])
_locals()
_map(function, iterable1 [, iterable2…])
_max(arg1 [, arg2]…)
_max(iterable)
_min(arg1 [, arg2]…)
_min(iterable)
_oct(n)
_open(filename_str, mode='rt')
__ord(char_str)
_pow(x, y[, z])
_print(objects, sep='', end='\n', file=sys.stdout)
_range(n)
_range(start, stop [, step])
_repr(obj)
_reversed(iterable)
_round(x [,ndigits])
_set([iterable])
_setattr(obj, namestr, value)
__sorted(iterable [, key] [, reverse])
_str(obj='')
_str(obj=b'' [, encoding='utf-8'])
_sum(iterable [, start])
_super(type)
_tuple([iterable])
_type(obj)
____zip(*iterables)

Appendix C: Set Methods
_setobj.add(obj)
__set_obj.clear()
_setobj.copy()
__set_obj.difference(other_set)
_setobj.difference_update(other_set)
__set_obj.discard(obj)
_setobj.intersection(other_set)
__set_obj.intersection_update(other_set)
_setobj.isdisjoint(other_set)
__set_obj.issubset(other_set)
_setobj.issuperset(other_set)
__set_obj.pop()
_setobj.remove(obj)
__set_obj.symmetric_difference(other_set)
_setobj.symmetric_difference_update(other_set)
__set_obj.union(other_set)
____set_obj.union_update(other_set)

Appendix D: Dictionary Methods
_dictobj.clear()
__dict_obj.copy()
_dictobj.get(key_obj, default_val = None)
__dict_obj.items()
_dictobj.keys()
__dict_obj.pop(key [, default_value])
_dictobj.popitem()
__dict_obj.setdefault(key, default_value=None)
_dictobj.values()
__dict_obj.update(sequence)

Appendix E: Statement Reference
_Variables and Assignments
_Spacing Issues in Python
_Alphabetical Statement Reference
_assert Statement
__break Statement
__class Statement
__continue Statement
__def Statement
__del Statement
__elif Clause
__else Clause
__except Clause
__for Statement
__global Statement
__if Statement
__import Statement
__nonlocal Statement
__pass Statement
__raise Statement
__return Statement
__try Statement
__while Statement
__with Statement
______yield Statement

Index

✦ Subjects


Python;Programming;Scripting;Computers;Software Egineering;Computer Science;NumPy;Regular Expressions;OOP;


📜 SIMILAR VOLUMES


Supercharged Python: Take Your Code to t
✍ Brian Overland 📂 Library 📅 2019 🏛 Addison-Wesley Professional 🌐 English

If you're ready to write better Python code and use more advanced features,<i><b>Supercharged Python</b></i>was written for you. Brian Overland distills advanced topics down to their essentials, illustrating them with simple examples and practical exercises.<br />Building on Overland's widely praise

OOP Concepts Booster: Take Your Coding S
✍ Rakesh Singh 📂 Library 📅 2019 🏛 Notion Press 🌐 English

The one thing that giants like Apple, Microsoft, and Facebook have in common is that they became tech powerhouses by following a simple principle; they constantly capitalize on innovative concepts. If you want to create revolutionary software as they have, then you need to follow in their footsteps.

OOP Concepts Booster : Take Your Coding
✍ Rakesh Singh 📂 Library 📅 2019 🏛 Notion Press 🌐 English

It’s time to level up your programming skills! The one thing that giants like Apple, Microsoft and Facebook have in common is that they became tech powerhouses by following a simple principle; they constantly capitalize on innovative concepts. If you want to create revolutionary software as they ha