Skip to content

Instantly share code, notes, and snippets.

View floer32's full-sized avatar

Michael Floering floer32

  • 17:02 (UTC -07:00)
View GitHub Profile
@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::
@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 / 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 / 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 / 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 / _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 / install_jce_policy_jars.sh
Last active August 29, 2015 14:25
install Java Cryptography Extensions (unlimited JCE)
# ABOUT: https://en.wikipedia.org/wiki/Java_Cryptography_Extension
# AUTHOR: gist.github.com/hangtwenty
# PREREQUISITE:
# Download jce_policy*.zip from Oracle:
# - Java 8: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
# - Java 7: http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
unzip $HOME/Downloads/jce_policy*.zip -d $HOME/Downloads/jce_policy_jars
jre_path="$JAVA_HOME/jre"
dest="$jre_path/lib/security"
@floer32
floer32 / printSystemProperties.java
Created July 16, 2015 23:50
print system properties in Java
Properties p = System.getProperties();
Enumeration keys = p.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
String value = (String)p.get(key);
System.out.println(key + ": " + value);
}
@floer32
floer32 / aws_lambda_gotcha.md
Created October 16, 2015 21:21
A gotcha I ran into while playing with AWS Lambda.

I started out pasting Python code into the AWS Lambda web GUI, right in that textarea. Got it working, making a call to a Slack API (whee!). So I had a working test case and I could test a refactor.

With the same code, I wanted to take advantage of the cool zipfile/ virtualenv upload support ("Create a Deployment Package") so I could use third-party libraries. Keeping the code itself the same (same as what I'd pasted into the textarea), the test should work again, as long as I packaged it right.

I kept trying to upload a zip with the same code, I thought, to AWS Lambda ... but I kept getting an error:

@floer32
floer32 / pytest-doctest-with-coverage.sh
Last active August 21, 2017 03:53
Run pytest with doctests and coverage from doctests AND normal unittests/pytests
# A coworker of mine covinced himself that the built-in pytest doctest support module didn't integrate with pytest-cov.
# He wrote his shim to get around this. Not necessary though.
# The gist is to make sure you have "--doctest-modules" and "--cov-report" and "--cov" arguments.
py.test . --doctest-modules --cov-report term --cov=.
# or
py.test mymodule --doctest-modules --cov-report term --cov=mymodule