Skip to content

Instantly share code, notes, and snippets.

@mbrenig
mbrenig / utftester.py
Created May 6, 2013 17:41
Unicode character detection across a directory.
import chardet # https://pypi.python.org/pypi/chardet
import os
import sys
import fnmatch
def detect(arg, dir, names):
for name in names:
if fnmatch.fnmatch(name, arg):
fpath = os.path.join(dir, name)
r = chardet.detect(fpath)
@mbrenig
mbrenig / gist:5664858
Last active December 17, 2015 19:59 — forked from anonymous/gist:5664828
#-------------------------------------------------------------------------
# Set up access mappings here.
#-------------------------------------------------------------------------
line_updated = False
if device_cutsheet_manager != None:
# Try the device cutsheet manager first.
line_updated = device_cutsheet_manager.set_access_info(lineinfo)
@mbrenig
mbrenig / gist:5715678
Created June 5, 2013 17:32
Euclid's greatest common divisor algorithm
def gcd(a,b):
while a != 0:
a, b = b % a, a
return b
@mbrenig
mbrenig / gist:5715776
Last active December 18, 2015 03:08
Generate list continued fractions for square root of number
from math import sqrt
def gcd(a,b):
while a != 0:
a, b = b % a, a
return b
def sqrt_cf(D):
A = []
@mbrenig
mbrenig / connect4.py
Last active December 20, 2015 13:18
connect4.py - play connect 4 in the terminal.
WIDTH = 7
HEIGHT = 6
NULL = " "
def connected_4(l):
# Tests whether or not the list, l, has
# four similar characters in a row.
last = None
run = 1
for item in l:
@mbrenig
mbrenig / markov.py
Last active January 19, 2018 22:35
Markov chain text replicator. Usage: markov.py sourcetext.txt 4 (the final value is the number of characters in a node of the chain)
import random
import sys
class Segment:
def __init__(self, listobj, length=1):
self.sequence = iter(listobj)
self.memory = []
self.length = length
self.start = True
self.end = False
@mbrenig
mbrenig / weirdcheck.py
Last active December 22, 2015 07:48
Check for uncommon characters in a specified input file. Usage: weirdcheck.py <inputfilename.txt>
import sys
print "Reading from: %s" % sys.argv[1]
print "----------------------------------"
inf = open(sys.argv[1],'r')
for (line_no, l) in enumerate(inf.readlines()):
for (pos_no, c) in enumerate(l):
if ord(c) > 128:
@mbrenig
mbrenig / taqua_mlhg_snippet.py
Created September 11, 2013 18:14
Snippet to force MLHG_HUNT_ON_NA to FALSE on all Taqua BGs. (Not recommended - please SFR).
def cust_specific_process():
for bgline in taqua_glob.bg_line_entries_list:
# Try to get mlhg lineinfo objects
mlhgs = []
try:
bg = bgline.parent_bg
mlhgs = bg.lineinfo[si.BG_MLHGS]
except:
pass
@mbrenig
mbrenig / snake.py
Last active December 23, 2015 06:49
Runs a simulated game of 3D snakes on a cube, and print frames to a snake.txt that can be copied into code that runs this: http://www.instructables.com/id/The-4x4x4-LED-cube-Arduino/
# Not much fun unless you have one of these:
# http://www.instructables.com/id/The-4x4x4-LED-cube-Arduino/
#
import random
DEBUG = True # Visualize the movement. Hit/hold return to progress the snake
CUBESIZE = 4
ouf = open("snake.txt",'w')
@mbrenig
mbrenig / dateutil.py
Last active May 25, 2016 12:02
Python libraries demo
# https://pypi.python.org/pypi/python-dateutil/2.5.3
# https://dateutil.readthedocs.io/en/stable/
from dateutil import parser # pip install python-dateutil
if __name__ == '__main__':
try:
a = parser.parse("18 Aug 2001")
print a
b = parser.parse("2016-04-05 12:33:55")
print b