Skip to content

Instantly share code, notes, and snippets.

@tchn
Last active September 27, 2015 14:37
Show Gist options
  • Save tchn/1285093 to your computer and use it in GitHub Desktop.
Save tchn/1285093 to your computer and use it in GitHub Desktop.
py func code snippet
def select_entry(seq, question):
for entry in seq:
print '%d) %s' % ((1 + seq.index(entry)), entry)
return input(question) - 1
def remove_duplicate(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if x not in seen and not seen_add(x)]
def list_optargs(option, opt, value, parser):
""" Turns an option arguments string into a list.
This function is an optparse callback function
to return a list of option argument values
passed as comma-separated string like "aaa, bbb".
Sample parser.add_options setting to use this callback:
parser.add_options('-s', '--sample', type='string', action='callback', callback=list_optargs, dest='sample', help='just a sample')
"""
value = re.sub(r'\s+', '', value)
setattr(parser.values, option.dest, value.split(','))
def get_binary_string(n):
"""
returns binary string representation of a given number
"""
r = ''
p = 0
pp = 1
while n >= pp:
if n & pp:
r += '1'
else:
r += '0'
p += 1
pp = 2 ** p
return r[::-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment