Dev: Py3compat module
ellisonbg edited this page Feb 14, 2013
·
2 revisions
Pages 142
- Home
- A gallery of interesting IPython Notebooks
- A gallery of interesting Jupyter and IPython Notebooks
- Code blocks and other ideas
- Cookbook: Branding the IPython notebook
- Cookbook: Connecting to a remote kernel via ssh
- Cookbook: Dated logging
- Cookbook: deploying ipython
- Cookbook: Dynamic prompt
- Cookbook: Hotkeys for faster notebook editing
- Cookbook: Ignoring some commands in history
- Cookbook: Index
- Cookbook: IPython and Emacs
- Cookbook: IPython and Python.NET
- Cookbook: Job control extension
- Cookbook: Launching IPython on OSX
- Cookbook: Macro arguments
- Cookbook: nbconvert templates
- Cookbook: Notebook utilities
- Cookbook: Running a file in the background
- Cookbook: Running screen
- Cookbook: Sending built in help to the pager
- Cookbook: Shadow history
- Cookbook: Standalone executable zip
- Cookbook: Storing aliases
- Cookbook: Styling Notebook
- Cookbook: Styling the QtConsole
- Cookbook: Updating code for use with IPython 0.11 and later
- Cookbook: Using IPython with Console2
- Cookbook: Using IPython with TextMate
- Dev: boot2docker
- Dev: Closing pull requests
- Dev: Coding style
- Dev: Compiling .LESS files
- Dev: Documenting IPython
- Dev: Extending the Messaging Protocol
- Dev: GitHub workflow
- Dev: Index
- Dev: IPython Sphinx directive
- Dev: Javascript Events
- Dev: Lab meetings on Air
- Dev: Meeting notes, February 6, 2013
- Dev: Meeting notes, January 6, 2013
- Dev: Meeting, January 2014
- Dev: Meeting, July 2013
- Dev: Meeting, September 2014
- Dev: Py3compat module
- Dev: PyData 2012 HackNight
- Dev: Releasing IPython
- Dev: Testing
- Dev: Testing kernels against message specification
- Dev: The perfect pull request
- Dev: URL mapping of IPython notebook
- Dev: Windows Sophos issues
- Dev:Codenames
- Extensions Index
- Fix the boot manager after install ubuntu with windows 8
- Frequently asked questions
- Install: Docker
- Install: Index
- Install: Mobile
- Install: Python 3
- IPEP 0: IPEP Template
- IPEP 10: kernel side filtering of display formats
- IPEP 11: Tab Completion System Refactor
- IPEP 12: Splitting KernelManager and KernelClient
- IPEP 13: Updating the Message Spec
- IPEP 14: Notebook Regression Tests
- IPEP 15: Autosaving the IPython Notebook
- IPEP 16: Notebook multi directory dashboard and URL mapping
- IPEP 17: Notebook Format 4
- IPEP 18: Text Editor for the Notebook
- IPEP 19: concurrent.futures (PEP 3148) for IPython.parallel
- IPEP 1: Cleanup and extension of the Magic system in IPython
- IPEP 20: Informal structure of cell metadata
- IPEP 21: Widget Messages
- IPEP 22: Notebook, new cell type "Comment"
- IPEP 23: Backbone.js Widgets
- IPEP 24: completion and object_info
- IPEP 25: Registry of installed kernels
- IPEP 26: Full Featured python js object bridge
- IPEP 27: Contents Service
- IPEP 28: Interleaving Widgets and Output
- IPEP 29: Project Governance
- IPEP 2: Input transformations
- IPEP 3: Multiuser support in the notebook
- IPEP 4: Python 3 Compatibility
- IPEP 5: Notebook JavaScript organization
- IPEP 6: Qt console additional pane
- IPEP 7: Subclients for the Notebook
- IPEP 8: Custom messages and message handlers
- IPEP 9: Slideshow from the notebook
- IPEPs: IPython Enhancement Proposals
- IPython 3.0 comm and widget migration document
- IPython kernels for other languages
- Lets talk Python
- News Categorization using Multinomial Naive Bayes
- Projects using IPython
- Projects using IPython: PyMAD
- Research at UC Berkeley using IPython
- Roadmap: IPython
- Roadmap: NBviewer
- ShowMeDo
- Sprints: PyCon2012
- Sprints: SciPy2012
- Sprints: SciPy2014
- Sprints: SciPy2014 Py3 BoF
- Sprints: SciPy2014 sprint ideas
- Syntax for code blocks
- Trash: 0.11 Migration
- Trash: Cookbook Adding support for ipipe
- Trash: Cookbook Custom completers
- Trash: Cookbook Custom magic commands
- Trash: Cookbook Custom magic examples
- Trash: Cookbook Defining prompts via hooks
- Trash: Cookbook envpersist
- Trash: Cookbook GVim integration
- Trash: Cookbook Integrate with any loop(panda3d example)
- Trash: Cookbook Interrupting threads
- Trash: Cookbook ipy_exportdb
- Trash: Cookbook ipy_render
- Trash: Cookbook Leo IPython Bridge
- Trash: Cookbook Logging
- Trash: Cookbook Moving config to IPython 0.11
- Trash: Cookbook Saving the session on exit
- Trash: Cookbook String list processing
- Trash: Cookbook The sh profile
- Trash: Cookbook User config file
- Trash: EuroSciPy2011
- Trash: IpyKit
- Trash: IPython:About
- Trash: Main Page
- Trash: Main Page PyCon12Sprint
- Trash: Old Embedding GTK
- Trash: Old Embedding Tkinter
- Trash: Parallel Computing
- Trash: Potential projects
- Trash: Py4Science
- Trash: Scratchpad
- Trash: Scratchpad Persistence
- Trash: Using ipipe
- Widgets
- Show 127 more pages…
Clone this wiki locally
The IPython.utils.py3compat module provides a number of functions to make it easier to write code for Python 2 and 3. We also use 2to3 in the setup process to change syntax, and the io.open() function, which is essentially the built in open function from Python 3.
The names provided are:
- PY3: True in Python 3, False in Python 2.
Unicode related
-
decode, encode: Shortcuts to decode or encode strings, using
sys.stdin.encodingby default, and using replacement characters on errors. -
str_to_unicode, unicode_to_str, str_to_bytes, bytes_to_str: Convert to/from the platform's standard
strtype (bytes in Python 2, unicode in Python 3). Each function is a no-op on one of the two platforms. - cast_unicode, cast_bytes: Accept unknown unicode or byte strings, and convert them accordingly.
- cast_bytes_py2: Casts unicode to byte strings on Python 2, but doesn't do anything on Python 3.
Miscellaneous
-
input: Refers to
raw_inputon Python 2,inputon Python 3 (needed because 2to3 only converts calls to raw_input, not assignments to other names). -
builtin_mod_name: The string name you import to get the builtins (
__builtin__-->builtins). - isidentifier: Checks if a string is a valid Python identifier.
-
open: Simple wrapper for Python 3 unicode-enabled open. Similar to
codecs.open, but allows universal newlines. The current implementation only supports the very simplest use. -
MethodType:
types.MethodTypefrom Python 3. Takes only two arguments: function, instance. The class argument for Python 2 is filled automatically. - doctest_refactor_print: Can be called on a string or a function (or used as a decorator). In Python 3, it converts print statements in doctests to print() calls. 2to3 does this for real doctests, but we need it in several other places. It simply uses a regex, which is good enough for the current cases.
-
u_format: Where tests use the repr() of a unicode string, it should be written
'{u}"thestring"', and fed to this function, which will produce'u"thestring"'for Python 2, and'"thestring"'for Python 3. Can also be used as a decorator, to work on a docstring. - execfile: Makes a return on Python 3 (where it's no longer a builtin), and upgraded to handle Unicode filenames on Python 2.