Skip to content

Instantly share code, notes, and snippets.

@vishnubob
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishnubob/9b99ac347cc945bbbfd3 to your computer and use it in GitHub Desktop.
Save vishnubob/9b99ac347cc945bbbfd3 to your computer and use it in GitHub Desktop.
Python Snippets
# Traceback at a specific point
import traceback
frames = 4
print str.join('', traceback.format_stack()[-(frames+1):-1])
# build a color name to RGB python module
import urllib2, json, pprint
url = "https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json"
colors = json.loads(urllib2.urlopen(url).read())
colors = {c: (int(v[1:3], 16), int(v[3:5], 16), int(v[5:7], 16)) for (c, v) in colors.items()}
content = "__color_tables__ = \\\n%s" % pprint.pformat(colors, indent=4)
f = open("colornames.py", 'w')
f.write(content)
# Get all of the assert conditions in unittest
[key for key in unittest.TestCase.__dict__.keys() if key.startswith("assert")]
# same thing, for the shell
python -c "import unittest; print '\n'.join([k for k in sorted(unittest.TestCase.__dict__.keys()) if k.startswith(\"assert\")])"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment