Skip to content

Commit 6adf5b1

Browse files
committed
Init
0 parents  commit 6adf5b1

File tree

11 files changed

+218
-0
lines changed

11 files changed

+218
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.egg-info
2+
.vscode
3+
env
4+
**/**/__pycache__
5+
.coverage
6+
.pytest_cache
7+
build
8+
dist

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python Code Parser
2+
3+
Parse Python code to extract information about functions, classes, methods, etc.
4+
5+
## Upload to PyPi
6+
7+
First you have to build the wheel file:
8+
9+
```bash
10+
python setup.py bdist_wheel
11+
```
12+
13+
Then the wheel file can be uploaded to PyPi with:
14+
15+
```bash
16+
twine upload --skip-existing dist/*
17+
```

pyproject.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[build-system]
2+
requires = ["setuptools", "setuptools-scm"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "python_code_parse"
7+
authors = [
8+
{name = "Jake Cyr", email = "cyrjake@gmail.com"},
9+
]
10+
version = "0.0.1"
11+
description = "Parse code to get information about functions, etc."
12+
readme = "README.md"
13+
requires-python = ">=3.7"
14+
keywords = ["code", "parse", "python", "ast", "abstract syntax tree"]
15+
license = {text = "MIT"}
16+
classifiers = [
17+
"Intended Audience :: Developers",
18+
"License :: OSI Approved :: MIT License",
19+
"Programming Language :: Python :: 3",
20+
]
21+
dependencies = []
22+
23+
[project.optional-dependencies]
24+
dev = [
25+
"twine",
26+
"wheel",
27+
]
28+

python_code_parse/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__all__ = ["get_all_function_info_from_code", "models"]
2+
from python_code_parse.get_all_function_info_from_code import (
3+
get_all_function_info_from_code,
4+
)
5+
from python_code_parse.models import FunctionArg, FunctionInfo
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import ast
2+
from typing import List
3+
from python_code_parse.models.function_arg import FunctionArg
4+
from python_code_parse.models.function_info import FunctionInfo
5+
6+
7+
def get_all_function_info_from_code(code: str) -> List[FunctionInfo]:
8+
"""Get a list of functions found in a code string."""
9+
10+
functions: List[FunctionInfo] = []
11+
tree: ast.Module = ast.parse(code)
12+
13+
for node in ast.walk(tree):
14+
if isinstance(node, ast.FunctionDef):
15+
args: List[FunctionArg] = []
16+
for arg in node.args.args:
17+
arg_str = arg.arg
18+
if arg.annotation:
19+
arg_str += ": " + ast.unparse(arg.annotation).strip()
20+
args.append(
21+
FunctionArg(
22+
name=arg.arg, annotation=ast.unparse(arg.annotation).strip()
23+
)
24+
)
25+
else:
26+
args.append(FunctionArg(name=arg.arg, annotation=""))
27+
28+
functions.append(
29+
FunctionInfo(
30+
name=node.name,
31+
args=args,
32+
return_type=ast.unparse(node.returns).strip()
33+
if node.returns
34+
else "",
35+
line=node.lineno,
36+
)
37+
)
38+
return functions

python_code_parse/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
from python_code_parse.get_all_function_info_from_code import get_function_info_from_code
3+
import json
4+
5+
6+
def main():
7+
file_path = os.path.join(os.path.dirname(__file__), "../test.py")
8+
9+
with open(file_path, encoding="utf-8") as f:
10+
code = f.read()
11+
12+
functions = get_function_info_from_code(code)
13+
json_version = json.dumps(functions, default=lambda o: o.__dict__, indent=4)
14+
15+
print(json_version)
16+
17+
18+
if __name__ == "__main__":
19+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from python_code_parse.models.function_arg import FunctionArg
2+
from python_code_parse.models.function_info import FunctionInfo
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class FunctionArg:
6+
"""A dataclass to hold information about a function argument."""
7+
8+
name: str
9+
annotation: str
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
from python_code_parse.models.function_arg import FunctionArg
4+
5+
6+
@dataclass
7+
class FunctionInfo:
8+
"""A dataclass to hold information about a function."""
9+
10+
name: str
11+
args: List[FunctionArg]
12+
return_type: str
13+
line: int

setup.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from setuptools import setup, find_packages
2+
3+
4+
def get_readme():
5+
with open("README.md") as f:
6+
return f.read()
7+
8+
9+
setup(
10+
long_description=get_readme(),
11+
packages=find_packages(),
12+
)

0 commit comments

Comments
 (0)