Every morning, when my alarm clock goes off, a little demon wakes up before I do and tries as hard as he can to delay my day.
Sometimes, he sets my alarm clock back 30 minutes. Other times, when he wakes up way before me, he just turns the alarm clock off and I sleep for another three hours. He used to whisper in my ear (when I was in school) that I was way ahead in my classes and that I should take a break from them, or at least from my earliest one. Now, he looks for my work laptop so he can send an "out sick" email to my boss.
Yeah, he's a jerk, but I've got his number. I put my alarm clock at the other end of my room, and leave my work laptop out in the car. He still makes it to the "snooze" button from time to time, but I always wake up and stop him before he can do anything more dangerous.
Problem is, I have to stay awake to keep him at bay. I have a 45-minute drive to work, which is almost entirely straight highway driving. From time to time, I catch him reaching for the wheel, or moving towards one of the pedals. So far, nothing's come of it, but something in the back of my head (maybe him) tells me that it's only a matter of time.
Monday, August 10, 2009
Monday, May 18, 2009
Django Middleware vs. Context Processors
This may be old news to many people, but it's something I just recently learned (after doing it wrong for about nine months), so I figured someone else may be able to benefit from my mistakes.
For a long time, when I needed to access the currently logged-in user in one of my Django views, they would follow this pattern:
[code lang="python"]from django.template import RequestContext
# some other view code
def my_view (request, obj_id):
context = RequestContext(request)
obj = get_object_or_404(SomeModel, pk=int(obj_id))
# do some stuff, including:
some_function(context["user"])
return render_to_response("some_url_name", {"some_var": some_val},
context_instance=context)[/code]
Now, seasoned Django vets (why are you reading this post, by the way?) are probably laughing, but this seemed perfectly fine to me. Luckily, my ignorance was revealed to me while asking on IRC about a problem which, while didn't seem so at the time, was completely tied to my misuse of RequestContext.
To put it simply, context processors are made to be used in templates. The only time they should ever be instantiated is at the very end of a view, like so:
[code lang="python"]return render_to_response("some_url_name", {"some_var": some_val},
context_instance=RequestContext(request))[/code]
I was using them all over views, and even in a few of my decorators. What's wrong with this? The main thing is, certain mutation functions are sometimes performed when a RequestContext is instantiated (such as user.get_and_delete_messages(), which gets all of a user's messages from the database and then deletes them), and performing them multiple times before loading the template can cause unexpected results. In my example, I was instantiating a RequestContext in a bunch of decorators, which meant that by the time my template was loaded, all of the user's messages were deleted (and stored in an earlier instance of RequestContext), making it look to me as if my auth messages were being thrown out.
What's the solution? Middleware. Middleware allows the programmer to attach variables to the request object before it reaches the view. With this (and the provided django.contrib.auth.middleware.AuthenticationMiddleware), I can still achieve my original goal (accessing the logged-in user from a view), but now I can do it without creating a RequestContext and potentially running mutation functions multiple times:
[code lang="python"]from django.template import RequestContext
# some other view code
def my_view (request, obj_id):
obj = get_object_or_404(SomeModel, pk=int(obj_id))
# do some stuff, including:
some_function(request.user)
return render_to_response("some_url_name", {"some_var": some_val},
context_instance=RequestContext(request))[/code]
I've removed all the references to RequestContext from my decorators, and made sure to only instantiate it at the very end of views. Now, messages work perfectly. I've even written my own middleware (which you can learn how to do here) to load instances of a few of my own models into the request.
To reiterate, I'm aware that this is common knowledge for many people, but it took me 9 months of moderate Django use and embarrassment on IRC to discover it for myself. Hopefully, this will help someone else in a similar position.
For a long time, when I needed to access the currently logged-in user in one of my Django views, they would follow this pattern:
[code lang="python"]from django.template import RequestContext
# some other view code
def my_view (request, obj_id):
context = RequestContext(request)
obj = get_object_or_404(SomeModel, pk=int(obj_id))
# do some stuff, including:
some_function(context["user"])
return render_to_response("some_url_name", {"some_var": some_val},
context_instance=context)[/code]
Now, seasoned Django vets (why are you reading this post, by the way?) are probably laughing, but this seemed perfectly fine to me. Luckily, my ignorance was revealed to me while asking on IRC about a problem which, while didn't seem so at the time, was completely tied to my misuse of RequestContext.
To put it simply, context processors are made to be used in templates. The only time they should ever be instantiated is at the very end of a view, like so:
[code lang="python"]return render_to_response("some_url_name", {"some_var": some_val},
context_instance=RequestContext(request))[/code]
I was using them all over views, and even in a few of my decorators. What's wrong with this? The main thing is, certain mutation functions are sometimes performed when a RequestContext is instantiated (such as user.get_and_delete_messages(), which gets all of a user's messages from the database and then deletes them), and performing them multiple times before loading the template can cause unexpected results. In my example, I was instantiating a RequestContext in a bunch of decorators, which meant that by the time my template was loaded, all of the user's messages were deleted (and stored in an earlier instance of RequestContext), making it look to me as if my auth messages were being thrown out.
What's the solution? Middleware. Middleware allows the programmer to attach variables to the request object before it reaches the view. With this (and the provided django.contrib.auth.middleware.AuthenticationMiddleware), I can still achieve my original goal (accessing the logged-in user from a view), but now I can do it without creating a RequestContext and potentially running mutation functions multiple times:
[code lang="python"]from django.template import RequestContext
# some other view code
def my_view (request, obj_id):
obj = get_object_or_404(SomeModel, pk=int(obj_id))
# do some stuff, including:
some_function(request.user)
return render_to_response("some_url_name", {"some_var": some_val},
context_instance=RequestContext(request))[/code]
I've removed all the references to RequestContext from my decorators, and made sure to only instantiate it at the very end of views. Now, messages work perfectly. I've even written my own middleware (which you can learn how to do here) to load instances of a few of my own models into the request.
To reiterate, I'm aware that this is common knowledge for many people, but it took me 9 months of moderate Django use and embarrassment on IRC to discover it for myself. Hopefully, this will help someone else in a similar position.
Sunday, May 10, 2009
Are you a better programmer than you were two years ago?
Two years ago, I was working on a fairly complicated (for my level of experience) web app for posting news articles. Quite a few times, I ran into situations where I was about to create tight coupling between two somewhat unrelated parts of my app. Sometimes, I wouldn't even recognize this as a problem. Other times, I would, but not be able to think of an easy fix, so I'd continue on.
Today, I find myself fixing tightly-coupled situations almost instinctively. I use design patterns that I was barely aware of two years ago, and I do it without straying into "design pattern fever" territory. This isn't to say I'm an expert at software design; it still takes a lot of thought to actually think of the best fix, and I'm sure I still make plenty of mistakes. My point is, my growth as a programmer is very obvious to me in these situations.
How about you? If you've been programming for more than two years, you're probably a better programmer than you were two years ago, but can you tell? If so, how can you tell? Can you give any good examples of moments when you realized it?
Today, I find myself fixing tightly-coupled situations almost instinctively. I use design patterns that I was barely aware of two years ago, and I do it without straying into "design pattern fever" territory. This isn't to say I'm an expert at software design; it still takes a lot of thought to actually think of the best fix, and I'm sure I still make plenty of mistakes. My point is, my growth as a programmer is very obvious to me in these situations.
How about you? If you've been programming for more than two years, you're probably a better programmer than you were two years ago, but can you tell? If so, how can you tell? Can you give any good examples of moments when you realized it?
Labels:
design patterns,
programming,
question,
tight coupling
Tuesday, May 5, 2009
Non-painful email on Django development servers
I've been actively learning and using Django since August 2008, and I've loved almost every bit of it. There are plenty of places to read all about the virtues of Django, so I'll leave that out for now.
One thing that's always bugged me about web development in general is the sending of emails. I do development on my local computer (with a badly set up Apache / MySQL / PHP / Python / whatever else stack), and I've never felt like dealing with the headache of setting up a mail server. This means, when I add something that's supposed to send an email (like an activation email after registration), I have to get very hacky to test and debug it (making sure the email text is being produced correctly, making sure it's being sent to and from the right people, etc.).
This was one of the few web development pains that I thought Django didn't solve. Whenever I'd test a bit of code that was supposed to send email, I'd get a "Connection refused" error page (meaning my computer has no mail server to send the email with). I would usually add in a bit of printf debugging to make sure the subject and body had the correct text, but beyond that, I'd usually wait to test the email portions until I uploaded to a server that could send email (usually the production server, unfortunately).
Yesterday, I bumped into a little section in the Django documentation that explains how to get around this. As usual, Python has all the solutions. First, set this code in your settings.py file:
[code lang="python"]EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025 # replace this with some free port number on your machine[/code]
Then, assuming you're on a Unix system (I'm on a Mac), run the following on the command line to start a "dumb" Python mailserver:
[code lang="bash"]python -m smtpd -n -c DebuggingServer localhost:1025[/code]
Make sure to replace 1025 with whatever you filled in for EMAIL_PORT.
Now, try running the email-sending code in your Python application. Voila! No error pages (or at least, none related to email), and the full text of the email (headers and all) appears in whatever command line prompt you ran the dumb mailserver on. This allows you to the see senders, recipients, subject, and body of the email being sent out, all without getting hacky or sending to an email account you own.
Taking this a step further, I created a small bash script called "dumbmail" in /usr/local/bin that looks like the following:
[code lang="bash"]#!/usr/bin/env bash
if [ -z $1 ]
then port=1025
else port=$1
fi
echo "Starting dumb mail server on localhost:$port"
python -m smtpd -n -c DebuggingServer localhost:$port[/code]
Now, when I'm testing a Django application and I get to a section that is going to send an email, I just run "dumbmail" (or "dumbmail some_number" if I need to use a different port, for some reason I can't imagine), and I'm ready to go.
Hope this helps people. The documentation was always there - I just never noticed that part until yesterday.
One thing that's always bugged me about web development in general is the sending of emails. I do development on my local computer (with a badly set up Apache / MySQL / PHP / Python / whatever else stack), and I've never felt like dealing with the headache of setting up a mail server. This means, when I add something that's supposed to send an email (like an activation email after registration), I have to get very hacky to test and debug it (making sure the email text is being produced correctly, making sure it's being sent to and from the right people, etc.).
This was one of the few web development pains that I thought Django didn't solve. Whenever I'd test a bit of code that was supposed to send email, I'd get a "Connection refused" error page (meaning my computer has no mail server to send the email with). I would usually add in a bit of printf debugging to make sure the subject and body had the correct text, but beyond that, I'd usually wait to test the email portions until I uploaded to a server that could send email (usually the production server, unfortunately).
Yesterday, I bumped into a little section in the Django documentation that explains how to get around this. As usual, Python has all the solutions. First, set this code in your settings.py file:
[code lang="python"]EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025 # replace this with some free port number on your machine[/code]
Then, assuming you're on a Unix system (I'm on a Mac), run the following on the command line to start a "dumb" Python mailserver:
[code lang="bash"]python -m smtpd -n -c DebuggingServer localhost:1025[/code]
Make sure to replace 1025 with whatever you filled in for EMAIL_PORT.
Now, try running the email-sending code in your Python application. Voila! No error pages (or at least, none related to email), and the full text of the email (headers and all) appears in whatever command line prompt you ran the dumb mailserver on. This allows you to the see senders, recipients, subject, and body of the email being sent out, all without getting hacky or sending to an email account you own.
Taking this a step further, I created a small bash script called "dumbmail" in /usr/local/bin that looks like the following:
[code lang="bash"]#!/usr/bin/env bash
if [ -z $1 ]
then port=1025
else port=$1
fi
echo "Starting dumb mail server on localhost:$port"
python -m smtpd -n -c DebuggingServer localhost:$port[/code]
Now, when I'm testing a Django application and I get to a section that is going to send an email, I just run "dumbmail" (or "dumbmail some_number" if I need to use a different port, for some reason I can't imagine), and I'm ready to go.
Hope this helps people. The documentation was always there - I just never noticed that part until yesterday.
Tuesday, April 28, 2009
Round Rectangles (or Why Steve Jobs is a Visionary)
A story was posted on Folklore.org (a site full of old stories about the creation and initial development of Apple computers) about the addition of rounded rectangles to an old drawing program, and Steve Jobs's involvement in them. This section in particular struck me:
I think this is a fantastic example of what makes Steve Jobs one of the few true visionaries in the world. In the face of a big advancement like fast ovals (yes, it was definitely a big deal at the time), I would've been more than satisfied. I may have asked for rounded rectangles in a second iteration, but it would've been a simple feature idea. I believe most people would've reacted the same way.
What makes Steve Jobs special is his ability to quickly identify what's really important. It seems so obvious after the fact. Of course people would like computers with translucent colored cases! Of course minimalist controls would make for a more accessible and desirable MP3 player! Of course rounded rectangles are an extremely common shape, and are really important to have in a drawing program! These ideas (and more) are all obvious now. But a year before Apple did them, other companies were struggling to innovate in these fields, and they were only "obvious" to Steve Jobs.
Of course, anyone can have a great idea that turns out to be the right way of doing things. This is why I distinguish between "true" and regular (false?) visionaries. People called M. Night Shyamalan a visionary after "The Sixth Sense" and "Unbreakable". He then went on to make one more pretty good but decidedly un-visionary movie (Signs), an arguably good but decidedly un-visionary movie (The Village, which I loved, but I understand why others didn't), and two duds (my apologies to the three people who liked them). The visionary label was applied to Shyamalan before he'd reached first base, and he got thrown out at second. This happens all the time, and these people are certainly not true visionaries.
True visionaries come up with visionary ideas so consistently, that it becomes expected of them. And no one (off the top of my head) has a more consistent history of this than Steve Jobs.
Bill fired up his demo and it quickly filled the Lisa screen with randomly-sized ovals, faster than you thought was possible. But something was bothering Steve Jobs. "Well, circles and ovals are good, but how about drawing rectangles with rounded corners? Can we do that now, too?"
"No, there's no way to do that. In fact it would be really hard to do, and I don't think we really need it". I think Bill was a little miffed that Steve wasn't raving over the fast ovals and still wanted more.
Steve suddenly got more intense. "Rectangles with rounded corners are everywhere! Just look around this room!". And sure enough, there were lots of them, like the whiteboard and some of the desks and tables. Then he pointed out the window. "And look outside, there's even more, practically everywhere you look!". He even persuaded Bill to take a quick walk around the block with him, pointing out every rectangle with rounded corners that he could find.
When Steve and Bill passed a no-parking sign with rounded corners, it did the trick. "OK, I give up", Bill pleaded. "I'll see if it's as hard as I thought." He went back home to work on it.
I think this is a fantastic example of what makes Steve Jobs one of the few true visionaries in the world. In the face of a big advancement like fast ovals (yes, it was definitely a big deal at the time), I would've been more than satisfied. I may have asked for rounded rectangles in a second iteration, but it would've been a simple feature idea. I believe most people would've reacted the same way.
What makes Steve Jobs special is his ability to quickly identify what's really important. It seems so obvious after the fact. Of course people would like computers with translucent colored cases! Of course minimalist controls would make for a more accessible and desirable MP3 player! Of course rounded rectangles are an extremely common shape, and are really important to have in a drawing program! These ideas (and more) are all obvious now. But a year before Apple did them, other companies were struggling to innovate in these fields, and they were only "obvious" to Steve Jobs.
Of course, anyone can have a great idea that turns out to be the right way of doing things. This is why I distinguish between "true" and regular (false?) visionaries. People called M. Night Shyamalan a visionary after "The Sixth Sense" and "Unbreakable". He then went on to make one more pretty good but decidedly un-visionary movie (Signs), an arguably good but decidedly un-visionary movie (The Village, which I loved, but I understand why others didn't), and two duds (my apologies to the three people who liked them). The visionary label was applied to Shyamalan before he'd reached first base, and he got thrown out at second. This happens all the time, and these people are certainly not true visionaries.
True visionaries come up with visionary ideas so consistently, that it becomes expected of them. And no one (off the top of my head) has a more consistent history of this than Steve Jobs.
Labels:
apple,
computers,
imac,
ipod,
m night shyamalan,
round rectangles,
steve jobs,
the sixth sense,
unbreakable,
visionary
Monday, April 27, 2009
A response to "The Extreme Google Brain"
A blogger named Joe Clark just made a post called The Extreme Google Brain. In it, he takes a side on the recent tiff between lead designer Douglas Bowman and Google, where the former left the latter out of frustration at having to prove every design decision with real-world test data.
Joe Clark whole-heartedly agrees with Bowman, as many others do (I myself am somewhat torn). However, I find Clark's article to be a ridiculous rant, full of stereotyping, fact-inventing, name-calling, and other marks of awful opinion pieces.
One frequently-used tactic in his piece is the inventing of a fact, followed by a single related fact that's supposed to prove it. Case in point:
Yes, males dominate engineering and high-end law. However, the cause is the topic of endless debates, and yet Clark claims it's due to "extreme-male-brain tendencies" like he read it in a science textbook. This "here's a fact I made up (hence an already-known, tangentially-related fact)" pattern repeats itself a few times.
When he's not making up facts, he's stereotyping a group of tens of thousands of people based on the few he knows.
You get the picture. Throughout the rest of the article, he:
There isn't really much else to say about this. I've read quite a few articles that side with Bowman, and quite a few that side with Google, and many of them on each side were great articles with great points. This was not one of them. I've never heard of Joe Clark before, and based on this, I hope I never do again.
Joe Clark whole-heartedly agrees with Bowman, as many others do (I myself am somewhat torn). However, I find Clark's article to be a ridiculous rant, full of stereotyping, fact-inventing, name-calling, and other marks of awful opinion pieces.
One frequently-used tactic in his piece is the inventing of a fact, followed by a single related fact that's supposed to prove it. Case in point:
Some of these boys and men exhibit extreme-male-brain tendencies, including an ability to focus obsessively for long periods of time, often on inanimate objects or abstractions (hence male domination of engineering and high-end law).
Yes, males dominate engineering and high-end law. However, the cause is the topic of endless debates, and yet Clark claims it's due to "extreme-male-brain tendencies" like he read it in a science textbook. This "here's a fact I made up (hence an already-known, tangentially-related fact)" pattern repeats itself a few times.
When he's not making up facts, he's stereotyping a group of tens of thousands of people based on the few he knows.
Apart from Bowman, I can think of only two Google employees I could stand to be around for longer than an elevator ride. My impression of “Googlers,” which I concede is based on little direct knowledge and is prejudicial on its face [note: apologizing in advance does not make it okay to say something idiotic], is one of undersocialized, uncultured, pampered, arrogant faux-savants who have cultivated an arrested adolescence that the Google working environment further nurtures. Their computer-programming skills, the sole skills valued by the company, camouflage the flaws of their neuroanatomy. Their brains are beautifully suited to the genteel eugenics program that is the Google hiring process but are broken for real-world use.
You get the picture. Throughout the rest of the article, he:
- Contends that A/B testing has no value.
- Makes up scenarios that he believes (and "speculate[s] that Bowman would not disagree") accurately represent Google meetings.
- Tells us that we can't disagree with Bowman and still feel that technology juggernauts are becoming better at visual design.
- Says that when a company uses anti-design (extremely minimal and not-necessarily-beautiful designs such as Google or Craigslist) and succeeds, they're succeeding despite the anti-design. He then concedes that can't prove it, but assures that if you're "visually literate" (which Adobe defines as the "ability to construct meaning from visual images", which anyone older than an infant can consistently do), you "just know it".
There isn't really much else to say about this. I've read quite a few articles that side with Bowman, and quite a few that side with Google, and many of them on each side were great articles with great points. This was not one of them. I've never heard of Joe Clark before, and based on this, I hope I never do again.
Labels:
adobe,
design,
douglas bowman,
extreme-male-brain,
google,
joe clark,
stereotypes,
visually literate
Sunday, April 12, 2009
Facebook causes me to facepalm
I looked at the "New Layout Vote" app on Facebook today. The wall is full of complaints about the new layout. While reading through them, I ran into this gem:

Ugh... honestly, how can she think that, without commenting, her picture is visible to everyone else looking at a page? Doesn't she wonder why she can't see everyone else's picture there as well?
And, as a side note, if she agreed to the Terms of Service (which she did if she has an account, which she does), then she definitely gave them permission to put her picture everywhere.
Ugh... honestly, how can she think that, without commenting, her picture is visible to everyone else looking at a page? Doesn't she wonder why she can't see everyone else's picture there as well?
And, as a side note, if she agreed to the Terms of Service (which she did if she has an account, which she does), then she definitely gave them permission to put her picture everywhere.
Subscribe to:
Posts (Atom)