This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IN, OUT = object(), object() | |
def parse(raw): | |
state = OUT | |
parsed = [] | |
count = 0 | |
for c in raw: | |
if c == '(': | |
if state is OUT: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import opcode | |
import struct | |
OPNAME = struct.Struct('B') | |
ARG = struct.Struct('H') | |
class Symbol(object): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# setup.py | |
from setuptools import setup | |
setup(name='lxml', | |
version='3.2', | |
py_modules=['lxml']) | |
# lxml.py | |
print 'FOOLED YOU' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import random | |
from itertools import tee, izip_longest | |
from collections import defaultdict | |
class Occurrences(object): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer