• This repository has been archived on 24/Feb/2019
  • Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Pies

PyPI version PyPi downloads Build Status License 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 than 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:

from 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 [email protected].


Thanks and I hope you enjoy pies!

~Timothy Crosley

More Repositories

1

streamdeck-ui

A Linux compatible UI for the Elgato Stream Deck.
Python
1,137
star
2

portray

Your Project with Great Documentation.
Python
860
star
3

jiphy

Your client side done in a jiphy. Python to JavaScript 2-way converter.
Python
583
star
4

hypothesis-auto

An extensions for Hypothesis that provides fully automatic testing for type hinted functions
Python
344
star
5

concentration

Stay focused on work when you want, and goof off when you don't.
Python
328
star
6

deprecated.frosted

A simple program which checks Python source files for errors.
Python
251
star
7

quickpython

A retro interactive coding environment powered by Python and nostalgia
Python
109
star
8

pdocs

A simple program and library to auto generate API documentation for Python modules.
Python
74
star
9

connectable

A very simple implementation of QTs signal / slot pattern in Python
Python
61
star
10

examples

Tests and Documentation Done by Example.
Python
24
star
11

thedom

A python framework to generate html and JavaScript from reusable and combine-able widgets.
Python
23
star
12

GuiBuilder

Click and Drop Editor For WebElements
Python
23
star
13

preconvert

A Library to enable preconversion of any Python type into one that is easily serializable
Python
17
star
14

cookiecutter-python

A cookie cutter template for modern Python projects using Python3.6+, poetry, mypy, py.test, and linters.
Shell
16
star
15

WebBot

WebBot is a collection of several tools that enables building Python web applications the same way native ones are built. As a result, the WebBot framework encourages reuse, concise code, rapid development, and happy developers.
Python
14
star
16

blog

Personal Blog
Python
7
star
17

DynamicForm

Ajax Abstraction Library for Python
Python
6
star
18

Html2Shpaml

Converts html to the more condensed and DRY sphaml format
Python
5
star
19

sigy

A library to enable reusing and composing Python function signatures.
Python
4
star
20

linux_environment

My common environment across all linux computers
Shell
4
star
21

CleanHTML

A Very Forgiving HTML/XML Cleaner
Python
4
star
22

RestClient

A Simple RestClient for Python - Works With AppEngine
Python
3
star
23

instantly

A Python Tool To Create and Expend Project Templates
Python
3
star
24

python3-project-template

Basic cookiecutter template for new Python3 projects
Python
3
star
25

website_change_alert

A simpe script to send a text when a website changes.
Python
3
star
26

camera_name_normalizer

Camera Name Normalization
Python
3
star
27

blox

Build it with Python
Python
3
star
28

preconvert_numpy

Preconverts common numpy types to their serializable (jsonifiable, msgpackable, etc...) form. A plugin for the preconvert Python library.
Python
2
star
29

simple_ci

An extremely simple ci server built with hug
Python
2
star
30

RedisStore

RedisStore enables interacting with Redis keys from within python as if they where native object types.
Python
2
star
31

dpy

A tool that automatically generates quality documentation for Python projects.
Python
2
star
32

todo_list

A remotely available todo list
Python
2
star
33

black_isort

A guaranteed black compatible import sorter with minimal settings.
Python
2
star
34

signature

An expiremental framework for composing Python function signature
Shell
1
star
35

webbot_example_postboard

Code for post board example shown in screen-cast http://www.youtube.com/watch?v=0L8TsmrZPLg
Python
1
star
36

sprints_kit

Evolving kit for leading open source sprints
1
star
37

intrigue_icon_set

A set of 100% free and original icons - with a unique minimalistic look
Perl
1
star
38

webelements_site

The code that powers the WebElements homepage
Python
1
star
39

mde

CLI Markdown Editor
Python
1
star
40

mac_environment

Personal MacOS environment setup instructions
Shell
1
star