Skip to main content

The simplest way to write one program that runs on both Python 2 and Python 3.

Project description

Pies

PyPI version PyPi downloads Bitdeli Badge

The simplest (and tastiest) way to write one program that runs on both Python 2.6+ and Python 3.

Let’s eat some pies!

Installing pies

pip install pies

or if you prefer:

easy_install pies

Overview

Pies is a Python2 & 3 Compatibility layer with the philosophy that all code should be Python3 code. Starting from this viewpoint means that when running on Python3 pies adds virtually no overhead.

Instead of providing a bunch of custom methods (leading to Python code that looks out of place on any version) pies aims to back port as many of the Python3 api calls, imports, and objects to Python2 - Relying on special syntax only when absolutely necessary.

How does pies differ from six?

Pies is significantly smaller and simpler then six because it assumes for everything possible the developer is using the Python 3 compatible versions included with Python 2.6+, whereas six tries to maintain compatibility with Python 2.4 - leading to many more overrides and further into different language territory. Additionally, as stated above, where possible pies tries to enable you to not have to change syntax at all.

Integrating pies into your diet

Using and integrating pies into an existing Python 3+ code base (to achieve Python 2 & 3 dual support) couldn’t be simpler:

from __future__ import absolute_import, division, print_function, unicode_literals

from pies.overrides import *

Then simply write standard Python3 code, and enjoy Python2 Support.

Works Unchanged (The Good)

The best part of Pies is how much Python3 code works unchanged in Python2

Functions:

  • round

  • next

  • filter

  • map

  • zip

  • input

  • range

Types:

  • object (str automatically has correct behavior on all versions of Python)

  • chr (creates a unichr object in Python2)

  • str (creates a unicode object in Python2)

  • dict (creating a dict using dict() will give you all the special Python3 itemview results, but using {} will not)

Imports:

  • html

  • http

  • xmlrpc

  • _thread

  • builtins

  • configparser

  • copyreg

  • queue

  • reprlib

  • socketserver

  • ipaddress

  • argparse

  • enum (also adds this library to Python 3.0-3.3)

Different Imports (The Bad)

Some Python3 Modules have moved around so much compared to their Python2 counterpart, that I found it necessary to create special versions of them to obtain the Python3 naming on both environments. Since these modules exist already in Python2 allowing them to be imported by the Python3 module name directly is not possible. Instead, you must import these modules from pies.

Example:

form pies import pickle

Full List:

  • dbm

  • urllib

  • collections

  • functools

  • imp

  • itertools

  • pickle

  • StringIO

  • sys

  • unittest

Special Syntax (The Ugly)

Sadly, there is still special syntax that is present for corner cases.

  • PY2 - True if running on Python2

  • PY3 - True if running on Python3

  • u(‘text’) - should replace u’text’ made available for ease of porting code from Python2

  • itemsview(collection) - should replace collection.iteritems() where you do not control the collection passed in

  • valuesview(collection) - should replace collection.values() where you do not control the collection passed in

  • keysview(collection) - should replace collection.keys() where you do not control the collection passed in

  • execute() - enables Python 3 style exec statements on both environments.

  • integer_types - may want to use isinstance(variable, integer_types) instead of type(variable, int) as long values will not match int in Python2.

  • NewClass(with_metaclass(metaclass, parent_class)) - Should replace both “metaclass = metaclass” and “NewClass(metaclass=metaclass)” as a way to assign meta-classes.

What Could be Improved?

I’m pretty sure a bunch. If you run into any problems or have any ideas please don’t hesitate to file a bug, submit a pull request, or email me at timothy.crosley@gmail.com.


Thanks and I hope you enjoy pies!

~Timothy Crosley

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pies-2.5.4.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

pies-2.5.4-py2.py3-none-any.whl (15.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pies-2.5.4.tar.gz.

File metadata

  • Download URL: pies-2.5.4.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pies-2.5.4.tar.gz
Algorithm Hash digest
SHA256 48bfe85159a3c2875fed57ecf3f773e29cd1c5a6395f8a6485f476e5c400e9c6
MD5 9e645ea199757da79e293cfaf862a29e
BLAKE2b-256 38d9a9e2c89e0c5d647f9deadc052852caf1297321cfb4d0abee37a63b9a6762

See more details on using hashes here.

File details

Details for the file pies-2.5.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pies-2.5.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fab2979fcfe7d9b661eb17e3fd5ddfa60f8692a33ef426c51921cb96e40761bc
MD5 d69556d303f7c4da9f40c01d53b4f103
BLAKE2b-256 2ece37abbaf34819fafadeeda74ee7dc785649a70a40ad79eecab6ae2b905aee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page