Skip to content

Commit 3183a8b

Browse files
authored
Merge pull request #317 from pyiron/pilot
Add docstrings and typehints with github co-pilot
2 parents 639ad21 + ca668a7 commit 3183a8b

19 files changed

+931
-227
lines changed

pysqa/cmd.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010

1111
def command_line(
1212
arguments_lst: Optional[list] = None, execute_command: callable = execute_command
13-
):
13+
) -> None:
1414
"""
1515
Parse the command line arguments.
1616
1717
Args:
18-
argv: Command line arguments
19-
execute_command: function to comunicate with shell process
18+
arguments_lst (Optional[list]): Command line arguments
19+
execute_command (callable): Function to communicate with shell process
2020
21+
Returns:
22+
None
2123
"""
2224
directory = "~/.queues"
2325
queue = None

pysqa/executor/backend.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
def execute_files_from_list(
1616
tasks_in_progress_dict: dict, cache_directory: str, executor
1717
):
18+
"""
19+
Execute files from the given list.
20+
21+
Args:
22+
tasks_in_progress_dict (dict): Dictionary of tasks in progress.
23+
cache_directory (str): Directory where the files are stored.
24+
executor: Executor object for executing tasks.
25+
26+
Returns:
27+
None
28+
"""
1829
file_lst = os.listdir(cache_directory)
1930
for file_name_in in file_lst:
2031
key = file_name_in.split(".in.pl")[0]
@@ -42,6 +53,16 @@ def execute_files_from_list(
4253

4354

4455
def execute_tasks(cores: int, cache_directory: str):
56+
"""
57+
Execute tasks from the given cache directory using the specified number of cores.
58+
59+
Args:
60+
cores (int): Number of cores to use for execution.
61+
cache_directory (str): Directory where the files are stored.
62+
63+
Returns:
64+
None
65+
"""
4566
tasks_in_progress_dict = {}
4667
with Executor(
4768
max_cores=cores,
@@ -61,9 +82,18 @@ def execute_tasks(cores: int, cache_directory: str):
6182
)
6283

6384

64-
def command_line(arguments_lst: Optional[list] = None):
85+
def command_line(arguments_lst: Optional[list] = None) -> None:
86+
"""
87+
Execute tasks from the command line.
88+
89+
Args:
90+
arguments_lst (Optional[list]): List of command line arguments.
91+
92+
Returns:
93+
None
94+
"""
6595
if arguments_lst is None:
6696
arguments_lst = sys.argv[1:]
6797
cores_arg = arguments_lst[arguments_lst.index("--cores") + 1]
6898
path_arg = arguments_lst[arguments_lst.index("--path") + 1]
69-
execute_tasks(cores=cores_arg, cache_directory=path_arg)
99+
execute_tasks(cores=int(cores_arg), cache_directory=path_arg)

pysqa/executor/executor.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def __init__(
2121
queue_adapter=None,
2222
queue_adapter_kwargs: Optional[dict] = None,
2323
):
24+
"""
25+
Initialize the Executor.
26+
27+
Args:
28+
cwd (Optional[str]): The current working directory. Defaults to None.
29+
queue_adapter: The queue adapter. Defaults to None.
30+
queue_adapter_kwargs (Optional[dict]): Additional keyword arguments for the queue adapter. Defaults to None.
31+
"""
2432
self._task_queue = queue.Queue()
2533
self._memory_dict = {}
2634
self._cache_directory = os.path.abspath(os.path.expanduser(cwd))
@@ -50,7 +58,18 @@ def __init__(
5058
)
5159
self._process.start()
5260

53-
def submit(self, fn: callable, *args, **kwargs):
61+
def submit(self, fn: callable, *args, **kwargs) -> Future:
62+
"""
63+
Submit a function for execution.
64+
65+
Args:
66+
fn (callable): The function to be executed.
67+
*args: Positional arguments to be passed to the function.
68+
**kwargs: Keyword arguments to be passed to the function.
69+
70+
Returns:
71+
Future: A Future object representing the execution of the function.
72+
"""
5473
funct_dict = serialize_funct(fn, *args, **kwargs)
5574
key = list(funct_dict.keys())[0]
5675
if key not in self._memory_dict.keys():
@@ -62,6 +81,13 @@ def submit(self, fn: callable, *args, **kwargs):
6281
return self._memory_dict[key]
6382

6483
def shutdown(self, wait: bool = True, *, cancel_futures: bool = False):
84+
"""
85+
Shutdown the Executor.
86+
87+
Args:
88+
wait (bool): Whether to wait for all tasks to complete before shutting down. Defaults to True.
89+
cancel_futures (bool): Whether to cancel pending futures. Defaults to False.
90+
"""
6591
if cancel_futures:
6692
cancel_items_in_queue(que=self._task_queue)
6793
self._task_queue.put({"shutdown": True, "wait": wait})

pysqa/executor/helper.py

+133-9
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,40 @@
33
import queue
44
import re
55
from concurrent.futures import Future
6+
from typing import List
67

78
import cloudpickle
89

910

1011
def deserialize(funct_dict: dict) -> dict:
12+
"""
13+
Deserialize a dictionary of serialized functions.
14+
15+
Args:
16+
funct_dict (dict): A dictionary containing serialized functions.
17+
18+
Returns:
19+
dict: A dictionary with deserialized functions.
20+
21+
"""
1122
try:
1223
return {k: cloudpickle.loads(v) for k, v in funct_dict.items()}
1324
except EOFError:
1425
return {}
1526

1627

17-
def find_executed_tasks(future_queue: queue.Queue, cache_directory: str):
28+
def find_executed_tasks(future_queue: queue.Queue, cache_directory: str) -> None:
29+
"""
30+
Find executed tasks from the future queue and update the task memory dictionary.
31+
32+
Args:
33+
future_queue (queue.Queue): The queue containing the futures of executed tasks.
34+
cache_directory (str): The directory where the task cache is stored.
35+
36+
Returns:
37+
None
38+
39+
"""
1840
task_memory_dict = {}
1941
while True:
2042
task_dict = {}
@@ -33,14 +55,36 @@ def find_executed_tasks(future_queue: queue.Queue, cache_directory: str):
3355

3456

3557
def read_from_file(file_name: str) -> dict:
58+
"""
59+
Read the contents of a file and return it as a dictionary.
60+
61+
Args:
62+
file_name (str): The name of the file to read.
63+
64+
Returns:
65+
dict: A dictionary containing the contents of the file, with the file name as the key.
66+
67+
"""
3668
name = file_name.split("/")[-1].split(".")[0]
3769
with open(file_name, "rb") as f:
3870
return {name: f.read()}
3971

4072

4173
def reload_previous_futures(
4274
future_queue: queue.Queue, future_dict: dict, cache_directory: str
43-
):
75+
) -> None:
76+
"""
77+
Reload previous futures from the cache directory and update the future dictionary.
78+
79+
Args:
80+
future_queue (queue.Queue): The queue containing the futures of executed tasks.
81+
future_dict (dict): A dictionary containing the current futures.
82+
cache_directory (str): The directory where the task cache is stored.
83+
84+
Returns:
85+
None
86+
87+
"""
4488
file_lst = os.listdir(cache_directory)
4589
for f in file_lst:
4690
if f.endswith(".in.pl"):
@@ -56,16 +100,50 @@ def reload_previous_futures(
56100
future_queue.put({key: future_dict[key]})
57101

58102

59-
def serialize_result(result_dict: dict):
103+
def serialize_result(result_dict: dict) -> dict:
104+
"""
105+
Serialize the values in a dictionary using cloudpickle.
106+
107+
Args:
108+
result_dict (dict): A dictionary containing the values to be serialized.
109+
110+
Returns:
111+
dict: A dictionary with serialized values.
112+
113+
"""
60114
return {k: cloudpickle.dumps(v) for k, v in result_dict.items()}
61115

62116

63-
def serialize_funct(fn: callable, *args, **kwargs):
117+
def serialize_funct(fn: callable, *args, **kwargs) -> dict:
118+
"""
119+
Serialize a function along with its arguments and keyword arguments.
120+
121+
Args:
122+
fn (callable): The function to be serialized.
123+
*args: The arguments to be passed to the function.
124+
**kwargs: The keyword arguments to be passed to the function.
125+
126+
Returns:
127+
dict: A dictionary containing the serialized function.
128+
129+
"""
64130
binary = cloudpickle.dumps({"fn": fn, "args": args, "kwargs": kwargs})
65131
return {fn.__name__ + _get_hash(binary=binary): binary}
66132

67133

68-
def write_to_file(funct_dict: dict, state, cache_directory: str):
134+
def write_to_file(funct_dict: dict, state: str, cache_directory: str) -> List[str]:
135+
"""
136+
Write the contents of a dictionary to files in the cache directory.
137+
138+
Args:
139+
funct_dict (dict): A dictionary containing the contents to be written.
140+
state (str): The state of the files to be written.
141+
cache_directory (str): The directory where the files will be written.
142+
143+
Returns:
144+
List[str]: A list of file names that were written.
145+
146+
"""
69147
file_name_lst = []
70148
for k, v in funct_dict.items():
71149
file_name = _get_file_name(name=k, state=state)
@@ -75,23 +153,69 @@ def write_to_file(funct_dict: dict, state, cache_directory: str):
75153
return file_name_lst
76154

77155

78-
def _get_file_name(name, state):
156+
def _get_file_name(name: str, state: str) -> str:
157+
"""
158+
Generate a file name based on the given name and state.
159+
160+
Args:
161+
name (str): The name to be included in the file name.
162+
state (str): The state of the file.
163+
164+
Returns:
165+
str: The generated file name.
166+
167+
"""
79168
return name + "." + state + ".pl"
80169

81170

82-
def _get_hash(binary):
171+
def _get_hash(binary: bytes) -> str:
172+
"""
173+
Get the hash of a binary using MD5 algorithm.
174+
175+
Args:
176+
binary (bytes): The binary data to be hashed.
177+
178+
Returns:
179+
str: The hexadecimal representation of the hash.
180+
181+
"""
83182
# Remove specification of jupyter kernel from hash to be deterministic
84183
binary_no_ipykernel = re.sub(b"(?<=/ipykernel_)(.*)(?=/)", b"", binary)
85184
return str(hashlib.md5(binary_no_ipykernel).hexdigest())
86185

87186

88-
def _set_future(file_name, future):
187+
def _set_future(file_name: str, future: Future) -> None:
188+
"""
189+
Set the result of a future based on the contents of a file.
190+
191+
Args:
192+
file_name (str): The name of the file containing the result.
193+
future (Future): The future to set the result for.
194+
195+
Returns:
196+
None
197+
198+
"""
89199
values = deserialize(funct_dict=read_from_file(file_name=file_name)).values()
90200
if len(values) == 1:
91201
future.set_result(list(values)[0])
92202

93203

94-
def _update_task_dict(task_dict, task_memory_dict, cache_directory):
204+
def _update_task_dict(
205+
task_dict: dict, task_memory_dict: dict, cache_directory: str
206+
) -> None:
207+
"""
208+
Update the task memory dictionary with the futures from the task dictionary.
209+
210+
Args:
211+
task_dict (dict): A dictionary containing the futures of tasks.
212+
task_memory_dict (dict): The dictionary to store the task memory.
213+
cache_directory (str): The directory where the task cache is stored.
214+
215+
Returns:
216+
None
217+
218+
"""
95219
file_lst = os.listdir(cache_directory)
96220
for key, future in task_dict.items():
97221
task_memory_dict[key] = future

0 commit comments

Comments
 (0)