-
Notifications
You must be signed in to change notification settings - Fork 23
[WIP]rewrite workflow implementation. Add prefect framework. prepare for paper. #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 250 commits
024372e
f06e4f9
71b0996
ad80c18
efb36b7
b7130de
fef222f
7c0a5d5
cfa7d71
eb846f2
b8420ae
5a9263d
7ec9cf7
e057cc5
f7cf1df
45cbec7
9b31c27
b5aa53b
1bbed62
5075455
066cc50
3e1aabc
ab7583d
9693f59
86e167d
6384958
6fd1225
fa9384d
f2c2d92
4ad0c62
ca64639
afbd308
bc75ba3
78cfc8f
9f44d6b
02bd08b
e6e9f5d
a72843b
1882b85
846a89b
42854a5
8d6aecc
65ee5e8
e7b0671
292f8a9
8a2ff8c
443e670
d34d16a
c055420
2718508
bee7c83
1162ccb
2318c09
99ed3dc
61bd829
9e4b5a3
29d7b7a
7467a85
fde752d
251ae3a
cc465b0
55a2c29
221445d
2a7b0e1
edee593
fd73f12
ae3434c
94fce85
7808bd8
3341a14
ab8ff64
8240239
71e19ae
82be973
a72d34f
b061de4
77d0898
b206e6c
6d41dd4
8ec85ac
5648868
1073723
8ba66cb
831b8b5
c2fa300
835d0f4
d8266ab
59601cc
1ebd52b
a066c98
6e7a6b9
7484f50
334c364
b3218e3
5777439
84a66c3
487d28d
f11764b
1b61936
9db9f0b
a2e50c0
7021421
8fb7b2f
ba5c1e7
7522ac0
3c5c86b
f53a986
edff7de
23623ee
d8d3189
7ae81d5
5cd091b
8265c72
ea6452c
c362aab
0b7cdc3
7f61b60
43aa25d
92af5d3
3443bc8
2cb1053
2f0c6c4
df15ddc
224b191
4d98a04
590fcfc
abf3fec
551ba03
78fe73c
20c9188
2a30479
f2388dd
79811b9
881ccec
910e359
60502c6
db55d2c
3ec59d5
4156c17
691c9bc
b66711b
9d3a857
9787d4e
9138f70
cc882fb
e4f79c4
af26a76
a07e9bc
8db82a2
dbf3b46
8c29f7d
74c11c5
c042df5
c81ae48
7e892a6
3c3a370
e22ef9b
f99465e
14a981f
621bb56
c3ecb61
db59da6
1200e91
026ab27
b2ec5e4
4f35af1
3c9fbd9
d0076f9
ca540d1
e6a8cab
5d8bbc2
42ae24b
0b9a1d0
b568940
d046593
19c81d5
3360eb3
dedc736
5cf792a
67ebf8f
1a2334d
8beb7cc
ebd0a88
68710aa
67449f7
c95a0dc
04c22f7
bb98509
2609384
565cf0c
76fe53a
3bc24bd
898c4fc
de6ae21
8530fce
8f2cec4
594b0a1
564017d
95b1403
3747fd5
623dc23
ad50f5e
dd021a9
8087253
40932f8
f061a9b
aefca89
1d84c8b
63864e3
238fd16
e76e956
1b29855
e844bdc
c76b61f
4c440a6
a74205a
70a4990
347d0ac
c408440
8a0e679
d8ba6fa
a25c159
bec938a
f090051
e293467
e60f63a
f505d72
52f9070
0cee3bb
d1a9c5c
ebf99d8
398adcf
ac7cb1d
fb429ed
b831d34
b644ded
8e3a1c6
9ba1d35
ade6c26
1255a30
e572496
0dac766
34b86be
e694a19
1327d04
a883b79
483544d
6b85bd3
d374484
c05c9fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| --- | ||
| description: General Rules | ||
| globs: *py,*json,*yaml,*ipynb,*sh,*md,*txt,*.lammps,*tex,*toml,*yml | ||
| alwaysApply: false | ||
| --- | ||
|
|
||
| <!-- - You can @ files here | ||
| - You can use markdown but dont have to --> | ||
| [base.py](mdc:dpti/workflows/simulations/base.py) [equi.py](mdc:dpti/equi.py) [equi_sim.py](mdc:dpti/workflows/simulations/equi_sim.py) [ti.py](mdc:dpti/ti.py) [mti.py](mdc:dpti/mti.py) [ti_sim.py](mdc:dpti/workflows/simulations/ti_sim.py) [hti.py](mdc:dpti/hti.py) [hti_liq.py](mdc:dpti/hti_liq.py) [hti_ice.py](mdc:dpti/hti_ice.py) [hti_water.py](mdc:dpti/hti_water.py) [hti_sim.py](mdc:dpti/workflows/simulations/hti_sim.py) [einstein.py](mdc:dpti/einstein.py) [di.py](mdc:dpti/workflows/service/di.py) [file_handler.py](mdc:dpti/workflows/service/file_handler.py) [job_executor.py](mdc:dpti/workflows/service/job_executor.py) [ti_line_flow.py](mdc:dpti/workflows/flows/ti_line_flow.py) | ||
|
|
||
|
|
||
| # 角色 | ||
| 你是一名精通科学计算和分子动力学模拟的高级工程师,拥有10年以上的Python科学计算应用开发经验,熟悉LAMMPS、DeepMD-kit、Prefect等开发工具和技术栈。你的任务是帮助用户设计和开发易用且易于维护的分子动力学自由能计算应用。始终遵循科学计算领域的最佳实践,并坚持代码可重复性和计算效率的原则。 | ||
| # 目标 | ||
| 你的目标是以用户容易理解的方式帮助他们完成分子动力学自由能计算应用的设计和开发工作,确保计算结果准确可靠、性能高#效、用户使用便捷。 | ||
| # 要求 | ||
| 在理解用户需求、设计计算流程、编写代码、解决问题和项目迭代优化时,你应该始终遵循以下原则: | ||
| 项目初始化 | ||
| 仔细阅读项目的README.md,理解自由能计算的基本原理、计算流程和技术实现方案 | ||
| 如果没有README.md,创建一个包含计算理论基础、输入输出格式、依赖环境等信息的文档 | ||
| # 需求理解 | ||
| 理解用户的具体计算需求,包括体系类型、温度压力条件、所需精度等 | ||
| 选择合适的热力学积分方法,避免不必要的计算 | ||
| # 计算流程设计 | ||
| 使用Prefect设计可重复、可追踪的计算工作流 | ||
| 确保计算任务在不同计算环境下都能正确运行 | ||
| # 代码编写 | ||
| 技术选型: | ||
| Python科学计算库(numpy, scipy)用于数据处理和分析 | ||
| LAMMPS用于分子动力学模拟 | ||
| Prefect 框架用于工作流管理 | ||
| DeepMD-kit用于机器学习势能计算 | ||
| # 代码结构: | ||
| 模块化设计,将计算、分析、可视化等功能分离 | ||
| 使用面向对象编程,提高代码复用性 | ||
| 遵循Python科学计算代码规范 | ||
| # 计算安全性: | ||
| 检查输入参数的合法性 | ||
| 保存计算中间结果,支持断点续算 | ||
| 验证计算结果的物理合理性 | ||
| # 性能优化: | ||
| 优化并行计算策略 | ||
| 减少不必要的IO操作 | ||
| 合理使用内存缓存 | ||
| # 测试与文档: | ||
| 编写单元测试验证计算正确性 | ||
| 提供详细的API文档和使用示例 | ||
| 添加计算原理和方法的说明 | ||
| # 问题解决 | ||
| 全面理解分子动力学模拟和自由能计算的原理 | ||
| 根据计算结果的异常分析问题原因 | ||
| 确保代码修改不影响计算的物理正确性 | ||
| # 迭代优化 | ||
| 收集用户反馈,优化计算流程和接口设计 | ||
| 及时更新文档,包括新增功能和优化建议 | ||
| 持续改进计算性能和稳定性 | ||
| # 方法论 | ||
| 1.Unix编程风格,开源软件设计风格,恰当根据项目或者模块需要,使用设计模式 | ||
| 2.坚持dont repeat your self 原则 | ||
| 3.在软件架构上给出成熟考虑。 | ||
| 2.回答问题时,也给出关于设计模式上的思考,提示用户相关联的最佳实践(如果有)。 |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,163 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # %% | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import argparse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Dict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def collect_python_files(directory: str) -> Dict[str, str]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Replace deprecated Static analysis correctly identifies that -def collect_python_files(directory: str) -> Dict[str, str]:
+def collect_python_files(directory: str) -> dict[str, str]:
-def group_by_directory(
- files: Dict[str, str], max_files_per_group: int = 20
-) -> Dict[str, Dict[str, str]]:
+def group_by_directory(
+ files: dict[str, str], max_files_per_group: int = 20
+) -> dict[str, dict[str, str]]:
-def save_grouped_json(groups: Dict[str, Dict[str, str]], output_base: str) -> None:
+def save_grouped_json(groups: dict[str, dict[str, str]], output_base: str) -> None:Also applies to: 46-47, 96-96 🧰 Tools🪛 Ruff (0.11.9)9-9: Use Replace with (UP006) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Collect paths and contents of all Python files in the specified directory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| directory: Directory path to scan | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dictionary containing file paths and their contents | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| python_files = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| base_dir = Path(directory).name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Use Path object for directory traversal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for file_path in Path(directory).rglob("*.py"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Skip __pycache__ directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if "__pycache__" in str(file_path): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Read file content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(file_path, encoding="utf-8") as f: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content = f.read() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Convert to relative path and include base directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| relative_path = str(file_path.relative_to(directory)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| full_path = f"{base_dir}/{relative_path}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| python_files[full_path] = content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Error reading file {file_path}: {e!s}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return python_files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def group_by_directory( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| files: Dict[str, str], max_files_per_group: int = 20 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Dict[str, Dict[str, str]]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Group files by their directory structure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| files: Dictionary of file paths and contents | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_files_per_group: Maximum number of files per group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dictionary of grouped files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # First group by directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dir_groups = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for file_path, content in files.items(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dir_name = str(Path(file_path).parent) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if dir_name not in dir_groups: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dir_groups[dir_name] = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dir_groups[dir_name][file_path] = content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Merge small groups and split large groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final_groups = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_group = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_group_size = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group_index = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for dir_name, dir_files in dir_groups.items(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # If adding this directory's files would exceed the limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_group_size + len(dir_files) > max_files_per_group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and current_group_size > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Save current group and start a new one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final_groups[f"group_{group_index}"] = current_group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_group = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_group_size = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group_index += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Add files to current group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_group.update(dir_files) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current_group_size += len(dir_files) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Don't forget to save the last group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current_group: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final_groups[f"group_{group_index}"] = current_group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return final_groups | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def save_grouped_json(groups: Dict[str, Dict[str, str]], output_base: str) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Save each group to a separate JSON file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| groups: Grouped files dictionary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_base: Base name for output files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_base = Path(output_base) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| base_name = output_base.stem | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parent_dir = output_base.parent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+104
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add validation for output directory writability. The function doesn't verify that the output directory exists or is writable before attempting to save files. output_base = Path(output_base)
base_name = output_base.stem
parent_dir = output_base.parent
+
+ # Ensure output directory exists and is writable
+ try:
+ parent_dir.mkdir(parents=True, exist_ok=True)
+ except Exception as e:
+ print(f"Error creating output directory {parent_dir}: {e}")
+ return📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for group_name, group_files in groups.items(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| output_file = parent_dir / f"{base_name}_{group_name}.json" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| wrapper = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "project_files": { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "description": f"Python source files collection - {group_name}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "base_directory": Path().absolute().name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "files": group_files, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(output_file, "w", encoding="utf-8") as f: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json.dump(wrapper, f, ensure_ascii=False, indent=2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Saved {len(group_files)} files to {output_file}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def main(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser = argparse.ArgumentParser( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description="Collect Python files content into JSON for context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.add_argument( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "-d", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "--directory", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default="../../../dpti/", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help="Directory to scan (default: current directory)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+127
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid hardcoded relative paths in default arguments The default directory path "../../../dpti/" is brittle and may break depending on where the script is executed from. Consider using a more robust approach. parser.add_argument(
"-d",
"--directory",
- default="../../../dpti/",
+ default=".",
help="Directory to scan (default: current directory)",
)📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.add_argument( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "-o", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "--output", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default="build/python_files_context.json", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help="Output JSON file base name (default: python_files_context.json)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parser.add_argument( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "-n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "--num-files", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type=int, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default=20, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| help="Maximum number of files per group (default: 20)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| args = parser.parse_args() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Collect Python files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| python_files = collect_python_files(args.directory) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Group files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| groups = group_by_directory(python_files, args.num_files) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Save grouped files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| save_grouped_json(groups, args.output) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"\nTotal files collected: {len(python_files)}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Split into {len(groups)} groups") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+148
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add validation and error handling for input directory The code doesn't check if the specified directory exists before attempting to scan it. Consider adding directory validation. args = parser.parse_args()
+# Validate input directory
+input_dir = Path(args.directory)
+if not input_dir.exists() or not input_dir.is_dir():
+ print(f"Error: Directory '{args.directory}' does not exist or is not a directory")
+ exit(1)
+
# Collect Python files
python_files = collect_python_files(args.directory)
+# Check if any files were found
+if not python_files:
+ print(f"Warning: No Python files found in {args.directory}")
+
# Group files
groups = group_by_directory(python_files, args.num_files)
# Save grouped files
save_grouped_json(groups, args.output)📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| main() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # import importlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # def _import_module(module_name): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # return importlib.import_module(f'dpti.workflows.{module_name}') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # except ImportError: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # return importlib.import_module(f'.{module_name}', package='dpti.workflows') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # MODULES_TO_IMPORT = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 'job_executor', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # # 'another_module', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # # 'yet_another_module', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # # dynamically import sub modules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # for module_name in MODULES_TO_IMPORT: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # globals()[module_name] = _import_module(module_name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commented-out module import system needs cleanup The file contains a commented-out dynamic module import system with only a Either:
The current state creates confusion about whether this functionality is intended to be used or not. If this is a work in progress, add a TODO comment explaining the plan. -pass
-# import importlib
+import importlib
-# def _import_module(module_name):
-# try:
-# return importlib.import_module(f'dpti.workflows.{module_name}')
-# except ImportError:
-# return importlib.import_module(f'.{module_name}', package='dpti.workflows')
+def _import_module(module_name):
+ try:
+ return importlib.import_module(f'dpti.workflows.{module_name}')
+ except ImportError:
+ return importlib.import_module(f'.{module_name}', package='dpti.workflows')
-# MODULES_TO_IMPORT = [
-# 'job_executor',
-# # 'another_module',
-# # 'yet_another_module',
-# ]
+MODULES_TO_IMPORT = [
+ 'job_executor',
+ # Add other modules as they become available
+]
-# # dynamically import sub modules
-# for module_name in MODULES_TO_IMPORT:
-# globals()[module_name] = _import_module(module_name)
+# dynamically import sub modules
+for module_name in MODULES_TO_IMPORT:
+ globals()[module_name] = _import_module(module_name)📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Update deprecated type annotations.
The
typing.Dictimport is deprecated in favor of the built-indicttype (Python 3.9+).🧰 Tools
🪛 Ruff (0.11.9)
6-6:
typing.Dictis deprecated, usedictinstead(UP035)
🤖 Prompt for AI Agents