Module to translate Python source code into other programming languages
Project description
Python Source Translator
Module to translate Python source code into other programming languages
It supports source conversion of most Python features, but the output code might need some minor tweaks to run in the target language.
Examples
Simple usage:
from source_translator import SourceCode
from source_translator.langs import cpp
code = SourceCode("""
def my_function(a: int, b: int) -> int:
'''
This function does something
'''
if a > b:
# Sum them
return a + b
else:
return a
""")
print(cpp.CppTranslator().convert(code))
/**
* This function does something
*/
int my_function(int a, int b)
{
if ( a > b )
{
// Sum them
return a + b;
}
else
{
return a;
}
}
Indentation styling:
from source_translator import SourceCode
from source_translator.langs import cpp
from source_translator.c_like import KandRStyle
code = SourceCode("""
def my_function(a: int, b: int) -> int:
'''
This function does something
'''
if a > b:
# Sum them
return a + b
else:
return a
""")
print(cpp.CppTranslator(KandRStyle(spaces=8)).convert(code))
/**
* This function does something
*/
int my_function(int a, int b) {
if ( a > b ) {
// Sum them
return a + b;
} else {
return a;
}
}
Converting a Python function:
import inspect
from source_translator import SourceCode
from source_translator.langs import cpp
def my_function(a: int, b: int) -> int:
'''
This function does something
'''
if a > b:
# Sum them
return a + b
else:
return a
code = SourceCode(inspect.getsource(my_function))
print(cpp.CppTranslator().convert(code))
Supported Languages
C++
from source_translator import SourceCode
from source_translator.langs import cpp
code = SourceCode("""
def my_function(a: int, b: int) -> int:
'''
This function does something
'''
if a > b:
# Sum them
return a + b
else:
return a
""")
print(cpp.CppTranslator().convert(code))
/**
* This function does something
*/
int my_function(int a, int b)
{
if ( a > b )
{
// Sum them
return a + b;
}
else
{
return a;
}
}
TypeScript
from source_translator import SourceCode
from source_translator.langs import ts
code = SourceCode("""
def my_function(a: int, b: int) -> int:
'''
This function does something
'''
if a > b:
# Sum them
return a + b
else:
return a
""")
print(ts.TypeScriptTranslator().convert(code))
/**
* This function does something
*/
function myFunction(a: number, b: number): number {
if ( a > b ) {
// Sum them
return a + b;
} else {
return a;
}
}
JavaScript
from source_translator import SourceCode
from source_translator.langs import ts
code = SourceCode("""
def my_function(a: int, b: int) -> int:
'''
This function does something
'''
if a > b:
# Sum them
return a + b
else:
return a
""")
print(ts.TypeScriptTranslator(False).convert(code))
/**
* This function does something
*/
function myFunction(a, b) {
if ( a > b ) {
// Sum them
return a + b;
} else {
return a;
}
}
License
Copyright (C) 2023-2024 Mattia Basaglia
GPLv3+ (see COPYING)
Development
Requirements
pip install twine build coverage
Running Tests
./test.sh [testcase]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
source_translator-1.0.0.tar.gz
(26.0 kB
view details)
Built Distribution
File details
Details for the file source_translator-1.0.0.tar.gz
.
File metadata
- Download URL: source_translator-1.0.0.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e13d6d82046f37760aab319da4a59f5a576e74e9f28dc2a2840e3d28822acfcd |
|
MD5 | a0dcccbde5f3ff8b1b59df72a18f1c09 |
|
BLAKE2b-256 | 535de47eacfa48ce0c1f22ed2e2783824b710e720db0b4e6104efa155010bbf6 |
File details
Details for the file source_translator-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: source_translator-1.0.0-py3-none-any.whl
- Upload date:
- Size: 24.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df28acf534eee145d52bf8f146b7ae0cf50214899938e79d1cb29d5f4c25ba20 |
|
MD5 | 3df3d2bdd32a89db6397b32ebe82f1e4 |
|
BLAKE2b-256 | 6ca6a990f900c9dd506ccc223c8fb658271855676cbcfba65b3937f22bb452a9 |