Thursday, June 5, 2008

Moved.

I've moved this blog to my hosting environment.

Friday, April 18, 2008

^ of ^

A colleague recently asked for my help regarding some data he had entered into a local MySql database. He needed to update some 60k records and had 4 separate CSV files containing the raw data. He wrote a (bad) perl script which was taking too long to convert these CSV files into simple update statements - he later fixed his script to make it execute a lot quicker.

His update statement was something like:

UPDATE table_name SET column1 = 'csv_value_4' WHERE column2 = 'csv_value_3' and column3 = 'csv_value_1';

The first thing I thought of was to use a regular expression and my favorite text editor to do a quick search and replace.

Now I know there's a lot of ways to do this, and there's a lot of regular expressions you could probably use.

Assuming the data is mostly clean, and you don't need to worry about commas in between quotations. You could search for the words between each comma which might look something like, "(\w+)," (I'm using the Java Regex format here btw).

However, I think a lot of people forget that regular expressions have the ^ (not) expression. With that in mind, and (again) not worrying about commas inside of quotations you can create this simple regex to find anything between commas, "([^,]*)," which basically says, group anything that's not a comma before a comma.

In his CSV there were 4 values, so my regex was, "([^,]*),([^,]*),([^,]*),([^,]*)\n" (he had some empty values).

My replacement expression was:
UPDATE table_name SET column1 = '$4' WHERE column2 = '$3' and column3 = '$1';\n

I just opened each of his CSV files in jEdit and did a search and replace - Power of Not.

Thursday, February 14, 2008

Agile development and a Wiki.

A quick search on google will land you plenty of results regarding this topic.

A lot of people use Wiki's for their documentation and I wish we could where I work ( I'll get them yet).

Here's one thing that I think many people miss about the whole Wiki idea, and it's something soooo very basic, you're going to laugh.

It's such a basic feature of EVERY Wiki that I think sometimes people forget.

Wiki's are designed to be modified by a group of people, and they are designed to be modified by that group of people OVER TIME.

A Wiki is the perfect agile tool in my opinion, and it can dramatically help in the development of anything (I think).

Consider this extremely simple scenario.

YOU, and I mean YOU, have this really great idea. It's general, it's festering in your brain, and you still need to work out the little bits.

So, you create a wiki. You start off by writing out your basic idea - all the stuff that's been festering for a week now, and then you invite some of your most trusted friends to view it and edit it.

Joe (everyone has a friend named Joe) sees a small part of your idea that he has an idea about himself, so he modifies your Wiki to include his ideas about your idea.

Bam! Now you have an even better and (perhaps) more complete idea.

This can go on for days, weeks or months. Hopefully when it's all "done" ('cause who knows if ideas can ever REALLY be completed), you have this extremely great idea that you and your friends thought up together.

Easy, and you haven't done anything that ALL Wiki's provide out of the box.

Friday, February 8, 2008

Fun with Ant and PulpCore

The company I currently work for uses Maven as their build tool, and it's been quite a while since I've actually used Ant.

Which is sad! Ant is an awesome tool.

I'll be honest, I haven't really delved into Maven enough to say I have the knowledge of it as I do Ant so I don't know if everything that is available in Ant, is available with Maven.

However, I was recently pointed to a great little project started by David Brackeen called PulpCore.

I joined the mailing list and came across a post with a tutorial for setting up a "Quick" PulpCore project of your own.

David has done something really cool with his binary downloads for PulpCore. When you download a binary for PulpCore and unzip it, you get the PulpCore framework, and a couple of quick setup templates which you can use to create your own projects.

Having an itch to play around with Ant again, I thought I'd try to see if I could create an Ant script to essentially do what the tutorial did.

I had a ton of fun, and I think I'll be bringing this up with my superiors at my current company to see if we can use Ant in other areas (since they're pretty stuck on Maven).

Anyway, my script only handles creating the project, you'll need to refer to the tutorial if you want to go further.

Here is the script:

<?xml version="1.0"?>
<project name="PulpCoreTemplates" basedir="." default="usage">
<property file="build.properties" />

<!-- PulpCore properties -->
<property name="pulpcore.home.dir" value=".." />
<property name="pulpcore.jars.dir" value="${pulpcore.home.dir}/build" />
<property name="pulpcore.templates.dir" value="${pulpcore.home.dir}/templates" />
<property name="pulpcore.templates.quick.dir" value="${pulpcore.templates.dir}/quick" />
<property name="pulpcore.templates.project.dir" value="${pulpcore.templates.dir}/project" />
<!-- this is a hack to get the actual path to pulpcore home and pulpcore jars directories -->
<!-- These temp files aren't actually created -->
<tempfile property="pulpcore.home.dir.tempfile" destDir="${pulpcore.home.dir}" />
<tempfile property="pulpcore.jars.dir.tempfile" destDir="${pulpcore.jars.dir}" />

<dirname property="pulpcore.home.path" file="${pulpcore.home.dir.tempfile}" />
<dirname property="pulpcore.jars.path" file="${pulpcore.jars.dir.tempfile}" />
<dirname property="pulpcore.templates.quick.path" file="${pulpcore.templates.quick.dir}/build.xml" />
<dirname property="pulpcore.templates.project.path" file="${pulpcore.templates.project.dir}/build.xml" />

<target name="pulpcore-directories">
<echo> pulpcore.home.path: ${pulpcore.home.path}</echo>
<echo> pulpcore.jars.path: ${pulpcore.jars.path}</echo>
</target>

<target name="usage" depends="pulpcore-directories">
<echo> quick - Sets up a new project using the "quick" template.</echo>
<echo> project - Sets up a new project using the "project" template.</echo>
</target>

<!-- Quick Project Target -->
<target name="quick" depends="pulpcore-directories">
<!-- Ask user for Project Name. -->
<input message="Project Name" addproperty="project.name" />
<!-- and their project directory - default will use ../Project Name -->
<input message="Project Directory (../${project.name})" addproperty="project.dir" defaultvalue="../${project.name}"/>
<!-- Ask user for project scene - default will be Project Name -->
<input message="Project Scene (${project.name})" addproperty="project.scene" defaultvalue="${project.name}"/>
<!-- Create the project directory -->
<mkdir dir="${project.dir}" />
<!-- Copy the quick template build.xml into the project directory. -->
<copy todir="${project.dir}">
<fileset dir="${pulpcore.templates.quick.dir}" />
</copy>
<!-- Create a simple build.xml that just uses the quick template build.xml -->
<echo file="${project.dir}/build.xml"><![CDATA[<?xml version="1.0"?>
<project name="${project.name}" default="build-and-run" basedir=".">
<!--
This is a generated file which simply points to the PulpCore
quick template build file.

If you want to see the entire file look here:
${pulpcore.templates.quick.path}/build.xml

To over write this file with that one, copy it to this directory
and replace the properties found here:
http://code.google.com/p/pulpcore/wiki/GettingStarted
-->

<property name="project.scene" value="${project.scene}" />
<property name="pulpcore.path" value="${pulpcore.jars.path}" />
<import file="${pulpcore.templates.quick.path}/build.xml" />
</project>]]>
</echo>
<!-- Do a manual clean of new project directory -->
<echo>Clean new project directory</echo>
<delete dir="${project.dir}/build" />
<delete>
<fileset dir="${project.dir}/src" excludes="background.png" />
</delete>
<!-- Create simple java file for user to start with -->
<echo file="${project.dir}/src/${project.scene}.java">
/*
* This is an ugly generated file, edit it to make it your own and
* make it beautiful.
*/
import pulpcore.scene.Scene2D;
import pulpcore.sprite.ImageSprite;

public class ${project.scene} extends Scene2D {
public void load() {
add(new ImageSprite("background.png", 0, 0));
}
}
</echo>
<!-- Provide user with some final instructions on how to build and run their new project -->
<dirname property="project.path" file="${project.dir}/build.xml" />
<echo>To build and run your new project:</echo>
<echo> 1: CD (Change Directory) into your project directory (${project.path}).</echo>
<echo> 2: Run the build command "ant build".</echo>
<echo> 3: Run the run command "ant run".</echo>
</target>
</project>


Download the current PulpCore binary, unzip it somewhere, and then copy and paste the above script into the templates directory (as build.xml). Afterwards, open up a console (command) and navigate into the templates directory. Type "ant quick" and then follow the on-screen instructions.

Currently, there is only a target to create a "Quick" project, but I'll repost when I have some more time to put together a target for the "Project" project.

I'm a huge jEdit fan, so I have to give props to my favorite text editor! Which I used to create this Ant Script.

Cheers, and have fun!