Skip to content

Instantly share code, notes, and snippets.

View mumbleskates's full-sized avatar
🤔

Kent Ross mumbleskates

🤔
  • 🌵
View GitHub Profile
# coding=utf-8
from collections import Counter, defaultdict
from functools import reduce
from itertools import tee
import re
LOW_PROBABILITY = 1.0 / (1 << 20)
@mumbleskates
mumbleskates / graphs.py
Created March 8, 2016 22:59
Pythonic Dijkstra path finding
# coding=utf-8
from collections import OrderedDict, namedtuple
from functools import total_ordering
from heapq import heappush, heappop
from itertools import count, zip_longest
INITIAL_START = object()
def minimum(stack):
gotten = []
try:
while True:
gotten.append(stack.pop())
except ValueError:
pass
if not gotten:
return 0
intervaltree
* storing timelines or other intervals
* sorting will not work, you have to fully search
* introducing interval trees, which allow O(log n) access and modification
* interval trees of interval trees for multi-dimensional data... most basic spatial data stuff
* intervaltree on pypi
fancy pythonic dijkstra
* finding shortest paths in a graph
* using heaps and making backpathing trees with tuples
@mumbleskates
mumbleskates / parens.py
Last active March 13, 2016 04:18
paren-matching checker
# coding=utf-8
from __future__ import unicode_literals
def check_parens(string, pairs="()"):
"""
Check a string for non-matching braces or other character pairs and return
a list of failures, or an empty set if everything is OK.
`if check_parens(string, brackets):` is a good way to find bad brackets in
@mumbleskates
mumbleskates / weelogfix.py
Last active March 21, 2016 05:06
Weechat logging file path unifier
# coding=utf-8
from datetime import datetime
from functools import partial
from itertools import chain, groupby
from operator import itemgetter
import os
import re
import sys
@mumbleskates
mumbleskates / datething.py
Last active April 7, 2016 19:08
Find the closest date to another date from some inputs
# coding=utf-8
from __future__ import unicode_literals
from datetime import datetime
DATE_FORMAT = "%m/%d"
def convert_date(date_str):
# coding=utf-8
import re
def simple_compress(s):
c = ''.join((part[1] + (str(len(part[0])) if len(part[0]) > 1 else '')) for part in re.findall(r'((.)\2*)', s))
return c if len(c) < len(s) else s
# coding=utf-8
from itertools import product
adjacents = ('08', '124', '1253', '236', '1457', '24568', '3569', '478', '57890', '689')
adjacents = {str(i): s for i, s in enumerate(adjacents)}
def get_pins_itertools(observed):
for pins in product(*(adjacents[ch] for ch in observed)):
@mumbleskates
mumbleskates / pybullet.py
Last active April 25, 2016 21:11
Python extension plugin for weechat that pushes smart notifications to PushBullet. Notifications are grouped per channel, have increasing delays, preview the first few lines, go away when you have been active in the channel, and detect when notifications have been dismissed remotely.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
try:
# noinspection PyUnresolvedReferences,PyShadowingBuiltins
str = unicode
except NameError:
# noinspection PyShadowingBuiltins,PyUnboundLocalVariable
str = str