Writing Code to run on Python 2.x and 3.x

Slides:



Advertisements
Similar presentations
Python Regrets OSCON, July 25, 2002
Advertisements

Bruce Beckles University of Cambridge Computing Service
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Munster Programming Training
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Walk through previous lecture. TSP Questions: How many tours are there? How do you solve it? How fast can you solve it?
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Unit 2 Expressions and variables; for loops Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Overview  Last week Python 3000 was released  Python 3000 == Python 3.0 == Py3k  Designed to break backwards compatibility with the 2.x.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Week 2 expressions, variables, for loops Special thanks to Scott Shawcroft, Ryan Tucker, Paul Beck, Hélène Martin, Kim Todd, John Kurkowski, and Marty.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Type Casting. Type casting Sometimes you need a piece of data converted to a different type In Python you do a typecast to cause that to happen int(3.599)
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python – It's great By J J M Kilner. Introduction to Python.
Discover Python 3 Slavek Kabrda Presented by
Python 3000.
Information and Computer Sciences University of Hawaii, Manoa
Python Review 1.
Introduction to Computing Science and Programming I
Strings CSCI 112: Programming in C.
Intro to ETEC Java.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Strings (Continued) Chapter 13
Introduction to Python
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Debugging and Random Numbers
Writing Sturdy Python In Three Parts: Unit Testing
CSCI206 - Computer Organization & Programming
Exceptions and files Taken from notes by Dr. Neil Moore
Ruth Anderson UW CSE 160 Winter 2017
Internet-of-Things (IoT)
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Introduction to Python
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Exceptions and files Taken from notes by Dr. Neil Moore
Count Controlled Loops (Nested)
slides created by Marty Stepp
expressions, variables, for loops
expressions, variables, for loops
Variables Title slide variables.
Repetition Structures
ERRORS AND EXCEPTIONS Errors:
(Oops! When things go wrong)
Programming Errors Key Revision Points.
Python Syntax Errors and Exceptions
CISC101 Reminders All assignments are now posted.
Python Basics with Jupyter Notebook
By Ryan Christen Errors and Exceptions.
Introduction to Computer Science
Python Review
Topics Introduction to File Input and Output
Copyright (c) 2017 by Dr. E. Horvath
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Presentation transcript:

Writing Code to run on Python 2.x and 3.x

A Decision Do you want to run on 2.5? 2.6? 2.7? Do you need 3.0? 3.1? 3.2? 3.3? 3.4? 3.5? Pypy 4.0.1 is pretty much 2.7 Pypy3 2.4.0 is pretty much 3.2 Jython 2.7.0 is pretty much 2.7

What's the difference? 2.5 is pretty ignorant of 3.x 2.6 and 2.7 were created to ease the transition to 3.x 3.x has some of 2.x' oddities cleaned up CPython is Python in C, AKA “Python” Pypy is Python in Python with a JIT: very fast Jython is Python in Java: interop with java libraries

Approaches 2to3 3to2 Run on both - we'll mostly talk about this

2to3 vs 3to2 2to3: converting code automatically from 2.x to 3.x 3to2 is the more complete conversion because of strings

These are the main things we'll discuss about running on both Automated tests Tox Six Print: statement vs function Strings: bytes and unicode Try/Except String formatting Imports Division Range

Automated tests Always useful Especially useful when making changes like this

Tox Test code under multiple interpreters this-interpreter

Six (and python2x3) Looks very useful I've not used it, but I started a similar project: python23 Byte strings I recommend you use Six instead of python2x3

Print 2.x: print 'abc' 3.x: print('abc') With a single argument, the difference vanishes 2.x: print('ab%s' % 'c') 3.x: print('ab%s' % 'c') 2.6 and up: from __future__ import print_function

Strings: Bytes In 2.5, byte strings are just str In 2.6 and 2.7, the b”prefix” is bytes, but they're really just str In 3.0 - 3.5, the b”prefix” is bytes, and they act like an array of small integers

Strings: Unicode In 2.4-2.7 (and likely earlier) the u'prefix' is for unicode In 3.0-3.2, the u'prefix' gives an error and unprefixed strings are unicode In 3.3, 3.4 and 3.5, the u'prefix' works again, and is just like an unprefixed, unicode string. Also unprefixed strings are unicode

Bytes, strings and I/O 2.x: os.read(os.open(), len) and open().read() both return str 3.x: os.read(os.open(), len) returns bytes open().read() returns unicode bufsock

Bytes and Unicode and your decision These are among the main determiners of what Python versions you should support

Try/Except: 2.4 and up (perhaps earlier) print(1/0) except ZeroDivisionError, extra: print('oops') This trips some very good programmers

Try/Except: 2.6 and up try: print(1/0) except ZeroDivisionError as extra: print('oops')

Try/Except: both 2.4-2.7 (perhaps earlier) and 3.x print(1/0) except ZeroDivisionError: extra = sys.exc_info()[1] print('oops') # I'm told this is slow on Pypy

String Formatting 2.7 & 3.0-3.5: print("abc{}ghi".format("def")) 2.4-2.7 (and much earlier), 3.0-3.5: print('abc%sghi' % 'def') “'F' strings” Warning: % string operator was nearly removed from 3.x, but it's likely here to stay

Imports try: # python 2 from cStringIO import StringIO except ImportError: # python 3 from io import BytesIO as StringIO

Division 2.x: 1 / 2 is 0 3.x: 1 / 2 is 0.5 print(int(1/2)) print(float(1)/2) 2.2 and up: from __future__ import division

range vs xrange In 2.x, range is always a list, and xrange is an iterator In 3.x, range is always an iterator, and xrange doesn't exist In 3.x, if you need a list, you can use list(range(5)) The author sometimes defines a “my_range” to get consistent semantics, with just a for loop and a yield

Questions? Questions?