Reference library

Automation & scripting

CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.

4 matches
Automation & scripting easy

How to Generate a cloud-init User Data Mock in Python

Generate a cloud-init user data mock for a VM using a dataclass and JSON in Python.

cloud-init automation dataclasses
Python
import json
from dataclasses import dataclass, asdict

@dataclass
class VMConfig:
    hostname: str
    cpus: int
    memory_mb: int
    ssh_key: str

def generate_cloud_init_mock(config: VMConfig) -> str:
    """Build a cloud-init user-data mock for a VM."""
    user_data = {
        "hostname": config.hostname,
    …
14 0 Open
Automation & scripting easy

How to Mock a Whisper API Transcription Stub in Python

Simulate an OpenAI Whisper-style transcription response with a dataclass request model and a mock function that returns structured audio transcription output.

mock whisper api-stub
Python
import json
from dataclasses import dataclass
from typing import Optional

@dataclass
class AudioRequest:
    file_path: str
    language: Optional[str] = None

    def to_api_payload(self) -> dict:
        return {"file": self.file_path, "language": self.language}

def mock_whisper_transcribe(payload: dict) -> dict:
…
15 0 Open
Automation & scripting easy

How to Save a VM Snapshot State to a JSON File in Python

Define a dataclass for a VM snapshot and serialize it to a JSON file, then reload it to verify the state.

json dataclass files
Python
import json
from dataclasses import dataclass, asdict
from pathlib import Path


@dataclass
class VMSnapshot:
    name: str
    memory_mb: int
    disk_gb: int
    state: str = "saved"

    def snapshot_to_file(self, path: Path) -> str:
        """Write snapshot state to a JSON file and return the filename."""
       …
13 0 Open
Automation & scripting easy

Resize Disk Partitions in Python (Mock Script)

A mock disk partition resize script that uses dataclasses to model partitions, validate new sizes, and output the updated layout as JSON.

disk partition dataclass
Python
#!/usr/bin/env python3
"""Mock script to demonstrate disk partition resize logic."""
import json
from dataclasses import dataclass
from typing import Dict


@dataclass
class Partition:
    name: str
    size_gb: int
    mount_point: str

    def to_dict(self) -> Dict[str, object]:
        return {
            "name": …
16 0 Open

Browse by section

Each section groups closely related Python snippets.

Automation & scripting — Python code examples

What you will find here

This page collects automation & scripting snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.