Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/haarcuba/warp2
access legacy Python 2 code from within Python 3
https://github.com/haarcuba/warp2
compatibility embed-python2-in-python3 legacy-python python python-2 python-3 python2 python3
Last synced: about 1 month ago
JSON representation
access legacy Python 2 code from within Python 3
- Host: GitHub
- URL: https://github.com/haarcuba/warp2
- Owner: haarcuba
- License: mit
- Created: 2017-06-03T21:23:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-06-03T23:23:04.000Z (over 7 years ago)
- Last Synced: 2024-09-29T17:03:43.520Z (about 2 months ago)
- Topics: compatibility, embed-python2-in-python3, legacy-python, python, python-2, python-3, python2, python3
- Language: Python
- Homepage: https://haarcuba.github.io/warp2/
- Size: 11.7 KB
- Stars: 13
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Warp 2
Warp 2 wraps Python 2 code and allows access to it by running in in a subprocess.
It communicates with the subprocess using [pickle](https://docs.python.org/3.6/library/pickle.html), so there are limitation to using it - if you need to send unpicklable data, that's a problem.
## Installation
$ pip install warp2
## Example
here's a Python 2 class of a greeter that tracks a word count:
```python
# this is in Python 2
import collectionsclass Greeter( object ):
def __init__( self ):
self._counts = collections.defaultdict( lambda: 0 )def say( self, what ):
print what
self._counts[ what ] += 1
return 'said: {}'.format( what )def counts( self ):
return dict( self._counts )
```here's how to use it from Python 3, note that you must provide a `thing` object for the Warp 2 library to use:
```python
# this is in Python 3
import warp2.warper
import randomgreeter = warp2.warper.Warper( 'import greeter ; thing=greeter.Greeter()' )
for _ in range( 10 ):
greeting = random.choice( [ 'hi', 'hello', "what's up", 'how are you' ] )
greeter.say( greeting )print( "summary: " )
print( greeter.counts() )
```Here's how to run this example from the root of this project (once warp2 is installed of course)
$ PYTHONPATH=examples/ python examples/three.py
what's up
hello
how are you
what's up
how are you
hello
hi
how are you
hi
what's up
summary:
{'how are you': 3, 'hi': 2, 'hello': 2, "what's up": 3}