Written with StackEdit.
Ordered by level of difficulty and probably age or experience of student, beginning with the easiest.
from __future__ import print_function | |
import socket | |
import sys | |
_module = sys.modules[__name__] | |
def disable_socket(): | |
""" disable socket.socket to disable the Internet. useful in testing. | |
.. doctest:: |
# based on: http://stackoverflow.com/questions/1015307/python-bind-an-unbound-method#comment8431145_1015405 | |
def bind(instance, func, as_name): | |
""" Turn a function to a bound method on an instance | |
.. doctest:: | |
>>> class Foo(object): | |
... def __init__(self, x, y): | |
... self.x = x | |
... self.y = y |
# INCORRECT! DON'T DO THIS! | |
>>> x = "www.alliancefrançaise.nu" # This is the problematic line. Forgot to make this a Unicode string. | |
>>> print x | |
www.alliancefrançaise.nu | |
>>> x.encode('punycode') | |
'www.Alliancefranaise.nu-h1a31e' | |
>>> x.encode('punycode').decode('punycode') | |
u'www.Alliancefran\xc3\xa7aise.nu' | |
>>> print x.encode('punycode').decode('punycode') | |
www.alliancefrançaise.nu |
""" Functions to "multiply" sequences in peculiar way. Intended for use in generating sets harmonic frequencies. | |
.. moduleauthor:: hangtwenty | |
A friend of mine was wondering how to do something like this so I just had fun coming up with a solution. | |
I may use it in Supercollider or something if I can figure out how to glue Python and SC together. | |
""" | |
def flat_product_of_sequences(seq1, seq2): | |
""" Given two flat lists of integers, return this program's particular kind of product of them. |
class EasyReprMixin(object): | |
""" A mixin that gives an OK default for `__repr__`. | |
I mix this into classes with instances that are static after creation, | |
i.e. with properties telling you about their contents. Wrappers for | |
data usually. | |
""" | |
def __repr__(self): | |
attributes = [ |
class DotDict(dict): | |
""" A dictionary whose attributes are accessible by dot notation. | |
This is a variation on the classic `Bunch` recipe (which is more limited | |
and doesn't give you all of dict's methods). It is just like a dictionary, | |
but its attributes are accessible by dot notation in addition to regular | |
`dict['attribute']` notation. It also has all of dict's methods. | |
.. doctest:: |
Written with StackEdit.
Ordered by level of difficulty and probably age or experience of student, beginning with the easiest.
import subprocess | |
import select | |
from logging import DEBUG, ERROR | |
def call(popenargs, logger, stdout_log_level=DEBUG, stderr_log_level=ERROR, **kwargs): | |
""" | |
Variant of subprocess.call that accepts a logger instead of stdout/stderr, | |
and logs stdout messages via logger.debug and stderr messages via | |
logger.error. |
from UserDict import IterableUserDict | |
import collections | |
__author__ = 'github.com/hangtwenty' | |
def tupperware(mapping): | |
""" Convert mappings to 'tupperwares' recursively. |
#!/bin/bash | |
# SPECIFY THE VIRTUALENV YOU WANT TO INSTALL cx_Oracle TO # | |
########################################################### | |
# Change this to whatever you want, but note that this script isn't going to make the virtualenv for you. | |
# It assumes the environment already exists. | |
MY_VIRTUAL_ENV='dynamic' | |
# INSTALL ORACLE INSTANT CLIENT'S DEPENDENCIES # |