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.
Metadata
Release files for csnake 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| csnake-0.4.0.tar.gz | 28.6 kB | Without provenance | Details for csnake-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| csnake-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for csnake-0.4.0-py3-none-any.whl |
Total release size: 58.9 kB
Release files / csnake-0.4.0.tar.gz
| Download URL | csnake-0.4.0.tar.gz |
|---|---|
| Size | 28.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dfed729c6f32ff44fd09797328aff5f077088a479c7da4bfa3af55dc6310bef2
|
|
BLAKE2b-256 checksum How to use checksums |
1700066c4ec4ccb138b5ff5d7a07e30ca22f4b240c296cc4148c8a8c043da8f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.4 Linux/5.15.154+
|
Release files / csnake-0.4.0-py3-none-any.whl
| Download URL | csnake-0.4.0-py3-none-any.whl |
|---|---|
| Size | 30.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8971b91c290357dba35a06ef9b16e5d3a9f8ae4027c8329f8207b40bb5dafebe
|
|
BLAKE2b-256 checksum How to use checksums |
683fdf3336c1eaf10ce5aaf9a97cda028f205e91e95d15fb0a94344c0bfd4902
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.4 Linux/5.15.154+
|
Security for csnake 0.4.0
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.