Skip to main content

C code generation helper package.

Project description

pipeline status coverage report

Csnake is a Python 3 package that helps you generate C code from Python.

Csnake provides you with a consistent and opinionated API that helps you structure your C-generating Python code. It does so by providing classes and functions for generating every C language construct.

Probably the most important feature is the ability to initialize a value to struct and array initializers from Python dicts and lists (actually, Maps and Collections), nested arbitrarily.

Here’s a taste:

from csnake import CodeWriter, Variable, FormattedLiteral
import numpy as np

var = Variable(
    "test",
    primitive="struct whatever",
    value={
        "field1": [{"x": num, "y": 10 - num} for num in range(2)],
        "field2": {"test": range(3), "field": np.arange(6).reshape(2, 3)},
        "field3": FormattedLiteral([30, 31, 32], int_formatter=hex),
        "field4": 8,
    },
)
cw = CodeWriter()
cw.add_variable_initialization(var)
print(cw)

This yields:

struct whatever test = {
    .field1 = {
        {
            .x = 0,
            .y = 10
        },{
            .x = 1,
            .y = 9
        }
    },
    .field2 = {
        .test = {0, 1, 2},
        .field = {
            {0, 1, 2},
            {3, 4, 5}
        }

    },
    .field3 = {0x1e, 0x1f, 0x20},
    .field4 = 8
};

As shown, numpy arrays are supported as values (so are sympy arrays), and values can be formatted by arbitrary functions (here we’re using hex to output ints as hex literals for member field3).

Motivation

Csnake’s varable generation was motivated by a common embedded development task: inputting data into C code.

Csnake should be of help when generating C code for representing data like bitmaps, fonts, statemachines, lookup tables - as arrays and structs. It can also be used for loop unrolling, templating, …

Csnake can be easily incorporated into a build system (Make, CMake, Scons,…), and also goes along great with Jinja2 and Ned Batchelder’s cog.

Documentation

Documentation (Sphinx) can be viewed on GitLab pages for this package.

Examples

Csnake is used on several of my yet-to-be-released open source embedded projects. I’ll be adding those (and other) examples along the way.

Credits

Csnake is a major re-implementation (and improvement) of C-Snake by Oliver (original idea) and Andrej (variable initialization idea and implementation, author of this package).

It’s provided under the MIT license.

Changelog

The changelog can be found within the documentation, here.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

csnake-0.4.0.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

csnake-0.4.0-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file csnake-0.4.0.tar.gz.

File metadata

  • Download URL: csnake-0.4.0.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/5.15.154+

File hashes

Hashes for csnake-0.4.0.tar.gz
Algorithm Hash digest
SHA256 dfed729c6f32ff44fd09797328aff5f077088a479c7da4bfa3af55dc6310bef2
MD5 5dab14212d66266124de3c1961e5031a
BLAKE2b-256 1700066c4ec4ccb138b5ff5d7a07e30ca22f4b240c296cc4148c8a8c043da8f0

See more details on using hashes here.

File details

Details for the file csnake-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: csnake-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/5.15.154+

File hashes

Hashes for csnake-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8971b91c290357dba35a06ef9b16e5d3a9f8ae4027c8329f8207b40bb5dafebe
MD5 859dd7e71d74fbcc46ecff184adce21c
BLAKE2b-256 683fdf3336c1eaf10ce5aaf9a97cda028f205e91e95d15fb0a94344c0bfd4902

See more details on using hashes here.

Supported by

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