Skip to content

Instantly share code, notes, and snippets.

View devnull255's full-sized avatar

Michael Mabin devnull255

  • Bloomington, MN
View GitHub Profile
@devnull255
devnull255 / parse_keywords_dec.py
Created November 20, 2023 01:49
Parse_keywords_decorator (py2)
from functools import wraps
def parse_keywords(func):
"""
decorates a python 2 function with the ability to parse list of key=value pair strings into a dict.
Dict is accessible through wrapper.arg_d attribute.
"""
@wraps(func)
def wrapper(*args, **kwargs):
wrapper.arg_d.update(dict([arg.split('=') for arg in args[0] if '=' in arg]))
result = func(*args, **kwargs)
@devnull255
devnull255 / laws
Created January 10, 2021 01:49
Print random line in a file, looping until interrupted
Engineering is done with numbers Analysis without numbers is only an opinion
To design a spacecraft right takes an infinite amount of effort This is why it's a good idea to design them to operate when some things are wrong
Design is an iterative process The necessary number of iterations is one more than the number you have currently done This is true at any point in time
Your best design efforts will inevitably wind up being useless in the final design Learn to live with the disappointment
(Miller's Law) Three points determine a curve
(Mar's Law) Everything is linear if plotted log-log with a fat magic marker
At the start of any design effort, the person who most wants to be team leader is least likely to be capable of it
In nature, the optimum is almost always in the middle somewhere Distrust assertions that the optimum is at an extreme point
Not having all the information you need is never a satisfactory excuse for not starting the analysis
When in doubt, estimate In an emergency, guess But be su
@devnull255
devnull255 / mike.rb
Created August 4, 2020 15:34
Toy rake
#!/usr/bin/env ruby
###########################
# mike is a toy rake
class Task
@@tasks = {}
@action = nil
def initialize(n,&action)
@name = n
if ! @@tasks.include? n
@devnull255
devnull255 / msgcount1.py
Created April 29, 2017 18:22
log extract 1
#!/usr/bin/env python
# Write a script which parses /var/log/messages and generates a CSV with two columns: minute, number_of_messages in sorted time order.
import re
from collections import defaultdict
msg_data = {}
for f in open('test.log'):
rec = f.split()
timestamp = ' '.join(rec[:3])
timestamp = timestamp[:-3]
msg_data[timestamp] += 1
@devnull255
devnull255 / StringCharAt.swift
Created December 8, 2016 18:48
Swift String Extension with charAt(at: Int) method to return a single char at zero-based int position
// In the process of creating my first Swift Application using the book "Cocoa Programming for OSX",
// I grumbled at what seemed like clumsy, string-handling, then hit a roadblock that nearly turned me completely off.
// The string handling code that had worked with Swift 1.2 wouldn't work at all in Swift 3.0. When I figured out
// what did work in the latest version I was even more annoyed at the verbosity of it. I could have just left it alone
// and dismissed it as not worth wasting any more of my time, but my curiosity got the best of me and I started looking at
// the language guide on Apple's Developer site. There I discovered something that totally reversed my thinking: extensions.
// I had also read why string-handling had to be that way (flexibility, multiple Unicode encodings, yada, yada, yada). But
// I learned that I could utilize extensions to add my own methods to the String class that would give me more convient
// string-handling for my own needs. So after much long-windedness, here is
@devnull255
devnull255 / persistent.py
Created March 21, 2016 23:37
Decorate a class to be persistent, utilizing pyyaml as serializer
import yaml
def persistent(K):
def __repr__(self):
i_vars = dict([(k,self.__dict__[k]) for k in self.__dict__ if not k.startswith('__') and not callable(self.__dict__[k])])
s = "<" + K.__name__ + '('
keywords = []
for i_var in i_vars:
keywords.append("%s=%s" % (i_var,repr(i_vars[i_var])))
@devnull255
devnull255 / parse_keywords
Created October 6, 2014 12:57
Return a dictionary from list of key=value strings
def parse_keywords(keywords):
"""
Returns a dictionary for valid key=value strings
"""
d = {}
try:
d = dict([x.split('=',1) for x in keywords])
except ValueError,e:
for kw in keywords: