Skip to content

Commit 22b78b8

Browse files
committed
Changed and added docstrings
1 parent 3bfd4da commit 22b78b8

10 files changed

Lines changed: 115 additions & 89 deletions

File tree

pycode/memilio-generation/memilio/generation/ast.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def create_ast_for_pickle(self):
7575
return ast_file_path
7676

7777
def get_file_args(self):
78-
"""Extract arguments from the compilation database, excluding unwanted ones.
78+
""" Extract arguments from the compilation database, excluding unwanted ones.
7979
8080
:returns: List of relevant file arguments.
8181
"""
@@ -95,7 +95,7 @@ def get_file_args(self):
9595
return file_args[1:-4]
9696

9797
def prepare_clang_command(self, file_args):
98-
"""Prepare the Clang command without executing it.
98+
""" Prepare the Clang command without executing it.
9999
100100
:param file_args: List of file arguments for the Clang command.
101101
:returns: List of command-line arguments for Clang.
@@ -107,7 +107,7 @@ def prepare_clang_command(self, file_args):
107107
return clang_cmd
108108

109109
def run_clang_command(self, clang_cmd):
110-
"""Execute the Clang command.
110+
""" Execute the Clang command.
111111
112112
:param clang_cmd: List of command-line arguments for Clang.
113113
:returns: Result of the Clang command execution.
@@ -122,7 +122,7 @@ def run_clang_command(self, clang_cmd):
122122
raise RuntimeError("Clang AST generation failed.")
123123

124124
def create_temp_ast_file(self, clang_cmd_result):
125-
"""Create a temporary file and write the Clang AST output to it.
125+
""" Create a temporary file and write the Clang AST output to it.
126126
127127
:param clang_cmd_result: Result of the Clang command execution.
128128
:returns: Path to the temporary AST file.
@@ -136,7 +136,7 @@ def create_temp_ast_file(self, clang_cmd_result):
136136
def _assing_ast_with_ids(self, cursor: Cursor) -> None:
137137
""" Traverse the AST and assign a unique ID to each node.
138138
139-
:param cursor: The current node (Cursor) in the AST to traverse.
139+
:param cursor: The current node of the AST.
140140
"""
141141
self.cursor_id += 1
142142
id = self.cursor_id

pycode/memilio-generation/memilio/generation/ast_handler.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class ASTHandler:
3434
""" ASTHandler class for creating ASTs.
3535
"""
3636

37-
# Todo: clean __init__()
3837
def __init__(self: T, conf: ScannerConfig) -> None:
3938
""" Basic constructor of the ASTHandler class
4039
@@ -74,7 +73,7 @@ def process_ast_file(ast_path: str, ast: AST) -> tuple[TranslationUnit, AST]:
7473
7574
:param ast_path: Represents the path to the generated AST file created by the clang process.
7675
:param ast: AST instance.
77-
:returns: TranslationUnit object.
76+
:returns: A tuple containing the TranslationUnit object and the AST object.
7877
"""
7978
idx = Index.create()
8079
tu = idx.read(ast_path)

pycode/memilio-generation/memilio/generation/default_generation_dict.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
#############################################################################
2+
# Copyright (C) 2020-2026 MEmilio
3+
#
4+
# Authors: Daniel Richter
5+
#
6+
# Contact: Martin J. Kuehn <[email protected]>
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#############################################################################
20+
21+
"""
22+
Default mapping dictionary used for generating or configuring model-related code components.
23+
24+
This dictionary provides default values for various model-related terms and file names, which can be used throughout the code generation process.
25+
It serves as a centralized reference for consistent naming conventions and can be easily modified if needed.
26+
"""
27+
128
default_dict = {
229
"model": "Model",
330
"agegroup": "AgeGroup",
@@ -14,6 +41,15 @@
1441
"drawsample": "draw_sample"
1542
}
1643

44+
"""
45+
Configuration list defining which bindings should be generated.
46+
47+
Each entry specifies a class or function to be created, including its name,
48+
type, and associated metadata.
49+
For class entries, the "methods" field lists the member functions that
50+
should be generated as part of the binding.
51+
"""
52+
1753
general_bindings_dict = [
1854
{
1955
"type": "class",

pycode/memilio-generation/memilio/generation/generator.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737

3838
class Generator:
39-
"""! Generates the model specific python bindings code with the information given by the IntermediateRepresentation.
39+
""" Generates the model specific python bindings code with the information given by the IntermediateRepresentation.
4040
"""
4141

4242
def __init__(self: Self) -> None:
@@ -45,11 +45,11 @@ def __init__(self: Self) -> None:
4545

4646
def create_substitutions(
4747
self: Self, intermed_repr: IntermediateRepresentation) -> None:
48-
"""! Create the substitutions needed to generate the bindings.
48+
""" Create the substitutions needed to generate the bindings.
4949
Divided into substitutions for the Python- and C++-file.
5050
Uses the string template methods from the template folder.
5151
52-
@param intermed_repr: Dataclass holding the model features.
52+
:param intermed_repr: Dataclass holding the model features.
5353
5454
"""
5555

@@ -90,13 +90,11 @@ def create_substitutions(
9090

9191
def generate_files(
9292
self: Self, intermed_repr: IntermediateRepresentation) -> None:
93-
"""! Generate the python bindings to the C++ code.
93+
""" Generate the python bindings to the C++ code.
9494
Template files for python and cpp from the template folder are used
9595
and the identifiers substituted with the corresponding substitutions.
9696
9797
:param intermed_repr: Dataclass holding the model features.
98-
:param self: Self:
99-
10098
"""
10199
with open(os.path.join(intermed_repr.python_generation_module_path,
102100
"memilio/generation/template/template_py.txt")) as t:

pycode/memilio-generation/memilio/generation/graph_visualization.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ def terminal_writer(level: int, cursor_label: str) -> None:
5454
def output_ast_png(cursor: Cursor, max_depth: int, output_file_name: str = 'ast_graph') -> None:
5555
""" Output the abstract syntax tree to a .png. Set the starting node and the max depth.
5656
57-
To save the abstract syntax tree as an png with a starting node and a depth u cann use the following command
57+
To save the abstract syntax tree as an png with a starting node and a depth u can use the following command
5858
5959
Example command: aviz.output_ast_png(ast.get_node_by_index(1), 2)
6060
6161
aviz -> instance of the Visualization class.
6262
6363
ast -> instance of the AST class.
6464
65-
.get_node_by_index -> get a specific node by id (use .output_ast_formatted to see node ids)
65+
.get_node_by_index -> get a specific node by id use (.output_ast_formatted to see node ids)
6666
67-
The number 2 is a example for the depth the graph will show
67+
The second parameter is the max depth the graph will show. The root node is depth 0, its children are depth 1 and so on.
6868
6969
:param cursor: The current node of the AST as a cursor object from libclang.
7070
:param max_depth: Maximal depth the graph displays.
@@ -84,7 +84,7 @@ def output_ast_png(cursor: Cursor, max_depth: int, output_file_name: str = 'ast_
8484

8585
@staticmethod
8686
def output_ast_formatted(ast: AST, cursor: Cursor, output_file_name: str = 'ast_formatted.txt') -> None:
87-
"""! Output the abstract syntax tree to a file.
87+
""" Output the abstract syntax tree to a file.
8888
8989
:param ast: ast object from AST class.
9090
:param cursor: The current node of the AST as a cursor object from libclang.
@@ -111,25 +111,24 @@ def file_writer(level: int, cursor_label: str) -> None:
111111
def indent(level: int) -> str:
112112
""" Create an indentation based on the level.
113113
114-
:param level: int:
115-
114+
:param level: int: The current depth in the AST for indentation purposes.
115+
:returns: A string representing the indentation for the given level.
116116
"""
117117
return '│ ' * level + '├── '
118118

119119

120120
def newline() -> str:
121-
""" Create a new line."""
121+
""" Creates a new line."""
122122
return '\n'
123123

124124

125125
def _output_cursor_and_children(cursor: Cursor, ast: AST, writer: Callable[[int, str], None], level: int = 0) -> None:
126-
"""Generic function to output the cursor and its children with a specified writer.
126+
""" Generic function to output the cursor and its children with a specified writer.
127127
128128
:param cursor: The current node of the AST as a libclang cursor object.
129129
:param ast: AST object from the AST class.
130130
:param writer: Function that takes `level` and `cursor_label` and handles output.
131131
:param level: The current depth in the AST for indentation purposes. Default value = 0)
132-
133132
"""
134133

135134
cursor_id = ast.get_node_id(cursor)
@@ -159,9 +158,7 @@ def _output_cursor_and_children_graphviz_digraph(cursor: Cursor, graph: Digraph,
159158
:param graph: Graphviz Digraph object where the nodes and edges will be added.
160159
:param max_d: Maximal depth.
161160
:param current_d: Current depth.
162-
:param parent_node: Name of the parent node in the graph (None for the root node).
163-
:param parent_node: str: (Default value = None)
164-
161+
:param parent_node: Name of the parent node in the graph (None for the root node). str: (Default value = None)
165162
"""
166163

167164
if current_d > max_d:

pycode/memilio-generation/memilio/generation/info_types.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1+
#############################################################################
2+
# Copyright (C) 2020-2026 MEmilio
3+
#
4+
# Authors: Daniel Richter
5+
#
6+
# Contact: Martin J. Kuehn <[email protected]>
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#############################################################################
20+
121
from dataclasses import dataclass, field
222

323

424
@dataclass
525
class method_type_info:
26+
""" Dataclass to represent the needed information of a method. Used for methods of classes in binding_type_info."""
627
type: str
728
name: str
829
cursorkind: str
@@ -15,6 +36,7 @@ class method_type_info:
1536
is_member: bool = False
1637

1738
def signature_key(self) -> tuple:
39+
""" Create a unique key for the method. This can be used to identify methods."""
1840
return (
1941
self.name,
2042
self.cursorkind,
@@ -26,6 +48,7 @@ def signature_key(self) -> tuple:
2648

2749
@dataclass
2850
class binding_type_info:
51+
""" Dataclass to represent the needed information of a class or function for binding generation."""
2952
type: str
3053
name: str
3154
cursorkind: str
@@ -42,6 +65,7 @@ class binding_type_info:
4265
template_args: list[str] = field(default_factory=list)
4366

4467
def signature_key(self) -> tuple:
68+
""" Create a unique key for the binding. This can be used to identify bindings."""
4569
return (
4670
self.name,
4771
self.cursorkind,

pycode/memilio-generation/memilio/generation/intermediate_representation.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ def set_attribute(self: Self, attribute_name: str, value: Any) -> None:
6262
"""Setter for the attributes of this class.
6363
6464
:param attribute_name: Name of the attribute in IntermediateRepresentation to be set.
65-
:param value: Value the attribute is set to. Needs to be of right type.
66-
:param self: Self:
67-
:param attribute_name: str:
68-
:param value: Any:
69-
65+
:param value: Value the attribute is set to. Needs to be of right type.
7066
"""
7167
self.__setattr__(attribute_name, value)
7268

@@ -87,9 +83,6 @@ def check_complete_data(self: Self, optional: Dict
8783
Called by the Scanner as last step of the data extraction.
8884
8985
:param optional: Dictionary of the optional data from the config.json. (Default value = Dict[str, Union[str, bool]])
90-
:param self: Self:
91-
92-
9386
"""
9487
assert (self.model_class != None), "set a model name"
9588
assert (self.namespace != None), "set a model namespace"

0 commit comments

Comments
 (0)