Skip to content

Instantly share code, notes, and snippets.

View floer32's full-sized avatar

Michael Floering floer32

  • 06:10 (UTC -07:00)
View GitHub Profile
@floer32
floer32 / _socket_toggle.py
Last active December 18, 2019 20:31
Disable the internet in Python. With py.test hooks. (Disable socket.socket.) GREAT for unit testing.
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::
@floer32
floer32 / python_bind_function_as_method.py
Created February 11, 2015 01:42
Bind Python function as method
# 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
@floer32
floer32 / quick_punycode_encode_decode_example.py
Last active December 24, 2019 15:30
[Regarding Python 2 - in Python 3 just use normal strings that are always Unicode.] // quick example of encoding and decoding a international domain name in Python (from Unicode to Punycode or IDNA codecs and back). Pay attention to the Unicode versus byte strings
# 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
@floer32
floer32 / harmonics.py
Last active August 29, 2015 13:56
Method to multiply (?) two sequences in certain way, that may be useful in generating sets of harmonic frequencies.
""" 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.
@floer32
floer32 / easy_repr_mixin.py
Last active August 29, 2015 13:55
A mixin that gives an OK default for `__repr__`. Format is like "ClassName(attributeA='foo', attributeB='bar', propertyA='baz', propertyB='quux')" where each of those values is the `repr()` of the value.
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 = [
@floer32
floer32 / dot_dict.py
Created January 16, 2014 17:20
DotDict is a variation of the classic "Bunch" recipe - just a dictionary, with all the normal dictionary methods, but the attributes are accessible by dot notation. Use this when you just really want to turn some dictionaries (i.e. from JSON) into something like JavaScript objects. Achieved with very little code. See bottom of docstring for Stac…
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.

Scratch Programming Activities

For the Veracode HacKIDthon

Ordered by level of difficulty and probably age or experience of student, beginning with the easiest.

@floer32
floer32 / logging_subprocess.py
Last active December 22, 2015 00:39 — forked from bgreenlee/logging_subprocess.py
Variant of subprocess.call that accepts a logger instead of stdout/stderr
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.
@floer32
floer32 / tupperware.py
Last active September 26, 2022 12:13
recursively convert nested dicts to nested namedtuples, giving you something like immutable object literals
from UserDict import IterableUserDict
import collections
__author__ = 'github.com/hangtwenty'
def tupperware(mapping):
""" Convert mappings to 'tupperwares' recursively.
@floer32
floer32 / linux_mint_cx_Oracle.sh
Last active June 4, 2018 08:07
Linux Mint 14 cx_Oracle and Oracle Instant Client setup.
#!/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 #