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.
You can simply install it with pip command line from the official PyPI site.
pip install potcFor more information about installation, you can refer to Installation.
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.
The native potc can transform many types, such as
frompotcimporttransobjfromeasydictimportEasyDictimportnumpyasnptransobj(1) # "1", int formattransobj('233') # "'233'", str formattransobj(1.2) # "1.2", float formattransobj([1, '2']) # "[1, '2']", list formattransobj((1, '2')) # "(1, '2')", tuple formattransobj({1, '2'}) # "{1, '2'}", set format transobj({1: '2'}) # "{1: '2'}", dict formattransobj(EasyDict(a=1, b=2)) # "EasyDict({'a': 1, 'b': 2})", external dict formattransobj(EasyDict) # "EasyDict", type formattransobj(np) # "numpy", module formatAnd 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
importmathfrompotcimporttransvarsif__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 codeprint(_code)The output should be
importmath__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
importmathfrompotcimporttransvarsfrompotc.fixtureimportrule, AddonsclassMyPair:
def__init__(self, first, second):
self.first=firstself.second=second@rule(type_=MyPair)defmypair_support(v: MyPair, addon: Addons):
returnaddon.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 codeprint(_code)The output should be like below, the MyPair class is supported by the new rule.
importmathfrom__main__importMyPair__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:
We appreciate all contributions to improve potc, both logic and system designs. Please refer to CONTRIBUTING.md for more guides.
potc released under the Apache 2.0 license.