Skip to content

Conversation

@saadiqbal09
Copy link

@saadiqbal09 saadiqbal09 commented Dec 25, 2025

This PR adds utility functions to export PSLab sensor data
to CSV and JSON formats.

  • Useful for IoT data logging and analysis
  • Pure Python implementation
  • Includes basic pytest-based tests
  • No hardware required

This improves data usability for PSLab users.

Summary by Sourcery

Add utility module for exporting PSLab sensor data to common file formats.

New Features:

  • Introduce CSV export helper for writing lists of dictionaries to files with headers.
  • Introduce JSON export helper for serializing data collections to formatted JSON files.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 25, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds 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 interactions

sequenceDiagram
    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
Loading

Flow diagram for CSV and JSON data export utilities

flowchart 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
Loading

File-Level Changes

Change Details Files
Introduce CSV export helper for list-of-dict sensor data.
  • Define export_to_csv(data, filename) that expects a non-empty list of dictionaries.
  • Return early without creating a file when data is falsy.
  • Write CSV using csv.DictWriter with fieldnames derived from the keys of the first element.
  • Always emit a header row followed by one row per dict in the input list.
pslab/utils/data_export.py
Introduce JSON export helper for arbitrary serializable sensor data.
  • Define export_to_json(data, filename) to write JSON to disk.
  • Use json.dump with a fixed indentation level for pretty-printed output.
  • Assume data is JSON-serializable and do not perform additional validation.
pslab/utils/data_export.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a 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_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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant