Skip to content

Instantly share code, notes, and snippets.

@royratcliffe
Last active February 13, 2026 18:16
Show Gist options
  • Select an option

  • Save royratcliffe/cbb98a962e0cbffe0804f79a9c3395c5 to your computer and use it in GitHub Desktop.

Select an option

Save royratcliffe/cbb98a962e0cbffe0804f79a9c3395c5 to your computer and use it in GitHub Desktop.
Python
# Ignore Python virtual environment directories.
/.venv/
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:2-3.14-trixie",
// Docker run arguments. Add more if you want to, for example to add more
// capabilities or set environment variables. More info:
// https://aka.ms/vscode-remote/containers/docker-run-args.
"runArgs": [
// Add --dns to fix "Temporary failure in name resolution" error. It
// adds the DNS server of the host machine to the container, which
// allows it to resolve domain names correctly. You can find the DNS
// server of the host machine by running "cat /etc/resolv.conf" on the
// host.
"--dns", "192.168.19.254"
]
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
if IS_WINDOWS:
import os
os.environ['PATH'] += ';' + os.path.dirname(__file__)
# Run a Python script in a virtual environment as superuser.
sudo -E env PATH=$PATH python script.py
venv:
python -m venv .venv
# Determine the correct path to the Python executable in the virtual environment
# based on the operating system. On Windows, the Python executable is located in
# the "Scripts" directory, while on Unix-like systems, it is located in the
# "bin" directory.
ifeq ($(OS),Windows_NT)
PYTHON = .venv/Scripts/python
else
PYTHON = .venv/bin/python
endif
upgrade-pip:
$(PYTHON) -m pip install --upgrade pip
requirements: venv upgrade-pip
$(PYTHON) -m pip install -r requirements.txt
# Utility target to print the value of a make variable.
# Usage: make print-VARIABLE_NAME
# Example: make print-PATH
.PHONY: print-%
print-%:
@echo $($*)
# This is a scratch file for testing code snippets. It is not meant to be run as a script.
import re
# Read the contents of /proc/mounts and split it into lines, then split each line into fields.
# The result is a list of lists, where each inner list represents a mount point and its details.
mounts = [
re.split(r"\s+", line)
for line in filter(
lambda line: line != "", re.split(r"\n", open("/proc/mounts", "r").read())
)
]
# Now we have a list of mounts, we can filter it to find the one that matches our desired mount point.
list(filter(lambda mount: mount[1] == "/media/usb/sda1", mounts))
# Alternatively, combine the filtering and splitting into a single step
# to avoid creating an intermediate list of all mounts. This will directly
# filter the mounts as we read them, which can be more efficient if there are
# many mounts.
list(
filter(
lambda mount: mount[1] == "/media/usb/sda1",
[
re.split(r"\s+", line)
for line in filter(
lambda line: line != "",
re.split(r"\n", open("/proc/mounts", "r").read()),
)
],
)
)
{
"debugpy.debugJustMyCode": false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment