Skip to content

Instantly share code, notes, and snippets.

@TimSC
TimSC / ucs2.py
Last active January 31, 2018 06:51
Encode to ucs-2 by using utf_16_be then checking for invalid results
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import struct
def CheckUcs2BEIsValid(e, big_endian=True):
#UCS-2 is a fixed width encoding. Therefore, check that the
#variable width aspect of UTF-16 is not used.
if big_endian:
struct_code = ">H"
@TimSC
TimSC / minimal_pyside2.py
Last active October 9, 2020 11:16
Minimal pyside2 application
# The code is placed into public domain by anatoly techtonik
# Feel free to copy/paste wherever you like
# Absolutely minimal example of PySide2 application with window
from PySide2 import QtGui, QtWidgets
# Get entrypoint through which we control underlying Qt framework
app = QtWidgets.QApplication([])
@TimSC
TimSC / parseshp.py
Created December 13, 2017 23:41
Write shapefile to WKT text
import os
import shapefile
from shapely.geometry import Polygon
if __name__ == "__main__":
sf = shapefile.Reader("TM_WORLD_BORDERS-0.3.shp")
assert len(sf.shapes()) == len(sf.records())
fieldIndex = {}
@TimSC
TimSC / interpolategpx.py
Created November 22, 2017 01:55
Interpolate missing positions in gpx
import xml.etree.ElementTree as ET
import dateutil.parser
from datetime import timedelta
class Interpolate(object):
def __init__(self, knownWpts):
self.knownWpts = knownWpts[:]
self.knownWpts.sort()
#for wpt in self.knownWpts:
@TimSC
TimSC / pysync.py
Created November 20, 2017 15:59
Invoke rsync using python/cron
#!/usr/bin/env python
import subprocess
import fcntl
import os
if __name__=="__main__":
lockFina = "docslock"
fullLockFina = os.path.join(os.path.dirname(os.path.realpath(__file__)), lockFina)
@TimSC
TimSC / getchangesets.py
Created October 18, 2017 20:49
Download a range of changesets from an OSM map server
import urllib2
import xml.etree.ElementTree as ET
import sys
import time
if __name__=="__main__":
top = ET.Element('osm')
i = 1000000001
running = True
@TimSC
TimSC / dumpmumps.m
Last active September 29, 2017 09:57
Trying to dump Mumps data
dumpusers; Dump users functionality
xmlEscapeApostrophe(string) ; Private ; Escape apostrophe
;
n out,x,c
;
s out=""
f x=1:1:$l(string) d
. s c=$e(string,x)
. i "'"[c s out=out_"'" q
@TimSC
TimSC / moose.py
Created September 9, 2017 18:24
Affine warp to straighen moose
from skimage.transform import PiecewiseAffineTransform, warp
from skimage.io import imread, imshow, imsave
import numpy as np
if __name__=="__main__":
img = imread("IMG_3785.JPG")
pw = PiecewiseAffineTransform()
@TimSC
TimSC / dkkchart.py
Created September 4, 2017 19:01
DGK Color Tools DKK chart
#In LAB colour space, DGK Color Tools DKK chart
img = np.zeros((18, 1, 3))
img[0,0,:] = [100, 0, 0] #White
img[1,0,:] = [73, 0, 0] #Grey 4
img[2,0,:] = [62, 0, 0] #Grey 3
img[3,0,:] = [50, 0, 0] #Grey 2
img[4,0,:] = [38, 0, 0] #Grey 1
img[5,0,:] = [0, 0, 0] #Black
img[6,0,:] = [52, 74, 54] #Red
@TimSC
TimSC / streambuf.i
Created September 1, 2017 06:43
Using std::streambuf from Python SWIG
%inline %{
//Based on http://swig.10945.n7.nabble.com/Using-std-istream-from-Python-tp3733p3735.html
//Add this to your SWIG interface file
class CPyOutbuf : public std::streambuf
{
public:
CPyOutbuf(PyObject* obj) {
m_PyObj = obj;
Py_INCREF(m_PyObj);
m_Write = PyObject_GetAttrString(m_PyObj, "write");