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:
```py
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))
```
```cpp
/**
* This function does something
*/
int my_function(int a, int b)
{
if ( a > b )
{
// Sum them
return a + b;
}
else
{
return a;
}
}
```
Indentation styling:
```py
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))
```
```cpp
/**
* 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:
```py
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++
```py
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))
```
```cpp
/**
* This function does something
*/
int my_function(int a, int b)
{
if ( a > b )
{
// Sum them
return a + b;
}
else
{
return a;
}
}
```
TypeScript
```py
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))
```
```ts
/**
* This function does something
*/
function myFunction(a: number, b: number): number {
if ( a > b ) {
// Sum them
return a + b;
} else {
return a;
}
}
```
JavaScript
```py
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))
```
```js
/**
* This function does something
*/
function myFunction(a, b) {
if ( a > b ) {
// Sum them
return a + b;
} else {
return a;
}
}
```
PHP
```php
/**
* This function does something
*/
function my_function(int $a, int $b): int
{
if ( $a > $b )
{
// Sum them
return $a + $b;
}
else
{
return $a;
}
}
```
License
-------
Copyright (C) 2023-2024 Mattia Basaglia
GPLv3+ (see COPYING)
Development
-----------
### Requirements
```bash
pip install twine build coverage
```
### Running Tests
```bash
./test.sh [testcase]
```
### Building Packages
```bash
python3 -m build --sdist
python3 -m build --wheel
twine upload dist/*
```

Raw data
{
"_id": null,
"home_page": "https://gitlab.com/mattbas/python-source-translator",
"name": "source-translator",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.9",
"maintainer_email": null,
"keywords": "transpiling, source convertion",
"author": "Mattia \"Glax\" Basaglia",
"author_email": "dev@dragon.best",
"download_url": "https://files.pythonhosted.org/packages/41/c0/fa77ab87ff04b47e9519312e8950f3a21fbb598b306384c8e6ba47e54fb7/source_translator-1.0.1.tar.gz",
"platform": null,
"description": "Python Source Translator\n========================\n\nModule to translate Python source code into other programming languages\n\nIt supports source conversion of most Python features, but the output code might\nneed some minor tweaks to run in the target language.\n\n\nExamples\n--------\n\nSimple usage:\n\n```py\nfrom source_translator import SourceCode\nfrom source_translator.langs import cpp\n\ncode = SourceCode(\"\"\"\ndef my_function(a: int, b: int) -> int:\n '''\n This function does something\n '''\n if a > b:\n # Sum them\n return a + b\n else:\n return a\n\"\"\")\n\nprint(cpp.CppTranslator().convert(code))\n```\n\n```cpp\n/**\n * This function does something\n */\nint my_function(int a, int b)\n{\n if ( a > b )\n {\n // Sum them\n return a + b;\n }\n else\n {\n return a;\n }\n}\n```\n\nIndentation styling:\n\n```py\nfrom source_translator import SourceCode\nfrom source_translator.langs import cpp\nfrom source_translator.c_like import KandRStyle\n\ncode = SourceCode(\"\"\"\ndef my_function(a: int, b: int) -> int:\n '''\n This function does something\n '''\n if a > b:\n # Sum them\n return a + b\n else:\n return a\n\"\"\")\n\nprint(cpp.CppTranslator(KandRStyle(spaces=8)).convert(code))\n```\n\n```cpp\n/**\n * This function does something\n */\nint my_function(int a, int b) {\n if ( a > b ) {\n // Sum them\n return a + b;\n } else {\n return a;\n }\n}\n```\n\nConverting a Python function:\n\n```py\nimport inspect\nfrom source_translator import SourceCode\nfrom source_translator.langs import cpp\n\n\ndef my_function(a: int, b: int) -> int:\n '''\n This function does something\n '''\n if a > b:\n # Sum them\n return a + b\n else:\n return a\n\n\ncode = SourceCode(inspect.getsource(my_function))\n\nprint(cpp.CppTranslator().convert(code))\n```\n\nSupported Languages\n-------------------\n\nC++\n\n```py\nfrom source_translator import SourceCode\nfrom source_translator.langs import cpp\n\ncode = SourceCode(\"\"\"\ndef my_function(a: int, b: int) -> int:\n '''\n This function does something\n '''\n if a > b:\n # Sum them\n return a + b\n else:\n return a\n\"\"\")\n\nprint(cpp.CppTranslator().convert(code))\n```\n\n```cpp\n/**\n * This function does something\n */\nint my_function(int a, int b)\n{\n if ( a > b )\n {\n // Sum them\n return a + b;\n }\n else\n {\n return a;\n }\n}\n```\n\nTypeScript\n\n```py\nfrom source_translator import SourceCode\nfrom source_translator.langs import ts\n\ncode = SourceCode(\"\"\"\ndef my_function(a: int, b: int) -> int:\n '''\n This function does something\n '''\n if a > b:\n # Sum them\n return a + b\n else:\n return a\n\"\"\")\n\nprint(ts.TypeScriptTranslator().convert(code))\n```\n\n```ts\n/**\n * This function does something\n */\nfunction myFunction(a: number, b: number): number {\n if ( a > b ) {\n // Sum them\n return a + b;\n } else {\n return a;\n }\n}\n```\nJavaScript\n\n```py\nfrom source_translator import SourceCode\nfrom source_translator.langs import ts\n\ncode = SourceCode(\"\"\"\ndef my_function(a: int, b: int) -> int:\n '''\n This function does something\n '''\n if a > b:\n # Sum them\n return a + b\n else:\n return a\n\"\"\")\n\nprint(ts.TypeScriptTranslator(False).convert(code))\n```\n\n```js\n/**\n * This function does something\n */\nfunction myFunction(a, b) {\n if ( a > b ) {\n // Sum them\n return a + b;\n } else {\n return a;\n }\n}\n```\n\n\nPHP\n\n```php\n/**\n * This function does something\n */\nfunction my_function(int $a, int $b): int\n{\n if ( $a > $b )\n {\n // Sum them\n return $a + $b;\n }\n else\n {\n return $a;\n }\n}\n```\n\nLicense\n-------\n\nCopyright (C) 2023-2024 Mattia Basaglia\n\nGPLv3+ (see COPYING)\n\n\nDevelopment\n-----------\n\n### Requirements\n\n```bash\npip install twine build coverage\n```\n\n### Running Tests\n\n```bash\n./test.sh [testcase]\n```\n\n### Building Packages\n\n```bash\npython3 -m build --sdist\npython3 -m build --wheel\ntwine upload dist/*\n```\n\ufffc\n",
"bugtrack_url": null,
"license": null,
"summary": "Module to translate Python source code into other programming languages",
"version": "1.0.1",
"project_urls": {
"Bug Reports": "https://gitlab.com/mattbas/python-source-translator/-/issues",
"Homepage": "https://gitlab.com/mattbas/python-source-translator",
"Source": "https://gitlab.com/mattbas/python-source-translator"
},
"split_keywords": [
"transpiling",
" source convertion"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8a9265a15948ce8add32be8746047697803b2a768fbf716250cd0c5b7d296def",
"md5": "156d372190e07a24a49f766738cbe638",
"sha256": "aa50b0010b4c3942e7be757acc751b9ea60c561fedb76ddea32d52d6b8b6e0b4"
},
"downloads": -1,
"filename": "source_translator-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "156d372190e07a24a49f766738cbe638",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.9",
"size": 27296,
"upload_time": "2024-11-23T10:28:05",
"upload_time_iso_8601": "2024-11-23T10:28:05.397033Z",
"url": "https://files.pythonhosted.org/packages/8a/92/65a15948ce8add32be8746047697803b2a768fbf716250cd0c5b7d296def/source_translator-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "41c0fa77ab87ff04b47e9519312e8950f3a21fbb598b306384c8e6ba47e54fb7",
"md5": "aaae732ff1af18d4599eb911933950b3",
"sha256": "1e63f74d6bdc7249448ec0fccd477203b813e35e3e74e37335ca351a44fd65bd"
},
"downloads": -1,
"filename": "source_translator-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "aaae732ff1af18d4599eb911933950b3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.9",
"size": 27738,
"upload_time": "2024-11-23T10:28:07",
"upload_time_iso_8601": "2024-11-23T10:28:07.944827Z",
"url": "https://files.pythonhosted.org/packages/41/c0/fa77ab87ff04b47e9519312e8950f3a21fbb598b306384c8e6ba47e54fb7/source_translator-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-23 10:28:07",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "mattbas",
"gitlab_project": "python-source-translator",
"lcname": "source-translator"
}