Skip to content

Instantly share code, notes, and snippets.

View mikofski's full-sized avatar
😎
Solving solar

Mark Mikofski mikofski

😎
Solving solar
View GitHub Profile
import ast
import math
SAFE_FX = {
'exp': math.exp,
}
SAFE_NODES = set(
(ast.Expression,
ast.Num,
import ast
allowed_functions = set([
#math library
'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh',
'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf',
'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod',
'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp',
'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians',
'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc',
@mikofski
mikofski / topological_sort.py
Created August 18, 2013 17:08
topological sorting methods
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
`Topological sort by Kahn (1962) on Wikipedia
<http://en.wikipedia.org/wiki/Topological_sorting>`_
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
@mikofski
mikofski / air.m
Created August 29, 2013 18:26
Thermal-physical properties of air at 1 atm between temperatures of 200 K and 350 K.
classdef air < handle
% AIR Thermal-physical properties of air at 1 atm between temperatures
% of 200 K and 350 K.
% Mark Mikofski
% Version 1-0, 2010-10-29
properties
description = 'air at 1 atm between 200 and 350 K'
end
properties
Temp
@mikofski
mikofski / polyfitZero.m
Last active April 30, 2021 05:15
fit (x,y) to polynomial of degree (degree) forcing y-intercept to zero
function [p,S,mu] = polyfitZero(x,y,degree)
% POLYFITZERO Fit polynomial to data, forcing y-intercept to zero.
% P = POLYFITZERO(X,Y,N) is similar POLYFIT(X,Y,N) except that the
% y-intercept is forced to zero, i.e. P(N) = 0. In the same way as
% POLYFIT, the coefficients, P(1:N-1), fit the data Y best in the least-
% squares sense. You can also use Y = POLYVAL(PZERO,X) to evaluate the
% polynomial because the output is the same as POLYFIT.
%
% [P,S,MU] = POLYFITZERO() Return structure, S, similar to POLYFIT for use
% with POLYVAL to calculate error estimates of predictions with P.
%% initialize workspace
close('all'),clear('all'),clc
rmdir('keys','s') % delete old keys
%% test encryption
PANGRAM = 'The quick brown fox jumped over the lazy dog.';
EncryptionUtil.testJschSECSH(PANGRAM) % run test
%% make SSH keys in workspace
keyFactory = java.security.KeyFactory.getInstance('RSA'); % make a key factory
% read public OpenSSH key
[keyln, totlines] = EncryptionUtil.readJschKeyFile('keys/publicJschSECSH.key',7);
@mikofski
mikofski / OpenSSH2JavaKeyPairDemo.java
Last active December 26, 2015 03:39
Demonstration of SSH -> Java KeyPair Conversion from SO (http://stackoverflow.com/a/19435226/1020470) by @erickson
static KeyPair OpenSSH2JavaKeyPairDemo(InputStream pub, InputStream pvt)
throws IOException, GeneralSecurityException
{
KeyFactory f = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pubspec = decodeRSAPublicSSH(readAllBase64Bytes(pub));
RSAPrivateCrtKeySpec pvtspec = decodeRSAPrivatePKCS1(readAllBase64Bytes(pvt));
return new KeyPair(f.generatePublic(pubspec), f.generatePrivate(pvtspec));
}
@mikofski
mikofski / metapoop.py
Last active October 22, 2022 12:21
Another Python metaclass primer
#! /usr/bin/env python
"""
Python metaclasses
==================
A metaclass is a class factory; metaclasses serve two purposes:
1. replace ``type`` as the base class metatype for classes with the
``__metaclass__`` attribute
@mikofski
mikofski / timeseries.py
Last active February 15, 2022 05:55
time series examples that subclass numpy ndarray
#! /usr/bin/env python
import numpy as np
from datetime import datetime, time, timedelta
import pytz
class Timeseries(object):
def __init__(self, x, t):
self.x = np.array(x)
self.t = np.array(t,dtype='datetime64[s]')