Skip to main content

potc

PyPI PyPI - Python Version Loc Comments

Docs Deploy Code Test Badge Creation Package Release codecov

GitHub stars GitHub forks GitHub commit activity GitHub issues GitHub pulls Contributors GitHub license

Python object to code framework, expression any python object to runnable python code.

Almost all the primitive types in python can be translated to source code format, and they will be runnable.

Installation

You can simply install it with pip command line from the official PyPI site.

pip install potc

For more information about installation, you can refer to Installation.

Documentation

The detailed documentation are hosted on https://potc-dev.github.io/potc/main/index.html.

Only english version is provided now, the chinese documentation is still under development.

Quick Start

The native potc can transform many types, such as

from potc import transobj
from easydict import EasyDict
import numpy as np

transobj(1)         # "1", int format
transobj('233')     # "'233'", str format
transobj(1.2)       # "1.2", float format
transobj([1, '2'])  # "[1, '2']", list format
transobj((1, '2'))  # "(1, '2')", tuple format
transobj({1, '2'})  # "{1, '2'}", set format  
transobj({1: '2'})  # "{1: '2'}", dict format
transobj(EasyDict(a=1, b=2))  # "EasyDict({'a': 1, 'b': 2})", external dict format
transobj(EasyDict)  # "EasyDict", type format
transobj(np)        # "numpy", module format

And so on, most of the native python types are covered.

In some cases, we need to translate the values into python script instead of simple expression, we can use transvars

import math

from potc import transvars

if __name__ == '__main__':
    _code = transvars({
        'arr': [
            1, 1.5, math.e,
        ],
        'vbytes_': b'klsdjflkds\\\x00',
        'empty_str': '',
        'ba': bytearray(b'a' * 20),
    }, reformat='pep8')  # auto reformat the code
    print(_code)

The output should be

import math

__all__ = ['arr', 'ba', 'empty_str', 'vbytes_']
arr = [1, 1.5, math.e]
ba = bytearray(b'aaaaaaaaaaaaaaaaaaaa')
empty_str = ''
vbytes_ = b'klsdjflkds\\\x00'

This script are runnable, can be imported directly into your python code. The import packages will also be generated (like import math).

In some complex cases, You can define you own rules to support more data types, such as

import math

from potc import transvars
from potc.fixture import rule, Addons


class MyPair:
    def __init__(self, first, second):
        self.first = first
        self.second = second


@rule(type_=MyPair)
def mypair_support(v: MyPair, addon: Addons):
    return addon.obj(MyPair)(v.first, v.second)


if __name__ == '__main__':
    _code = transvars({
        'arr': [
            1, 1.5, math.e,
        ],
        'vbytes_': b'klsdjflkds\\\x00',
        'empty_str': '',
        'ba': bytearray(b'a' * 20),
        'c': MyPair(1, 2),
    }, trans=[mypair_support], reformat='pep8')  # auto reformat the code
    print(_code)

The output should be like below, the MyPair class is supported by the new rule.

import math

from __main__ import MyPair

__all__ = ['arr', 'ba', 'c', 'empty_str', 'vbytes_']
arr = [1, 1.5, math.e]
ba = bytearray(b'aaaaaaaaaaaaaaaaaaaa')
c = MyPair(1, 2)
empty_str = ''
vbytes_ = b'klsdjflkds\\\x00'

For more quick start explanation and further usage, take a look at:

Contributing

We appreciate all contributions to improve potc, both logic and system designs. Please refer to CONTRIBUTING.md for more guides.

License

potc released under the Apache 2.0 license.

Metadata

Release files for potc 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for potc 0.1.3
File Size Uploaded
potc-0.1.3.tar.gz 29.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for potc 0.1.3
File Interpreter ABI Platform
potc-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 64.8 kB

Release files / potc-0.1.3.tar.gz

Download URL potc-0.1.3.tar.gz
Size 29.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e8b8baf12f2bb57da6bfd9a2f797016e1b9d49ab938139c185f1d9aae575367a
BLAKE2b-256 checksum
How to use checksums
b5616ba95609e3eff00e3596105da04276562c4a575eca39093ac4d3daf60a59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release files / potc-0.1.3-py3-none-any.whl

Download URL potc-0.1.3-py3-none-any.whl
Size 35.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
279bab679cb5617d3e358a7dc0a8c42d43a11c777e60fb84aeac732bd396c94d
BLAKE2b-256 checksum
How to use checksums
cc64632f368bd40adf3de1b8d8492189ae0afa9a736fa7f39b811ef00244c4c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.14

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page