Migrate Python from 2.X to 3.X WHO AM I? C++ & PYTHON DEVELOPER - - PowerPoint PPT Presentation

migrate python from 2 x to 3 x who am i c python developer
SMART_READER_LITE
LIVE PREVIEW

Migrate Python from 2.X to 3.X WHO AM I? C++ & PYTHON DEVELOPER - - PowerPoint PPT Presentation

Migrate Python from 2.X to 3.X WHO AM I? C++ & PYTHON DEVELOPER 6,5 years in 20,5 years in CAD&Numeric C++ simulations PHILIPPE BOULANGER 3 years in 19,5 years in embedded Python programming 11,5 years in finance


slide-1
SLIDE 1

Migrate Python from 2.X to 3.X

slide-2
SLIDE 2

WHO AM I?

slide-3
SLIDE 3

PHILIPPE BOULANGER

6,5 years in CAD&Numeric simulations 3 years in embedded programming 11,5 years in finance 19,5 years in Python 20,5 years in C++

C++ & PYTHON DEVELOPER

@Pythonicien

slide-4
SLIDE 4

CONCERNS

slide-5
SLIDE 5

SOME NEWS

▪ 2017 ➢Instagram migrated major part of its code to Python 3 ▪September 2018 ➢Dropbox announced the end of its migration to Python 3 (they began in 2015)!

slide-6
SLIDE 6

TECHNICAL ASPECTS (1/2)

▪ Support for Pyt ython 2.7 will ill stop soon ➢ January the 1st 2020 ▪ Some li libraries are no no more compliant ➢ Django, numpy (2019), etc. ▪Pyt ython 4 ➢ Will arrive in the next few years (2023 ?)

slide-7
SLIDE 7

TECHNICAL ASPECTS (2/2)

▪ Asynchronous programming (a (asyncio) ▪ Consistency ➢ Return generator instead of containers ➢ Functional programming

slide-8
SLIDE 8

DIFFERENCES (1/3)

Python 2 Python 3 print print ‘blabla' print(‘blabla’)

slide-9
SLIDE 9

DIFFERENCES (1/3)

Python 2 Python 3 print print ‘blabla' print(‘blabla’) raise raise IOError, ‘file error' raise IOError(‘file error’)

slide-10
SLIDE 10

DIFFERENCES (1/3)

Python 2 Python 3 print print ‘blabla' print(‘blabla’) raise raise IOError, ‘file error' raise IOError(‘file error’) long long(myvar) 5/2 = 2 int(myvar) 5/2 = 2.5 5//2 = 2

slide-11
SLIDE 11

DIFFERENCES (2/3)

Python 2 Python 3 string unicode str str bytes

slide-12
SLIDE 12

DIFFERENCES (2/3)

Python 2 Python 3 string unicode str str bytes dict, map, zip dict.items(): list dict.keys()[0] dict.iteritems() dict.items(): dict_items list(dict.keys())[0] dict.items()

slide-13
SLIDE 13

DIFFERENCES (3/3)

u’toto’ b’titi’ instruction unicode() method __unicode__() StringIO/BytesIO

UNICODE MANAGEMENT

slide-14
SLIDE 14

FINANCIAL ASPECT

▪ Mig igration costs?

➢ Heavy costs at short term ➢ Few costs at long term

▪ Costs to to keep Pyt ython 2?

➢ No immediat costs ➢ Heavy cost at middle/long term

slide-15
SLIDE 15

GOAL…

slide-16
SLIDE 16

NON-REGRESSION TESTS

slide-17
SLIDE 17

▪ Is the migration a success? ▪ Are the performances as good as the 2.X version? ▪ What is the coverage of the tests? ▪ We need in indicators!

HOW TO VALIDATE THE MIGRATION?

slide-18
SLIDE 18

UNIT TESTS

▪ Use a unit test for a small part of code testing (As a function). ▪ Utopic goal: have unit tests for all API.

slide-19
SLIDE 19

FUNCTIONAL TESTS (1/2)

▪ Functional tests are more complex because several API are linked but cover a real service or functionality. ▪ Objective: cover most of the functionalities as

  • possible. Having a tool to mesure code coverage

will be useful.

slide-20
SLIDE 20

FUNCTIONAL TESTS (2/2)

▪ The need to automate tests is increasing with program size ▪ A functional test could be:

➢ A chain of API calls ➢ GUI actions (use of UFT/QTP)

slide-21
SLIDE 21

PERFORMANCES TESTS

▪ You need to validate that migration keep the application performances : algorithms used in libraries could be replaced between versions, some conflicts between libraries could appears… ➢ Load tests?

slide-22
SLIDE 22

COVERAGE TESTS (1/2)

▪ Knowing the number of lines of codes tested when all the tests are using (unit, functional

  • r

performance)

https://coverage.readthedocs.io/en/coverage-4.5.1a

slide-23
SLIDE 23

COVERAGE TESTS (2/2)

▪ According to my experience, with less than 60%

  • f covered code, the chance of having hidden

bugs is very important ▪ A good target is 80% of covered code

Target 80% of covered code

slide-24
SLIDE 24

GUI TESTS

▪ Test the GUI

  • Either manual
  • Or use tool like UFT (previous name: QTP)

▪ Allow to automate test as if it was done by a user

slide-25
SLIDE 25

HUMAN TESTS

▪ A developer tests the code from a way which corresponds to the implementation he done, a real user tests according to its habits ➢Update GUI controls, click… raise events and code execution and the order of calls can change the behavior ➢human add random part inside tests

slide-26
SLIDE 26

PERIMETER

slide-27
SLIDE 27

PERIMETER?

?

slide-28
SLIDE 28

ENVIRONMENT

OS DB PYTHON DISTRIBUTION TOOLING PROCESSOR

slide-29
SLIDE 29

WHAT MODULES ARE LOADED? (1/2)

▪ Standard modules in Python? ▪ Modules which were developed in intern? ▪ What are the external modules?

slide-30
SLIDE 30

WHAT MODULES ARE LOADED? (2/2)

▪ How to determine the list of dynamically loaded modules… Available since Python 2.3.

slide-31
SLIDE 31

AND THEN: PROBLEMS?…

▪ Is there module:

➢ with ended support or unmigrated?

➢ with modified API? ➢ licencing changed? ➢ library name changed?

slide-32
SLIDE 32

NON-PYTHON MODULES

▪ Problems with C/C++ written modules

➢ C++ compiler migration

➢ porting C++ libraries ➢ tools problems (swig…) ➢ licences, etc…

slide-33
SLIDE 33

MIGRATION METHODOLOGY

slide-34
SLIDE 34

▪ « Divide to reign »: it will be better to migrate small groups of files to minimize interactions. ▪ Create bundles in using module dependencies (have a graph should be useful), internal or external module…

SPLIT IN BUNDLES

slide-35
SLIDE 35

PORTING EXTERNAL CODE (1/2)

▪ External code has no dependency with house-made code: start with them will be a good idea. ▪ Take count of tools: ➢Compiler ➢Integration tools in Python: swig, boost.python, etc. ➢External libraries

slide-36
SLIDE 36

PORTING EXTERNAL CODE (2/2)

▪ Library was ported or not? ▪ API changed? ▪ Licensing changed? ▪ Is there constraints according to the versions of different libraries? ▪ Is source code available ?

slide-37
SLIDE 37

ADD PYTHON 3.X CHANGES INSIDE PYTHON 2.X CODE (1/3)

▪ from __future__ import division

  • PEP 238: Changing the Division Operator

▪ from __future__ import print_function

  • PEP 3105: Make print a function
slide-38
SLIDE 38

ADD PYTHON 3.X CHANGES INSIDE PYTHON 2.X CODE (2/3)

▪ from __future__ import absolute_import

  • PEP 328: Imports: Multi-Line and Absolute/Relative

▪ from __future__ import unicode_literals

  • PEP 3112: Bytes literals in Python 3000
slide-39
SLIDE 39

ADD PYTHON 3.X CHANGES INSIDE PYTHON 2.X CODE (3/3)

▪ Six : six.readthedoc.io

Python 2 Python 3 Six to add 3.X features in 2.X code

slide-40
SLIDE 40

TOOLS (1/2)

▪ 2to3

mathilde@pc-moi~ 2to3 example.py RefactoringTool: Refactored example.py

  • -- example.py (original)

+++ example.py (refactored) @@ -1,9 +1,9 @@

  • from urllib2 import urlopen

+ from urllib.request import urlopen

my_url = “http://pythonprogramming.net” try: x = urlopen[my_url].read()

  • print x
  • except Exception, e:
  • raise IOError, "Error 404"

+ print(x) +except Exception as e: + raise IOError("Error 404")

RefactoringTool: Files that need to be modified: RefactoringTool: example.py

slide-41
SLIDE 41

TOOLS (2/2)

▪ 2to6

➢ Based on 2to3

➢ For compilancy between 2 and 3 ➢ Add __future__, six

slide-42
SLIDE 42

REFACTORING

slide-43
SLIDE 43

REFACTORING (1/6)

▪ listdir vs scandir

slide-44
SLIDE 44

REFACTORING (2/6)

▪ Use generators

slide-45
SLIDE 45

REFACTORING (3/6)

▪ Comprehension containers

slide-46
SLIDE 46

REFACTORING (4/6)

▪ String format

name = ‘‘Toto’’ ‘‘My name is %s’’ % ( name ) # since 1.x ‘‘My name is {}’’.format( name ) # since 2.0 f‘‘My name is {name}’’ # since 3.6

slide-47
SLIDE 47

REFACTORING (5/6)

▪ JIT compiler: numba

slide-48
SLIDE 48

REFACTORING (6/6)

▪ Cache strategy

slide-49
SLIDE 49

TO CONCLUDE

slide-50
SLIDE 50

AND NOW…

▪ Migrations are like children: each of them is different ▪ Split in steps… ➢ After each step, TEST!!!! ▪ You will have difficulties but keep hope.

slide-51
SLIDE 51

ENABLER

CONT CONTACT CT Philippe BOULANGER Python Expertise Manager Philippe.boulanger@invivoo.com www.invivoo.com www.blog.invivoo.com www.xcomponent.com

BORDEAUX Rue Lucien Faure 33000 Bordeaux LONDRES Landsdowne House / City Forum 250 City Road – London EC1V 2PU PARIS 13, Rue de l’abreuvoir 92400 Courbevoie