Skip to content

Instantly share code, notes, and snippets.

View tclarke's full-sized avatar

Trevor R.H. Clarke tclarke

View GitHub Profile
@tclarke
tclarke / perf_time.py
Created March 12, 2022 00:11
Simple performance timer context manager
from time import perf_counter
from datetime import timedelta
class PerfTimer:
def __init__(self, name="Timer"):
""" Create a performance timer context manager.
These output the start and end of a block of code and the time it took to execute.
They can be nested for hierarchical timing.
@param name The name to output in the status messages
@tclarke
tclarke / resistor_ratio_match.py
Last active February 1, 2021 20:47
Find E96 resistor pairs which match a specific ration.
#!/usr/bin/env python3
import numpy as np
##
# Define different resistor series. You almost never see e3 and e6 but they are here for completeness.
# E96 are widely available and generally inexpensive so that's the default series.
# 40% tolerance
e3 = np.array([1. , 2.2, 4.7])

Keybase proof

I hereby claim:

  • I am tclarke on github.
  • I am retrev (https://keybase.io/retrev) on keybase.
  • I have a public key ASATQZGBLX_lq8tVGEiLqRiQYNYsEcBLuyEsuG6MFK5JGwo

To claim this, I am signing this object:

@tclarke
tclarke / env_override.py
Created September 27, 2016 12:09
Django settings overrides from env variables
for k, v in os.environ.iteritems():
if k.startswith("MYAPP_"):
try:
locals()[k[6:]] = type(locals()[k[6:]])(v)
except KeyError:
locals()[k[6:]] = v
@tclarke
tclarke / maze.py
Created February 16, 2014 20:50
Cellular Automata 2D Maze Generator
import numpy as np
import cv2
# cellular automata class based on code from http://www.loria.fr/~rougier/teaching/numpy/scripts/game-of-life-numpy.py
class CA(object):
def __init__(self, s, b, shape, init_fill=0.1):
assert(type(s) == type([]) and type(b) == type([]))
self.s,self.b = s,b
self.z = (np.random.random(shape) < init_fill).astype("uint8")
@tclarke
tclarke / gist:7490840
Created November 15, 2013 20:13
errors building pdal
[ 54%] Building CXX object src/CMakeFiles/pdalcpp.dir/UserCallback.cpp.o
[ 54%] Building CXX object src/CMakeFiles/pdalcpp.dir/Utils.cpp.o
[ 55%] Building CXX object src/CMakeFiles/pdalcpp.dir/Vector.cpp.o
[ 55%] Building CXX object src/CMakeFiles/pdalcpp.dir/Writer.cpp.o
[ 55%] Building C object src/CMakeFiles/pdalcpp.dir/third/md5-pdal.c.o
[ 56%] Building CXX object src/CMakeFiles/pdalcpp.dir/XMLSchema.cpp.o
[ 56%] Building CXX object src/CMakeFiles/pdalcpp.dir/drivers/buffer/Reader.cpp.o
[ 57%] Building CXX object src/CMakeFiles/pdalcpp.dir/drivers/faux/Reader.cpp.o
[ 57%] Building CXX object src/CMakeFiles/pdalcpp.dir/drivers/faux/Writer.cpp.o
[ 57%] Building CXX object src/CMakeFiles/pdalcpp.dir/drivers/las/GeotiffSupport.cpp.o
@tclarke
tclarke / gist:6458240
Created September 6, 2013 00:58
A fun little example of Benford's Law
import numpy
def fib(a,b):
"Generator for the fibonacci sequence starting at position a,b"
while True:
yield b
a,b=b,a+b
def ld(v):
"Determine the left-most digit in v"
@tclarke
tclarke / gist:5247416
Last active May 14, 2019 23:13
A sample ZMQ 3.x Python pub/sub example.
import zmq
def publisher():
c=zmq.Context()
s=c.socket(zmq.PUB)
s.bind('tcp://127.0.0.1:1234')
s.send("foobie")
def subscriber():
c=zmq.Context()
@tclarke
tclarke / gist:4987644
Last active December 13, 2015 22:49
Flip an Opticks RasterElement
DataAccessor acc = pRaster->getDataAccessor();
DataAccessor outacc = pOutRaster->getDataAccessor(writableDataRequest.release());
/** code to flip horizontally **/
for (int row = 0; row < numrows; ++row) {
VERIFY(acc.valid() && outacc.valid());
memcpy(outacc->getRow(), acc->getRow(), rowSizeInBytes);
// note: pixeldatatype is the C++ type for BSQ..for BIP, you'll need to use a struct..somthing like this for 10 bands 2-byte data
// struct {uint16_t data[10];}
// or just use a second loop with appropriate memcpy's
std::reverse((pixeldatatype*)outacc->getRow(), ((pixeldatatype*)outacc->getRow()) + pixelsPerRow);