Oh Well
Adobe makes flash searchable. 1/2 ain’t bad.
Adobe makes flash searchable. 1/2 ain’t bad.
AKA string formatting, AKA Putting values of variables into strings, AKA that thing you do with the funny symbols to get the values of variables inside strings.
At the most basic level, most programmers find that at some point they need to insert the values of a whole bunch of variables into a string. The naive approach is generally to concatenate a bunch of strings together.
>>>first_name = 'Zack' >>>last_name = 'Halbrecht' >>>output = "Hello " + first_name + " " + last_name >>>print output 'Hello Zack Halbrecht'
Besides being inefficient to concatenate strings this way, what if one of the variabes you are trying to interpolate isn’t a string, but an integer? Then you’ve got to go and cast each variable using str()? Ugly. Ew. I can’t even write the code demonstrating this out of priciple. Luckily, there’s a better way. Well, there are actually two.
The first one basically dictates that you are going to create a string, and within that string you will put in placeholders where you want your variables to end up. The placeholder marker is a combination of the percent symbol (%) and a conversion type symbol which is going to determine how a variable’s value is formatted within the string. More on this later. After the string, you use the string formatting operator (% again) and pass it a tuple of the variables you want to appear in the string, in the order you want them to appear.
>>>output = "%s %s" % (first_name, last_name) >>>print output 'Zack Halbrecht'
Damn. That’s concise. So what’s going on here? %s is a placeholder for a string. The first %s will get replaced by the first value of the tuple in this case, the value of first_name. Same with the second %s and the second value of the tuple. This is great when you’ve got less than five variables to interpolate, but what if you have 10? 20? Some huge amount? You don’t want to worry about the ordering. Well, of course there’s an easy way to deal with this situation too.
>>>output = "%(fname)s %(lname)s" % {'fname': first_name, 'lname': last_name}
>>>print output
'Zack Halbrecht'
OK. Same concept. Except this time we’re giving our % operator a dictionary of name/value pairs and our output string contains identifiers that act as lookup keys on the dictionary. Cool. We no longer have to worry about ordering. When using this method, every placeholder inside the string MUST have a parenthesized identifier. Another benefit of using this method is that if you want to insert the value of a variable in more than one place, it only has to be specified once in the dictionary. Sweet.
Oh duh, and one of the coolest things about this is that interpolation will actually do type coercion. So if you’ve got something like:
>>>output = "%(fname)s %(lname)s is %(age)i" % {'fname': first_name, 'lname': last_name, 'age': 28.5}
That %i is telling the interpolation operator to take the value of the age variable, whatever it is, in this case a float, and convert it into its string representation as an integer (using the %i). Pretty sweet. Normal concatenation with + will just get you a nice fat ValueError there.
>>>print output 'Zack Halbrecht is 28'
I realize a lot of this is covered in the truly wonderful Dive Into Python, but I was surprised to learn that the technique of using a dictionary instead of a tuple wasn’t covered. Additionally it seems that when just starting out programming in general, not a lot of people know this process as ‘string interpolation’ or ‘string formatting’, and it might help someone out there to have an article that covers it as one of the silly ways its described in the intro. Thoughts?
I’d really like to see Martin Yan (of Yan Can Cook fame) take on Iron Chef Chen Kenichi (Iron Chef Chinese). This would be a battle of epic proportions! Make it happen foodtv.
At Home:
At Work:
If you blow a tech interview because you are greatly under qualified and honestly don’t know the things required for the job, but they still give you an offer anyway, DON’T TAKE THAT JOB. It’s a trap. It most likely means the people hiring you are less competent than yourself and you’re going to be doing all the work.
In some cases, this might be OK though. If you are straight out of school and you don’t have “real world” experience, this might be a great way to get a lot of it very quickly. The thing to keep in mind when going this route is to try to learn the proper way to do your duties (training, researching, books, etc…), and then instilling those best practices into the organization.
Over the past five and a half years, I seem to have been building from the sub-basement up, but I feel like I’ve accomplished quite a bit in terms of infrastructure and software development practice that this company will be much better off with after I’m gone. I mean come on! Before I started here no one was using version control! Argh. At least it only took one major disaster to convince the CEO we needed to get off our butts and put some common sense things in place. Here are the things that I’d recommend you set up immediately, if they are not already in place when you get there.
Those are the top four I can’t imagine living without again, but if I find myself in this position again, depending on the influence my role has, I’ll be hooking this stuff up, or running for the door.
I might stop by from time to time to see what’s going on, but I’m out. This is just total bullshit.
It’s pretty funny that we actually have Microsoft to thank for the original XHR. Makes you wonder what else is hiding in their R&D labs.
This guy is so great. First JSON message failed because of JavaScript limitation on not allowing reserved keywords as name part of object literals.
Great post over there today. My favorites have been the Every Turbo Boost in Knight Rider one and The Big Lebowski – The Fucking Short Version.
This Greenhouse business is ridiculous. Most downloader-type applications are. Do I really have to:
Has the world gone mad? I should be able to log on to a website, see a list of games I’ve purchased, and download them indiscriminately.
On the good side, it’s nice that they are thinking about OS X and Linux from the get go. Well, OS X anyhow.
© expletive inserted. Powered by WordPress using the DePo Skinny Theme.