Page 16 of 28
Folding in vim
If you work with large files, in particular large markup documents vim’s folding feature can make your day a whole lot better. Just like the fancy folding that IDEs like Eclipse and Visual Studio provide, vim can reduce whole sections of text to a single line making it easy to skip around and keep your attention focused on what is important. There are a number of different methods that vim can automatically figure out where to fold your file but I’ve found that indent
does what I want most of the time. To turn folding on and off easily, I wrote the following function in my ~/.vimrc
set foldmethod=indent set nofoldenable function ToggleFolding() if &foldenable set nofoldenable else set foldenable endif endfunction
I bound this to <F2> with the following:
nmap<F3> :call ToggleFolding()<CR>
You can open and close folds using zo
& zc
and jump between them with zk
& zj
.
Profiling Perl, part 1
An important and often-overlooked part of programming is profiling of code to find out where the most time is being spent and then figuring out how the resulting sections of your program can be improved. This aspect of writing code gets little attention from people writing user-bound applications because generally, computers are fast enough that even atrocious code can complete before the user is ready to respond. Despite this fact execution speed is still important, especially in data processing code such as that which parses large datasets (which I often find myself doing) or converts videos, for instance. If your code consists of a number of nested loops, you can probably benefit from profiling it and speeding up the most oft-used pieces. Even if that doesn’t apply, timing the execution of statements will certainly improve your understanding of which operations are fast and which drag you down.
The very simplest form of profiling code is using manually activated timers. In Perl, this is accomplished with the Benchmark::Timer
module as follows:
use strict; use warnings;use Benchmark::Timer; my $timer = Benchmark::Timer->new();
$timer->start('overall');
$timer->start('single'); sleep 5; $timer->stop('single');
$timer->start('loop'); for (my $x = 0; $x < 5; $x++) { sleep 1; } $timer->stop('loop');
$timer->stop('overall');
print "The single 'sleep' took " . $timer->result('single') . " seconds.\n"; print "The 'sleep' loop took " . $timer->result('loop') . " seconds.\n"; print "Overall, this took " . $timer->result('overall') . " seconds.\n";
Which, when run, produces something like the following output:
~$ ./foo.pl The single 'sleep' took 5.002198 seconds. The 'sleep' loop took 5.020094 seconds. Overall, this took 10.022404 seconds.
Easy and quick. Next time, Devel::DProf.
More sed
So, if you come from the world of Perl, like I do, you’re probably very familiar with the usage of regular expressions to match (m/(foo)bar(baz)/) and replace (s/foo(bar)/baz$1/) strings. sed
acts upon lines and often you want to delete a range of lines. For the following regex, I had a document something like this:
<Recording modeType="foo"> <foo>bar</foo> <baz>qux</baz> </Recording> <Recording modeType="fp"> <foo>bar</foo> <baz>qux</baz> </Recording> <Recording modeType="bar"> <foo>bar</foo> <baz>qux</baz> </Recording>…and I wanted to remove any of the blocks of
modeType
‘fp’. To do so, I tell sed
to find lines with the modeType
of 'fp’, and a subsequent line with a closing 'Recording’ tag and then ’d'elete them all:
sed -i -e '/modeType="fp"/,/Recording>/d'
Simple. The more you get to know it, the more awesome sed
is.
Perl's Taint Mode
Perl is a great language for web programming and one of the features that makes it well suited is Taint Mode. Perl’s Taint Mode, quite simply, restricts what the programmer can do with variables that are acquired from user input. If a programs relies on the user to specify something, it’s a bad assumption to treat those values as safe for use in operations that may affect the outside world, such as opening files and using tainted values in system()
calls. Perl wont restrict you from using tainted values in innocuous things such as print statements but it does pass taintedness on to other variables that a tainted value touches. Furthermore, modules such as DBI support recognizing taintedness of variable for safety.
To learn more, check out these articles:
Chevrolet HHR Panel
It’s not often that I praise any of the American auto manufacturers, least of all Chevrolet. This isn’t because I’m a European car fanboy or otherwise prejudiced against American cars but simply because they churn out so much crap. The chronic problem for manufacturers is that of least-common-deonminator, good-enough, penny-pinching engineering.
Though I’m a little bit behind the ball, Chevy announced that they would be making a panel-van version of their retro-styled HHR mini-SUV; this is the first good idea to come out of the C-shop in a long time. Small businesses have needs to transfer things just like larger ones. Plumbers and contractors have a reason to have a full-size van, since they want to carry around a lot of tools and spare parts. If you’re not into construction, however, that’s a bit much. Beyond being more vehicle than you want to buy and getting terrible gas mileage, it’s difficult to park and maneuver. Sure, you can get a minivan for your small business but that leaves you little area to put logos and it’s also too low, down below the windows. Furthermore, a minivan is still often more space than is necessary. So you could look at small hatchbacks such as the Mazda 3 or Honda Accord which are nice, but a little bit too small and still offer little space for advertising on the outside.
The HHR is just the right size; it’s Goldilocks’ preferred size, providing enough room to care somewhat bulky items (flowers, small catering gear, computers), a few cubbys for tools and some good space for plastering a logo on the outside. I really think this is perfect for small business like florists, computer support and even someone like Charm City Cakes, who are often on Food Network transporting a wedding cake in a huge Econoline van.
Interfacial Fracture of Oreo Cookie Filling
While reading the Wikipeidia page on adhesive I came across this nugget of wisdom on food decomposition:
Another example is when someone tries to pull apart Oreo cookies and all the filling remains on one side. The goal in this case is an adhesive failure, rather than a cohesive failure.
Apparently, when you’re trying to break apart an Oreo and get all the filling to stay on one side, say, to make a quad-stuf (yep, only one ‘f’) Oreo, you are hoping for an adhesive or interfacial failure, that is, a failure of the adhesive (white, corn-syrupy goodness) where it meets the adherent (black cookie piece).
Also, for another barely-related angle is this page from a UFC forum discussing the caloric content of foods someone mentions their method of making Oreos less bad for you:
i just want everyone to know this lil diet trick i use to cut calories out of my double stuff oreos, if you take 2 of them apart and make 1 quad stuff oreo you eliminate 2 of the chocholat cookies there for cutting out calories while you enjoy a nice diet quad stuff oreo
Though I’d say the perfect ratio of filling to cookie is somewhere between 2 and 3 to one, I often make quad-stuf monsters.
Changing default paper size in Ubuntu
Ubuntu’s default paper size is the very international A4. Unfortunately, here in the United States the standard size of paper is Letter (8½ inches by 11 inches). When trying to print, Ubuntu will tell printers to use A4 paper unless you select ‘Letter’ in the print dialog each time. To change this default, simply run the following command which will set the value in /etc/papersize
:
sudo dpkg-reconfigure libpaper1
Cell Phone Premonitions
The Freakonomics Blog mentioned a story about mobile phone providers knowing that something was happening near the I-35 bridge in Minneapolis due to hugely increased call volumes. Later in the article there is also a mention of T-Mobile tracking the change in commuting patterns by the movement of cell phones.
Cell phone activity, just like any side-channel attack, can be exploited to learn a lot about a population. There are certainly legal and privacy implications to such data being made public, but it would be amazing to look at from a research point of view.
4 a.m.
The TED conference this year featured a great talk by Rives who spun an amazing tale about the use of the time 4 a.m. in literature, music and history.
Tokyo Ska Paradise Orchestra
Really, what other band will you ever see with a sousaphone and steel drum on stage?