{"id":16135260,"url":"https://github.com/xnuinside/py-module-parser","last_synced_at":"2025-03-18T15:31:17.581Z","repository":{"id":163240369,"uuid":"638161988","full_name":"xnuinside/py-module-parser","owner":"xnuinside","description":"Python Module Parser is a library that parses Python modules and outputs information about imports, functions, variables, and their corresponding line numbers.","archived":false,"fork":false,"pushed_at":"2023-12-17T10:55:27.000Z","size":147,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-28T10:34:15.566Z","etag":null,"topics":["ast","language-parser","module","parser","parser-library","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xnuinside.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.txt","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-09T08:02:50.000Z","updated_at":"2024-08-27T22:06:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"601e477f-4806-4228-9efe-9e7d2360bfd9","html_url":"https://github.com/xnuinside/py-module-parser","commit_stats":{"total_commits":11,"total_committers":2,"mean_commits":5.5,"dds":"0.18181818181818177","last_synced_commit":"a9962eb3b5e7800511297219ccec1d36fbec061a"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fpy-module-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fpy-module-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fpy-module-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnuinside%2Fpy-module-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xnuinside","download_url":"https://codeload.github.com/xnuinside/py-module-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243936382,"owners_count":20371504,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","language-parser","module","parser","parser-library","python"],"created_at":"2024-10-09T23:06:37.022Z","updated_at":"2025-03-18T15:31:17.576Z","avatar_url":"https://github.com/xnuinside.png","language":"Python","readme":"# 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","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnuinside%2Fpy-module-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxnuinside%2Fpy-module-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnuinside%2Fpy-module-parser/lists"}