Archive for the ‘Randomness’ Category

Water: Blue Gold

Posted 477 days ago by Alex in Internet, Randomness

Back in college, I remember my Geography professor saying that potable water (water that is safe for drinking) is the most valuable resource humans have. That if you wanted to make a billion dollars and save the world, solve Earth’s water problems. Today is a day to reflect on that.

Of all the water on the Earth, only 2.5% of it is suitable for drinking and less than 1% of that water is readily accessible to humans. This means that approximately 1 in 8 people don’t have access to clean drinking water. Here are some disheartening facts about water:

  • More people have access to cell phones than to toilets. As a result, tons of untreated human waste make their way to water sources causing a litany of diseases, and even death.
  • The US, Mexico and China lead the world in bottled water consumption, with people in the US drinking an average of 200 bottles of water per person each year. Over 17 million barrels of oil are needed to manufacture those water bottles, 86 percent of which will never be recycled.
  • 17% of the world’s population lack access to clean drinking water. This is improved from 22% in 1990.
  • Water shortages near water bottling plants have been reported all over, including Wisconsin, New Hampshire, Florida and Texas.
  • Countries that have made decisions to not permit the privitization of water include Uruguay, Malaysia and India.
  • The NRDC tested 103 bottled waters and found over 25% were simply tap water Several tested failed the bacteria test.
  • 1.5 million barrels of crude oil are used annually to produce the plastic used to bottle water.
  • 90% of the cost of a bottle of water is for label, cap and bottle.

Recursive JSON to URL function

Posted 666 days ago by Alex in Randomness, Software

While working on a snazzy new toy for Velocity, I needed a script to transform JSON to URL parameters. I found this clever method for doing it, but sadly, it wasn’t recursive, so it would only generate URL parameters for the first level of JSON elements.

I rewrote the function to recurse over all elements in the object tree and spit them out in a URL-safe string.

  var JSON = {
    params : function(a1){
      var u=[];
      for(x in a1){
        if(a1[x] instanceof Array)
          u.push(x+"="+encodeURI(a1[x].join(",")));
        else if(a1[x] instanceof Object)
          u.push(JSON.params(a1[x]));
        else
          u.push(x+"="+encodeURI(a1[x]));
      }
      return u.join("&");
    }
  };
var doc = {
  "env":{
    "platform":"linux",
    "browser":"Firefox%2F3.5.8"
  },
  "query":{
    "project":"user-tracking",
    "fetch_time":"0"
  },
  "user":{
    "locale":"en-US",
    "cluster":""
  },
  "last_modified":"1270872812"
};

Reloading SQLite data in Rails

Posted 669 days ago by Alex in Randomness, Software

While developing a new system built on rails, I encountered a peculiar problem. I wanted to reset my database, but keep the data that was currently living it it. This turned out to be a unique problem for the following reasons:

  • Rails has the functionality to ‘seed’ the database using the seeds.rb file and running: “rake db:seed” This is great if you already have the data written out in pretty rails code. If your data lives in the database, resetting it will wipe out the data.
  • You can only seed data if it has a model associated with it. I’m working with data that uses ‘has_and_belongs_to_many’, which requires a non-modeled table in the db to hold the many-to-many association. You can’t import these associations in Rails.
  • Looking online, I found a nifty rake task to dump the SQL data from the database to a file. You can then load it into the current environment’s db after you’ve done your magic. I found that over at Agile Web Operations. The only problem is, it doesn’t work for sqlite. :(

So I did what any self-respecting developer would do… I rewrote it to do what I needed.

My new rake task implements the following changes:

  1. Works with sqlite3
  2. Only saves the ‘INSERT’ statements from the dump. No need for the schema info from sqlite since it’s already in rails.
  3. Fixes a bug where data loaded was always being loaded into the test db. I like flexibility to load into any environment.

Save the following code into /lib/tasks/seed.rake:

namespace :db do
  namespace :seed do
    require 'db/seed_tables'

    desc "dump the tables holding seed data to db/RAILS_ENV_seed.sql. SEED_TABLES need to be defined in config/environment.rb!!!"
    task :dump => :environment do
      config = ActiveRecord::Base.configurations[RAILS_ENV]
      dump_cmd = "echo '.dump #{SEED_TABLES.join(" ")}' | sqlite3 #{config['database']} | fgrep INSERT > db/#{RAILS_ENV}_seed.sql"
      system(dump_cmd)
    end

    desc "load the dumped seed data from db/RAILS_ENV_seed.sql into the test database"
    task :load => :environment do
      config = ActiveRecord::Base.configurations[RAILS_ENV]
      load_cmd = "sqlite3 #{config['database']} < db/#{RAILS_ENV}_seed.sql"
      system(load_cmd)
     end
  end
end

And, like the previous incarnation, load up the names of the tables you want to dump into a file at /db/seed_tables.rb:

SEED_TABLES = {
  "accounts",
  "products",
  "users"
]

Here’s an example of how I used it:

rake db:seed:dump
rake db:reset
rake db:seed:load

Multi-domain WordPressMU Made Easy

Posted 688 days ago by Alex in Randomness, Software

So I was fighting with WordPress MU for about 6 hours today attempting to get it to properly serve blogs for multiple domains and paths. This seemed like it wasn’t going to work since WPMU is configured to use either subdomains or subdirectories and all multi-domain solutions seem to only work when using subdomains.

Somehow, through the magic of mod_rewrite I was able to get multiple domains working while using the ‘subdirectory’ configuration in WPMU. The only problem I ran into was that I was unable to login to wp-admin in the non-default domains. I realized this is because of the ‘domain’ defined in the cookie. To remedy that, I changed a single line in my wp-config.php to the following:

define('DOMAIN_CURRENT_SITE', $_SERVER[ 'HTTP_HOST' ] );

That tells WPMU that the current domain is the one PHP sees. Surprisingly this fixed the cookie issue, and hasn’t triggered any other errors. We’ll see how it holds up.

Pittsburgh Winters are Getting Old

Posted 758 days ago by Alex in Randomness

So it’s been snowing here pretty much non-stop for the past two weeks. While I occasionally enjoy shoveling snow, it’s getting a bit tedious. It’s also thoroughly annoying that other homeowners in the area don’t shovel their sidewalks. Come on people, I’m walkin’ here!

And that light half-rain, half-snow BS that was coming down last night… that needs to go away and not come back. Driving around with that freezing on my windshield is no bueno.

I thought global warming was supposed to make the North Pole a balmy 75°F year-round. What happened Mr. Gore?

Game on! Resolutions for a new decade

Posted 762 days ago by Alex in Randomness

Ok, so bite me. I haven’t updated this beast of a blog with posts in forever and a day.* That will change… starting, now!

This super-awesome girl I know resurrected her blog with a ‘New Year’s Resolutions for 2010′ post, so I guess I will do the same. I’ve never been one for resolutions but this is a new decade, so it’s as good a time to start as any. Roll the list!

  1. Do more than 30 minutes of exercise 3 times per week. I’m currently doing simple calisthenics in the morning everyday, but they take less than 5 minutes and are hardly a workout.
  2. Be more spontaneous — a little spontaneity never hurt anyone. Will planning to be more spontaneous cause a temporal rift in the space-time continuum? I hope not.
  3. Read more for pleasure. Reading about current events too often makes me too pessimistic. I would like to find a good fiction series to delve into.
  4. Become more proactive about maintaining friendships. This means calling distant friends/relatives and bugging people to hang out more. Going for beers is a good time.
  5. Start writing/painting/drawing/playing more. Note to self: Start blogging more often. Start a journal. Learn a new instrument (Rock Band doesn’t count).

Ok, there. It’s out on the Internet for all the world to see. That means it’s true and I have to abide by it.

Note to self: Fix CSS up in here. It looks like sh*t.

* I have been a good site admin and have been upgrading the WP backend regularly. :P