Skip to main content

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

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/*

Project details


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.1.0.tar.gz (28.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

source_translator-1.1.0-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file source_translator-1.1.0.tar.gz.

File metadata

  • Download URL: source_translator-1.1.0.tar.gz
  • Upload date:
  • Size: 28.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for source_translator-1.1.0.tar.gz
Algorithm Hash digest
SHA256 4c37a982ec6f38626ddcc14ce987300d6c847fd758cdfdd12b8777a86712becf
MD5 16e023ca27d837d7573aea4b8db7e304
BLAKE2b-256 c63cba12540750ab2e0c248b5f010081a775060d0d00ce26c8e42b0a486056bb

See more details on using hashes here.

File details

Details for the file source_translator-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for source_translator-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54a77ca78b991a4b48458820f5af69f262c8ff726dfaeedc9988e41a127c3107
MD5 2f87f95c0cbe5eeb1bfe135d53fe2e7e
BLAKE2b-256 c56701e4f347a9af74c66390f14923798cbe23703958053ff16abfe5bd949b07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page