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
Translate Python into C:
from py2c.shortcuts import trans_c
print(trans_c('''
def hello(a: int, b: int = 5) -> 'const char*':
return 'positive' if a + b > 0 else 'negative or zero'
def main() -> int:
print(hello(-10))
return 0
'''))This will be printed:
#include "stdio.h"
const char* hello(int a, int b) {
return (a + b > 0) ? "positive" : "negative or zero";
}
const char* hello(int a) {
return hello(a, 5);
}
int main(void) {
printf(hello(-10));
return 0;
}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) -> 'const char*':
return 'positive' if a + b > 0 else 'negative or zero'
def main() -> int:
print(hello(-10))
return 0
'''
with open('c_code.c') as c_file:
translator = TranslatorC(save_to=c_file)
translate(translator, my_python_code)Translate Python into C and run this C-code from Python:
from py2c.shortcuts import inline_c
def someFunctionCalledFromTheCppFunction(var: int, var2: int) -> int:
return var + var2
def somePurePythonFunction(var: int, var2: int) -> int:
return var * var2
@inline_c
def someFunctionThatGetsCompiledAndCachedAtExecutionTime(var: int, var2: int) -> int:
return someFunctionCalledFromTheCppFunction(var, var2)
theAnswerToTheUniverse: int = someFunctionThatGetsCompiledAndCachedAtExecutionTime(30, 2) + somePurePythonFunction(5, 2)
print(f"theAnswerToTheUniverse: {theAnswerToTheUniverse}")Output: theAnswerToTheUniverse: 42
Translate, compile and run per one time:
python3 demo.py 1> demo.c && g++ demo.c && g++ demo.c && ./a.outRun Python-code and C-code together:
python3 demo_inline_c.pyUse this command to install Py2C:
pip install git+https://github.com/syeysk/sy_py2c.git
apt install python3.11-devWrite your python version instead of python3.11.
Use this command to uninstall Py2C:
pip uninstall py2cpy2c.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 (returningopenfunction)write_to- a file object to write a result c-code. Ifwrite_toisNone(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.TranslatorCorpy2c.TranslatorCppsource_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.pyCreate your_output_code.c with c-code:
py2c your_source_code.py -o your_output_code.cPrint c-code into console:
py2c your_source_code.py -pGet help, make issue or discuss Py2C:
Get fresh news about another my projects:
