Skip to content

Instantly share code, notes, and snippets.

View Arachnid's full-sized avatar

Nick Johnson Arachnid

View GitHub Profile
import heapq
class EventReactor(object):
"""Processes a set of EventStream and executes their events in order."""
def __init__(self):
self.time = 0.0 # Current time
self.events = [] # Holds (time, event function, stream) tuples in a heapq
def add_stream(self, stream):
"""Adds an event stream to the reactor.
def prefetch_refprops(entities, *props):
fields = [(entity, prop) for entity in entities for prop in props]
ref_keys = set(prop.get_value_for_datastore(x) for x, prop in fields)
ref_keys.discard(None)
ref_entities = dict((x.key(), x) for x in db.get(ref_keys))
for (entity, prop), ref_key in zip(fields, ref_keys):
prop.__set__(entity, ref_entities[ref_key])
return entities
@Arachnid
Arachnid / gist:1148600
Created August 16, 2011 07:17
sscanf example for decoding colors
char data[10];
for(int i = 0; i < 9; i++)
data[i] = Serial.read();
data[9] = '\0';
int r, g, b;
sscanf(data, "%3x%3x%3x", &r, &g, &b);
// r, g and b now contain the color values
// Flags for the direction register
#define FLAG_M1_CCW 0
#define FLAG_M1_CW 1
#define FLAG_M2_CCW 2
#define FLAG_M2_CW 3
// Flags for the inopts register
#define FLAG_PULLUP_I1 0
#define FLAG_PULLUP_I2 1
#define FLAG_INVERT_I1 2
@Arachnid
Arachnid / gist:1064314
Created July 5, 2011 05:58 — forked from MrTrick/gist:1064300
17x17 max solution
+V----------------+
| # # # <
| # # # # # ######|
| # # # # # |
| # # # # ########|
| # # # # # |
| # # # # # ######|
| # # # |
|######## ####### |
| |
<ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
<ns0:Document>
<ns0:Style id="BasicStyle">
<ns0:BalloonStyle>
<ns0:text>$[description]</ns0:text>
</ns0:BalloonStyle>
<ns0:IconStyle>
<ns0:color>FFFFFFFF</ns0:color>
<ns0:scale>1.1</ns0:scale>
<ns0:hotSpot x="0.5" xunits="fraction" y="0" yunits="fraction" /><ns0:Icon>
application: zip-site
version: main
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
import bisect
class NFA(object):
EPSILON = object()
ANY = object()
def __init__(self, start_state):
self.transitions = {}
self.final_states = set()
self._start_state = start_state
def batch_get_lists(lists):
key_set = set()
for l in lists:
key_set.update(l)
all_keys = list(key_set)
all_entities = db.get(all_keys)
entity_dict = dict(zip(all_keys, all_entities))
return [[entity_dict[x] for x in l] for l in lists]
def find_room_plans(plan, depth):
if depth == 0:
return [plan]
prev = plan[-1]
heads = [x[0] for x in prev]
ret = []
perms = []
unique_permutations(heads, perms)
for perm in perms:
if any(x[0] == y[0] for x,y in zip(heads, perm)):