Skip to content

Instantly share code, notes, and snippets.

@alaniwi
alaniwi / networks.pl
Created April 8, 2019 08:20
network range calculator
#!/usr/bin/perl
$ip = $ARGV[0] or $ip=<STDIN>;
chomp($ip);
@ip=split(/\./,$ip);
if ($#ip != 3 or scalar grep {/[^0-9]/ or $_ > 255} @ip) {
die "$ip not in IPV4 format\n";
}
@alaniwi
alaniwi / mylib_logging.py
Last active April 16, 2019 17:46
simple logging wrapper module
import logging
"""
Logging wrapper which allows a library ("mylib") to use python logging while giving
some control to the calling code over which logger it should use.
Code within the library should call mylib_logging.get_logger where it would
otherwise call logging.getLogger. Calling code can call mylib_logging.set_logger
before using the library to tell it which logger to use, but if this is not
done, then it will fall back to using logging.getLogger.
import time
import random
try:
import builtins as bi
except ImportError:
import __builtin__ as bi
bi.__import2__ = bi.__import__
@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:
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 / 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'
@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 / 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 / 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
#!/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