-
Notifications
You must be signed in to change notification settings - Fork 229
Add CSV and JSON export utilities for PSLab sensor data #260
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
Open
saadiqbal09
wants to merge
2
commits into
fossasia:main
Choose a base branch
from
saadiqbal09:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds simple CSV and JSON export utilities for PSLab sensor data, implemented as filesystem-based helper functions with minimal validation and accompanying I/O behavior. Sequence diagram for CSV and JSON export utility interactionssequenceDiagram
actor Caller
participant data_export_module
participant csv_module
participant json_module
participant FileSystem
Caller ->> data_export_module: export_to_csv(data, filename)
alt data is empty
data_export_module -->> Caller: return
else data is not empty
data_export_module ->> FileSystem: open(filename, write, newline empty)
FileSystem -->> data_export_module: file_handle
data_export_module ->> csv_module: DictWriter(file_handle, fieldnames from data[0].keys())
csv_module -->> data_export_module: writer
data_export_module ->> writer: writeheader()
data_export_module ->> writer: writerows(data)
data_export_module ->> FileSystem: close(file_handle)
data_export_module -->> Caller: return
end
Caller ->> data_export_module: export_to_json(data, filename)
data_export_module ->> FileSystem: open(filename, write)
FileSystem -->> data_export_module: file_handle
data_export_module ->> json_module: dump(data, file_handle, indent 4)
json_module -->> data_export_module: complete
data_export_module ->> FileSystem: close(file_handle)
data_export_module -->> Caller: return
Flow diagram for CSV and JSON data export utilitiesflowchart TD
A["Start"] --> B["Caller invokes export_to_csv or export_to_json with data and filename"]
B --> C{"Function selected"}
C --> D["export_to_csv(data, filename)"]
C --> H["export_to_json(data, filename)"]
%% CSV path
D --> E{"Is data empty"}
E --> F["Return without creating file"]
E --> G["Open filename for write with newline empty using csv module"]
G --> I["Create DictWriter with fieldnames from first dict keys"]
I --> J["Write header row"]
J --> K["Write all data rows"]
K --> L["Close file"]
L --> M["End"]
F --> M
%% JSON path
H --> N["Open filename for write"]
N --> O["Use json module to dump data with indent 4"]
O --> P["Close file"]
P --> M
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey - I've found 1 issue, and left some high level feedback:
- Consider making
export_to_csvbehavior explicit for emptydata(e.g., create an empty file with just a header, or raise a clear error) rather than silently returning, so callers can rely on a predictable outcome. - It may be useful for both
export_to_csvandexport_to_jsonto accept a file-like object in addition to a filename, which would make them easier to use in memory-only or web/CLI contexts.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider making `export_to_csv` behavior explicit for empty `data` (e.g., create an empty file with just a header, or raise a clear error) rather than silently returning, so callers can rely on a predictable outcome.
- It may be useful for both `export_to_csv` and `export_to_json` to accept a file-like object in addition to a filename, which would make them easier to use in memory-only or web/CLI contexts.
## Individual Comments
### Comment 1
<location> `pslab/utils/data_export.py:11` </location>
<code_context>
+import csv
+import json
+
+def export_to_csv(data, filename):
+ """
+ data: list of dictionaries
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider adding an explicit encoding when opening the CSV file.
Relying on the platform default encoding can cause failures or inconsistent results on different systems, particularly with non-ASCII data. Please specify an explicit encoding (e.g. `encoding="utf-8"`) for consistent behavior.
```suggestion
with open(filename, "w", newline="", encoding="utf-8") as f:
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds utility functions to export PSLab sensor data
to CSV and JSON formats.
This improves data usability for PSLab users.
Summary by Sourcery
Add utility module for exporting PSLab sensor data to common file formats.
New Features: