tips
Upstart Notes
I think I’m going to try to keep my notes about Upstart here in the hopes that it may save some others time in the future.
Where scripts should live:
/etc/init
Failed to connect to socket:
status: Unable to connect to system bus: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
Run commands as root (duhh):
sudo [start|stop|restart] SERVICE
Information regarding all the stanzas:
Notes on Upgrading Django
I recently pushed a bunch of code to my staging server for various projects after upgrading to Django 1.2.4, and things got a little frustrating, so I figured I’d share. So, obviously, I had upgraded in my local dev environment, tests pass and, to be completely honest, these are pretty small sites that I had assumed made little use of things that were backwards incompatible. Which, as it turns out, I was correct in assuming, but I was getting errors like:
[Fri Jan 07 20:56:54 2011] [error] [client 192.168.2.102] Traceback (most recent call last):
[Fri Jan 07 20:56:54 2011] [error] [client 192.168.2.102] File "/usr/lib/pymodules/python2.6/django/core/handlers/wsgi.py", line 230, in __call__
[Fri Jan 07 20:56:54 2011] [error] [client 192.168.2.102] self.load_middleware()
[Fri Jan 07 20:56:54 2011] [error] [client 192.168.2.102] File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 42, in load_middleware
[Fri Jan 07 20:56:54 2011] [error] [client 192.168.2.102] raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e)
[Fri Jan 07 20:56:54 2011] [error] [client 192.168.2.102] ImproperlyConfigured: Error importing middleware django.middleware.csrf: "No module named csrf"
[Sat Jan 08 14:37:28 2011] [error] [client 192.168.2.102] mod_wsgi (pid=22250): Exception occurred processing WSGI script '/var/www/myproject/myproject/wsgi/app.py'.
It was obvious that by requiring a new feature (in this case CSRF), my app was parking because it was using an older version of Django.
Long story short, make sure that all previous versions of Django are gone before you re-install. Because I was a n00b when I set up my staging server, my Django install wasn’t sandboxed via a virtualenv and so I had artifacts of it in my PYTHONPATH which my apps were using. For me, I even found pieces in /usr/lib/pymodules/python2.6 on Ubuntu 10.04, weird considering global packages are usually installed in /usr/local/lib/python2.6/dist-packages. One way to get to the bottom of it all, is to grab your entire PYTHONPATH:
python -c 'import sys; print sys.path'
And make sure that Django is no where to be found before re-installing. Once I got all this cleared up, I went ahead and created virtualenvs for each of my projects so this wouldn’t happen again and so that I could isolate problems more effectively.
I guess this is just another little tidbit of being relatively new to python/django administration.
It’s all obvious in the end.
Hope this helps.
jQuery $.live() Equivalent in YUI 3
It’s been a while since I’ve posted and since I started at Yahoo! so I figured I’d give a little love to the new toys I’ve had the pleasure of playing with as of late. Clearly, we’re using YUI here at Yahoo! and because of that I’d had to adjust my thinking on a few goto front-end techniques that I relied on jQuery for. I hope to post more on these adjustments I’ve had to make, but we’ll see.
Aside from general patterns involving YUI loader, a major jQuery feature I realized that I had come to rely on is jQuery’s $.live() method.
Let’s say I have some markup like:
<div id="wrap">
<span id="button">Click Me!</span>
</div>
That’s being dynamically inserted into the DOM.
I got pretty used to doing something like:
$("#id").live("click", function() {
// do stuff
}
Super handy, but not exactly the best performance-wise. If you read the jQuery documentation (or even the jQuery source), you’ll soon find out that what’s going on behind the scenes is an implied event delegation to the root of the DOM. But, not to get too far off topic, there’s a lot of overhead that comes at the expense of this convenience.
YUI provides the same sort of functionality, albeit in a more explicit fashion via the Event delegate() method. I personally like the design decision that was made by the YUI team considering the performance hits that can be involved with using jQuery’s live method incorrectly, but to be completely fair, you can’t really mind the cleanliness of jQuery’s API. Anyways, you can always read the YUI 3 Event docs, but the above simply becomes:
YUI().use('node', 'node-event-delegate', function(Y) {
Y.one('#wrap').delegate('click', function(e) {
// do stuff
}, '#id');
});
I really like the cleanliness of jQuery, but sometimes, you gotta make a little lemonade outta the lemons you were given, even if they’re a little bit rounder and less yellow than the ones you’ve seen before.
There are definitely some caveats in the jQuery way of doing things as well as in YUI, but I feel like it’s a whole lot harder to screw it up in YUI.
Now, go try it yourself.
Filtering Special Characters Injected by Microsoft Word
I have been having trouble with users pasting content directly from M$ Word into admin fields, so I set out to create some sort of filter that replaces these little terds with something more HTML friendly. Here’s what I came up with, efficiency aside:
Please fork and enhance if you find more of these evil demon characters anywhere.
I guess it should also be noted that this can be called from any model save method on fields that you wish to filter.
UPDATE:
Just use SmartyPants: http://pypi.python.org/pypi/smartypants/
Tab-Complete SSH Logins
A smart guy named Dave Hull (@davehull) shared this piece of bash magic with me a while back and I have relied on it ever since because it’s just that handy. It’s a bit of a stretch to say that typing out “ssh” every time is that annoying, but when you can tab-complete hostnames to login, pure bliss ensues. Ok, so to do this we just need one magical little bash script:
#!/bin/sh
ssh $(basename $0) $*
I usually name this “ssh-to”. Doesn’t really matter though. What matters is how you link it up. But first, make sure this is somewhere in your PATH, like ~/bin and also make sure it’s executable:
mv ssh-to ~/bin
chmod +x ~/bin/ssh-to
Last step is just symlinking to a hostname:
cd ~/bin
ln -s ssh-to some.host.com
And that’s it. You should be able to type “some.<tab>” and the hostname will be tab-completed. Super handy if you’re like me and login to the same hosts frequently.
Notes on PIL + virtualenv + Ubuntu
Just some quick notes on installing PIL in a virtualenv in Ubuntu:
Most forums or message boards suggest:
sudo apt-get install python-imaging
to install PIL, but that doesn’t exactly do when you’re trying to install into a virtualenv with –no-site-packages, so… you try to install from source by downloading the tarball and doing the usual:
wget http://effbot.org/downloads/Imaging-1.1.7.tar.gz
tar -xzf Imaging-1.1.7.tar.gz
cd Imaging
sudo python setup.py install
In some cases this works, but other times build will fail with this:
...
_imaging.c:3138: warning: return type defaults to ‘int’
_imaging.c: In function ‘DL_EXPORT’:
_imaging.c:3138: error: expected declaration specifiers before ‘init_imaging’
_imaging.c:3149: error: expected ‘{’ at end of input
A little known fact is that if you first install python developer tools, the build will go smoothly:
sudo apt-get install python-dev
This may install a few packages other than just python2.6-dev, but whatever. You should now be able to build and install as usual:
sudo python setup.py install
Took me some poking around, so I figured I’d share, maybe save someone some time.
Handy One-Liners
It’s been a while since I’ve posted, so I figured I’d just go with something that I found to have been pretty useful as of late. Peteris Krumins recently posted a great article about one-liners, so I figured I’d add a few of my own to that list:
- Python SMTP Server
python -m smtpd -n -c DebuggingServer localhost:1025This just starts an email consumer on your localhost on port 1025. When emails are sent to it, they are echoed to STDOUT instead of being routed. Handy when developing features that require email.
- Git Graph ‘Pretty Print’
git log --graph --abbrev-commit --pretty=oneline --decorateThis really just serves as a replacement to gitk or GitX in a (command-line) pinch.
- Remove .pyc
find . -name '*.pyc' -exec rm {} \;Does just what you’d think it does: removes all .pyc files from the current directory you are in.
These are all pretty much just great candidates for bash aliases, but I figured I’d share since I was inspired. Thanks Peteris, and if you haven’t checked out his article, do so IMMEDIATELY:
URL encoding unicode characters in python+django
Let’s say you’re like me and you need to URL encode a user generated string to be sent across the pipe to a web service of some variety. Well what happens when that user’s copying stuff straight from Microsoft Word, or creating unicode characters somehow? You flex your 1337 python muscles that’s what. This deals with with Django and Python’s urllib and the exception that gets thrown when you try to url encode unicode characters such as:
'ascii' codec can't encode character u'\xb0' in position 96: ordinal not in range(128)
Let’s say your user is creating data that translates to this dictionary that you would normally pass to urllib.urlencode():
data = { 'param1': u'ßßß', 'param2': u'\u2044' }
Here’s a fancy one liner that I use to ensure my data dictionary is going to check out:
import types
data = dict([(k,v.encode('utf-8') if type(v) is types.UnicodeType else v) \
for (k,v) in data.items()])
This makes sure that for every item in your data dictionary that is of unicode type, it is first UTF-8 encoded before passing it in to urllib.urlencode()
How ’bout them apples?
Search
What I'm Doing...
- Everyone that considers themselves a Mizzou fan can... well, go fsck themselves. #checkyofilesystem 13 hrs ago
- Missouri sucks. Period. 13 hrs ago
- @aaronky seat's taken brah! You're not welcome in these parts. in reply to aaronky 15 hrs ago
- More updates...
Powered by Twitter Tools

Social Links