py-module-parser


Namepy-module-parser JSON
Version 0.5.0 PyPI version JSON
download
home_pagehttps://github.com/xnuinside/py-module-parser
SummaryPython Module Parser is a library that parses Python modules and outputs information about imports, functions, variables, and their corresponding line numbers. This makes it easier to analyze and understand the structure of your Python code.
upload_time2023-12-17 10:57:32
maintainer
docs_urlNone
authorIuliia Volkova
requires_python>=3.8,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python Module Parser
![badge1](https://img.shields.io/pypi/v/py-module-parser) ![badge2](https://img.shields.io/pypi/l/py-module-parser) ![badge3](https://img.shields.io/pypi/pyversions/py-module-parser)![workflow](https://github.com/xnuinside/py-module-parser/actions/workflows/main.yml/badge.svg)

Python Module Parser is a library that parses Python modules and outputs information about imports, functions, variables, and their corresponding line numbers. This makes it easier to analyze and understand the structure of your Python code.

To get more samples of output - check tests: tests/test_py_module_parser.py

This project inspired by https://github.com/xnuinside/codegraph and https://github.com/xnuinside/py-models-parser and will be used as a parser inside them in the future. 

## Features

- Parse Python modules and extract information about imports, functions, and variables
- Identify line numbers for each import, function, and variable
- Represent the extracted information in a structured format

## Installation

To install the Python Module Parser library, use `pip`:

```bash
    pip install py-module-parser
```


## Usage

Example with different output formats provided in example/example.py

```python
    # to run example
    python example/example.py

```

Here's a simple example of how to use the Python Module Parser library:

```python
from py_module_parser import PyModulesParser

source_code = """from django.db import models


class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)

"""
parsed_output = PyModulesParser(source_code).parse()
print(parsed_output)
```

This will output:

```python
[
    FromImportOutput(
        lineno_start=1,
        lineno_end=1,
        module='django.db',
        imports=[
            ImportOutput(
                lineno_start=1,
                lineno_end=1,
                name='models',
                alias=None,
                node_type='import'
            )
        ],
        node_type='import_from'
    ),
    ClassOutput(
        lineno_start=4,
        lineno_end=7,
        name='Musician',
        parents=['models.Model'],
        attrs=[
            VariableOutput(
                lineno_start=5,
                lineno_end=5,
                name='first_name',
                type_annotation=None,
                value=CallOutput(
                    lineno_start=5,
                    lineno_end=5,
                    func_name='models.CharField',
                    args=[],
                    kwargs={'max_length': 50},
                    node_type='call'
                ),
                properties={},
                node_type='variable'
            ),
            VariableOutput(
                lineno_start=6,
                lineno_end=6,
                name='last_name',
                type_annotation=None,
                value=CallOutput(
                    lineno_start=6,
                    lineno_end=6,
                    func_name='models.CharField',
                    args=[],
                    kwargs={'max_length': 50},
                    node_type='call'
                ),
                properties={},
                node_type='variable'
            ),
            VariableOutput(
                lineno_start=7,
                lineno_end=7,
                name='instrument',
                type_annotation=None,
                value=CallOutput(
                    lineno_start=7,
                    lineno_end=7,
                    func_name='models.CharField',
                    args=[],
                    kwargs={'max_length': 100},
                    node_type='call'
                ),
                properties={},
                node_type='variable'
            )
        ],
        node_type='class'
    )
]
```

To parse from file, you can use method `parse_from_file`

```python
from py_module_parser import parse_from_file

parsed_output = parse_from_file(file_path='path_to/python_module.py')
print(parsed_output)
```

## Parser Output

By default parser output is a ParserOutput model -it is a list with some additional methods.
List contains inside a nodes from parsed files - all Nodes is a Pydantic models with nested objects.

List of possible node types exists as enum in py_modules_parser.NodesTypes:
    
VARIABLE = 'variable' - all variables like `a='b'`
CALL = 'call' - all calls - function calls or class calls like `func_name(a)` or `ClassA()`
CLASS = 'class' - classes defenitions `class SomeClass: ...`
FUNCTION_DEF = 'func_def' - functions defenitions `def some_func(): ...`
IMPORT = 'import' - imports like `import module`
IMPORT_FROM = 'import_from' - imports like `from module import name`

Output is a list of pydantic models specific for each node.
Node type is always stored in argumen `node_type`

Because, output models is Pydantic models - you can do anything with them that you can do with pydantic models.

### Output group_by_type


```python

    parsed_output = PyModulesParser(source_code).parse()
    parsed_output_group = parsed_output.group_by_type()
    print("Result group_by_type: \n", parsed_output_group)
    # you can also use parsed_output.group_by_type().json()
    # to get json output grouped by type

```

### Output json

```python

parsed_output = PyModulesParser(source_code).parse()
parsed_output_json= parsed_output.json()
print("Result in json: \n", parsed_output_json)

```
### Output dict

```python

parsed_output = PyModulesParser(source_code).parse()
parsed_output_group_dict = parsed_output.group_by_type().model_dump()
print("Result as python dict: \n")
print(parsed_output_group_dict)

```


## TODO in future releases
1. Implement parsing of function arguments
2. Implement parsing of function returns
3. Implement parsing of nested classes

## Changelog
** 0.5.0 - Update pydantic to 2.x version **

1. Pydantic is updated to 2.5.2
2. VariableOutput.default was renamed to VariableOutput.value


** 0.4.0 - First stable release**

1. Renamed FuncCallOutput to CallOutput to include Class calls as well as function calls.
2. Added Enum to define available NodeTypes as py_module_parser.NodeTypes.
3. Added parsing of function definition nodes.
4. Implemented parsing of decorators.
5. Parser results can now be dumped to Python dictionaries or JSON, as well as grouped by type. For more details, please refer to the example/ directory and the README file.





            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/xnuinside/py-module-parser",
    "name": "py-module-parser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Iuliia Volkova",
    "author_email": "xnuinside@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/46/68/17f7155eb3a47aad46b8cff650b0f55da76689c547b6cc9ffa0ca438ee59/py_module_parser-0.5.0.tar.gz",
    "platform": null,
    "description": "# Python Module Parser\n![badge1](https://img.shields.io/pypi/v/py-module-parser) ![badge2](https://img.shields.io/pypi/l/py-module-parser) ![badge3](https://img.shields.io/pypi/pyversions/py-module-parser)![workflow](https://github.com/xnuinside/py-module-parser/actions/workflows/main.yml/badge.svg)\n\nPython Module Parser is a library that parses Python modules and outputs information about imports, functions, variables, and their corresponding line numbers. This makes it easier to analyze and understand the structure of your Python code.\n\nTo get more samples of output - check tests: tests/test_py_module_parser.py\n\nThis project inspired by https://github.com/xnuinside/codegraph and https://github.com/xnuinside/py-models-parser and will be used as a parser inside them in the future. \n\n## Features\n\n- Parse Python modules and extract information about imports, functions, and variables\n- Identify line numbers for each import, function, and variable\n- Represent the extracted information in a structured format\n\n## Installation\n\nTo install the Python Module Parser library, use `pip`:\n\n```bash\n    pip install py-module-parser\n```\n\n\n## Usage\n\nExample with different output formats provided in example/example.py\n\n```python\n    # to run example\n    python example/example.py\n\n```\n\nHere's a simple example of how to use the Python Module Parser library:\n\n```python\nfrom py_module_parser import PyModulesParser\n\nsource_code = \"\"\"from django.db import models\n\n\nclass Musician(models.Model):\n    first_name = models.CharField(max_length=50)\n    last_name = models.CharField(max_length=50)\n    instrument = models.CharField(max_length=100)\n\n\"\"\"\nparsed_output = PyModulesParser(source_code).parse()\nprint(parsed_output)\n```\n\nThis will output:\n\n```python\n[\n    FromImportOutput(\n        lineno_start=1,\n        lineno_end=1,\n        module='django.db',\n        imports=[\n            ImportOutput(\n                lineno_start=1,\n                lineno_end=1,\n                name='models',\n                alias=None,\n                node_type='import'\n            )\n        ],\n        node_type='import_from'\n    ),\n    ClassOutput(\n        lineno_start=4,\n        lineno_end=7,\n        name='Musician',\n        parents=['models.Model'],\n        attrs=[\n            VariableOutput(\n                lineno_start=5,\n                lineno_end=5,\n                name='first_name',\n                type_annotation=None,\n                value=CallOutput(\n                    lineno_start=5,\n                    lineno_end=5,\n                    func_name='models.CharField',\n                    args=[],\n                    kwargs={'max_length': 50},\n                    node_type='call'\n                ),\n                properties={},\n                node_type='variable'\n            ),\n            VariableOutput(\n                lineno_start=6,\n                lineno_end=6,\n                name='last_name',\n                type_annotation=None,\n                value=CallOutput(\n                    lineno_start=6,\n                    lineno_end=6,\n                    func_name='models.CharField',\n                    args=[],\n                    kwargs={'max_length': 50},\n                    node_type='call'\n                ),\n                properties={},\n                node_type='variable'\n            ),\n            VariableOutput(\n                lineno_start=7,\n                lineno_end=7,\n                name='instrument',\n                type_annotation=None,\n                value=CallOutput(\n                    lineno_start=7,\n                    lineno_end=7,\n                    func_name='models.CharField',\n                    args=[],\n                    kwargs={'max_length': 100},\n                    node_type='call'\n                ),\n                properties={},\n                node_type='variable'\n            )\n        ],\n        node_type='class'\n    )\n]\n```\n\nTo parse from file, you can use method `parse_from_file`\n\n```python\nfrom py_module_parser import parse_from_file\n\nparsed_output = parse_from_file(file_path='path_to/python_module.py')\nprint(parsed_output)\n```\n\n## Parser Output\n\nBy default parser output is a ParserOutput model -it is a list with some additional methods.\nList contains inside a nodes from parsed files - all Nodes is a Pydantic models with nested objects.\n\nList of possible node types exists as enum in py_modules_parser.NodesTypes:\n    \nVARIABLE = 'variable' - all variables like `a='b'`\nCALL = 'call' - all calls - function calls or class calls like `func_name(a)` or `ClassA()`\nCLASS = 'class' - classes defenitions `class SomeClass: ...`\nFUNCTION_DEF = 'func_def' - functions defenitions `def some_func(): ...`\nIMPORT = 'import' - imports like `import module`\nIMPORT_FROM = 'import_from' - imports like `from module import name`\n\nOutput is a list of pydantic models specific for each node.\nNode type is always stored in argumen `node_type`\n\nBecause, output models is Pydantic models - you can do anything with them that you can do with pydantic models.\n\n### Output group_by_type\n\n\n```python\n\n    parsed_output = PyModulesParser(source_code).parse()\n    parsed_output_group = parsed_output.group_by_type()\n    print(\"Result group_by_type: \\n\", parsed_output_group)\n    # you can also use parsed_output.group_by_type().json()\n    # to get json output grouped by type\n\n```\n\n### Output json\n\n```python\n\nparsed_output = PyModulesParser(source_code).parse()\nparsed_output_json= parsed_output.json()\nprint(\"Result in json: \\n\", parsed_output_json)\n\n```\n### Output dict\n\n```python\n\nparsed_output = PyModulesParser(source_code).parse()\nparsed_output_group_dict = parsed_output.group_by_type().model_dump()\nprint(\"Result as python dict: \\n\")\nprint(parsed_output_group_dict)\n\n```\n\n\n## TODO in future releases\n1. Implement parsing of function arguments\n2. Implement parsing of function returns\n3. Implement parsing of nested classes\n\n## Changelog\n** 0.5.0 - Update pydantic to 2.x version **\n\n1. Pydantic is updated to 2.5.2\n2. VariableOutput.default was renamed to VariableOutput.value\n\n\n** 0.4.0 - First stable release**\n\n1. Renamed FuncCallOutput to CallOutput to include Class calls as well as function calls.\n2. Added Enum to define available NodeTypes as py_module_parser.NodeTypes.\n3. Added parsing of function definition nodes.\n4. Implemented parsing of decorators.\n5. Parser results can now be dumped to Python dictionaries or JSON, as well as grouped by type. For more details, please refer to the example/ directory and the README file.\n\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python Module Parser is a library that parses Python modules and outputs information about imports, functions, variables, and their corresponding line numbers. This makes it easier to analyze and understand the structure of your Python code.",
    "version": "0.5.0",
    "project_urls": {
        "Homepage": "https://github.com/xnuinside/py-module-parser",
        "Repository": "https://github.com/xnuinside/py-module-parser"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "273d774a1ceab1d7ea098bd3dc3d33b876a53cc568d2038187133adbc3af0f28",
                "md5": "69499801929b2f2f2cf527469eca9306",
                "sha256": "cf424fdf6160a6d9612ded721c47396fc4b5e6c061c1d4b33dcb4b709ef96183"
            },
            "downloads": -1,
            "filename": "py_module_parser-0.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "69499801929b2f2f2cf527469eca9306",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 8062,
            "upload_time": "2023-12-17T10:57:30",
            "upload_time_iso_8601": "2023-12-17T10:57:30.626935Z",
            "url": "https://files.pythonhosted.org/packages/27/3d/774a1ceab1d7ea098bd3dc3d33b876a53cc568d2038187133adbc3af0f28/py_module_parser-0.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "466817f7155eb3a47aad46b8cff650b0f55da76689c547b6cc9ffa0ca438ee59",
                "md5": "b9c280a66b0eaac0eeedc3a444fd603a",
                "sha256": "34c8aebb7bd32b1d5e2a3ccff6f54f1bc1ca0bf2b7340c292f4b02a7fe7130c0"
            },
            "downloads": -1,
            "filename": "py_module_parser-0.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b9c280a66b0eaac0eeedc3a444fd603a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 8353,
            "upload_time": "2023-12-17T10:57:32",
            "upload_time_iso_8601": "2023-12-17T10:57:32.361520Z",
            "url": "https://files.pythonhosted.org/packages/46/68/17f7155eb3a47aad46b8cff650b0f55da76689c547b6cc9ffa0ca438ee59/py_module_parser-0.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-17 10:57:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "xnuinside",
    "github_project": "py-module-parser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "py-module-parser"
}
        
Elapsed time: 0.15820s