ast2json


Nameast2json JSON
Version 0.4 PyPI version JSON
download
home_pagehttps://github.com/Psycojoker/ast2json
Summaryconvert a python source code into json-dumpable data (dict and lists with strings, ints, ...)
upload_time2025-02-07 13:54:01
maintainerNone
docs_urlNone
authorLaurent Peuch
requires_pythonNone
licenseBSD
keywords ast
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            Introduction
============

ast2json is a small module that will convert any python AST node into its ast
representation formatted in a JSON compatible python representation (list and
dicts containing string, ints and null).

Installation
============

::

    pip install ast2json
    # or to have lastest version
    pip install git+git://github.com/Psycojoker/ast2json.git

Usage
=====

::

    >>> import json
    >>> from ast import parse
    >>> from ast2json import ast2json

    >>> ast = ast2json(parse(open('some_python_source_file.py').read()))
    >>> print json.dumps(ast, indent=4)

If you are lazy, "str2json" will apply the "parse" method of ast on a string for you, so you'll be able to write:

::

    >>> str2json(open('some_python_source_file.py').read())

You can also run via the command line to parse Python source from stdin and print to stdout:

::

    $ ast2json < some_python_source_file.py


Example
=======

This is the result of converting 'print "Hello World!"' (and applying json.dumps on the result).

::

    {
        "body": [
            {
                "_type": "Print", 
                "nl": true, 
                "col_offset": 0, 
                "dest": null, 
                "values": [
                    {
                        "s": "Hello World!", 
                        "_type": "Str", 
                        "lineno": 1, 
                        "col_offset": 6
                    }
                ], 
                "lineno": 1
            }
        ], 
        "_type": "Module"
    }


Changelog
=========

0.4 (2025-02-07)
----------------

* you can now use "ast2json" directly from the CLI (it reads stdin) thx to @vergenzt
* output keys are now sorted to be deterministic
* various small code improvements

0.2.1 (2016-11-13)
------------------

* fix broken pip installation by @abolger

0.2 (2016-09-03)
----------------

* python 3 support and some unit testing by Juncheol Cho @zironycho

Licence
=======

BSD

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Psycojoker/ast2json",
    "name": "ast2json",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "ast",
    "author": "Laurent Peuch",
    "author_email": "cortex@worlddomination.be",
    "download_url": "https://files.pythonhosted.org/packages/2b/dc/7c11f80a782f051beafcc4707dd54888e7fcdeea5afa9236aa2e2cfa7073/ast2json-0.4.tar.gz",
    "platform": null,
    "description": "Introduction\n============\n\nast2json is a small module that will convert any python AST node into its ast\nrepresentation formatted in a JSON compatible python representation (list and\ndicts containing string, ints and null).\n\nInstallation\n============\n\n::\n\n    pip install ast2json\n    # or to have lastest version\n    pip install git+git://github.com/Psycojoker/ast2json.git\n\nUsage\n=====\n\n::\n\n    >>> import json\n    >>> from ast import parse\n    >>> from ast2json import ast2json\n\n    >>> ast = ast2json(parse(open('some_python_source_file.py').read()))\n    >>> print json.dumps(ast, indent=4)\n\nIf you are lazy, \"str2json\" will apply the \"parse\" method of ast on a string for you, so you'll be able to write:\n\n::\n\n    >>> str2json(open('some_python_source_file.py').read())\n\nYou can also run via the command line to parse Python source from stdin and print to stdout:\n\n::\n\n    $ ast2json < some_python_source_file.py\n\n\nExample\n=======\n\nThis is the result of converting 'print \"Hello World!\"' (and applying json.dumps on the result).\n\n::\n\n    {\n        \"body\": [\n            {\n                \"_type\": \"Print\", \n                \"nl\": true, \n                \"col_offset\": 0, \n                \"dest\": null, \n                \"values\": [\n                    {\n                        \"s\": \"Hello World!\", \n                        \"_type\": \"Str\", \n                        \"lineno\": 1, \n                        \"col_offset\": 6\n                    }\n                ], \n                \"lineno\": 1\n            }\n        ], \n        \"_type\": \"Module\"\n    }\n\n\nChangelog\n=========\n\n0.4 (2025-02-07)\n----------------\n\n* you can now use \"ast2json\" directly from the CLI (it reads stdin) thx to @vergenzt\n* output keys are now sorted to be deterministic\n* various small code improvements\n\n0.2.1 (2016-11-13)\n------------------\n\n* fix broken pip installation by @abolger\n\n0.2 (2016-09-03)\n----------------\n\n* python 3 support and some unit testing by Juncheol Cho @zironycho\n\nLicence\n=======\n\nBSD\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "convert a python source code into json-dumpable data (dict and lists with strings, ints, ...)",
    "version": "0.4",
    "project_urls": {
        "Homepage": "https://github.com/Psycojoker/ast2json"
    },
    "split_keywords": [
        "ast"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "f0efcc0efc91e4f0f98780ca77f073578f9b7e5e687c5577bdc5537a3b2d7bf3",
                "md5": "36cd5df97b96fc152d99134c7aff9c07",
                "sha256": "df60746b65384a7fdfc7ef29a6f703ef9cc6e643586e7636e0a44a9f47901810"
            },
            "downloads": -1,
            "filename": "ast2json-0.4-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "36cd5df97b96fc152d99134c7aff9c07",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 5495,
            "upload_time": "2025-02-07T13:53:58",
            "upload_time_iso_8601": "2025-02-07T13:53:58.921762Z",
            "url": "https://files.pythonhosted.org/packages/f0/ef/cc0efc91e4f0f98780ca77f073578f9b7e5e687c5577bdc5537a3b2d7bf3/ast2json-0.4-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "2bdc7c11f80a782f051beafcc4707dd54888e7fcdeea5afa9236aa2e2cfa7073",
                "md5": "12ee61e47e94b065b61113750ccfe9ab",
                "sha256": "b5cc42e97c29b77845b5d4ec32e74dcac538fb4c61a7faa570964bc96b19aeb7"
            },
            "downloads": -1,
            "filename": "ast2json-0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "12ee61e47e94b065b61113750ccfe9ab",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4734,
            "upload_time": "2025-02-07T13:54:01",
            "upload_time_iso_8601": "2025-02-07T13:54:01.230066Z",
            "url": "https://files.pythonhosted.org/packages/2b/dc/7c11f80a782f051beafcc4707dd54888e7fcdeea5afa9236aa2e2cfa7073/ast2json-0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-07 13:54:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Psycojoker",
    "github_project": "ast2json",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "ast2json"
}
        
Elapsed time: 0.76493s