Tuesday, May 27, 2008

a quick tip: the disk "name-here" is in use and could not be ejected

(permalink)
Under Leopard I seem to get this error relatively often when I'm trying to eject my external disks. Even after quitting all my applications, the error doesn't go away. I never want to disconnect my drive without unmounting it (maybe it's an OCD thing). Here's my solution:

First, get the device name of the drive: Disk utility > Select volume > Click Info > Then note the disk identifier.

Open up terminal to check which applications are using this drive (substitute your drive name):
lsof /dev/disk2s10


Quit (or kill) all the processes that are using the resource. If nothing is returned (as is frequently the case with me), then force umount the drive (substitute your drive mount point):
sudo umount -f /Volumes/Backup


I'm adding this because the man page on umount doesn't document the force (-f) option.

Labels: , ,

Saturday, May 17, 2008

Extending the user model: profiles in Django

(permalink)
I think the documentation about using profiles & the get_profile() function isn't as clear and/or elegant as it could be. The problem with django's recommended implementation of profiles is that when a user is created, a user's profile isn't automatically created - it's not part of the same model. Managing the two can be tricky - if you mess up, you're bound to get ObjectDoesNotExist errors.

My solution was to never use get_profiles() directly - but instead add a helper method where any ObjectDoesNotExist errors are caught and resolved:


First, create a profile class in models.py:
from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    karma = models.IntegerField(default=1, core=True)
    url = models.URLField(default="", core=True)
    user = models.ForeignKey(User, unique=True)

Note, if you want to be able to create profiles using the Django admin interface, change the last line to the following:
user = models.ForeignKey(User, unique=True, edit_inline=models.TABULAR, num_in_admin=1,min_num_in_admin=1, max_num_in_admin=1,num_extra_on_change=0)


Add this line to settings.py:
AUTH_PROFILE_MODULE = 'core.UserProfile'

(change your line to 'appname.ModelName' - note, do NOT include your project name or anything else. Just your application name and model name (the model name is UserProfile, unless you change it).

Finally, add to views.py:
from django.contrib.auth.models import User
from project.core.models import UserProfile
from django.core.exceptions import ObjectDoesNotExist

def get_or_create_profile(user):
    try:
        profile = user.get_profile()
        except ObjectDoesNotExist:
        #create profile - CUSTOMIZE THIS LINE TO OYUR MODEL:
        profile = UserProfile(karma='1', url='http://example.org', user=user)
        profile.save()
    return profile


Now, in views.py, to access a user's profile, you only need two lines:
user = User.objects.get(pk = user_id)
user.userprofile = get_or_create_profile(user)


And, say if you wanted to change a value:
user.userprofile.karma += 1
user.userprofile.save()


Easy, eh?

Labels: , ,

Wednesday, May 14, 2008

SICP and promises

(permalink)
A few days ago I blogged about be working through SICP in ARC. I was going to track my progress on bluwiki.

My personal goal was to make some progress every day. That hasn't happened.

I'm thinking about abandoning ARC, and just working through SICP in scheme. Scheme & ARC are remarkably similar, it seems the only difference is semantics. It'd be a lot easier scheme, since I wouldn't have to translate all the problems. Also, the language is more mature - so I suspect the libraries are more robust.

We'll see - I'm going to take a crack on SICP 1.2 and 1.3 this weekend, and I'll make a decision after that.

Labels: , ,

Tuesday, May 13, 2008

One concrete tip for the self-employed programmer (that procrastinates)

(permalink)
I just read an article about procrastination that I found interesting (mostly because I agree with it). Procrastination is great - as I explained in the comments:
... procrastination paid my way through school. When you don't start studying for tests until a few hours before, you suddenly find yourself with a lot of time on your hands.


Procrastination teaches you how to work efficiently under pressure - a very important skill to have. The experienced procrastinator becomes a pro at putting out fires, and ironically, quickly getting things done. So why do procrastinators fail at life? Partially because there isn't enough that absolutely needs to get done. Procrastinators need pressure.


So what's a procrastinator to do? Start fires.


In software development this translates to setting artificial deadlines. Make promises - and judge your performance by whether you keep them. Here are some examples:

  • Promise your mom that you'll blog at least once per week (I do).
  • Promise a new feature on your website by a specified date.
  • Make promises on your blog, to whomever will listen (if you don't keep your promise, publicly apologize too).
  • Promise your clients you'll have respond to their emails within 12 hours.


And here's my favorite example: find someone important to you and make them your boss (I've chosen my girlfriend). At the beginning of every week spend an hour with that person and explain to them what you're going to do in the next week. Make promises. Then, check in with them throughout the week, and at the end of the week, conduct a "performance review." It helps if you can create some sort of incentive to meet your targets (you can be creative here... ).

Labels: , ,

Monday, May 12, 2008

My first attempt at a python/django application

(permalink)
You know that "a-ha" moment when yo finally get something? I think I might've just had it today. I've built my first (working) python / django application - and I think I'm actually starting to understand django & OO programming. I still have a long way to go, but it's a relief to get the hang of things.

Here it is: b l a c k l i s t e d - it's a fun little search box that will tell you if your name appears on a government watch-list. Pretty basic, but hey, what do you expect?

Labels: ,

Saturday, May 10, 2008

Now using DISQUS for blog comments

(permalink)
I've switched this blog to DISQUS for the comment system. Going forward, you might want a disqus account to post a comment (but it's not required!). For posts previous to this one, you'll be able to add comments either through Blogger or disqus.

Labels:

Thursday, May 08, 2008

(re)discovering IRC

(permalink)
Analog IRC
Analog IRC,
originally uploaded by thowi.
I've rediscovered IRC. I love doing this, there's a wealth of information on there. It's so much easier to get help if google isn't giving you any love. One word of caution: just don't abuse those who help you, and try to help others when you can :)

Here are the channels I currently hang out on, feel free to stop by & say hi:

#bluwiki
#elc
#startups
#python
#django
#arc
#gentoo
#debian
#bash
#rubycodejam
#lighttpd (these guys are especially friendly)
#friendly-coders
#gcj

If You Put That Picture On The Internet I'll Call My Lawyer

(permalink)
Awesome. I hope somebody recognizes this guy and tells him he's all over the 'net.

how to dive into web programming

(permalink)
I'm just starting to get serious about web programming, and I've done a decent amount of research about how to master it. I quickly articulated this advice in an email to a friend, and I thought I'd post it here:


Her question:
From: AM
To: Sam Odio
Subject: hi i'm curious..

Hi,

I saw that you're doing SCIP - so then I looked up what a functional programming language was, and according to wikipedia functional languages "have largely been emphasized in academia rather than in commercial software development." But it notes that you can use it for commercial use as well. So are you doing it just to learn or is to serve some future practical purpose?

I was just wondering b/c I always want to know what the best language to learn is (not that I have much of an intention of learning any sort of functional programming after getting extremely confused by your progress tracking on bluwiki) and I also know that this is all based largely on personal preference, but i'm still curious....

AM.


My response:
From: Sam Odio
To: AM
Subject: Re: hi i'm curious..

my suggestion:

1) learn an object oriented language & framework (I'm doing pything w/ djano, you can also do ruby/rails or php/symfony)
2) learn functional programming w/ SICP if you want to get good (CLisp, scheme, arc)

#2 will take a lot of time, and yield few direct results. But it'll help you understand what is capable w/ programming. Long-term results, not short-term. If you want ONLY short-term results, learn php/symfony. PHP is not completely object oriented, but quick & dirty.

-s


What does everyone think? did I give her the right advice?

Labels: , , , , , ,

Wednesday, May 07, 2008

Working through SICP... in arc

(permalink)
I've decided to work through the classic functional programming book, Structure and Interpretation of Computer Programs. The book is well reviewed, and supposedly a "must-read" for anyone getting into programming - especially functional programming. It served as the basis for MIT's intro programming course for several decades.

Well I thought I would add a twist by doing all the examples in arc, instead of scheme. I have no idea if this is wise, or even practical, but it's worth a shot.

I'm going to attempt every exercise in the book, first rewriting it into arc (if applicable), and then solving it. You can follow my progress here: http://bluwiki.com/go/User:Sam_Odio/SCIP_in_Arc.

Labels: ,