csnake


Namecsnake JSON
Version 0.4.0 PyPI version JSON
download
home_pagehttps://gitlab.com/andrejr/csnake
SummaryC code generation helper package.
upload_time2024-07-13 17:26:59
maintainerNone
docs_urlNone
authorAndrej Radović
requires_python<4.0,>=3.9
licenseMIT
keywords codegen c embedded
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ######
csnake
######

.. image:: https://gitlab.com/andrejr/csnake/badges/master/pipeline.svg
   :alt: pipeline status
   :target: https://gitlab.com/andrejr/csnake/pipelines
.. image:: https://gitlab.com/andrejr/csnake/badges/master/coverage.svg
   :alt: coverage report
   :target: https://andrejr.gitlab.io/csnake/coverage/index.html

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,
``Map``\s and ``Collection``\s), nested arbitrarily.

Here's a taste:

.. code-block:: python

   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:

.. code-block:: c

    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 <https://nedbatchelder.com/code/cog/>`_.

Documentation
=============

Documentation (Sphinx) can be viewed on
`GitLab pages for this package <https://andrejr.gitlab.io/csnake/>`_.

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 <https://github.com/SchrodingersGat/C-Snake>`_
by
`Oliver <https://github.com/SchrodingersGat>`_
(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 <https://andrejr.gitlab.io/csnake/changes.html>`_.

            

Raw data

            {
    "_id": null,
    "home_page": "https://gitlab.com/andrejr/csnake",
    "name": "csnake",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "codegen, c, embedded",
    "author": "Andrej Radovi\u0107",
    "author_email": "r.andrej@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/17/00/066c4ec4ccb138b5ff5d7a07e30ca22f4b240c296cc4148c8a8c043da8f0/csnake-0.4.0.tar.gz",
    "platform": null,
    "description": "######\ncsnake\n######\n\n.. image:: https://gitlab.com/andrejr/csnake/badges/master/pipeline.svg\n   :alt: pipeline status\n   :target: https://gitlab.com/andrejr/csnake/pipelines\n.. image:: https://gitlab.com/andrejr/csnake/badges/master/coverage.svg\n   :alt: coverage report\n   :target: https://andrejr.gitlab.io/csnake/coverage/index.html\n\nCsnake is a Python 3 package that helps you generate C code from Python.\n\nCsnake provides you with a consistent and opinionated API that helps you\nstructure your C-generating Python code.\nIt does so by providing classes and functions for generating every C language\nconstruct.\n\nProbably the most important feature is the ability to initialize a value to\n``struct`` and *array* initializers from Python *dicts* and *lists* (actually,\n``Map``\\s and ``Collection``\\s), nested arbitrarily.\n\nHere's a taste:\n\n.. code-block:: python\n\n   from csnake import CodeWriter, Variable, FormattedLiteral\n   import numpy as np\n\n   var = Variable(\n       \"test\",\n       primitive=\"struct whatever\",\n       value={\n           \"field1\": [{\"x\": num, \"y\": 10 - num} for num in range(2)],\n           \"field2\": {\"test\": range(3), \"field\": np.arange(6).reshape(2, 3)},\n           \"field3\": FormattedLiteral([30, 31, 32], int_formatter=hex),\n           \"field4\": 8,\n       },\n   )\n   cw = CodeWriter()\n   cw.add_variable_initialization(var)\n   print(cw)\n\n\nThis yields:\n\n.. code-block:: c\n\n    struct whatever test = {\n        .field1 = {\n            {\n                .x = 0,\n                .y = 10\n            },{\n                .x = 1,\n                .y = 9\n            }\n        },\n        .field2 = {\n            .test = {0, 1, 2},\n            .field = {\n                {0, 1, 2},\n                {3, 4, 5}\n            }\n\n        },\n        .field3 = {0x1e, 0x1f, 0x20},\n        .field4 = 8\n    };\n\nAs shown, ``numpy`` arrays are supported as values (so are ``sympy`` arrays),\nand values can be formatted by arbitrary functions (here we're using ``hex`` to\noutput ints as hex literals for member `field3`).\n\nMotivation\n==========\n\nCsnake's varable generation was motivated by a common embedded development\ntask: inputting data into C code.\n\nCsnake should be of help when generating C code for representing data like\nbitmaps, fonts, statemachines, lookup tables - as arrays and structs.\nIt can also be used for loop unrolling, templating, ...\n\nCsnake  can be easily incorporated into a build system (Make, CMake,\nScons,...), and also goes along great with Jinja2 and\n`Ned Batchelder's cog <https://nedbatchelder.com/code/cog/>`_.\n\nDocumentation\n=============\n\nDocumentation (Sphinx) can be viewed on\n`GitLab pages for this package <https://andrejr.gitlab.io/csnake/>`_.\n\nExamples\n========\n\nCsnake is used on several of my yet-to-be-released open source embedded\nprojects. I'll be adding those (and other) examples along the way.\n\nCredits\n=======\n\nCsnake is a major re-implementation (and improvement) of\n`C-Snake <https://github.com/SchrodingersGat/C-Snake>`_\nby\n`Oliver <https://github.com/SchrodingersGat>`_\n(original idea) and Andrej (variable initialization idea and implementation,\nauthor of this package).\n\nIt's provided under the MIT license.\n\nChangelog\n=========\n\nThe changelog can be found within the documentation, \n`here <https://andrejr.gitlab.io/csnake/changes.html>`_.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "C code generation helper package.",
    "version": "0.4.0",
    "project_urls": {
        "Documentation": "https://andrejr.gitlab.io/csnake/",
        "Homepage": "https://gitlab.com/andrejr/csnake",
        "Repository": "https://gitlab.com/andrejr/csnake"
    },
    "split_keywords": [
        "codegen",
        " c",
        " embedded"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "683fdf3336c1eaf10ce5aaf9a97cda028f205e91e95d15fb0a94344c0bfd4902",
                "md5": "859dd7e71d74fbcc46ecff184adce21c",
                "sha256": "8971b91c290357dba35a06ef9b16e5d3a9f8ae4027c8329f8207b40bb5dafebe"
            },
            "downloads": -1,
            "filename": "csnake-0.4.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "859dd7e71d74fbcc46ecff184adce21c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 30259,
            "upload_time": "2024-07-13T17:26:58",
            "upload_time_iso_8601": "2024-07-13T17:26:58.291681Z",
            "url": "https://files.pythonhosted.org/packages/68/3f/df3336c1eaf10ce5aaf9a97cda028f205e91e95d15fb0a94344c0bfd4902/csnake-0.4.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1700066c4ec4ccb138b5ff5d7a07e30ca22f4b240c296cc4148c8a8c043da8f0",
                "md5": "5dab14212d66266124de3c1961e5031a",
                "sha256": "dfed729c6f32ff44fd09797328aff5f077088a479c7da4bfa3af55dc6310bef2"
            },
            "downloads": -1,
            "filename": "csnake-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "5dab14212d66266124de3c1961e5031a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 28619,
            "upload_time": "2024-07-13T17:26:59",
            "upload_time_iso_8601": "2024-07-13T17:26:59.940451Z",
            "url": "https://files.pythonhosted.org/packages/17/00/066c4ec4ccb138b5ff5d7a07e30ca22f4b240c296cc4148c8a8c043da8f0/csnake-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-13 17:26:59",
    "github": false,
    "gitlab": true,
    "bitbucket": false,
    "codeberg": false,
    "gitlab_user": "andrejr",
    "gitlab_project": "csnake",
    "lcname": "csnake"
}
        
Elapsed time: 3.82948s