Translator from Python into C (русская версия)
Use Python-syntax to write a clear C-code for your microcontrollers:
About another variants of Python to C translating
Using is really simple with shortcuts:
from py2c.shortcuts import trans_c
my_python_code = '''
def hello(a: int, b: int = 5) -> string:
return 'good' if a + b > 0 else 'not good'
'''
c_code = trans_c(my_python_code)
print(c_code)
Without shortcuts if you want more control:
from py2c.bytecode_walker import translate
from py2c.translator_cpp import TranslatorC
my_python_code = '''
def hello(a: int, b: int = 5) -> string:
return 'good' if a + b > 0 else 'not good'
'''
with open('c_code.c') as c_file:
translator = TranslatorC(save_to=c_file)
translate(translator, my_python_code)
Use this command to install Py2C:
pip install git+https://github.com/syeysk/sy_py2c.git
Use this command to uninstall Py2C:
pip uninstall py2c
py2c.shortcuts.trans_c(source_code, write_to=None)
- translate a python-code into c-code. Arguments:
source_code
- a source python-code like unicode string (type ofstr
) or text file object (returningopen
function)write_to
- a file object to write a result c-code. Ifwrite_to
isNone
(as default), the function will return the c-code as string (type ofstr
)
py2c.TranslatorC(save_to)
- class of python-to-c translator. Contains some methods to translate constructions. Arguments:
save_to
- a file object to write the result c-code
py2c.translate(translator, source_code: str)
- convert source_code
into AST, walk it and build c-code. Arguments:
translator
- a instance of translator. For example,py2c.TranslatorC
orpy2c.TranslatorCpp
source_code
- a source python-code (type ofstr
)
You can use py2c
or python -m py2c
equivalently.
Create your_source_code.c with c-code:
py2c your_source_code.py
Create your_output_code.c with c-code:
py2c your_source_code.py -o your_output_code.c
Print c-code into console:
py2c your_source_code.py -p
If you need help, ask me:
Telegram to get fresh news: