Bestand wählen

Support Python 2 and 3 with the same code

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Erkannte Entitäten
so that good morning everybody Chevron job there is the Python is full of 15 years he has written articles and that the book on Python also he's a regular speaker at that Linux and Python conferences so and also he maintains that FTP you until the library which is quite handy thank you for
and today he's going to discuss the problems and backs practices for maintaining code for Python 2 and 3 and it's going to be able made of 35 minute stock and we have 5 minutes for puny afterward so please long-term Chevron
few thank you very much wonderful good morning some yeah I wanted to sort out what is support on and 3 with the same code may be helpful interactions something about
me out if something's already mentioned uh but uh and look at the URI in chemical engineering but in 2000 I kind of switched sides I mean programming since I was 15 before fall and then in 2000 they became a full-time software developer since 2 thousand 5 I'm self-employed meant I in in office if the client library and the starting point for this talk was down that was myself in the situation when user is also called like and we support and if if you and there was 1 ticket and then at some point there was a question the mailing lists and but a had shied away but from this and dance so I went through OK yeah last year we had this all I had to play the beauty of 3 0 reduces with Python 3 years in addition to all to 6 and 2 7 OK now apply to apply and and I think most in the audience who have actually have replicas of this is from the Latin site when you go to Donald section and that's something to apply them 3 and this is the page you get when you click on that link also at the test pattern to with this legacy Diamond doesn't really say you shouldn't use Python to work anymore but what unites strongly in that direction and 3 is present future of the language OK so is applied to obsolete for those of you who made this death and on the other hand it is and because of the where you want to use uh there's lots and lots of legacy code and um and the gap widens was also go down the tree and solving the the right when you say a young thought high from our attitude install python you get to an orange even preinstalled usually on Ubuntu and then and also the on the record OK implied 3 is optional uh Python um greater enterprise the most of the yet getting them our dynamic they have applied prepackaged now but that became and also if you want to host if you by hosting and don't do the thing yourself that most small hosting office available for plant until I think and as many libraries don't have applied in 3 Russian yet and the are I I guess that's the reason why some of you are you can have because you want to change that OK so my recommendation is to use Python 3 if you can't and um 1 thing I find very important this also if you will migrate or adapt the library you use the transition for all those who stay up here I need I still need your library to migrate myself always the myself myself therefore Python 3 OK there are different approaches to the 1st 1 of them died in the beginning when applied 3 came out at the recommendation of from the applied on development team was used to to 3 right so a Python tool called convert this to 2 2 with 2 to 3 with this command line tool would um then laid out there was some 3 to tools and 2 with an I think it's rarely used but the idea is that you'll that Python 3 close called usually looks a bit cleaner and it's nice to be able to maintain tight and we'll call it then to maintain my OK but what I'm going to use the Python word ended up doing mostly doing as a developing by having the same code for Python 2 and 3 and so you don't have to to work at run 2 to 3 um neither of by the user needs to run the smaller do you would during development if you want to test and part for Python 2 and 3 so that's just always the same source code yeah 1 major problem and on the OK I mean there are many things if you look at what's new and and Reese year-old there's lots of stuff like that but I think the most important thing when it comes to this adoption of the cold pipe and re-use despite the is unique 0
and so I want to just say maybe a refresher um so
some some something on this OK so we have in young adult Titan 2 and 3 we have got bites of bytes of a byte strings I think both Python 2 terminology is mostly by strings and impact freely when you read documentation that satisfy this property by extremists out so because the intention is you don't use it for strings of not for correct data only for encoded corrected data OK and so these are the we are kind of robots that you lose storm discourse and always look at when you need to make a decision now my told that it needs to go somewhere and I need to encode it and on the other hand and unique texts presence correct data well and characters have from a cold cold points and a and this is unrelated to all the correct as salt so it's not just another and correct encoding like that 1 also was some code page I I don't know an but some of the characters and I'm not useful points and uh at this point it's unrelated tool babies find how this is later represented and bytes OK so this is the context can be encoded 2 bytes no you can choose an encoding yeah of course the encoding so suppose the correct if you have in your string and unique ring so if you have for example Chinese characters to can't this and called selected 1 and this is the uh at the bottom of the page this is just 1 example of this would be the unique all cold points for this German about room to here uh and and this is come in and the bites if you encode this to the called string to UTF-8 this 1 by becomes these 2 wives otherwise it's unchanged but this depends completely on the decoding on the on the encoding you you apply um if something
that might be confusing while you are working on this adoption Python 3 is that both Python 2 and 3 called default string type so that you would like a bike literally in Python 3 this is the byte string type the binary type here and in Python 3 it's actually the unique code size type a textile and I also have yonder the instinct Pakistan bytes and Python 3 and unique called uh equal type in part can 0 here 1 major change and this is your again 1 of the difficulties fall pipe and we support often is in Python 2 you can and just yet and byte string and a unit called string out prefix and um mostly of works less of despite string contains anything known as the then you get a unique cold the cold air on um dependent on the process data sold sold 1 day you run the cold environments fine and do it the other day you read a text file which has and will notice something or some special corrector and you get an exception and patterns we this this change and economy you don't have these implicit conversions animal get a tighter ahead every time you want to at the byte string and string which I think most is a good thing but yeah but but it's makes it this migration on the seduction pardon 3 uh in Python 3 you have to be explicit and so for example if you provide string which has this the prefix we need to be coded um explicitly then this becomes the unique string and this this entire country in and this is the pattern unique ordered red OK but in cold in actually code you shouldn't usually do it like this so what I will talk about the state of OK but also in Country the I mean but try to find the only thing that I find logical and that makes sense is that everything almost everything which took an um strings on byte strings in Python 2 requires unique strings soul in Python 3 this is again part default this is a unique code string this works but if you passed the construct of decimal bytes string complaints and it is time there also a new defined API and Python 3 beyond that and the other 1 thing is that and if you 9 Python 2 if you open a text file uh it's it's the it's a byte strings ifyou open a text file for reading and it always gives you byte strings and young 1 whereas in Python 3 is unique all strings using and Python to adjust us whether you choose text binary only changes line ending conversions and there's also normal final objects the return value also open depends on the arguments of the open call the good thing for those migration opposite option for Python 3 is that this um open function which is forbidden often in Python 3 is also available in the I O model and piping 6 and 2 7 the 1 thing that struck me up in another just want to where it is that there is some evidence that not all at and argument values answers the on the court strings now or I mean system in read gets unique called strings and right requires court argument that you cannot run by using the bonfire tributes which gives you the file object which and works with robots of the binary data OK so that
young so no most of Introduction now and you know that's how you actually do this migration some tips all that's the and OK you should have complemented to unit tests possible unit test and I like this primarily by uh on I think Tuesday and the way in which also mentioned approval testing now which as also if you don't have unit test but I I recommend you have them and if you don't have unit test so you should have at least have some ultimate the test that you can run on your code and the cold so that the unit has pass 100 % apartment to tool and since you are just starting out forming by we can't expect that have a work on Python 3 actually a lots of of these tests will fail if you just do the experiment and you have unit tests for Python 2 words meant to run the same test with 1 3 you probably get lots of failures and also OK that's completely normal also sometimes I and I noticed that something if that most of these various come from a string literals in the test fold in the unit as and sometimes do another thing is that sometimes you only need to change a few functions and have which can and do the conversions in your code and maybe even then lots of tests pass again even on the part of B you can also do this test or try running time even the pattern to worship but with the option of minus 3 this is supported in part and I think from Python 2 6 up uh will give you some information would print some mornings or information on things that need to change from 1 3 you know why you are changing the code for all to adapt to Python 3 now make sure all the and and the guy you have to changes to the actual called the production code and the tests and keep them in sync and soul did so so you don't have died you can not that many of the test OK I wanted I have found this nice coupon I I can imagine many of you will know this already this is Fox um nice because it you can't use the rate is around python 2 and 3 year in the test also a Python and 3 and you so so you can check if they still work Python to when they they already worked up to is to which extent they work for my country and I also like that the uh . implicitly checks whether the packaging works for you the library of for for your cold OK since we
are going to support heightened and 2 and 3 with the same code in you run did I mean to do that we use the use useful but there I recommend running at once 1 soul uh for example print became a function and some exceptions and yard changed pipe and you be quiet exceptions and that and 2 to 3 years does many of these are straightforward conversions so in this way it's nice um you should and have a look at the the documentation on for tool to 3 and on the on the about the fixes and that's also dance different steps this different conversion that can be applied to low so it's not just everything or nothing but but you can yeah yeah individually at time on certain conversions and you should exclude the future fix because this removes all from future our imports statements what you don't want you want to want to keep them from future imports and boilerpipe to common and cold many of the changes would be in as you don't want to keep them literally and the print conversions probably want want to keep as they are and there's some changes for example uh changed uh module names for example conflict past and pipe which was spelled with a capital C and capital key is small lowercase in Python 3 total 3 and it expects that to that Python 3 is final target destination it removes of just changes the import to all lowercase conflict parser but if you look at the changes that 2 to 3 bits you see yeah you would therefore the version control system and you see what changes so is for example you see the change import statement and you need to take care of this have different cold or switch or something later for Python 3 yeah young to really check all the changes and 1 someone I talked with that I think yesterday also um dimension and or suggest that running pixels individually and sold with each day you see the changes only from this fixed and of course this requires that you will know all the fixed us I mean you can get this information but the idea to make sure should make sure that you don't forget any of them OK and after he into 2 2 3 and and only major changes everything and at this point should run again and to
it OK and so if you're lucky and you already have an API that um not
that you can keep for quiet and 3 all doesn't make you jump from if you want to support python 23 I was not so lucky with FT so and I had a young guy it had to change API is to support item 2 and 3 so the new with 5 to Q 2 3 is not better because compatible with the Q to 8 which was previous versions um OK and the standard value Python tool and got almost everything that accepts a string accepts identical of lights and in Python 3 would break sections you have to use in the court for strings In my suggestion there was use unit called for text data if you have to be if you want to decide and should you should decide on on unit called for text data not keep the the bytes interface that you may have a ball python and you need to know all of die even defined what data is fixated on the outside that sometimes and color cases that way now you really have to think or a maybe even define what where something is supposed to be unique code you 1
recommendation encode and decode text that system boundaries and so every round this example is reading of file from from the file system and decoded and later if you for example you want to there's always something to some other the most you and coat this but only then also you should try to have most of your code you with on strings so if you will you I as far as strings are concerned I mean of course the other types on the fact that yeah so if you look at cold so you don't have to thing IronPython to with this will do that thing on this case I would have liked strings and when I run this on heightened read these strings but I think they will be unique old strings or something like that so you should avoid this I should try to um if you have strings in your coat and not try to get to the point that yeah you can confidently say that don't know the locations where it's unique code and went to solve OK
I sometimes it can get down the hot uh for example in the putative nominee image shows something the this is called
you can write that the future it's mostly is sitting on top of and the arc and encapsulates the FTP FTP live FTP uh but it's more high-level now you can uh right that the intention is that you can write code as if you were using the OS module all sh to module always particle and even you can you can use Locke and this is beyond source objects who supported the with statement of support that you can use this fired up and out and have these convenience methods like Donald for example and you can also open remote file yeah and should reach read from remote files for example right to remote fights you can
form here since the the L 1 difficulty with 50 putative was that I don't only have to work converge between bites and unique holds for the full 5 content for the remote fights but the heart of plant was dealing with the with the encoding of filenames of directory names and I send them all was something and I also check
of it cannot be solved some sections so what you see on the next slide has this upper right corner and on the other side of the that you see the this spot Council for example
I made this diagram and attach notes and young hold these interfaces had a fumble Python 2 and 3 to 2 to wreck my mind from those and then get this straight away should deal with this this the the other part
well I mean since I'm using FTP learn how those FTP that kind of this and Python tool and Python 3 where are the conversions because uh the string arguments fall finance and that an FTP the uses of it also requires unique hotel and so I was wondering how how much through how do they encode dissolve well how do they know the encoding of something else when I want to find the sentence to the FTP this finding so it really depends on your project complicated that sometimes maybe it's straightforward then you're kind of lucky I mean doing even more likely it is you can keep your API and only make internal changes but especially if you need to change the API you can't come up with a keen on the idea that that is still expected by the user or know that that a user can easily work with important when we have you need to think about again and that's really the part that can't be all that matters some similar
don't that functions all methods except both from the cold all byte strings something like the i if I get byte string I convert this to unique cold or if I get a unique code string icon that was the light string or something because this makes the API confusing and you always have to think about this this now under which conditions this survived string unique called string and and also makes the test more complicated now because you always have to make a check both um and this have to take all fall code and byte strings for the arguments and imagine you have something that takes 2 or 3 strings and you want to test all combinations maybe even more so you should try to toward those um In the special case of like objects or strings for parts uh because in both in Python 2 and 3 you can use all these API that except fine of directory names with either byte strings and unique code strings and the young under the will be called different API is operating system so and this also gave me some headaches with executed because I do this as well and sold in this case I I have to accept both unique port of odds and do different things and because I tried to mimic the from for example the always module OK if you would yeah but if you don't except the strings but let the except highlight objects so this is handled the fall so it's not part of the library anymore so if you can get away with this is that due to problems with to use finite objects on instead of accepting financed things I mean you can of course still have this for user convenience but this makes the heart and soul and also avoids a different API so Python 23 for example for life to future I was thinking at some point for backwards compatibility when you run on Python 3 and I open a text file for reading and it should give you a nasty fall to be backwards compatible and if you bite strings in Python 3 if you use the courts things because we expect this on Python 3 have but this would really be a mess so I I made up my mind and and oral summaries of the advantages and disadvantages simple this statistical common pipe and what do you think how should I deal with this and uh I got to answers uh saying explicitly go for the unified API even if it breaks completed How can I think this was a very good decision to make yeah also make a list of changes before actually actually changing the API on because this would double fully make sure that you don't forget to change and that is that they need to change different parts of the code and they don't want to get forget something and it also helps you write somebody's notes for example 5 2 q rolled and what's new and executed trees role so I would say I could take this this and make sure I don't forget anything and you could also use commit messages but um commitments as I usually more fine-grained and but maybe not so useful for this purpose yeah and another 2 and united and if you need to change the API the major version of the 1st part and young and yeah I mean it's after all adding part 3 support is a major change and so and so you can get away with with the API changes so I think it's justified I mean it's not just a trick but I I think that's fine
looking at some other tips and generated on this applied the
induction and read what's new in Python 3 at least I actually recommend you also I mean not instead but also in read this pointing to Python 3 I have links at the end of the slides and I would the slides online uh but even if you just search on the net for wanting to Python 3 you will probably get it this way right and this is practically and online book which is really nice yeah I also support only to 6 and up because Python 2 and Python patterns 6 and 5 7 have some very useful pipe and Ricci feature vector for example you can save from future import print functions and you will have print as a function of the same behavior as in 5 3 also would be exceptions and solar up starting from Python 2 6 you can write except Exception class as exceptional objects I can't do this in Python 2 5 and if you need to exceptional objects for exception handling and want to support to 5 no oceans gets really messy and it's not really nice to this and front I guess I was so Python 2 6 1 2 7 have this I O model so you can just you can say um if you want to and from BIO import open and then you have be open the now like appeared in open function from 3 but have it available in Python if you need to support 5 2 5 Japan use the 6 library and and yeah and sold giant summary anything behold to 6 probably the output to support the looking things the OK that there will still be some things that need to be different for Python 2 and 3 and I recommend and you know they are not the only person recommending this Solomon that true to user content module for example all pipe on
for I think you turn it looks
like this and you just say if for Python tool and if the pot conversions button to by the way um use this indexing if you want to run you called on Python 2 6 because entitled to send you this is the name to and you can write about major but this doesn't work on part 2 6 and during the entire to fulfill the entire to type Whiteside now this is much easier than reading the cold and then trying to read remember our regenerate and all on likened to work and they see I'm seeing the bytes type something and this yeah electoral the and if you have a larger
project and constant also have a look at the future of 6 libraries I think the future library looks at the
mall no more than that it's actually a new then the 6 library and uh for in the future I decided to not use it because if computer doesn't have any dependencies that apart from the standard library and I didn't want to introduce dependency just for these few things that you saw in my on book by OK and tool and die for every Python 5 minutes at least what I suggest that these are some changes which make a python 2 cold behave more like and we call for example the absence of student also require and the field division of the integer division or the has changed from Python 2 to 3 and for example cues from future import division you get this type 3 behavior for integer division I already mentioned the print function unique up with when you do a function from Portugal but there's also the literal strings and you called and even on a Python tool will become unique called strings in called string utterances the the attainment of you can use I have all of this died at 150 equal present Python and 3 3 and up is that you prefix this was removed and Python here all but was reintroduced in 3 3 the but you still have to know what string type address on and yeah and it's very event of taste and whether you will generally used the court that rests with import or if you use the prefix explicitly
OK and then summary and so python 2 were still in wide use but young I recommend using or developing for Python 3 if you can we are using the same source code followed to support client 23 years so feasible makes sense but also larger projects doing this string of for example have gone this way and have the same source called 1 2 and 3 and you need to know the concepts of funicle points and encodings and the changes from Python with 3 so now again what's new in patent B C role and of wanting to yeah you should have has for adapting to 3 otherwise it's much more difficult you should at least have some test even if you you have an idea what kind of acceptance as sort of prove disapprove test that Emily mentioned there you should prefer API is important more right model pipe on planned and implemented silly API changes carefully I mean as you would design a I mean if you designed your API for your library from scratch maybe even who would do what makes sense for quite 23 now I already mentioned reading what's new and countries year all and then your life you can if you can actually use require and atleast 5 and 6 because this will give you several of these future imports and um you don't that have if you require if you want to support identify for example in your you have to write some convoluted called maybe yeah OK it will be more thank you
very much for the you have a question please
raise your hand and now I will come by the microphone and we the most of questions from on the approaches were also maintained both questions reasons I wasn't very well on its that they wall feasible in Golden gives you that you give something up to give some of the features of by 3 or so precise answers by longtime struggling to find out of those increments in bars 3 of the admittedly with 1st
wasn't you where you can really raise so you you you you catch exception you want to raise another 2nd with the same respect the original 1 and you don't like the race exception Common you the the Digimon event is going to be a OK in Python 3 of what you probably knew this you already have this and this change exceptions and in Python to I think the values you can use an additional comma-separated
document fall to to give a traceback object or something that but if you're in the end you have to do it just kind of manually and then setting these X-ray tributes and that's also 1 thing I thought about it the you all of the sort
of antiviral told was really interesting 30 what about what about according to the directions and he was overseas versions like for by law and so some of the the and
I have migrated or adapted and easy extension so far so come out here what from what I see that changing as the sentence is much more complicated it is but there is a document Egyptian Python I think it's in the directory they give you guidance on porting C extensions from 500 by 3 the parable question think of a term that's used last questions thank you for the I think right now you 1 of the most competent persons for the whole thing in the world economy tensions that could have happened to what we call the your FTP utility library again starting from scratch all the knowledge you know what do you measure it take a by means of presentation to to do it again with more than 1 additional I can read maybe we go on maybe less I I I really don't know I mean I mean this was stretched out a lot of several weeks because this is the free time getting ready to find all these things out you get out here and I forget getting OK and that's then I have this habit of reading a lot of stuff before I start so as I did some research people a for example this kind a complex modulus is also mentioned by find some blog posts or something this recommendation and but I think it makes sense anyway um yes the so if I would say it takes several person-days if you just mechanically apply all these things OK some of the some of the got kind of mechanical changes can be done by 2 to 3 so this is a key steps and I find it very useful to run 2 to 3 and not not only got because it I mean you should read what's new in Python 3 but running 2 to 3 on your code will also give you some insight on maybe some things you forgot what when venue will affect the what's new documents here so it was here that change some things and and you might wanna know what to do change their the something obviously if this seems to be something different and Python 3 about and embarrassed by 2 of the big things lordship on few
Domain <Netzwerk>
Schreiben <Datenverarbeitung>
Hinterlegungsverfahren <Kryptologie>
Einheit <Mathematik>
Zellulares neuronales Netz
Lesen <Datenverarbeitung>
Objekt <Kategorie>
Bus <Informatik>
Installation <Informatik>
Konvexe Hülle
Binder <Informatik>
Offene Menge
Lateinisches Quadrat
Prozess <Physik>
Statistische Schlussweise
HIP <Kommunikationsprotokoll>
Komponente <Software>
Lineares Funktional
Konfiguration <Informatik>
Arithmetisches Mittel
Automatische Indexierung
Projektive Ebene
Faltung <Mathematik>
Web Site
Physikalisches System
Operations Research
Bildgebendes Verfahren
Universal product code
Physikalisches System
Umsetzung <Informatik>
Objekt <Kategorie>
Brennen <Datenverarbeitung>
Offene Menge
Umsetzung <Informatik>
Gesetz <Physik>
Befehl <Informatik>
Kategorie <Mathematik>
Güte der Anpassung
Kontextbezogenes System
Rechter Winkel
SOLOMON <Programm>
Lesen <Datenverarbeitung>
Klasse <Mathematik>
Migration <Informatik>
Inhalt <Mathematik>
Demo <Programm>
Elektronische Publikation
Maschinelles Sehen
Web log
Formale Sprache
Einheit <Mathematik>
Prozess <Informatik>
Funktion <Mathematik>
Inklusion <Mathematik>
Folge <Mathematik>
Mixed Reality
Funktion <Mathematik>
Gewicht <Mathematik>
Interaktives Fernsehen
Kombinatorische Gruppentheorie
Elektronische Publikation
Formale Sprache
Avatar <Informatik>


Formale Metadaten

Titel Support Python 2 and 3 with the same code
Serientitel EuroPython 2014
Teil 57
Anzahl der Teile 120
Autor Schwarzer, Stefan
Lizenz CC-Namensnennung 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen.
DOI 10.5446/20034
Herausgeber EuroPython
Erscheinungsjahr 2014
Sprache Englisch
Produktionsort Berlin

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Stefan Schwarzer - Support Python 2 and 3 with the same code Your library supports only Python 2, - but your users keep nagging you about Python 3 support? As Python 3 gets adopted more and more, users ask for Python 3 support in existing libraries for Python 2. This talk mentions some approaches for giving users a Python 3 version, but will quickly focus on using the very same code for a Python 2 and a Python 3 version. This is much easier if you require Python 2.6 and up, and yet a bit easier if you require Python 3.3 as the minimum Python 3 version. The talk discusses main problems when supporting Python 3 (some are easily solved): * `print` is a function. * More Python APIs return iterators that used to return lists. * There's now a clear distinction between bytes and unicode (text) strings. * Files are opened as text by default, requiring an encoding to apply on reading and writing. The talk also explains some best practices: * Start with a good automatic test coverage. * Deal with many automatic conversions with a one-time 2to3 run. * Think about how your library should handle bytes and unicode strings. (Rule of thumb: Decode bytes as early as possible; encode unicode text as late as possible.) * Should you break compatibility with your existing Python 2 API? (Yes, if there's no other way to design a sane API for Python 2 and 3. If you do it, raise the first part of the version number.) * Try to keep code that's different for Python 2 and 3 minimal. Put code that needs to be different for Python 2 and 3 into a `compat` module. Or use third-party libraries like `six` or `future`. Finally, the talk will mention some helpful resources on the web.
Schlagwörter EuroPython Conference
EP 2014
EuroPython 2014

Ähnliche Filme