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
@mikofski
mikofski / django_service.py
Last active May 31, 2022 02:05
Start a Windows service to run the Django debug server - WARNING! not recommended for deployment!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Attribution: Hijacked from tracservice.py by Florent Xicluna <[email protected]>
# http://trac-hacks.org/wiki/WindowsServiceScript
#
# To use this class, users must do the following:
# 1. Download and install the PyWin32all package
# (http://starship.python.net/crew/mhammond/win32/)
# 2. Edit the constants section with the proper information.
@mikofski
mikofski / vcvarsall.bat.patch
Last active April 9, 2017 20:40
Fix for vcvarsall.bat to build Python-2.7 extensions using v90 instead of sdk7
--- C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.txt
+++ C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat
@@ -16,23 +16,23 @@
goto :eof
:amd64
-if not exist "%~dp0bin\amd64\vcvarsamd64.bat" goto missing
-call "%~dp0bin\amd64\vcvarsamd64.bat"
+if not exist "%~dp0bin\vcvars64.bat" goto missing
+call "%~dp0bin\vcvars64.bat"
@mikofski
mikofski / JSONFormatter.py
Created January 11, 2014 05:17
pygments JSONFormatter
from pygments.lexers import MatlabLexer
from pygments.formatter import Formatter
from pygments.token import STANDARD_TYPES
from pygments import highlight
class JSONFormatter(Formatter):
def format(self, tokensource, outfile):
tokensource = [{STANDARD_TYPES[k]: v} for k, v in tokensource]
json.dump(tokensource, outfile)
@mikofski
mikofski / help_server.py
Created January 10, 2014 00:37
simple non-blocking asynchronous web server to serve help docs since sphinx search doesn't work on google chrome otherwise
# -*- coding: utf-8 -*-
"""
Starts the help server so that Sphinx search works even in Google Chrome.
"""
import SimpleHTTPServer
import SocketServer
from threading import Thread
from Queue import Queue
@mikofski
mikofski / solarPosition.m
Last active March 20, 2025 02:40
solar position calculator
function [angles,projection] = solarPosition(datetime,latitude,longitude,time_zone,rotation,dst)
%SOLARPOSITION Calculate solar position using most basic algorithm
% This is the most basic algorithm. It is documented in Seinfeld &
% Pandis, Duffie & Beckman and Wikipedia.
%
% [ANGLES,PROJECTION] = SOLARPOSITION(DATE,TIME,LATITUDE,LONGITUDE,TIME_ZONE)
% returns ZENITH & AZIMUTH for all DATE & TIME pairs at LATITUDE, LONGITUDE.
% ANGLES = [ZENITH,AZIMUTH] and PROJECTION = [PHI_X, PHI_Y]
% PHI_X is projection on x-z plane & PHI_Y is projection on y-z plane.
% DATETIME can be string, vector [YEAR, MONTH, DAY, HOURS, MINUTES, SECONDS],
@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]')
@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 / 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));
}
%% 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 / 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.