Skip to main content

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

Markarth is available on the PyPI index. You can install it using pip:

pip install markarth

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.

The module's functionality is divided in two main steps:

  • one in which the code parsed with ast is analyzed to detect types of variables. Results of elementary operations are used, together with invocations of functions like len().
  • another in which the types collected are converted to cython codelines.

There are additional functionalities to provide conversion options, in order to use cython.double instead of cython.float as type during conversion.

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

Metadata

Release files for markarth 0.0.5

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

Source distribution (sdist)

Source distribution for markarth 0.0.5
File Size Uploaded Provenance
markarth-0.0.5.tar.gz 26.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for markarth 0.0.5
File Interpreter ABI Platform
markarth-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 50.0 kB

Release files / markarth-0.0.5.tar.gz

Download URL markarth-0.0.5.tar.gz
Size 26.7 kB
Tags Source
SHA-256 checksum
How to use checksums
815c1b8fd221c130d9a49e7021fa6f0ad601701462777ff9b1749839bb3661bb
BLAKE2b-256 checksum
How to use checksums
3251271736acfa93299e7dc142ddc1cf426a61242c4bcffacb34d035a8d50ccb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Release files / markarth-0.0.5-py3-none-any.whl

Download URL markarth-0.0.5-py3-none-any.whl
Size 23.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d9b1f29eb357b3d074d80c9b51b8bd51dabf55935780443c183e9b9a60dbbd6
BLAKE2b-256 checksum
How to use checksums
61d4378ab4cc4b1b8554eee4a8e6b7ffb89258c9bb46f75fdbb02136c7f2c996
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Security for markarth 0.0.5

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