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
import math
from builtins import frozenset, object
from typing import Any, Callable, Dict, List

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>', Callable[[Any], Any],
            b'x\x9cm\x8d?O\x83@\x1c\x86\x0f\xaa\x16Q\x13\x13\xbfDY\xb8"\xf6\x80\xc4\xb80^\xc2P\x17\xb6\xcb\xfd3\xbd\x08\\\x7f\xdcQm\xa2\x89\x8b~n\xa9\xb3\xc3\xf3\xe6I\x9e\xe1\xfdZHe\xba.e\xa7\x8d\x99\x1c5\xf7\x9a\xbdL\x83\xf4\xc6\x0e1\xa0\xd5\x7f]Z\xa5c\x08V4\xa0h&\xa4u\x1d} \x15\xdc\xa1g\x04aC\xc3\x1fX$m\x80\x10z\x87\xb3o8ow\xb3b\xdf\xefOd\xe4X\x1d\x1e\x8a7|\xbf\xe6r\x93\x91\xbc(yA\xaa\xb2\xccr\xa2s\x95\x8bJ\x8a\x8c\xe8B\xf1l#\x88\xd6k\xac\xact\xd8\xd9i\x94\x1a\xfb\xc9\xdb\xd1\xf0\xcea\x98\x8c|e\xce\xf3\xd1\xe3\x03\x1f]\xaato\xd3\xfd\x11.\xdah\xbe{\xecx/\x14\x7f\x82%\xbd\xad\x11DI\xe2\xe1r\x0b\xb1dLL\xa6\xf3f`,f\xac\xe7\x7f\xb2[6\xcd\'\\5\x1e\xae\xb7p\x93\xfe\x02\xf9\x9fT\x94'
        ),
        't': {
            'c': print,
            'z': 2
        }
    }, {},
    frozenset({1, 2}), max, min, NotImplemented,
    range(10), (7 + 8j)
]
empty_str = ''
long_list = [
    123, {b'klsdjf', '1'}, 123, {b'klsdjf', '1'}, 123, {b'klsdjf', '1'}, 123,
    {b'klsdjf', '1'}, 123, {b'klsdjf', '1'}, 123, {b'klsdjf', '1'}, 123,
    {b'klsdjf', '1'}, 123, {b'klsdjf', '1'}, 123, {b'klsdjf', '1'}, 123,
    {b'klsdjf', '1'}
]
np_module = numpy
np_object = typed_object(
    ndarray,
    b'x\x9cUN\xbbr\x830\x10\x14\xc2y)\xce\xfb\xd5\xa4HIE\x9d"\x9d;k\x86\x82\x8aN#\x0bM\xc2\x0c\x02V9\n\xba4\xb6\xe3\xafN\x80Q\xe3-vgn\xf7\xf6\xee\'6eU\xd7\xa9\x9aX(\xe3\xad&\xab\xb4\xf7z\x10`\xc9\x91\xf9iIi"/\x10\x1d\xcd+\xd7\xb5\x9e\x94k\xcb\xbe\xb6\x02\xbcxe\x8c5\xbd\xeb\x86\xd4\xb4\xde\xa6\xca\xf55Us\xa7\xea\x9d\xa6/\xc4[,r\x9c\x14\xcb1\xa8\xbc5m\xf3M\xbe7\x84\xd3\x1d\xcer\x9c\x9by[4ex\xe4B\xb2-\xc4*\xda\xe0r\x8fe"#\xc9%\xdf\xe1*\xe4J\x1a\xba\xf1\xf0u\xc1\xc7\xbe\xea\x1d7\x87\xdf=ns\xdc%2.\xa2q\xf6\x81\xfb,\xcb\xd6\x7f#f\x92\x8c\xf0\xb09\xac\xde&s\x02\x0f\x1a\x07]\x04\xc5#\xe1)#<\xe7xI\xff\x01\xb4\xcaZm'
)
typing_ = Callable[[int, str], Dict[str, List[int]]]
vbytes_ = b'klsdjflkds\\\x00'

It is runnable, you can take a try.