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 | |
import unittest | |
from unicodedata import normalize | |
# http://en.wikipedia.org/wiki/Clean_URL#Slug | |
def slugify(string, delim="-"): | |
string = normalize("NFKD", string).lower() | |
return re.sub(r"\W+", delim, string).strip(delim) | |
class TestSlugify(unittest.TestCase): |
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
class Factory(object): | |
inst=None | |
@classmethod | |
def get(self): | |
if self.inst == None: | |
# new instance of self | |
self.inst = self # instantiate() to run __init__ | |
self.inst.__init__(self) | |
# apply @classmethod to all methods | |
for name,attr in self.__dict__.items(): |
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 fib(n): | |
x,y = 1,1 | |
for i in range(n): | |
yield x | |
x,y = y, x+y | |
def main(): | |
for i in range(1,10): | |
print(fib(i)) | |
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 flatten(seq): | |
for item in seq: | |
if hasattr(item, "__iter__"): | |
yield from flatten(item) | |
else: | |
yield item |
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 random import randint | |
from math import log10, floor | |
def human_format(num, ends=["", "K", "M", "B", "T"]): | |
# divides by 3 to separate into thousands (...000) | |
return ends[int(floor(log10(num))/3)] | |
if __name__ == '__main__': | |
for i in range(10): | |
x = randint(1,10**i) |
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 contextlib import contextmanager | |
from shutil import rmtree | |
from tempfile import mkdtemp | |
import unittest | |
import os | |
@contextmanager | |
def working_directory(path): | |
current_dir = os.getcwd() | |
os.chdir(path) |
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 | |
def slugify(string, delim="-"): | |
return re.sub(r"\W+", delim, string.lower()) | |
class Provider(object): | |
""" A provider holds its publishers alongside their | |
subscribers and can run the published content to | |
the appropriate subscribers""" |
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 | |
import hashlib | |
class TemplateError(Exception): | |
pass | |
class Template(object): | |
pattern = r"(\{[a-zA-Z0-9-_\|\.]+\})" | |
def __init__(self, string): |
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 random import randint | |
def sequence(length=3, depth=0, max_depth=2): | |
for i in range(1,length): | |
if randint(0,1) and depth <= max_depth: | |
yield list(sequence(3, depth+1)) | |
else: | |
yield randint(1,9) | |
if __name__ = "__main__": |
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 collections import defaultdict | |
#------------------------ | |
# $COUNTING | |
#------------------------ | |
names = ["tom", "dick", "harry", "harry", "dick", "tom", "tom", "dick", "harry"] | |
d = defaultdict(int) | |
for name in names: |