David Ziegler's personal blog of computing, math, and other geekery.


04 Feb 2010

Test Database Settings in Django

For early stage local development with Django, I typically use sqlite. It’s easy to setup, delete the database if I need to, etc. Later on though, I find that it’s a good idea to switch my local database to whatever I’m using in production (postgresql, mysql, etc), either because I want to make sure that my schema migrations work, or I might have some custom non-database agnostic SQL written for an app.

The problem is that if you’re not using sqlite, Django tests run incredibly slow. With sqlite, Django will use an in-memory database for testing that is an order of magnitude faster than mysql or postgresql.

For some reason there’s no way to specify your test database engine in settings.py, but if you do the following settings hack you can use the in-memory database as your test database.

Create a file called settings_test.py that contains:

from settings import *

DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'dev.db' 
DATABASE_USER = ''   
DATABASE_PASSWORD = ''         
DATABASE_HOST = ''         
DATABASE_PORT = '' 

And when running tests just do:

python manage.py test --setting=settings_test

You can obviously include other variables in this file if you want create some test specific behavior. For example, if you have a signal that makes an API call to some remote server everytime you create a user, you might not want to do this during testing. The simple way to deal with this would be to create a variable called RUNNING_TESTS and default it to False in settings.py and set it to TRUE in settings_test.py.

Hopefully now you have one less excuse for writing unit tests, and the people who inherit your code won’t have to track you down and strangle you.

Comments (View)

08 Jan 2010

See Which Twitterers Don’t Follow You Back (updated)

It turns out that if you were following more than 100 people or had more than 100 followers, there was a bug in my script to check who on Twitter doesn’t follow you back. Since I’m not super popular and have less than 100 for both, it took me a while to figure this out.

The getFriends and getFollowers api methods in python-twitter are paginated to 100 results per call, so I needed to modify the script to loop over the paginated results. If I was doing anything more complicated I’d probably use tweepy because it’s a more robust api wrapper, but whatever.

Also, my follower/followee count is still less than 100, so feel free to let me know if this doesn’t work, or follow me an twitter so I can test it myself :)

Comments (View)

03 Dec 2009

Programming Gloves

Unfortunately a lot of the code I’m working on is on lock down at the moment, but I thought that since it’s winter, I’d share a little trick to keep your hands warm during those cold late night coding sessions.

If you’re like me, you’re unnecessarily frugal. This means wearing 3 layers of clothes, a blanket, and maybe even a snuggie before thinking about turning on the heat. Unfortunately, computer work requires finger mobility which generally leaves my hands exposed while I’m working. It used to get so cold that my hands would be in physical pain unless I stopped every 5 minutes to sit on them to warm them up.

Hobo gloves would probably work, except they come up a little too high on your fingers to type, and I don’t have a pair of gloves lying around to cut the fingers off (obviously, my unnecessary frugality prevents me from buying them). So if you’re like me and cheaper than a hobo, you can make some fancy fingerless gloves out of old socks.

Just cut 5 holes on the end, and you’re done. I recommend thick socks obviously to keep your hands warmer, and black socks are probably a little more stylish.

This is also a good use for all of those lame socks that go all the way up your calf (I don’t know why my mom keeps sending these to me?), because it’ll keep your forearms warm.

Try it, it works.

Comments (View)

02 Nov 2009

Halloween

This is me as Kim Jong Il.

kim jong il

Unfortunately, my dedication to the role meant shaving a receding hairline, which means I now have a shaved head. My alternative costume was to be Kim Jong ILL, North Korea’s finest gangster rapper.

Comments (View)

30 Sep 2009

A replacement for django-admin.py startproject

When I create new Django projects, I find myself doing a lot of the same things over and over. For instance, the file structure of each project is pretty much identical, and looks something like this:

  • deploy
    • wsgi_handler.py
  • docs
  • env (my virtualenv folder)
  • src
    • apps
      • profiles
      • photos
      • etc.
    • localsettings.py
    • manage.py
    • scripts
    • settings.py
    • static
      • css
      • images
      • js
    • templates
    • urls.py

The things is, I’m lazy, and it’s tedious to create all those directories, create my wsgi_handler, uncomment the admin app, update my settings.py, urls.py, etc for every project I create. I basically end up copying/pasting/deleting from old projects.

So to automate some of that, I made a small reusable django app to serve as a replacement for django-admin.py startproject. So instead of

 django-admin.py startproject project_name

you would do

 create_project project_name

To install, either clone the git repository which you can find here:

http://github.com/dziegler/django-create-project

or install with pip using:

pip install -e git+git://github.com/dziegler/django-create-project.git#egg=django-create-project

I made this mainly for my benefit, so some of the settings are tuned to my preferences. For example, it changes TIME_ZONE in in settings.py from ‘America/Chicago’ to ‘America/Los_Angeles’, and automatically installs django_extensions, debug_toolbar, and django-css because I use those in all of my projects. Since it’s on github, it’s fairly easy to fork and customize to fit your preferences. You can find it here:

http://github.com/dziegler/django-create-project

Comments (View)

21 Sep 2009

Procrastination

I just wasted an hour solving this stupid puzzle: http://www.techcrunch.com/2009/09/21/google-is-searching-for-beautiful-minds-but-so-far-no-m-i-t-students-have-broken-its-code/

I say stupid, because once you discover the answer you’re like, why did I just waste my time solving this? It’s not a cool puzzle that you might have to write a neat algorithm to decipher, it’s just one of those “ah-ha!” type puzzles, which to me, don’t really tell you that much about a person if you were looking to hire them.

If you call the number they admit that they’re not even Google! I don’t particularly want a job either at Google or in Massachusetts, but now I’m annoyed that they lied to me. I’m still curious though, so I left a message and I’ll post info if they call me back. Probably not a good way to hire people by lying to them off the bat though.

Click here for the answer

Comments (View)

29 Aug 2009

Email Fail

I just noticed that my mail client will occasionally auto-select my old work email address for the “from” field when composing new emails. Unfortunately, I no longer have access to emails sent to this address.

So if I wrote you an email and you responded to it, but I never replied back, this is probably the reason. Sigh.

Comments (View)

19 Aug 2009

Feeling Nostalgic

I had the urge to create a retro version of my homepage today. I cheated a little bit because I didn’t have the patience to use frames or tables.

http://www.davidziegler.net/retro.html

Comments (View)

11 Aug 2009

Sikarra

No updates in a while because I’ve been traveling around a lot. I did manage to build this in my free time though: http://www.sikarra.com

It’s basically an online wishlist that lets you bookmark stuff you’d like to buy. Nothing fancy or anything, it’s something that I mainly just wanted for myself. I use Amazon wishlist a lot, but I didn’t like how I could only bookmark Amazon products. I have some cool features in mind that I’d like to build into it, but it depends on how much free time I have or if other people actually start to use it.

When my birthday came around my parents had no idea what to get me, so I figured I’d make it easy to share with your friends and family.  The link to my public list is just http://www.sikarra.com/david

Maybe I can market this to camwhores or something? Who knows.

Comments (View)

24 Jul 2009

SF Theater Buzz

It looks like it’s safe to mention this now. I recently finished building this site for a client: http://www.sftheaterbuzz.org

My friend Kim Nguyen did the design and artwork, and I think it looks pretty awesome. You should definitely hire her, as long as it doesn’t interfere with her working with me. The whole thing is written with Django and jQuery, and I used django-css with CleverCSS to write the CSS.

It’s a little sad because there are no reviews yet, but I don’t think the client has had time to promote it. I hope he gets around to it, because I’d hate to see such a beautiful site go to waste. Also, if you’re in the Bay Area, check out SF Theater Festival this Sunday. It’s free theater, and maybe afterwards you might feel like writing about what you saw…hint hint.

Comments (View)

Page 1 of 3