Skip to content

Instantly share code, notes, and snippets.

@pkienzle
pkienzle / vfs.py
Created May 21, 2025 20:56
Open a zip file as a filesystem that works with the usual python functions in os and builtins
"""
Redirect calls to open, etc. to a virtual file system.
Use this to mount a zip file as a file system, and then all subsequent calls
to chdir, open, etc. will reference files in the zip file instead of the disk.
This will only work for packages which do all their I/O in python, and not
those which use direct calls to the C library, so for example, it will not
work with h5py. XML parsing from a zip file is also unlikely to work since
expat uses the C library directly for parsing.
@pkienzle
pkienzle / venv.sh
Last active October 24, 2022 20:20
Very simple python virtual environment management for bash/dash/zsh
# Source this into your ~/.bashrc or ~/.zshrc to add a simple "venv" command for
# creating/activating python environments.
# venv
# Show available environments.
# venv name
# Activates the environment in ~/.venv/name
# venv -n[3.x] name [packages...]
# Creates an environment using python 3.x (default 3.8).
# rm -r ~/.venv/name
@pkienzle
pkienzle / cell_title.py
Last active October 4, 2022 21:52
Markdown title in code cell
## Add this text to the top of your notebook to define '%title' magic.
## Use it to put an H3 title in the cell output, with the title shown
## in the JupyterLab Table-of-Contents sidebar:
## %title Cell title $some_local_variable
from IPython.core.magic import register_line_magic
@register_line_magic
def title(msg):
"""Add a markdown title (level 3) to the current cell output stream."""
from IPython.display import display, Markdown
display(Markdown("### "+msg))
@pkienzle
pkienzle / gtimer.py
Last active September 2, 2022 20:03
Time gpu operations in torch using `with gtimer('blockname'): ... torch code ...`
# This code is in the public domain
# Author: Paul Kienzle
from contextlib import contextmanager
try:
import torch
except ImportError:
pass
_gtimer_block = []
@pkienzle
pkienzle / rev.py
Last active November 15, 2024 15:49
Pure python git commit id and timestamp for current branch
"""
Get commit id from the git repo.
Drop the file rev.py into directory PACKAGE_PATH of your application. From
within that package you can then do::
from . import rev
rev.print_revision() # print the repo version
commit = rev.revision_info() # return commit id
@pkienzle
pkienzle / zotscan.py
Last active July 22, 2025 21:28
scan zotero database for missing attachments
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import glob
import shutil
import sqlite3
from os.path import join as joinpath, expanduser, exists, isabs, realpath
@pkienzle
pkienzle / slit_test.py
Created March 23, 2015 21:20
slit smearing
r"""
Explore different integration schemes for slit smearing.
Introduction
============
Slit smearing evaluates the following integral:
.. math::
@pkienzle
pkienzle / vf.py
Created July 21, 2011 23:37
volume fraction example for refl1d
from refl1d.names import *
D2O = Material('D2O',natural_density=1)
polymer = SLD('polymer',rho=2.4)
solvated_poly = Mixture.byvolume(D2O,polymer,40)
sample = silicon(0,4) | solvated_poly(100,20) | D2O
T = numpy.linspace(0, 5, 100)
probe = NeutronProbe(T=T, dT=0.01, L=4.75, dL=0.0475)