Last active
August 29, 2015 14:01
-
-
Save vishnubob/9b99ac347cc945bbbfd3 to your computer and use it in GitHub Desktop.
Python Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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