Skip to main content

A Python-to-C++ translator.

Project description

py2cpp

A Python-to-C++ translator that converts a restricted subset of Python into clean and readable C++ code. It offers both Ahead-of-Time (AOT) compilation through a command-line tool and Just-in-Time (JIT) compilation via a decorator.

Online Simulator

Key Features

  • Dual Compilation Modes:
    • Ahead-of-Time (AOT): Convert entire Python files to C++ source code or executables using the py2cpp command-line tool.
    • Just-in-Time (JIT): Use the @jit decorator to compile and run specific Python functions in C++ at runtime for a performance boost.
  • Readable C++ Code: Translates Python syntax into human-readable and clean C++ code.
  • Type-Hint Driven: Utilizes Python type hints to generate corresponding C++ types.
  • C-Style Data Structures: Define C-like structures directly in Python using the @c_struct decorator.
  • Fine-Grained Type Control: Provides special types like c_int, c_double, c_char, etc., for precise control over the generated C++ data types.
  • Python Interoperability: Allows passing Python objects to and from JIT-compiled functions.

Note:

The actual translation may vary based on the specific implementation details of py2cpp.

This project doesn't optimize your code for performance or memory usage.

Project Goals

  1. Provide a static translation layer. (Without using A.I.)
  2. Offer a simplified Python-like syntax that maps cleanly to C++ constructs.
  3. Covering all python syntax constructs as much as possible.
  4. Minimize boilerplate on both the Python input side and the C++ output side.

Installation

pip install py2cpp

Usage

Command-Line Usage (AOT)

The py2cpp command-line tool translates .py files into .cpp files or executables.

# Translate to C++
py2cpp input.py

# Translate to a C++ executable
py2cpp input.py -c exe

# Specify output file
py2cpp input.py -o output.cpp
py2cpp input.py -o output.exe

JIT-Compilation Usage

Use the @jit decorator for on-the-fly compilation and execution of Python functions.

from py2cpp.core.jit import jit

@jit
def add(a: int, b: int) -> int:
    return a + b

# The 'add' function is now a compiled C++ function
result = add(5, 10)
print(result)

Examples

AOT Compilation Example

This Python code:

def add(a: int, b: int) -> int:
    return a + b

x = input()
y = 5

print(add(int(x), y))

translates to this C++ code:

#include <iostream>
#include <string>

long add(long a, long b) {
    return a + b;
}

int main() {
    std::string x;
    std::getline(std::cin, x);
    long y = 5;
    std::cout << add(std::stol(x), y) << std::endl;
    return 0;
}

JIT Compilation Example

Here is a simple example of using the @jit decorator to accelerate a function:

from py2cpp.core.jit import jit
from time import time

@jit
def fast_add(a: int, b: int) -> int:
    n = a
    for _ in range(100000000):
        n += b
    return n

start = time()
result = fast_add(10, 20)
print(f"Result: {result}")
print(f"Execution time: {time() - start:.4f}s")

License

MIT License. See LICENSE file for details.

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

py2cpp-1.7.1.tar.gz (30.6 kB view details)

Uploaded Source

Built Distribution

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

py2cpp-1.7.1-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file py2cpp-1.7.1.tar.gz.

File metadata

  • Download URL: py2cpp-1.7.1.tar.gz
  • Upload date:
  • Size: 30.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for py2cpp-1.7.1.tar.gz
Algorithm Hash digest
SHA256 7a3b84eaf127dc712704c7d1893ae9fdaf876e4dab01d634332fc0282aa24c6c
MD5 f34693a2407573569758dd5a8dd186ff
BLAKE2b-256 526d90c116343941391f1caa805e919a6ab9ba6d840284b16775a13f570c924d

See more details on using hashes here.

File details

Details for the file py2cpp-1.7.1-py3-none-any.whl.

File metadata

  • Download URL: py2cpp-1.7.1-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for py2cpp-1.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 73f975d0af16983be691bd10583f70dba8360bbf34fbca39cb5b022d903a1098
MD5 046c3b8d35e9303054f81ae32a9bc594
BLAKE2b-256 bf4bdbf6140fadbf10cdaabef6661a3c4fab1dde7f301669dbc330efa409d84e

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