Skip to content

Instantly share code, notes, and snippets.

View markrwilliams's full-sized avatar

Mark Williams markrwilliams

View GitHub Profile
from twisted.internet import reactor, defer, protocol, task
from twisted.internet.ssl import ClientContextFactory
from twisted.words.protocols import irc
class NoPingBotProtocol(irc.IRCClient):
def __init__(self, *args, **kwargs):
self.nickname = 'noping'
self.password = 'balloonatics'
>>> A.mro()
[<class '__main__.A'>, <type 'object'>]
>>> B.mro()
[<class '__main__.B'>, <type 'object'>]
>>> set.mro()
[<type 'set'>, <type 'object'>]
>>> dict.mro()
[<type 'dict'>, <type 'object'>]
>>> class C(A, B): pass
...
from itertools import chain, combinations
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def remove_indices(s, indices):
ret, prev = '', 0
def collect_keys(d):
if not isinstance(d, dict):
return []
level = d.keys()
for k, v in d.items():
level.extend('%s.%s' % (k, c) for c in collect_keys(v))
return level
def analyze(paths):
import csv
from parsley import makeGrammar
import sys
raw = r'''
squote = '\''
escaped_squote = '\\' squote
float = <digit+ '.' digit*>:f -> float(f)
int = <digit+>:i -> int(i)
string = squote <(escaped_squote | ~squote anything)*>:s squote -> s
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
from __future__ import division
import random
from itertools import tee, izip_longest
from collections import defaultdict
class Occurrences(object):
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:
# setup.py
from setuptools import setup
setup(name='lxml',
version='3.2',
py_modules=['lxml'])
# lxml.py
print 'FOOLED YOU'
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)