<commit_before>Connecting different Python interpreters
==========================================

Connect to Python2/Numpy from Python3
----------------------------------------

Here we run a Python3 interpreter to connect to a Python2.6 interpreter
that has numpy installed. We send items to be added to an array and
receive back the remote "repr" of the array::

    import execnet
    gw = execnet.makegateway("popen//python=python2.6")
    channel = gw.remote_exec("""
        import numpy
        array = numpy.array([1,2,3])
        while 1:
            x = channel.receive()
            if x is None:
                break
            array = numpy.append(array, x)
        channel.send(repr(array))
    """)
    for x in range(10):
        channel.send(x)
    channel.send(None)
    print (channel.receive())

will print on the CPython3.1 side::

    array([1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

A more refined real-life example of python3/python2 interaction
is the anyvc_ project which uses version-control bindings in
a Python2 subprocess in order to offer Python3-based library
functionality.

.. _anyvc: http://bitbucket.org/RonnyPfannschmidt/anyvc/overview/

Work with Java objects from CPython
----------------------------------------

Use your CPython interpreter to connect to a `Jython 2.5.1`_ interpreter
and work with Java types::

    import execnet
    gw = execnet.makegateway("popen//python=jython")
    channel = gw.remote_exec("""
        from java.util import Vector
        v = Vector()
        v.add('aaa')
        v.add('bbb')
        for val in v:
            channel.send(val)
    """)

    for item in channel:
        print (item)

will print on the CPython side::

    aaa
    bbb

.. _`Jython 2.5.1`: http://www.jython.org
<commit_msg>add a example for reconfiguring string coercion<commit_after>Connecting different Python interpreters
==========================================

Connect to Python2/Numpy from Python3
----------------------------------------

Here we run a Python3 interpreter to connect to a Python2.6 interpreter
that has numpy installed. We send items to be added to an array and
receive back the remote "repr" of the array::

    import execnet
    gw = execnet.makegateway("popen//python=python2.6")
    channel = gw.remote_exec("""
        import numpy
        array = numpy.array([1,2,3])
        while 1:
            x = channel.receive()
            if x is None:
                break
            array = numpy.append(array, x)
        channel.send(repr(array))
    """)
    for x in range(10):
        channel.send(x)
    channel.send(None)
    print (channel.receive())

will print on the CPython3.1 side::

    array([1, 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

A more refined real-life example of python3/python2 interaction
is the anyvc_ project which uses version-control bindings in
a Python2 subprocess in order to offer Python3-based library
functionality.

.. _anyvc: http://bitbucket.org/RonnyPfannschmidt/anyvc/overview/


Reconfiguring the string coercion between python2 and python3
-------------------------------------------------------------

Sometimes the default configuration of string coercion (2str to 3str, 3str to 2unicode)
is inconvient, thus it can be reconfigured via `gw.reconfigure`.


    >>> import execnet
    >>> execnet.makegateway("popen//python=python3.1")
    <PopenGateway id='gw0' receive-live, 0 active channels>
    >>> gw=execnet.makegateway("popen//python=python3.1")
    >>> gw.remote_exec("channel.send('hello')").receive()
    u'hello'
    >>> gw.reconfigure(py3str_as_py2str=True)
    >>> gw.remote_exec("channel.send('hello')").receive()
    'hello'
    >>> ch = gw.remote_exec("channel.send(isinstance(channel.receive(), bytes)")
    >>> ch.send('a')
    >>> ch.receive()
    False
    >>> gw.reconfigure(py2str_as_py3str=False)
    >>> ch = gw.remote_exec("channel.send(isinstance(channel.receive(), bytes)")
    >>> ch.send('a')
    >>> ch.receive()
    True


Work with Java objects from CPython
----------------------------------------

Use your CPython interpreter to connect to a `Jython 2.5.1`_ interpreter
and work with Java types::

    import execnet
    gw = execnet.makegateway("popen//python=jython")
    channel = gw.remote_exec("""
        from java.util import Vector
        v = Vector()
        v.add('aaa')
        v.add('bbb')
        for val in v:
            channel.send(val)
    """)

    for item in channel:
        print (item)

will print on the CPython side::

    aaa
    bbb

.. _`Jython 2.5.1`: http://www.jython.org

{'fraction_non_alphanumeric': 0.1722173531989483, 'fraction_numerical': 0.020157756354075372, 'mean_word_length': 2.389012620638456, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2733635', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1360, 'n_words': 424}