Skip to content

Instantly share code, notes, and snippets.

@rfong
Last active February 2, 2016 02:00
Show Gist options
  • Save rfong/24dbdbe48985ec0011f1 to your computer and use it in GitHub Desktop.
Save rfong/24dbdbe48985ec0011f1 to your computer and use it in GitHub Desktop.
useful python stuff I lug around
import itertools
def printable_percent(numerator, denominator, precision=2):
"""Truncated readable percentage"""
return ('%.' + str(precision) + 'f') % (numerator * 100.0 / denominator)
def generate_all_binary_states(keys):
"""
For testing, generate all True/False combinations for a set of keys.
Return a list of dictionaries {key: boolean}
"""
return [
dict(zip(keys, combo)) for combo in
itertools.product((False, True), repeat=len(keys))
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment