Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python3
"""
Display various file statistics for each directory.
"""
import sys
import os
import stat
from time import strftime, localtime
#!/usr/bin/env python3
"""
Looks for files (or directories) in a given directory, whose filenames
match a specified strptime format.
For those that are more than a specified number of days old, deletes certain
files, such that the interval between the ones that are retained does not
exceed the maximum specified interval.
@alaniwi
alaniwi / check_index_node.py
Created January 20, 2023 17:18
Icinga plugin to test that certain known facet values exist when doing an ESGF search.
#!/usr/bin/env python3
"""Icinga plugin to test that certain known facet values exist when doing an
ESGF search.
The most important check is on the values for index_node. Because of the way
that the value for index_node is set by the publisher (it matches the index
hostname that it is publishing to), the set of facet values for index_node
can be used to test whether all of the Solr shards are responding properly.
#!/usr/bin/env python
help = ("Compresses a netCDF file by compressing time slices and "
"concatenating them. Requires NCO utilities.")
import os
import sys
import subprocess
import argparse
import shutil
@alaniwi
alaniwi / iterator.py
Created July 31, 2020 14:55
iterator with `at_end` property
class MyIter:
def __init__(self, iterable):
self._iterator = iter(iterable)
self.at_end = False
self._prefetch_next()
def __next__(self):
if self.at_end:
raise StopIteration
@alaniwi
alaniwi / pretty.py
Last active July 31, 2020 14:50
Commented pretty-printer
def commented_pprint(obj, name='obj', indent=4):
"""
Pretty-print object obj, which can contain nested dictionaries, lists and tuples,
with comment lines showing how to index it.
"""
pairs = list(_yield_pprint_lines(obj, name=name, indent=indent))
max_len = max(len(line) for line, _ in pairs)
for line, comment in pairs:
print(f'{line:{max_len}} {comment}')
@alaniwi
alaniwi / conf_ints.py
Created July 27, 2020 14:09
Confidence intervals of the binomial distribution
import numpy as np
import scipy.stats
from scipy.interpolate import interp1d
#import matplotlib as mpl
from matplotlib import pyplot as plt
def cints(n, levels):
ci_out = np.zeros((len(levels), 1 + n))
@alaniwi
alaniwi / example.sh
Last active June 30, 2023 14:08
Script to run commands in different "panes" of a terminal window
# example usage:
#
# this runs the following commands simultaneously, each in different areas of the screen:
# 1) echo hello
# 2) cal
# 3) for i in 1 2 3 4 5 ; do date; sleep 1; done
#
# it waits for 2 seconds after they have all finished, before clearing the screen
#
./run_in_panes.py -s 2 'echo hello' 'cal' 'for i in 1 2 3 4 5 ; do date; sleep 1; done'
def explore(l, c, t): # l = 2d list of connections; c = current node; t = target node
if c == t: # base case
return [[c]]
else: # recursive case
conns = []
if c in i:
conns.extend([[c] + conn
for conn in explore([k for k in l if i != k],
(i[0] if c == i[1] else i[1]),
t)
@alaniwi
alaniwi / gist:a607e46f0fa5044f2c5fef532c205b0c
Created June 15, 2020 13:45
modified ZipFile.write method
def write(self, filename, arcname=None, compress_type=None,
fp=None, mtime=None, size=None, isdir=None,
mode=None):
"""Put the bytes from filename into the archive under the name
arcname."""
if not self.fp:
raise RuntimeError(
"Attempt to write to ZIP archive that was already closed")
if isdir: