Installing pip, package installer for Python, on Mac.

In my last post on Installing memcached for Django I mentioned pip as a very easy way to install python-memcached, a module I use with Django to enable caching of the dynamic content.

Before pip installation you have to make sure you have distribute on your system. Here’s how to install it, given that you have python (remember, you don’t need to type the dollar sign in the terminal):

$ curl http://python-distribute.org/distribute_setup.py | python

Once the installation script is done, go ahead for the next one:

$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

Super easy! Now get yourself some ice cream.

Installing memcached for Django

Memcached is very popular caching system with API available for many languages and environments. Today I installed it on my OS X development server that runs Django. These instructions might be helpful if you’re up to the same task.

Memcached needs an event notification library libevent to run. After downloading the source code from libevent public repository go ahead and unpack it. Open terminal in the folder you have unpacked the source code and type

./autogen.sh
./configure
make
make verify
sudo make install

This should be it for libevent.

Now to the memcached.

curl -O http://memcached.org/latest
tar -zxvf memcached-1.x.x.tar.gz
cd memcached-1.x.x
./configure
make && make test
sudo make install

And last but not least you will need Python-memcached installed. It’s very easy if you have pip and I highly recommend getting it if you don’t. Here’s the line

pip install python-memcached

This is really easy if you don’t run into errors. Which I did when I tried to install it on a hosted server. I didn’t get past ‘make verify’ command for the libevent. I’ll have to do some digging and I will give you a report on my progress. Keep reading.

Before you buy, Econofy.

It’s been a wonderful weekend. I’m very thankful to guys at CleanWebHack and all the sponsors.

It all started in April 2011 on Pachube International Internet of Things Hackathon. Will Wagner came up with an idea of quantifying a living space in terms of energy consumption and finding ways to improve. He was there with his friends Brandon and Paul, whom I met at nyhacker townhall event and I jumped in. We ended up as a single team to finish the hackathon in NYC (it was really big in London as we saw through a video conference call.) Even though we had a working app after 15 hours of non-stop hacking and coding, it wasn’t enough to see that Econofy’s great potential.
econofylayoutalpha

Later we decided to use Amazon Advertising API to get hand on the wide range of appliances and electronics. Will put a lot of time in scraping the Energy Star spreadsheets to get the data on their energy efficiency. It was getting somewhere, but something was missing. Some glue that holds it together.

After a few google chat brainstorm we settled to implement star ratings for products based on their energy efficiency and roll out the ROI analysis some bits and pieces of which we already had in the codebase.

e-star

2.9 e-stars

ROIanalysis

Now we have humongous amount of work to do and it’s best news for me. Econofy won an audience award and best overall award of CleanWebHack, which means that it is something that people actually want to have. And we really want to build it.

Installing WordPress on a Mac over SSH, Part 2.

If you followed Installing WordPress on a Mac over SSH but for some reason you need to have 2 or more blogs hosted, this post is for you.

If something is not working as you’d expect take a look at Installing Multiple Blogs at the WordPress Codex for additional reference.

Using instructions from the first part go through steps 1 and 2 to get a new installation of wordpress. It may sound counterintuitive but that’s the way you do it, a new installation for every new blog.

Now instead of creating a new config file from the sample and editing it, you can copy an existing one. There’s a trick though, you have to edit the table prefix, so your new installation knows what tables in the database it needs to create and use.

cp ~/Sites/existing_blog/wp-config.php ~/Sites/new_blog/
vi ~/Sites/new_blog/wp-config.php

Once in the editor (use your editor of choice if you’re not familiar with vi), find $table_prefix and replace wp_ with something that your new blog will use. I replaced it with bd_ for a friend’s bopdigger | Thoughts on Improvisation and Music blog.

When you’re done with configuration go ahead with the step 4 to edit hosts file and virtual hosts for Apache. You won’t need step 5 as you already have the user and database set up. Last step is changing the DNS records and you’re done.

Who burst the Clean Energy Bubble of the 2000s and what do we do now.

I just got a new issue of Wired and have read an article on renewable energy. Turns out a lot of people (mostly rich people, so no worries here) lost money pursuing dreams of clean energy. None of the favorites of this race – Solar, Wind, Algae, Biofuels, Fuel cells – crossed the finish line first. The one that did was Natural Gas.

Natural gas is not renewable in conventional sense of the word. But it is much than oil and coal. And it turns out 24% of US electricity comes from natural gas. It’s also a most popular heating source.

A few interesting things about natural gas -

Shale gas
Recently US became a leader in production of shale gas. Experts say the drilling companies exaggerate the benefits and potential, but it looks pretty good – they say the whole country could be powered for 100 years on the shale gas alone.

Here are some projected basins (worldwide, but some countries are not included in the report) – shale gas

Edit: as pointed out by William, the shale gas is very bad for water resources.

CNG-powered vehicles
Those are cool. Both my dad and my brother had converted their cars at the beginning of the 2000s to run on natural gas as well as on petroleum. And it was in Russia. There weren’t many filling stations but it was cheaper and cleaner. I always wondered why this thing never took off in the US.

There is more then hundred thousands buses here that run on natural gas though – CNG powered bus

Escaping gas
Technology is seldom perfectly efficient and we don’t get all the gas that’s out there and we can’t use it all for good cause. Some gas will escape.

There’s always gas where oil is. When the oil is the main target, a lot of natural gas gets wasted. There’s also blowouts (or gushers). One of the worst gushers happened in 1985 in Atyrau, Kazakh SSR, Soviet Union erupting about 60 bln cubic feet of natural gas. I pay one buck for a cubic feet to heat my home.

Summary
Although I believe in renewable and clean energy I propose we don’t loose focus on the old sources of energy and make them more efficient while making the energy consumption smarter. Let’s innovate both for tomorrow and for today.

If This Then That.

If you knew about a new post on my blog from twitter, it’s because of this -

ifttt

If you came here from facebook, it’s due to a task that I created on ifttt.com that looks like this -

ifttt-fb

Thanks to IfThisThenThat I can connect my Instagram to my Dropbox, get emails about expected precipitation (gets quite handy when you ride a motorcycle to work), get a text message when something rare comes up on craigslist (like a first generation Honda Insight with a CVT transmission).

You can create your own tasks or use recipes made by others. My favorites are Text to escape, Note to self and Help me find my lost phone!

I haven’t been able to hook it up with the stocks yet. Also, after I set it up to thank all my new followers on Twitter, I found that those are mostly bots.

What do you use ifttt.com for?

Praise the dd, I saw the light. Cloning disks with Linux and dd.

I saw the light, I saw the light,
No more darkness, no more night…
- Hank Williams, “I saw the light”

I consider myself a lucky fellow. But not until today it extended to relationships with hard drives. They always failed me. And some of you might know The Cloud wasn’t always around.

Last week I was asked to rescue a couple of computers with soon-to-die HDD’s. I had a thumb drive with Clonezilla Live on it and I tried to clone them. It would go very slow, showing somewhat around 400kb/s (?) and then spur a lot of text in the command line finally freezing the system.

clonezilla errors

Last friday before calling it a day I tried to boot from one of the original drives and it didn’t go. I had assumed the Clonezilla ruined it somehow and decided not to fiddle with mechanical disk drives again.

Today I found a copy of Ubuntu Live on another thumb drive and decided to give dd a try. I’ve read Using DD for disk cloning a while ago and I have played with copying blocks from a disk or /dev/random to /dev/null, but never had a chance to clone physical hard drives. It really turned out to be as easy as typing in terminal the following command

dd if=/dev/sda of=/dev/sdb

You might need to put sudo in front of it if you’re not root. Most importantly, you have to double check that if (input file) and of (output file) are not misplaced. ALL DATA on sdb will be lost (technically, if sdb is bigger than sda, some of the data will remain, but that’s not what you’re aiming for.) To check which drive is source and which should be target go back to terminal and use hdparm

hdparm -i /dev/sda
hdparm -i /dev/sdb

This should give you enough information to identify the drives. By the way, your disks could be different from sda and sdb. To find out what they are type

fdisk -l

When you finally decide to hit enter and launch dd you might begin to wonder, why there’s no progress indication. Don’t worry. There’s an app for that. Just open another Terminal window and use

ps -a | grep dd

to get an Id of dd’s process. In my case it was 5643. Replace $pid with this value in the next command

watch -n 10 $pid

and you’re good to go. Every ten seconds dd will report on its progress.

dd progress

There’s more tricks behind the dd’s belt. I used bs=8096 and conv=noerror arguments when I ran it, but I’m not sure how it affected the outcome. Next time I’m going to try to pipe it to itself for a speedup. As an asymmetrical copying program, dd reads first, then writes, then repeats. Supposedly you can gain 100% in performance if copying symmetrically, like this

dd if=/dev/sda | dd of=/dev/sdb

I hope you’re now ready to join the club of dd believers.

PureFTPd on Snow Leopard

I’ve spoken before about Installing WordPress on a Mac over SSH, and if you have an older mac with Leopard or Snow Leopard acting as a web server you might set it up to serve ftp as well.

Unfortunately, OS X’s built-in ftp server lacks in functionality and flexibility even if you have a server edition of the OS. Fortunately, there’s an open source alternative. It’s called pureFTPd.

There’s a similar blog post – pureFTPd and Snow Leopard. It gave me some insight but there’s many flaws in the instructions. Let me try to improve on it.

If you don’t have MacPorts go ahead and install it. It’s a very useful tool. Once you’re done with that, type

sudo port install pure-ftpd

in terminal. It tells you to test the server with “ftp localhost”, don’t do it just yet. First you now need to set up the users and the start the server (this is the recommended way)

pure-ftpd &

Now we need to add a user in OS X Preferences Accounts Pane. Make sure you choose Sharing Only from the pick list.
Adding FTP User

PureFTPd has a great support for virtual users, which means you don’t need to add system users to allow for more ftp accounts. Here’s a reference from pureFTPd documentation. To add a virtual user go back to terminal and type

sudo pure-pw useradd testuser -u ftpuser -d /Users/ftpuser/testuser

You will be asked for a password twice. Note, you might need to replace ftpuser with your own value you chose when creating a system user. Also, the user is not actually added until you commit your changes. Here’s how you do it

sudo pure-pw mkdb

You should be good to go now. Let me know if you have any problems running pureFTPd on Snow Leopard.

Using Batch Apex

There’s a page on the web with the same title – Using Batch Apex. I tried to digest it in one sitting and it proved to be somewhat hard. So I went to Stack Overflow for help.

If you’re having the same problem, check out Dummy updates of Salesforce database records and execution governors. Nothing beats a simple working example. Just make sure you have an object called Acme_Portfolio__c with an Id field.

I’d like to elaborate on getting the most out it. Let’s say you just need to trigger updates. And you can use

Database.executeBatch(new batchClass());

to execute the batch and then check the results in Setup | Monitoring | Apex Jobs. What if you want to schedule it and get some kind of report on the executed (or failed) batches?

Turns our it’s pretty easy. Just wrap it in a class that implements Schedulable interface.

global class batchSchedule implements Schedulable
{
global void execute(SchedulableContext SC)
{
Database.executeBatch(new batchClass());
}
}

Now you can use the finish() function to add reporting functionality.

global void finish(Database.BatchableContext BC)
{
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id = :BC.getJobId()];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Update of Acme Portfolio ' + a.Status);
mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

The code is pretty much self explanatory and doesn’t require any changes to work. It pulls out all the stats on the executed batches, then gets an email of the user who created the class. Messaging.sendEmail() takes care of the rest.

Hope this helps someone gets a quick hold of Batch Apex.

Efficiency of Locomotion

Steve Jobs makes a great point about computers increasing human brain efficiency just like bicycles increase the efficiency of locomotion.

Interestingly there is some locomotion involved while working with computers. Mostly hand movements and eye movements. It’s obvious that keyboard is superior to mouse in this respect, you don’t need to move much when you type and you do when you, for example, scroll around and copy/paste text using contextual menu.

Not that we don’t need motion. We certainly do and I love workouts. But there are times when you need more concentration and mousing around doesn’t help.

You probably know a lot of those who praise Vi/Vim as a best editor. I think of joining them. Check out how it looks like
vi editor

Not only editing text or code in Vi doesn’t involve mice but even arrow keys are not in favor. Moving around the text gets done by the middle row keys, which require the least hand motion possible.

Another great example of an app that’s designed for keyboard. It’s Notational Velocity – a very simple and incredibly useful tool for note taking. Here’s a screen shot

And the last app I wanted to mention is Alfred. It lets you find files, launch applications and much more just by typing parts of their names.

Please let me know what tools you use.