Skip to content

Instantly share code, notes, and snippets.

@slinkp
slinkp / gist:7796628
Created December 4, 2013 22:18
Python traceback example
Traceback (most recent call last):
...
data = [(entry['key'], entry['count']) for entry in response['data']['subtotal']]
TypeError: 'NoneType' object is unsubscriptable
@slinkp
slinkp / gist:7796609
Created December 4, 2013 22:17
Traceback example
Traceback (most recent call last):
...
data = [(entry["key"], entry["count"]) for entry in response["data"]['subtotal']]
TypeError: 'NoneType' object is unsubscriptable
Locals at innermost frame:
{ 'foo': [],
'bar': True,
@slinkp
slinkp / gist:7796543
Last active September 4, 2018 18:38
Log locals on any logging.exception() call
import pprint
MAX_VARS_LINES = 30
MAX_LINE_LENGTH = 100
class VerboseExceptionFormatter(logging.Formatter, object):
def __init__(self, log_locals_on_exception=True, *args, **kwargs):
self._log_locals = log_locals_on_exception
@slinkp
slinkp / gist:7203134
Created October 28, 2013 19:34
Python: getattr() vs. hasattr()
Beware of hasattr(). It will return False on *any* exception. This is rarely what you want.
>>> class Thing(object):
... @property
... def foo(self):
... return self.this_will_raise_attribute_error
... @property
... def bar(self):
... return 1 / 0 # ZeroDevisionError...
>>>
@slinkp
slinkp / pyflakespep8.py
Created July 29, 2013 17:37
A pyflakes + pep8 mashup script.
#!/usr/bin/env python
"""
TODO: replace this with http://flake8.readthedocs.org/en/latest/#flake8 ?
Combines pep8 and pyflakes output for emacs;
treats pep8 as warnings, pyflakes as errors.
From https://bitbucket.org/keegan_csmith/dotfiles/raw/tip/misc/pyflakespep8.py
modified a bit by slinkp.
@slinkp
slinkp / .emacs-fragment.el
Created July 29, 2013 17:36
.emacs config snippet for wiring up pep8 and pyflakes to flymake, via an external script.
(when (load "flymake" t)
;; For Python, I use pyflakes combined with pep8 via a little script
;; that mashes them up. Might want to try flake8 (see pypi) ... it
;; adds a complexity checker.
(defun flymake-pyflakespep8-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
try:
print "hi", nothing
except NameError:
print "ok"
Check out the tornado source.
Switch to the branch1.2.
Apply this patch:
diff --git a/website/website.py b/website/website.py
index 39b7aed..902be9a 100644
--- a/website/website.py
+++ b/website/website.py
@@ -49,6 +49,7 @@ settings = {
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
@slinkp
slinkp / thing.json
Created August 6, 2012 20:41
some json that github issues api doesn't like
{"body": "This code: https://github.com/openplans/openblock/blob/master/ebpub/ebpub/geocoder/base.py#L498\r\n\r\nruns into a few problems when convert_to_block is True. Seems it's been copied from somewhere else but not fully adapted. For easier reference the code is:\r\n\r\n{{{\r\n#!python\r\n except InvalidBlockButValidStreet, e:\r\n # If the exact address couldn't be geocoded, try using the\r\n # normalized location name.\r\n if convert_to_block:\r\n block_name = address_to_block(kwargs['location_name'])\r\n if block_name != kwargs['location_name']:\r\n try:\r\n result = BlockGeocoder._do_geocode(block_name)\r\n result['result']['address'] = block_name\r\n except InvalidBlockButValidStreet, another_e:\r\n pass\r\n except AmbiguousResult, another_e:\r\n pass\r\n # No joy, return all blocks.\r\n logger.debug('Invalid block for %r,
@slinkp
slinkp / prior_art.txt
Created June 8, 2012 17:14
Pypi packages mentioning "Spam"
Python packages matching "spam" on pypi:
=========================================
Note i'm marking things that only deal with moderation, manual
flagging, and the like, as irrelevant.
RELEVANT
-----------------------------------------------------