This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.14 | |
""" | |
Demonstration of Python 3.14's new t-string feature for secure command execution. | |
This script shows how t-strings help prevent command injection attacks. | |
""" | |
import subprocess | |
import os | |
import sys | |
from string.templatelib import Template, Interpolation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.14 | |
# -*- coding: utf-8 -*- | |
""" | |
A demonstration of PEP 750's t-strings for safer command execution. | |
This script defines a `run_command` function that utilizes a custom | |
t-string tag to safely execute shell commands using the `subprocess` module. | |
It then contrasts this safe approach with a vulnerable command execution | |
using `os.system` and regular f-strings to highlight how t-strings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
# ✅ SAFE: run a command using t-string and subprocess.run | |
def run_command(cmd: t-str): | |
"""Run a shell command safely using t-strings and subprocess.run.""" | |
print(f"Running securely: {cmd}") | |
result = subprocess.run(cmd, capture_output=True, text=True) | |
print("Output:", result.stdout.strip()) | |
if result.stderr: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from pathlib import Path | |
import pytest | |
from mockito import when, mock | |
from django.urls import reverse | |
from django.shortcuts import get_object_or_404 | |
from ninja.testing import TestClient |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from django.contrib.auth.models import User | |
from ninja.testing import TestClient | |
from .models import Product, CartItem, Cart | |
from .endpoints import api | |
@pytest.fixture | |
def user(db): # db fixture assumed from pytest-django | |
user = User.objects.create_user(username='testuser', password='testpass') | |
Cart.objects.create(user=user) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_bluesky_link_calculator(): | |
assert ( | |
bsk_calculate_link_size("http://google.com") | |
== bsk_calculate_link_size("https://google.com") | |
== bsk_calculate_link_size("https://google.com/") | |
== bsk_calculate_link_size("https://google.com#") | |
== 10 | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the HTML Video element | |
const bgVideo = document.getElementById('bgVideo'); | |
// The canvas element on which we display the animation | |
const canvas = document.getElementById('spaceCanvas'); | |
const ctx = canvas.getContext('2d'); | |
canvas.width = window.innerWidth; | |
canvas.height = window.innerHeight; | |
// I create a second canvas for performance reasons (explained later) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import queue | |
import re | |
import time | |
import mido | |
import requests | |
# I don't even remember why it's there. Permission issue I worked | |
# around? Maybe I replaced that with an udev rules? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import tempfile | |
import heapq | |
from array import array | |
def int_array_from_file(int_file, buffer_size=4000): | |
int_array = array("i") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
py311.json | |
========== | |
Performance version: 1.0.9 | |
Report on Linux-5.15.0-84-generic-x86_64-with-glibc2.31 | |
Number of logical CPUs: 8 | |
Start date: 2023-10-03 15:46:40.037527 | |
End date: 2023-10-03 16:30:51.189955 |
NewerOlder