Git Product home page Git Product logo

supporting-python-3's Introduction

Supporting Python 3

https://api.travis-ci.org/regebro/supporting-python-3.png

This is a free book about how to make your code support Python 3.

It costs nothing, and you can both publish it and contribute to it!

Reading this book

Supporting Python 3 is available for free on http://python3porting.com/ . You can also download a PDF version from https://gumroad.com/l/python3 for any price you like, and you can buy a paper version from https://www.createspace.com/4312357 for $4.45, which is the printing cost of the book, and hence as close to free as you can possibly come.

As of today, the name of the book in all these places are "Porting to Python 3", which is the old name of the book before I made it into a community effort. This will change sometime during 2015, and the above links will also change as a result.

Contributing to this book

To contribute to this book, fork it on GitHub.

If you are making any changes or additions that fall under copyright law, you must first sign the Contributor License Agreement.

Follow the instructions in INSTALL.rst on how to install it, and make the changes you like, and create a pull request.

It's probably a good idea to talk to Lennart Regebro before making major changes or additions.

License

This book is (c) Lennart Regebro 2011-2015, and is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

See http://creativecommons.org/licenses/by-nc-sa/3.0/

This means you can publish this book or parts of it, but not in such a way that you make money from it. Should you want to use this book in a commercial setting, please contact Lennart Regebro, [email protected].

The book in it's current form uses Flux Bold, which is a commercial font. If you want to publish this book in any form, you need either to replace this font, or buy a license for the usage that you intend.

Generating the PDFs

Making a PDF is not just a matter of doing make pdf. Doing so will indeed create a PDF, but you need to create four PDF's when releasing the book.

You need:

* A print PDF, 6" x 9", black only.
* A screen PDF, 8.27" x 11", with syntax highlighting.
* A tablet PDF, 7" x 9", with syntax highlighting.
* A phone PDF, 4.8" x 7.2", with syntax highlighting.

You do this by changing the conf.py. For the print PDF, the pygments_style should be "none" otherwise it should be "sphinx".

To select the paper size, change the used variable in print_latex_elements between print_form, screen_form, tablets_form or phone_form.

You don't need to do anything more for the Print PDF, it's done, but the other PDF's should have the front and back covers merged. This can be done with tools like pdfunite:

$ pdfunite covers/PhoneFront.pdf build/latex/SupportingPython3.pdf \
    covers/PhoneBack.pdf SupportingPython3-phone-1.0-dev.pdf

An additional hitch is that each release of Sphinx or texlive will have changes that break the PDF generation, so it's highly unlikely that this book will generate cleanly on your computer. It works on Ubuntu 14.04LTS, but not on Ubuntu 16.04, for example.

You have to install at least the following packages to build the book:

``texlive texlive-xetex texlive-latex-extra lmodern fonts-texgyre
  fonts-linuxlibertine python-sphinx poppler-utils``

EPUB Generation

It is also possible to make an EPUB format of this book with the command make epub - note that at the current time this will lack the front and back covers.

The resulting eBook, in EPUB format, will be placed in the file build/epub/SupportingPython3.epub _or it's windows equivalent.

HTML Generation

It is also possible to make both standalone HTML and HTML directory, (directories with index.html starting points), formats of this book with the commands make html and make dirhtml - note that at the current time these will lack the front and back covers.

The resulting books will be placed in the directories build/html and build/dirhtml respectively.

supporting-python-3's People

Contributors

brettcannon avatar cavokz avatar do3cc avatar gadgetsteve avatar kwadrat avatar lincolnq avatar markdoliner avatar michaeljoseph avatar mkcor avatar regebro avatar staffanm avatar turbo87 avatar wooble avatar zackse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

supporting-python-3's Issues

sys.version should not be used for version checks

The string sys.version should not be used for version checks as there is no guarantee across implementations that the version number is at the beginning of the string. Instead sys.version_info should be used which is a tuple of integers.

No mention of Py_FindMethod getattr/setattr for Python extensions

Your book, especially http://python3porting.com/cextensions.html, has already been very helpful to start porting rdiff-backup to Python3, but I'm stuck on the disappearance of Py_FindMethod as used in rdiff_backup/_librsyncmodule.c. According to my research:

  1. the Python documentation isn't too explicit about what this function was used for (no examples)
  2. the subtle difference between tp_getattr/tp_settatr and tp_getattro/tp_setattro could be clearer (beside the fact that the first ones are outdated).
  3. there are two cases: you can use the PyObject_GenericG/SetAttr or not, but the criteria for this decision are unclear to me.
  4. If you can use the Generic functions, you don't need Py_FindMethod so that's easy, but there are no explanations if you can't (which seems to be my specific case) on how to get rid of the method without breaking your code.

So quite a lot of missing explanations, I think, which you could possibly highlight in an update.

Crowdfunding promise delivery

These are the things that I promised to do in the crowd funding campaign, https://github.com/regebro/supporting-python-3/issues/new

  • Clean up the code (done)
  • Document how to generate the HTML for the site (done)
  • Move the code to Github (done)
  • Rename the book into "Supporting Python 3" (done)
  • Update the "About this book" page to reflect the books community status and contributors. (done)
  • Create a contributor licensing scheme
  • Set up automatic testing
  • Set up automatic generation of HTML and PDF's and publish

De-emphasize 2to3

2to3 is today only relevant if you still need to support both Python 2.5 and Python 3.2.

Seems that 2to3 doesn't support issue with Exception object (A.9)

Section A.9 says that the issue with Exception objects that are not iterable and indexable is supported by 2to3.
But it seems that it doesn't. I tried the code presented as the example:

e = Exception('arg1', 'arg2')
e[1]

and found that it is not converted by 2to3. This fix is not mentioned neither in documentation, nor in the code of fix_except fixer.
Maybe am I missing something?

The current README.rst does not mention epub generation

Currently the README.rst goes into great depth about pdf generation in a number of page layouts but does not mention the fact that the makefile includes targets for html, dirhtml and epub.

This gives the impression that only pdf output is available and that anybody looking for epub support is going to have to do it themselves.

Python 2-style `round()` implementation is not always correct

Thanks for this guide, I've found it a really useful resource when porting some old Python 2 code ❤️

One thing I did notice: the round() substitute given for Python 2-like behaviour in Python 3 is close, but fails to give the exact Python 2 behaviour in a few cases.

For example:

Python 2.7.18 (default, Jul  1 2022, 12:27:04) 
[GCC 9.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> def my_round(x, d=0):
...     p = 10 ** d
...     if x > 0:
...         return float(math.floor((x * p) + 0.5))/p
...     else:
...         return float(math.ceil((x * p) - 0.5))/p
... 
>>> my_round(353.765, 2)
353.77
>>> round(353.765, 2)
353.76

Another implementation that I found seems to be a slight improvement. It is also tested against examples taken from the Python 2 test suite.

>>> round2(353.765, 2)
353.76

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.