How to Design a Cloud Data Helper Class in Python

A beginner-friendly Python helper class that saves, loads, and aggregates JSON records locally, simulating cloud-style data handling.

Easy Python 3.9+ Aug 9, 2026 Cloud + Python 12 views 0 copies

Python code

46 lines
Python 3.9+
import json
from pathlib import Path
from datetime import datetime


class CloudDataHelper:
    """Beginner-friendly helper for working with cloud-based JSON data."""

    def __init__(self, base_dir="cloud_data"):
        self.base_dir = Path(base_dir)
        self.base_dir.mkdir(exist_ok=True)

    def save_record(self, record: dict, filename: str) -> Path:
        """Save a record to a JSON file with timestamp."""
        filepath = self.base_dir / f"{filename}_{datetime.now():%Y%m%d_%H%M%S}.json"
        filepath.write_text(json.dumps(record, indent=2))
        return filepath

    def load_records(self, prefix: str) -> list:
        """Load all JSON records with given prefix."""
        records = []
        for filepath in self.base_dir.glob(f"{prefix}_*.json"):
            records.append(json.loads(filepath.read_text()))
        return records

    def aggregate(self, records: list, key: str) -> dict:
        """Count occurrences of each value for a given key."""
        counts = {}
        for record in records:
            value = record.get(key, "unknown")
            counts[value] = counts.get(value, 0) + 1
        return counts


if __name__ == "__main__":
    helper = CloudDataHelper()

    helper.save_record({"user": "alice", "plan": "free", "region": "us-east"}, "user")
    helper.save_record({"user": "bob", "plan": "pro", "region": "eu-west"}, "user")
    helper.save_record({"user": "carol", "plan": "free", "region": "us-east"}, "user")

    all_users = helper.load_records("user")
    plan_counts = helper.aggregate(all_users, "plan")

    print(f"Loaded {len(all_users)} user records")
    print(f"Plan distribution: {plan_counts}")

Output

stdout
Loaded 3 user records
Plan distribution: {'free': 2, 'pro': 1}

How it works

The CloudDataHelper class uses pathlib.Path for cross-platform file handling and auto-creates a directory for cloud-like data storage. The save_record method appends a timestamp to filenames to avoid collisions and produce unique versions. load_records uses glob patterns to fetch all files with a given prefix, making it easy to retrieve data based on logical groups. aggregate counts occurrences for a specified key using a dictionary and the get method to handle missing keys gracefully. This structure mirrors minimal cloud storage patterns, teaching users how to organize file-based data before moving to real cloud services.

Common mistakes

  • Forgetting to include the timestamp in filenames, which can cause record collisions during saves.
  • Using `glob` patterns that don't match the actual naming convention after adding timestamps.
  • Assuming all records have the aggregation key without using `.get()` to avoid KeyError.
  • Not creating the base directory before saving, leading to FileNotFoundError.

Variations

  1. Use a different timestamp format like `%Y%m%d_%H%M%S_%f` for millisecond precision.
  2. Store records in separate JSON files per entity type by passing different prefixes.

Real-world use cases

  • Managing user profile snapshots in local development before syncing to cloud storage.
  • Storing and analyzing API responses received from cloud services like AWS S3 or Azure Blob.
  • Creating simple offline data ingestion scripts for edge devices that later upload to the cloud.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Cloud + Python

Related tutorials and quizzes for this topic.