Skip to content

Instantly share code, notes, and snippets.

View markrwilliams's full-sized avatar

Mark Williams markrwilliams

View GitHub Profile
IN, OUT = object(), object()
def parse(raw):
state = OUT
parsed = []
count = 0
for c in raw:
if c == '(':
if state is OUT:
(defun python-swap-quotes ()
(interactive)
(save-excursion
(let ((state (syntax-ppss)))
(when (eq 'string (syntax-ppss-context state))
(let* ((left (nth 8 state))
(right (1- (scan-sexps left 1)))
(newquote (if (= ?' (char-after left))
?\" ?')))
(dolist (loc (list left right))
import sys
import opcode
import struct
OPNAME = struct.Struct('B')
ARG = struct.Struct('H')
class Symbol(object):
import re
URL = re.compile('^((?P<scheme>[^:/?#]+):)?'
'(//(?P<authority>[^/?#]*))?'
'(?P<path>[^?#]*)'
'(\?(?P<query>[^#]*))?'
'(#(?P<fragment>.*))?')
print URL.match('http://fake.com:8080/search?q=123&business=Nothing%20Special').groupdict()
import re
urldecoder = re.compile('%([0-9a-fA-F]{2})')
def urldecode(s):
bs = bytearray(s)
offset = 0
for encoded in urldecoder.finditer(s):
start, end = encoded.span()
bs[start + offset:end + offset] = chr(int(encoded.group(1), 16))
import lxml.etree
from zipfile import ZipFile
class DocX(object):
DOCPATH = 'word/document.xml'
def __init__(self, fn):
with ZipFile(fn).open(self.DOCPATH) as f:
self.doc = lxml.etree.parse(f)
# setup.py
from setuptools import setup
setup(name='lxml',
version='3.2',
py_modules=['lxml'])
# lxml.py
print 'FOOLED YOU'
def merge(classes_list):
merged = [None]
while classes_list:
for classes in classes_list:
head = classes[0]
tails = [c[1:] for c in classes_list]
if not any(head in tail for tail in tails):
if head != merged[-1]:
merged.append(head)
for classes in classes_list:
from __future__ import division
import random
from itertools import tee, izip_longest
from collections import defaultdict
class Occurrences(object):
import re
from collections import namedtuple
ParseResult = namedtuple('ParseResult', 'captured remaining matched')
class Rule(object):
def __init__(self, value, bind=None):
self.value = value
self.bind = bind