info: append grade details from grades_summary.json#5
Conversation
Summary of ChangesHello @kowyo-bot, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds a useful feature to display grade composition details when querying for course information. The implementation is well-structured, introducing helper functions to load grade data and print the details. My review includes a couple of suggestions to enhance code quality and maintainability. Specifically, I recommend using more specific exception handling for robustness and refactoring a piece of logic to be more concise and idiomatic, which will make the code easier to read and maintain. Overall, the changes are solid and correctly implement the new functionality.
| except Exception as e: | ||
| logger.warning(f"无法读取 {path.name}: {e}") |
There was a problem hiding this comment.
It's a good practice to catch more specific exceptions rather than the generic Exception. This prevents catching unexpected errors like KeyboardInterrupt or SystemExit and makes the error handling more robust and explicit. In this case, you could catch json.JSONDecodeError for issues with JSON parsing and OSError for file-related problems.
| except Exception as e: | |
| logger.warning(f"无法读取 {path.name}: {e}") | |
| except (json.JSONDecodeError, OSError) as e: | |
| logger.warning(f"无法读取或解析 {path.name}: {e}") |
src/hoa_majors/cli/info.py
Outdated
| grade_items = None | ||
| for k in year_major_keys: | ||
| if k in entry: | ||
| grade_items = entry.get(k) | ||
| break | ||
| if grade_items is None and year_default_key and year_default_key in entry: | ||
| grade_items = entry.get(year_default_key) | ||
| if grade_items is None and "default" in entry: | ||
| grade_items = entry.get("default") |
There was a problem hiding this comment.
The logic for selecting grade_items can be significantly simplified to be more concise and Pythonic. You can build a list of keys to try in order of precedence, and then use a generator expression with next() to find the first matching item. This is more readable and maintainable than the current series of if statements and redundant checks.
search_keys = year_major_keys + ([year_default_key] if year_default_key else []) + ["default"]
grade_items = next((entry[key] for key in search_keys if key in entry), None)
What
When running:
the command will append grade composition details from
grades_summary.json(if present).Match order
For a given
<course_code>, we select grade items in this order:year_major(tries${year}_${major_code}first, then${year}_${major_name}to match upstream keys like2021_自动化)year_default(e.g.2021_default)defaultIf no match is found, nothing is appended.