Skip to content

Commit 37ee652

Browse files
committed
starting refactory-ing. hahahaha
1 parent c8f3208 commit 37ee652

10 files changed

Lines changed: 5939 additions & 744 deletions

toolFactory/astFactory.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
from Z0Z_tools import writeStringToHere
1717
import ast
1818

19+
"""
20+
class Name(expr):
21+
...
22+
ctx: expr_context # Not present in Python < 3.13 if not passed to `__init__`
23+
24+
TODO protect against AttributeError (I guess) in DOT, Grab, and ClassIsAndAttribute
25+
add docstrings to warn of problem, including in Make
26+
27+
"""
28+
1929
class AnnotationsAndDefs(TypedDict):
2030
astAnnotation: ast.expr
2131
listClassDefIdentifier: list[ast_Identifier | str_nameDOTname]

toolFactory/databaseAST.csv

Lines changed: 2635 additions & 0 deletions
Large diffs are not rendered by default.

toolFactory/databaseAST.py

Lines changed: 57 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,181 +1,63 @@
1-
from ast import AST, Constant
2-
from itertools import chain
3-
from pathlib import Path
4-
from types import GenericAlias
5-
from typing import Any, cast
6-
import ast
71
import pandas
8-
import sys
9-
10-
pathFilenameDatabaseAST = Path('/apps/astToolkit/toolFactory/databaseAST.csv')
11-
12-
listASTClasses: list[type[AST]] = []
13-
for astClass in sorted([AST, *chain(*map(lambda c: c.__subclasses__(), [AST, Constant, *AST.__subclasses__()]))], key=lambda c: c.__name__):
14-
if not issubclass(astClass, AST): continue
15-
listASTClasses.append(astClass)
16-
17-
versionMajor: int = sys.version_info.major
18-
versionMinor: int = sys.version_info.minor
19-
versionMicro: int = sys.version_info.micro
20-
21-
listRecords: list[dict[str, Any]] = []
22-
23-
fieldRenames = { "asname": "asName", "ctx": "context", "func": "callee", "orelse": "orElse", }
24-
25-
defaultValues = { "context": "ast.Load()", "level": "0", "type_ignores": "[]", "posonlyargs": "[]", "kwonlyargs": "[]", "defaults": "[]", "kw_defaults": "[None]", "decorator_list": "[]", "finalbody": "[]", }
26-
27-
for astClass in listASTClasses:
28-
c = astClass.__name__
29-
b = cast(Any, astClass.__base__).__name__
30-
ff = astClass._fields
31-
aa = astClass._attributes
32-
d = 'deprecated' if bool(astClass.__doc__ and 'Deprecated' in astClass.__doc__) else None
33-
34-
base_typing_TypeAlias = None
35-
typeString_typing_TypeAlias=None
36-
isList2Sequence=False
37-
keywordArguments=None
38-
kwargAnnotation=None
39-
keywordArgumentsDefaultValue=None
40-
41-
if sys.version_info >= (3, 13):
42-
try:
43-
field_types_dict: dict[str, Any] = astClass._field_types
44-
except Exception:
45-
field_types_dict = {}
46-
47-
for field, fieldType in field_types_dict.items():
48-
typeString: str | None = None
49-
if not isinstance(fieldType, GenericAlias):
50-
try:
51-
typeString = fieldType.__name__
52-
except Exception as ERRORmessage:
53-
typeString = str(fieldType)
54-
else:
55-
typeString = str(fieldType)
56-
57-
fieldRename = fieldRenames.get(field, None)
58-
59-
defaultValue = defaultValues.get(field, None)
60-
61-
_attribute=None
62-
63-
listRecords.append({
64-
'astClass': c,
65-
'versionMajor': versionMajor,
66-
'versionMinor': versionMinor,
67-
'versionMicro': versionMicro,
68-
'base': b,
69-
'base_typing_TypeAlias': base_typing_TypeAlias,
70-
'field': field,
71-
'fieldRename': fieldRename,
72-
'_attribute': _attribute,
73-
'typeString': typeString,
74-
'typeString_typing_TypeAlias': typeString_typing_TypeAlias,
75-
'list2Sequence': isList2Sequence,
76-
'defaultValue': defaultValue,
77-
'keywordArguments': keywordArguments,
78-
'kwargAnnotation': kwargAnnotation,
79-
'keywordArgumentsDefaultValue': keywordArgumentsDefaultValue,
80-
'deprecated': d,
81-
})
82-
83-
field=None
84-
fieldRename=None
85-
typeString=int.__name__
86-
defaultValue=None
87-
for _attribute in aa:
88-
listRecords.append({
89-
'astClass': c,
90-
'versionMajor': versionMajor,
91-
'versionMinor': versionMinor,
92-
'versionMicro': versionMicro,
93-
'base': b,
94-
'base_typing_TypeAlias': base_typing_TypeAlias,
95-
'field': field,
96-
'fieldRename': fieldRename,
97-
'_attribute': _attribute,
98-
'typeString': typeString,
99-
'typeString_typing_TypeAlias': typeString_typing_TypeAlias,
100-
'list2Sequence': isList2Sequence,
101-
'defaultValue': defaultValue,
102-
'keywordArguments': keywordArguments,
103-
'kwargAnnotation': kwargAnnotation,
104-
'keywordArgumentsDefaultValue': keywordArgumentsDefaultValue,
105-
'deprecated': d,
106-
})
107-
108-
_attribute=None
109-
110-
if not field_types_dict and not aa:
111-
listRecords.append({
112-
'astClass': c,
113-
'versionMajor': versionMajor,
114-
'versionMinor': versionMinor,
115-
'versionMicro': versionMicro,
116-
'base': b,
117-
'base_typing_TypeAlias': base_typing_TypeAlias,
118-
'field': field,
119-
'fieldRename': fieldRename,
120-
'_attribute': _attribute,
121-
'typeString': typeString,
122-
'typeString_typing_TypeAlias': typeString_typing_TypeAlias,
123-
'list2Sequence': isList2Sequence,
124-
'defaultValue': defaultValue,
125-
'keywordArguments': keywordArguments,
126-
'kwargAnnotation': kwargAnnotation,
127-
'keywordArgumentsDefaultValue': keywordArgumentsDefaultValue,
128-
'deprecated': d,
129-
})
130-
131-
def oneShotMakeDataframe(listData: list[dict[str, Any]]):
132-
global pathFilenameDatabaseAST
133-
dataframeTarget = pandas.DataFrame(listData, columns=list(listData[0].keys()))
134-
pathFilenameDatabaseAST = pathFilenameDatabaseAST.with_stem(pathFilenameDatabaseAST.stem + str(versionMinor))
135-
dataframeTarget.to_csv(pathFilenameDatabaseAST) # type: ignore
136-
137-
oneShotMakeDataframe(listRecords)
138-
139-
def getDataframe():
140-
pass
2+
from toolFactory import pathFilenameDatabaseAST
1413

1424
"""
143-
See /apps/astToolkit/typeshed/stdlib/ast.pyi for information about the following.
144-
column: 'base_typing_TypeAlias'
145-
I think _Slice is the only one.
5+
rename 'astClass' to 'ClassDefIdentifier'
6+
7+
new column
8+
classAs_astAttribute
9+
value
10+
ast.Attribute(ast.Name('ast'), ClassDefIdentifier)
11+
put the actual ClassDefIdentifier in the string in the column.
12+
13+
new column
14+
classVersionMinorMinimum
15+
for each ClassDefIdentifier,
16+
get unique values of 'versionMinor' and sort them,
17+
validate the data: if the values are not contiguous from smallest to largest, give an error,
18+
validate the data: if the largest value is not 13, give an error,
19+
validate the data: if the smallest value is less than 9, give an error,
20+
if the smallest value is 9, record -1 in the column,
21+
else, record the smallest value in the column.
22+
23+
new column
24+
fieldVersionMinorMinimum
25+
for each ClassDefIdentifier,
26+
for each field of the ClassDefIdentifier,
27+
get unique values of 'versionMinor' and sort them,
28+
validate the data: if the values are not contiguous from smallest to largest, give an error,
29+
validate the data: if the largest value is not 13, give an error,
30+
validate the data: if the smallest value is less than 9, give an error,
31+
if the smallest value is 9, record -1 in the column,
32+
else, record the smallest value in the column.
14633
147-
column: typeString_typing_TypeAlias
148-
_Identifier, _Pattern, _Slice, _SliceAttributes
149-
The typeString reports the primitive name or the non-alias name no matter what is written in the stub file.
150-
This column tracks how the TypeAlias is used in typeshed, so, for example, only record "_Pattern" if it is used for that field in that class.
151-
Furthermore, this is not the same as my TypeAlias, such as `ast_Identifier`.
152-
"""
153-
"""
154-
Use apps/astToolkit/toolFactory/astFactory.py, especially the `match attributeIdentifier` code block as the source of the following
155-
column: list2Sequence
156-
bool
157-
False
158-
159-
column: fieldRename
160-
string
161-
If I renamed the field, such as `attributeIdentifier = 'callee'`
162-
163-
column: defaultValue
164-
string representations of various types
165-
if I set a default value
16634
167-
column: keywordArguments
168-
bool
169-
If I treat the field as a `**keywordArguments` in `Make`. Look for `continue` in the case of `match attributeIdentifier`.
170-
Also, all _attribute should have this column set to True.
171-
172-
column: kwargAnnotation
173-
string
174-
If I treated the field as a `**keywordArguments` in `Make`, I might have changed the `**keywordArguments` annotation, such as `ast.Name('intORstr')`.
175-
All _attribute should have this column set to "int".
176-
The normal annotation is `int`, so for this example, store "str" in this column.
177-
178-
column: keywordArgumentsDefaultValue
179-
string or int
180-
For example, `cast(ast.Call, cast(ast.Return, cast(ast.FunctionDef, ClassDefMake.body[-1]).body[0]).value).keywords.append(ast.keyword(attributeIdentifier, ast.Constant(0)))`
18135
"""
36+
37+
cc=[
38+
'astClass',
39+
'versionMajor',
40+
'versionMinor',
41+
'versionMicro',
42+
'base',
43+
'base_typing_TypeAlias',
44+
'field',
45+
'fieldRename',
46+
'_attribute',
47+
'typeC',
48+
'typeStub',
49+
'type_field_type',
50+
'typeStub_typing_TypeAlias',
51+
'list2Sequence',
52+
'defaultValue__dict__',
53+
'defaultValue',
54+
'keywordArguments',
55+
'kwargAnnotation',
56+
'keywordArgumentsDefaultValue',
57+
'deprecated',
58+
]
59+
60+
def getDataframe() -> pandas.DataFrame:
61+
indexColumns = ['astClass', 'versionMajor', 'versionMinor', 'versionMicro', 'base', 'field', '_attributes', 'typeString']
62+
dataframeTarget = pandas.read_csv(pathFilenameDatabaseAST, index_col=indexColumns) # pyright: ignore[reportUnknownMemberType]
63+
return dataframeTarget

0 commit comments

Comments
 (0)