Friday, June 25, 2010

DOS Scripting Text Parsing

I've been most unfortunate to have to be involved in some Windows scripting at work. What is that you say? A colleague of mine recently needed a script that rearranged some images and icons based on some fields in a database. I started off with exporting the database records into a comma separated file.

A few tips about DOS scripting:
  1. www.dostips.com: One of the best resources on the web on DOS.
  2. REM: Useful for commenting your scripts. I cannot emphasize enough how important it is to comment your scripts.
  3. @echo off: Setting this at the beginning of your script stops the script from printing out the commands it is executing. This makes the output nice and tidy, printing only when echo is called explicitly. Not setting it is good for debugging.
Lets start of with some simple examples and pitfalls:

Parsing text and printing out line numbers:
See the script listing below:


Note that the delims option is set to blank (delims=). This is to counter the fact that spaces and tabs are the default delimiters so you need to override this. Another interesting aspect in the script is the use of the "enabledelayedexpansion". This allows variables in for loops to be evaluated correctly.

Splitting a line delimited by commas
Once your lines have been split, you may want to split the individual lines either by comma (for CSV files) or other delimiters.


This will display the first 3 words of each line after line.

Update: I've found a site with interesting tutorials on DOS.

Tuesday, June 22, 2010

Arduino Unboxing

Finally after a few days of waiting, my Arduino (I/O) board has arrived and I can get stuck into some prototyping. For those not in the know, Arduino is an open source physical computing platform based on a simple microcontroller board.

Pics below of my box...




More soon on my exploits and the abilities of this nifty little board. Did I mention its dead easy to program?

Wednesday, June 9, 2010

Perils of a Polyglot - part 2

This is part 2 of my Perils of a Polyglot series. I looked at tokens to mark identifiers ($ vs @) and where to put semicolons (;) to terminate statements. I'll continue this rant looking at other pet hates.

3. if, elseif, else if, elif, end if, then, else:
This is probably a point about scope, whitespace and indentation (I'll whinge about that later). Different programming languages have different ways of doing pretty much the same thing. I must say the most annoying of them all I find is Python. Why? First, 'elif' for 'elseif'. Second, parenthesis outside the condition is optional. That may be convenient but I always pause a second thinking is it right?

if request == "good":
#do something good
elif request == "bad":
#do something bad
else:
#do nothing

You can also do:

if (abooleanVariable):
#do somethihing

VB.NET takes the Pseudo code analogy a bit to serious for me. If ... Then ... ElseIf .. Then .. End If. I always wonder, "If End If is two words, why is ElseIf one word?". Another interesting point is the fact that an If statement spanning a single line does not need an "End If". Example below:

If Not anObject Is Nothing Then anObject.ToString()

VB.NET also brings you niceties such as no parentheses around the conditions and real worlds for the boolean operators (And, Not). Thankfully the Visual Studio IDE should keep you from straying with its auto correct features that include correcting the case of reserved keywords. Nice.

Finally, how the major programming languages handle scope within conditional statements (This could be repeated when b***ching about indentation and whitespace) :

Curly Braces:
  1. Java
  2. C++
  3. C#
  4. PHP
  5. Javascript

The Rest:
  1. Python (Indentation)
  2. Ruby (Keywords: end)
  3. VB.NET (Keywords: Then, EndIf)
Enough about conditionals, I'll try cover the rest of the issues in one post next time.

Tuesday, June 8, 2010

Perils of a Polyglot

A polyglot is linguist - a person who speaks more than one language. Most programmers worth their salt tend to be polyglots in the never ending quest to improve productivity and I'm no different. My day-to-day programming is most in Java and .NET languages VB.NET and C# as I'm mostly doing enterprise development and integration work. My website runs on Joomla! in PHP and I occasionally write some modules for it. The rest of the small projects I work on are some Python (for Google App Engine), Ruby (on Rails) and I'll include Javascript because with node.js and coffee script, I'm doing quite a bit of Javascript.

One of the greatest pains of jumping between different programming languages is the different syntax rules and the obscure error messages you get when you violate these rules. You stare at the code, the error message and nothing makes sense, especially if you are not using an IDE. It was when I was facing such a mistake that I got the idea for this post. So I'll start with that.

1. In PHP variables are denoted with '$' not '@'.
You can guess that I'd been doing some Ruby programming before this. The code I wrote was:

@usr = new StdClass();

The error message was 'Parse error: syntax error, unexpected '=' in on line '. How cryptic is that! I should have typed:

$usr = new StdClass();

However, just @usr; is a valid PHP statement!

2. Where to put those semicolons?
Semicolons are neat statement delimiters. Where to put them and where not to is an error that's easy to make and easy to pick up but worth a mention.

Of the programming languages I use the following use semicolons:
  1. Java
  2. Javascript
  3. C++
  4. C#.NET
  5. PHP
And the languages that don't:
  1. Visual Basic .NET (and VB6)
  2. Python
  3. Ruby
Next post I'll look at the "if, elseif, else if, elif, end if, then" dilemma, Case Sensitivity, Whitespace (tabs vs space), Comments and much more. Share your pain in the comments.

Casein CMS on Heroku

When evaluating some Ruby on Rails CMSes I mentioned Casein (which I wouldn't really classify as a CMS). Its more of a plugin that provides admin authentication and pretty CRUDification of resources. I'm doing my development staging on Heroku and needed to ensure that it runs on Heroku. Here's a couple of Gotchas!

Gems
Gems on Heroku are installed using a .gems manifest file. 2 gems required: calendar_date_select (>=1.15) and will_paginate (>=2.3.11). Adding these two gems to the manifest will ensure they are installed when you push the application.

calendar_date_select --version '>=1.15'
will_paginate --version '>=2.3.11'

You'd be forgiven for thinking this would resolve the issue, however, there is another reference to the gems as part of the plugin that causes the application to fail. In /vendor/plugins/casein/init.rb comment out the two lines that configure the gem.

#config.gem "calendar_date_select", :version => '1.15'
#config.gem 'mislav-will_paginate', :version => '~> 2.3.11', :lib => 'will_paginate', :source => 'http://gems.github.com'

Caching
As part of the magic the Heroku fairy godmother provides (Building upon the AWS and S3 storage service), the clock strikes 12 when you attempt to write to the file system. Simple, Heroku slugs operate in a readonly context. The caching currently written in Casein, primarily for static resources like Stylesheets and Javascript, attempts to write to the file system. The first instance you will encounter this is on the authentication page. The layout file is in /vendor/plugins/casein/app/views/layouts/casein_auth.html.erb. Change the line:

<%= javascript_include_tag :defaults, "/casein/javascripts/login.js", :cache => "casein_auth_javascripts" %>

to:

<%= javascript_include_tag :defaults, "/casein/javascripts/login.js" %>

That should get you up and running. My next stumble was running the casein:init rake task but as I'm probably not sticking with the platform I may or may not investigate a solution for that.

UPDATE: Not sure what's wrong with the rake task, but the task is not doing much. See /vendor/plugins/casein/tasks/casein_tasks.rake to see what the task does. It runs the migration, removes all existing users and then creates the admin. If you've already run this locally, I'd suggest simply using heroku db:push to transfer your local data to Heroku. Sadly I'm not going ahead with Casein so this is it for me.

Monday, June 7, 2010

Ruby on Rails customizable CMS

As mentioned in a previous post, I'm staging my coming-out-of-the-ruby-on-rails-closet party with the rewriting of my scheduling system, Chex. The system was coded from scratch on python/django on the Google App Engine. One of the things I didn't like about the original application was the fact that the static content was tightly coupled to the functionality of the application. My experience with this blog and my website is that it is much simpler to update the content using specialized systems. Especially as I program in many different languages, if I want to quickly change some presentation aspects and my day programming job does not include that language, I don't want to be fiddling with code, sticking semicolons where they don't belong!

My initial googling returned several possible options I could use, including Radiant, BrowserCMS, ... Here's a dated list of the top 20. I found overall that there were a handful that were mature in the classic CMS sense (coming from someone who's website runs on Joomla!) and off course plenty that were experimental and flaky. I'll leave the critique to others.

This evaluation process got me thinking about what a CMS is and what I was really after. I needed to do registration, authentication and quickly managing static content. I also needed something that would drop into my application. After reading an insightful article on how Rails + Plugins effectively equated a CMS, I'm feeling upto the challenge for creating a DIY which hopefully can become reusable in time.

Monday, May 31, 2010

Quartz Task Scheduling on JBOSS 4.2.2

I've been working on a J2EE app which needed to run a specific task at 8.00 AM every morning. I've used Quartz as a scheduler with Spring on Weblogic, but I'm not using Spring on my current project so I was probably going to have to use Quartz raw. That is, until I read that JBOSS has Quartz built in. This has been handled as a resource adapter. That's the quartz-ra.rar file.

How to use this goodness, is to create a Message driven bean (MDB). You can do this by creating a class that extends the org.quartz.Job interface. See an example here.

Notice that I have commented out the annotations just above the class definition as I believe those are deployment specific decisions hence would prefer to make those declarations in configuration files. If you're not as picky you could uncomment them. Note that the @ResourceAdapter annotation is JBOSS specific and would impact negatively on the portability of your code!

In your ejb-jar.xml you define the class as an EJB. See example here.

If you're following my advice and not using proprietary annotations, you'll need to create a jboss.xml file like this.

The jboss.xml file creates the link between the EJB and the resource adapter while the MDB definition in the ejb-jar.xml file. Be careful with the cronTrigger property as it does not behave in the exact same manner that cron does in UNIX (Cron on Wikipedia). There is an additional position on the left for seconds. Follow the Quartz Scheduler tutorial here. Deploy and enjoy!