Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Outline

Python 2 to Python 3 Migration Example

Documentation

This example demonstrates how to use Codegen to automatically migrate Python 2 code to Python 3. For a complete walkthrough, check out our tutorial.

What This Example Does

The migration script handles five key transformations:

  1. Convert Print Statements

    # From:
    print "Hello, world!"
    print x, y, z
    
    # To:
    print("Hello, world!")
    print(x, y, z)
  2. Update Unicode to str

    # From:
    from __future__ import unicode_literals
    
    text = unicode("Hello")
    prefix = "prefix"
    
    # To:
    text = str("Hello")
    prefix = "prefix"
  3. Convert raw_input to input

    # From:
    name = raw_input("Enter your name: ")
    
    # To:
    name = input("Enter your name: ")
  4. Update Exception Handling

    # From:
    try:
        process_data()
    except ValueError, e:
        print(e)
    
    # To:
    try:
        process_data()
    except ValueError as e:
        print(e)
  5. Modernize Iterator Methods

    # From:
    class MyIterator:
        def next(self):
            return self.value
    
    
    # To:
    class MyIterator:
        def __next__(self):
            return self.value

Running the Example

# Install Codegen
pip install codegen

# Run the migration
python run.py

The script will process all Python files in the repo-before directory and apply the transformations in the correct order.

Understanding the Code

  • run.py - The migration script
  • input_repo/ - Sample Python 2 code to migrate

Learn More

Morty Proxy This is a proxified and sanitized view of the page, visit original site.