-
Notifications
You must be signed in to change notification settings - Fork 1
Add data processing utilities #48
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,64 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Utility functions for data processing.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def load_config(filepath): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(filepath, "r") as f: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config = json.load(f) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check failure on line 11 in data_utils.py
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| config = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+13
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. Two issues:
Move
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def merge_records(primary, secondary): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| merged = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for key in primary: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| merged[key] = primary[key] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check failure on line 21 in data_utils.py
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for key in secondary: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if key not in merged: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| merged[key] = secondary[key] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check failure on line 28 in data_utils.py
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| merged[key] = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return merged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+16
to
+31
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. The try/except blocks inside both loops are dead code. You're iterating Also violates the bare
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def validate_and_transform(records): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| results = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for record in records: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| transformed = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "id": record["id"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "name": record.get("name", "unknown"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "score": int(record.get("score", 0)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| results.append(transformed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check failure on line 44 in data_utils.py
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. Bare At minimum, catch only the specific exceptions you expect (
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return results | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def export_to_file(data, output_path, format="json"): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.makedirs(os.path.dirname(output_path), exist_ok=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
Guard the call:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if format == "json": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(output_path, "w") as f: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json.dump(data, f, indent=2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif format == "csv": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with open(output_path, "w") as f: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if data: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers = data[0].keys() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f.write(",".join(headers) + "\n") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for row in data: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f.write(",".join(str(row.get(h, "")) for h in headers) + "\n") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+55
to
+61
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. Manual CSV construction using
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check failure on line 62 in data_utils.py
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. Bare
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
All four public functions (
load_config,merge_records,validate_and_transform,export_to_file) are missing docstrings. Project coding standards require docstrings on every public function describing what it does, its parameters, and its return value.