Reference library

Automation & scripting

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

4 matches
Automation & scripting easy

Aggregate Log Errors Count by Hour in Python

Counts ERROR log lines per hour using regex and Counter, returning a sorted dictionary of hourly totals.

logs regex counter
Python
import re
from collections import Counter
from datetime import datetime

def aggregate_errors_by_hour(log_lines):
    pattern = re.compile(r'^(\d{4}-\d{2}-\d{2} \d{2}):\d{2}:\d{2}.*ERROR')
    hourly_counts = Counter()
    
    for line in log_lines:
        match = pattern.match(line)
        if match:
            ho…
20 0 Open
Automation & scripting easy

Fill PDF Form Fields from a Mock Template in Python

Fills a PDF-style form template dictionary with user data, preserving template fields and formatting output as JSON.

pdf forms json
Python
import json

template = {
    "first_name": "",
    "last_name": "",
    "email": "",
    "phone": "",
    "date_of_birth": "",
    "address": "",
    "city": "",
    "state": "",
    "zip_code": "",
    "agree_to_terms": False
}


def fill_pdf_form(template: dict, data: dict) -> dict:
    for key, value in data.items…
10 0 Open
Automation & scripting easy

How to Import Users from CSV into LDAP-like Dicts in Python

Reads a CSV of user records and converts each row into an LDAP-style dictionary with standard attributes using Python's csv module.

csv ldap import
Python
import csv
import io
from pathlib import Path


def mock_ldap_import(csv_path):
    """
    Reads a CSV file with user data and returns a list of LDAP-like user dicts.
    Adds standard LDAP attributes that would come from directory schema.
    """
    with open(csv_path, newline="", encoding="utf-8") as csvfile:
    …
14 0 Open
Automation & scripting easy

How to Map Network Drive Paths to Local Paths in Python

Convert mock SMB network drive paths (like 'S:\reports\q1.xlsx') to local placeholder paths and back using a simple mapping dictionary in Python.

network path-mapping smb
Python
"""Map mock SMB network drive paths to local placeholder paths."""
from dataclasses import dataclass

@dataclass(frozen=True)
class NetworkDrive:
    letter: str
    remote_path: str

DRIVES = {
    "S:": NetworkDrive("S", r"\\server01\shares\sales"),
    "M:": NetworkDrive("M", r"\\server02\media\movies"),
    "X:": …
14 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.