Python Code
Samples
Easy snippets you can copy, study, and run in the browser editor.
How to Build a Mock Trivy Image Scan Gate in Python
Simulate a Trivy image scan and enforce a security gate that fails the pipeline when vulnerabilities meet or exceed a severity threshold.
import json
import sys
def mock_trivy_scan(image_name, severity_threshold="HIGH"):
"""Simulate a Trivy image scan result."""
mock_vulnerabilities = [
{"ID": "CVE-2023-1234", "Severity": "HIGH", "Package": "openssl", "FixedVersion": "3.0.9"},
{"ID": "CVE-2024-5678", "Severity": "CRITICAL", "Pa…
How to Mock a CI Pipeline with Build, Test, and Deploy Stages in Python
Simulate a three-stage CI pipeline (build, test, deploy) in Python with random pass/fail logic, early exit on failure, and measured stage durations.
import time
import random
from dataclasses import dataclass
@dataclass
class StageResult:
name: str
status: str
duration: float
def run_stage(name: str, success_chance: float = 0.9) -> StageResult:
"""Simulate a pipeline stage with random success/failure."""
start = time.time()
time.sleep(r…
How to Mock a GitHub Actions Workflow in Python
Build a dataclass-based model of a GitHub Actions workflow and simulate its execution to validate steps and outputs before deployment.
import json
from dataclasses import dataclass, asdict
from typing import List, Dict, Any
@dataclass
class Step:
name: str
run: str
@dataclass
class Job:
name: str
steps: List[Step]
runs_on: str = "ubuntu-latest"
@dataclass
class Workflow:
name: str
jobs: List[Job]
def to_github_a…
How to simulate a Jenkins pipeline in Python
Simulate a Jenkins-style pipeline in Python by running sequential stages and checking aggregate success.
def run_stage(name, duration, fn):
print(f"[Pipeline] Running stage: {name}")
result = fn()
print(f"[Pipeline] Stage '{name}' completed in {duration}s -> {result}")
return result
def build_project():
print(" compiling source...")
return "BUILD_OK"
def run_tests():
print(" executing unit…
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
- Pick a topic section — strings, lists, files, functions, and more
- Open a sample, read How it works, and copy the code block
- 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.