Skip to main content

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;
    }
}

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

pip install twine build coverage

Running Tests

./test.sh [testcase]

Building Packages

python3 -m build --sdist
python3 -m build --wheel
twine upload dist/*

Metadata

Release files for source-translator 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for source-translator 1.1.0
File Size Uploaded Provenance Actions
source_translator-1.1.0.tar.gz 28.7 kB Without provenance Details for source_translator-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for source-translator 1.1.0
File Interpreter ABI Platform Actions
source_translator-1.1.0-py3-none-any.whl Python 3 none any Details for source_translator-1.1.0-py3-none-any.whl

Total release size: 57.4 kB

Release files / source_translator-1.1.0.tar.gz

Download URL source_translator-1.1.0.tar.gz
Size 28.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4c37a982ec6f38626ddcc14ce987300d6c847fd758cdfdd12b8777a86712becf
BLAKE2b-256 checksum
How to use checksums
c63cba12540750ab2e0c248b5f010081a775060d0d00ce26c8e42b0a486056bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / source_translator-1.1.0-py3-none-any.whl

Download URL source_translator-1.1.0-py3-none-any.whl
Size 28.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54a77ca78b991a4b48458820f5af69f262c8ff726dfaeedc9988e41a127c3107
BLAKE2b-256 checksum
How to use checksums
c56701e4f347a9af74c66390f14923798cbe23703958053ff16abfe5bd949b07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Security for source-translator 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page