Supporting to Python 3: An In-Depth Guide
Ready to make the leap from Python 2 to Python 3? ‘Porting to Python 3: An In-Depth Guide’ is your must-have companion for navigating this transition. It’s packed with practical advice, real-world examples, and a dash of humor for good measure. If you’re worried about breaking your code or just want a smoother ride into the future of Python, this book’s got your back.
Contents of the Book
- About this book
- A note on terminology
- Foreword
- Welcome to Python 3
- The time is nigh
- What if I can’t switch right now?
- Python and its versions
- Further resources
- Migration strategies
- Only supporting Python 3
- Separate branches for Python 2 and Python 3
- Converting to Python 3 with 2to3
- Using Distribute to support the 2to3 conversion
- Python 2 and Python 3 without conversion
- Using 3to2
- Which strategy is for you?
- Applications
- Python modules and packages
- Summary
- Preparing for Python 3
- Run under Python 2.7.
- Use // instead of / when dividing integers
- Use new-style classes
- Separate binary data and strings
- When sorting, use key instead of cmp
- Use rich comparison operators
- Comparatively tricky
- Implementing __hash__()
- Make sure you aren’t using any removed modules
- Testing coverage and tox
- Optional: Use the iterator-methods on dictionaries
- 2to3
- Using 2to3
- Explicit fixers
- Distributing packages
- Running 2to3 on install
- Supporting multiple versions of Python with Distribute
- Running tests with Distribute
- Running 2to3 with Distribute
- Common migration problems
- Incorrect imports
- Relative import problems
- Unorderable types, __cmp__ and cmp
- Sorting
- Sorting Unicode
- Bytes, strings and Unicode
- Byte literals
- Binary data in Python 2 and Python 3
- Nicer solutions
- Manipulating binary data
- Reading from files
- Replacing UserDict
- CSV API changes
- Running doctests
- write() has a return value
- Types are now classes
- Handling expected exceptions
- String representation
- Unicode representation
- dict and set order
- Improving your code with modern idioms
- Use sorted() instead of .sort()
- Coding with context managers
- Advanced string formatting
- Class decorators
- Set literals
- yield to the generators
- More comprehensions
- The next next()
- New modules
- Abstract base classes
- multiprocessing and futures
- numbers and fractions
- Supporting Python 2 and 3 without 2to3 conversion
- Supporting the print() function
- Handling exceptions
- Import errors
- Integer incompatibilities
- More bytes, strings and Unicode
- Two times three is “six”
- Migrating C extensions
- Before you start
- Object initialization
- Module initialization
- Changes in Python
- Strings and Unicode
- Extending 2to3 with your own fixers
- When fixers are necessary
- The Parse Tree
- Creating a fixer
- Modifying the Parse Tree
- Finding the nodes with Patterns
- Language differences and workarounds
- apply()
- buffer()
- callable()
- Classes
- Comparisons
- coerce() and __coerce__
- Dictionary methods
- except
- Exception objects
- exec
- execfile
- file
- filter()
- Imports
- Indentation
- input() and raw_input()
- Integer division
- long
- map()
- Metaclasses
- .next()
- Parameter unpacking
- raise
- range() and xrange()
- repr() as backticks.
- Rounding behavior
- Slice operator methods
- Sorting
- StandardError
- String types
- Reorganizations and renamings
- The standard library
- urllib, urllib2 and urlparse
- Removed modules
- Moved builtins
- string module removals
- Function and method attribute renamings
Book Description
Moving projects from Python 2 to Python 3 can feel like juggling flaming torcheswhile riding a unicycle! ‘Supporting to Python 3: An In-Depth Guide walks you through every twist and turn of the migration process. Whether you’re working on a legacy project or just curious about what all the fuss is about, this guide helps you dodge the most common pitfalls and get your code running smoothly on modern Python.
Book Overview
This guide doesn’t just list changes; it explains why they matter. You’ll get hands-on tips, practical strategies, and even some laughs along the way. The author breaks down complex concepts into bite-sized pieces, making it easy to follow even if you’re not a Python guru yet. If you’re looking for more foundational knowledge before diving in, check out this beginner-friendly resource on coding in Python 3. Together, these books cover everything from syntax updates to best practices for writing future-proof code.
Why Read This Book
Let’s be honestupgrading code can be a pain. But sticking with outdated versions is even worse! This book keeps things light and approachable, so you won’t fall asleep halfway through. Plus, you’ll find lots of practical examples that actually work in the real world. Want to take your skills beyond the basics? You might also enjoy exploring advanced Python topics and techniques that build on what you’ll learn here.
Who This Book Is For
If you maintain legacy code, work on open-source projects, or just want your scripts to survive the next decadethis book’s for you. It’s written for developers who care about clean code and smooth upgrades (and maybe hate debugging cryptic errors as much as I do). Even if you’re new to porting projects, you’ll find clear explanations and actionable advice throughout.
What You Will Learn
- The key differences between Python 2 and Python 3
- How to identify compatibility issues in your codebase
- Step-by-step strategies for porting small and large projects
- Best practices for testing during migration
- Troubleshooting tips for common migration headaches
- Tools that make porting easier (and less scary)
- Writing code that works seamlessly across versions (if you really have to)
- How to future-proof your Python projects for years to come
Leave a Reply