Skip to content

Instantly share code, notes, and snippets.

View joedougherty's full-sized avatar

Joe Dougherty joedougherty

View GitHub Profile
@joedougherty
joedougherty / walkjson.py
Last active May 12, 2016 15:00
walkjson
import json
def walkjson(json):
tree = []
if isinstance(json, dict):
for key in json.keys():
value = json[key]
tree.append(key)
tree.append(walkjson(value))
@joedougherty
joedougherty / model_rides.py
Last active August 25, 2016 15:06
How do different river sprints affect overall avg mph?
def avg_speed(distance_in_miles, time_in_minutes):
time_in_hrs = time_in_minutes/60.0
return float(distance_in_miles)/float(time_in_hrs)
def river_sprint(avg_mph, river_distance=4.5):
"""
Return the time it would take (in minutes) to
sprint the river at speed given by avg_mph.
"""
return (float(river_distance)/float(avg_mph))*60
@joedougherty
joedougherty / autodetect.py
Last active November 7, 2016 15:55
autodetect.py
#!/usr/bin/env python
import serial
import serial.tools.list_ports as list_ports
def autodetect():
disconnected = True
while disconnected:
ports = list(list_ports.comports())
# Identify correct port ... somehow
import logging
def create_logger(self):
# See https://wingware.com/psupport/python-manual/2.3/lib/node304.html
logger = logging.getLogger(self.name)
hdlr = logging.FileHandler(self.logfile)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)
""" Generator-based versions of tokenize and read_from_tokens from Norving's "How to write a Lisp Interpreter in Python" """
from itertools import takewhile
def tokenize(chars):
''' Convert a string expression into a generator of tokens. '''
return (t for t in chars.replace('(', ' ( ').replace(')', ' ) ').split())
def read_from_tokens_gen(tokens, current_token=None):
@joedougherty
joedougherty / for_parsimonious.py
Last active July 11, 2018 04:00
Parse sentential logic w/Parsimonious
# -*- coding: utf-8 -*-
from parsimonious.grammar import Grammar
try:
from string import lowercase
except:
from string import ascii_lowercase as lowercase
def vars_expr():
def find_matching_node(expr, matching_fn, parent_node=None, parent_rel=None):
if isinstance(expr, Expression) and matching_fn(expr):
return (expr, parent_node, parent_rel)
if isinstance(expr, Term):
return
left_match = find_matching_node(expr.left, parent_node=expr, parent_rel='left')
right_match = find_matching_node(expr.right, parent_node=expr, parent_rel='right')
if left_match:
from copy import deepcopy
class ClausePair:
def __init__(self, c1_set, c2_set):
self.c1, self.c2 = frozenset(c1_set), frozenset(c2_set)
def __eq__(self, other):
if isinstance(other, self.__class__):
return ((self.c1 == other.c1 and self.c2 == other.c2) or
@joedougherty
joedougherty / goForth_intro.txt
Last active September 30, 2017 21:38
goForth in 10 minutes
TIILE: gForth Overiew
ORIG DATE: 20170930
LAST REV DATE: 20170930
AUTHOR: mj <modusjonens@gmail.com>
==========================================================
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
==========================================================
@joedougherty
joedougherty / forth_ideas.cr
Created February 27, 2018 22:08
forth ideas (in crystal)
class Stack
def initialize
@contents = [] of Int32
end
def pop
@contents.pop()
end
def push(value)