Skip to main content

Automatic code conversion from python to cython

Project description

markarth

Coverage badge

markarth is a library to automatically cythonize python code.

It can receive in input python code, evaluate the primitive types that variables are going to possess, and then add codelines for pure python cython mode.

Example

This is an example of python code using markarth to take a code section as a string and returns a string with cython annotations.

code = '''
def stuff(a : int, b : int, c : float = 0.4, d = None) -> int:
    sum = 0
    m = 11
    onono = 17.4
    for i in range(4):
        p = 7
        h = float(64) * p
        sum += i
        sum = 5 * 18
    return float(sum)

'''

cycode = convert_code(code)

print(cycode)

The resulting modified code will be the following, with cython.declare codelines:

import cython

def stuff(a : int, b : int, c : float = 0.4, d = None) -> int:
    h = cython.declare(cython.float)
    p = cython.declare(cython.int)
    i = cython.declare(cython.int)
    onono = cython.declare(cython.float)
    m = cython.declare(cython.int)
    sum = cython.declare(cython.int)
    sum = 0
    m = 11
    onono = 17.4
    for i in range(4):
        p = 7
        h = float(64) * p
        sum += i
        sum = 5 * 18
    return float(sum)

Installation

At the moment there is no PyPI package, so if someone really (really?) wants to run such thing, the repo should be cloned, to then run the setup.py script in the cloning folder

git clone https://github.com/nucccc/markarth
cd markarth
pip install .

Internal functioning

It makes heavy usage of the ast module in order to parse python code, while leveraging type annotations to infer variable types, thus adding some declarations that with python would speed the code up.

Possible improvements

Several possible improvements can be done (apart from testing and discovering bugs, obviously). Among them:

  • Store and extrapolate types of containers (lists, dicts, sets, ecc.)
  • Identify numpy array and type them as memory views
  • Detect := walrus operators
  • Possibility of selecting between pure python mode conversion and old style pure cython with cdef VARNAME int codelines

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

markarth-0.0.4.tar.gz (25.9 kB view details)

Uploaded Source

File details

Details for the file markarth-0.0.4.tar.gz.

File metadata

  • Download URL: markarth-0.0.4.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for markarth-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a6ca5185949acc3a78e02994f88f44f9f2f68ad0dd774124ad8848b979c6ba9b
MD5 798b474847a3822781ea9716ed78b099
BLAKE2b-256 079ce2744985362e68e6fa70f4f35ef464a3e2196ad9d3ef6d2007e7274a926b

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