Quick Start

After the installation is complete, we can run some simple examples. This page will show you this.

Simple Translation

A simplest translation can be processed with the code below.

1
2
3
4
5
6
7
8
9
from potc import transobj

if __name__ == '__main__':
    print("transobj(1):", transobj(1))
    print("transobj([1, 2, 'sdkfj\u0123']):",
          transobj([1, 2, 'sdkfj\u0123']))

    print('empty set:', transobj(set()))
    print('1-lengthened tuple:', transobj((1,)))

The output result should be

1
2
3
4
transobj(1): 1
transobj([1, 2, 'sdkfjģ']): [1, 2, 'sdkfjģ']
empty set: set()
1-lengthened tuple: (1, )

Most of the primitive data types supported by python can be turned back to runnable source code with potc.

Scriptable Translation

If we need to translate multiple values, and dump the translation result to a runnable .py script, we can do like this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import math
from typing import Callable, Dict, List

import numpy as np

from potc import transvars

if __name__ == '__main__':
    _code = transvars({
        'complex_array': [
            1, 1.5, math.e, object(),
            float('nan'), float('+inf'), float('-inf'),
            'str', None, {1, 2, 't' * 30},
            {
                1: 2, 'c': 3,
                'g': lambda x: x ** 2,
                't': dict(c=print, z=2),
            },
            {},
            frozenset({1, 2, }),
            max, min, NotImplemented,
            range(10), 7 + 8.0j,
        ],
        'vbytes_': b'klsdjflkds\\\x00',
        'empty_str': '',
        'ba': bytearray(b'a' * 20),
        'long_list': [123, {'1', b'klsdjf'}] * 10,
        'np_object': np.array([[1, 2, ], [3, 4]]),
        'np_module': np,
        'typing_': Callable[[int, str], Dict[str, List[int]]],
    }, reformat='pep8')  # auto reformat the code
    print(_code)

The output result (as well as the dumped script) should be

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import math
from builtins import frozenset, object
from typing import _GenericAlias

import numpy
from numpy import ndarray
from potc.supports import function, typed_object

__all__ = [
    'ba', 'complex_array', 'empty_str', 'long_list', 'np_module', 'np_object',
    'typing_', 'vbytes_'
]
ba = bytearray(b'aaaaaaaaaaaaaaaaaaaa')
complex_array = [
    1, 1.5, math.e,
    typed_object(
        object,
        b'x\x9ck`NN\xc9\xcc\xc9\xd1\x8b\x07\x91\\\xf19\xf9\x89)\xf1%\x95\x05\xa9\\\x85\x0c\x11l\x0c\x0c\x0c\xf9IY\xa9\xc9%\x85\x8c\xad\x85LA\x85\xcc\x9a\x8d\x85,z\x00\xb3Q\x0f\xb5'
    ), math.nan, +math.inf, -math.inf, 'str', None,
    {1, 2, 'tttttttttttttttttttttttttttttt'}, {
        1:
        2,
        'c':
        3,
        'g':
        function(
            '<lambda>',
            typed_object(
                _GenericAlias,
                b"x\x9ck`N\x8e\xcf/H-J,\xc9/\xe2JO-\xc9,I\xcd\xe5*dH.\xa9,\xc8\xccK\xe7rN\xcc\xc9IL\xcaI\xe5*d\x8c-d\x82\x89:\xe6Ur\x152'f0\xb7\x15\xb2\xb4\x15\xb2\x06\x15\xb2\xe9\x01\x00\x9c\x00\x17\xf2"
            ),
            b"x\x9cm\x90KK\xc3@\x14\x85'\xad\xda\x18\x1f\xad\xf8'\xdaM\xa6\x8f\xf4\x05\xe2&\xcb@\x16u\x93\xdde\xe6\xce\x94\x06\x93LofR-Xpc\x7f\xb7\xa9k\x17\xdf\xe1\xc0\x81s\xe0|wQ\xe5E\x11\xc2E\x03\xc0Z\x0b\xa7a\xdbT\xe8rS\x05\xc4\x86\xff\xe5h\x94\x0e\xc8\x1b&^\xc2Z:I\x1c\xfb_Ly\xcf\xec\x8dQ'M:g\xea\x8e2\x8f1\xf6IW?t\x9d\xedZ\xcb]\xb9\xbf0Y\x1c\xd7\x87h\xf9\xc1#5\xc1h&p\x16m\xf5X\xa0\\N\xe5b\xba\x8c\xc6+\x19\xad\xe7\x93\xd5|\x8c+\x94\xd3\x99\xe6\xca\xa0\xe5\xd645j\xee\x1ag\xea\\\x14\x96S\x93\xe3;X'j\xc7\x0f\xa2\xb6\xa1\xd2\xa5\t\xf7G\xba\xc9\xfcv\xee\xa5\x10\xa5T\xe2\x95z\xc9 f\xe4\x8fF\x8en7\x14 \x80l\xf2\xc2\xe5\x15@\x00P\x8a?\xb3\xeb\xa5\xa9\xa3\xbb\r\xdd\x9f\xe8\xe1D\x8fY\xbf\xad\x00\x10Ue\x9c\xb8\\a\x01\xa8\x7f\xa2\x81=\xd3\x93\x0c\x7f\x01V\x16_K"
        ),
        't': {
            'c': print,
            'z': 2
        }
    }, {},
    frozenset({1, 2}), max, min, NotImplemented,
    range(10), (7 + 8j)
]
empty_str = ''
long_list = [
    123, {'1', b'klsdjf'}, 123, {'1', b'klsdjf'}, 123, {'1', b'klsdjf'}, 123,
    {'1', b'klsdjf'}, 123, {'1', b'klsdjf'}, 123, {'1', b'klsdjf'}, 123,
    {'1', b'klsdjf'}, 123, {'1', b'klsdjf'}, 123, {'1', b'klsdjf'}, 123,
    {'1', b'klsdjf'}
]
np_module = numpy
np_object = typed_object(
    ndarray,
    b"x\x9c-\x8b\xb9\x12\x820\x18\x84s\xe0\x15\xcfG\xb0\xa4JmaGgfRP\xd1e0\xa4`\x86\xeb\x8f\xa1\xa0\xb3Q\xe4\xa9\x15$[\xec7\xb3\xc7\x93\xea,/\n\xae&gJ[\x93:\xa3Rk\xd3\x8e\x01\nu\xd5\x96M\xc7um\r/\xdb\xc2\xe5s\xa3\xac\xd1u\xf5p\xb6\xd5\x8e\x01\x9eW\xac\xca\xfc\x8f\x08\xf4\x02\x1a\xe1;\x04=,B\x81\x05\x11\xe4\rK\xbf\xcb\\\xd7\x18\x06\xab\x84 \x84\xf2\x0b\xac\x87O\x0f\x9b\x18X(h\x82\xc7\xec\n[)\xe5\xed;\xeao\x029\xd8\xdd\x87\xe8<\x95\x93\x88'\xf5\x0c<a\xef\xe0 \x1d\x1cc8\xf1\x1f\x14\xb8?\xa9"
)
typing_ = typed_object(
    _GenericAlias,
    b'x\x9cE\xcc\xbf\x0e\x820\x18\x04\xf0*\x8a\xf2\xf9__\xc2\x89\x97\xd0\xd1\xa9\x93\x8bijih\x93\n\x1c|\x8b\x9b\x0b}n1&\xba\xdc\xf0\xbb\xcb\xbd\x12\xa3\xea\xc6\xb6\x9a\xeb\x96J\xcb\x9e\xed\x83 \x0c?\x1b_\x95t\xd2!\xe8{\xb0\x84\xd1\r\xe3\xa3)|\x08\xb9\xfa$\xa9P\xebB\r\xbb\xa1L\xae\x89\x10\xc2W\x8cI\x8f\xa9D\xea\xbe\xd2q\x8bY\x8f\xb9Df\xdd\xef\xf4\xec\r\x13\xc8e\x7f\xba\xf8n\xa0\x85K#\x96\x12\xab\x88u\xc4Fb\x1b\xb1\x8b\xd8K\x1c\xf27(\xf14\xe9'
)
vbytes_ = b'klsdjflkds\\\x00'

It is runnable, you can take a try.