Reference library

Python Code Samples

Medium snippets you can copy, study, and run in the browser editor.

5 matches
Files & data medium

Chunk Large File Upload Simulation by Blocks in Python

A Python script reads a large binary file in fixed-size chunks and simulates a block-by-block upload with per-chunk SHA256 hashing.

file i/o chunking hashing
Python
import os
import hashlib
from pathlib import Path


def read_file_in_chunks(file_path, chunk_size=8196):
    """Yield chunks of a file as bytes."""
    with open(file_path, 'rb') as f:
        while chunk := f.read(chunk_size):
            yield chunk


def simulate_chunked_upload(file_path, chunk_size=8196):
    """S…
15 0 Open
Cloud + Python medium

How to mock boto3 S3 upload file wrapper in Python

Wrap an S3 put_object call in a testable function that returns metadata, and mock boto3 to verify the upload without touching AWS.

boto3 s3 aws
Python
import boto3
import io


def upload_file_to_s3(file_obj, bucket, key, object_metadata=None):
    """Upload a file-like object to S3 and return a metadata dict."""
    s3 = boto3.client("s3")
    content = file_obj.read()
    s3.put_object(
        Bucket=bucket,
        Key=key,
        Body=content,
        Metadata=…
13 0 Open
Cloud + Python medium

How to mock boto3 S3 upload in Python

Shows how to mock the boto3 S3 client with unit tests and wrap an upload function to return a dictionary with status details.

boto3 s3 mocking
Python
import boto3
from unittest.mock import Mock, patch

class S3Uploader:
    def __init__(self, bucket_name):
        self.bucket_name = bucket_name
        self.s3 = boto3.client("s3", region_name="us-east-1")

    def upload_file(self, local_path, s3_key):
        self.s3.upload_file(local_path, self.bucket_name, s3_ke…
12 0 Open
Cloud + Python medium

Mock GCP storage bucket blob upload in Python

Simulate uploading a blob to a GCP Storage bucket for testing without hitting the cloud.

gcp mock storage
Python
import io
from datetime import datetime
from unittest.mock import MagicMock, patch


class MockBlob:
    """Simulates a GCP storage blob for unit testing."""
    def __init__(self, name):
        self.name = name
        self.uploaded_at = None
        self.content = b""

    def upload_from_file(self, file_obj):
    …
14 0 Open
API design & gRPC medium

How to Parse Multipart Form Data in Python

Parse multipart/form-data uploads using the Python standard library's cgi module to extract both regular fields and file uploads.

multipart cgi form-data
Python
import cgi
from io import BytesIO

def parse_multipart_form(headers, body_bytes):
    content_type = headers.get("Content-Type", "")
    content_length = int(headers.get("Content-Length", len(body_bytes)))
    
    # Create a file-like object from bytes for cgi.FieldStorage
    body_file = BytesIO(body_bytes)
    
   …
13 0 Open

Browse by section

Each section groups closely related Python snippets.

Guide: free Python code samples library

Copy-ready Python snippets for learners and developers

PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.

How to use this library

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. Run it in the IDE, tweak values, then take a related quiz or tutorial lesson

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.