Saturday, February 28, 2009

The most useful piece of code I have written for Android so far

The following code generates a new tab whenever there is an error and prints the error inside the tab. AWESOME. It is quick and easy, and provides you with immediate feedback when something goes wrong.

} catch (Exception e) {
tabHost.addTab(tabHost.newTabSpec("ERROR")
.setIndicator("ERROR")
.setContent(R.id.errorText)
);
TextView errorText = (TextView) findViewById(R.id.errorText);
StringBuilder error = new StringBuilder();
error.append("Error: " + e.getMessage() + "\n");
for(StackTraceElement te : e.getStackTrace()) {
error.append("f: " + te.getFileName() + ", m: " + te.getMethodName() + ", l: " + te.getLineNumber() + "\n");
}
errorText.setText(error.toString());
}


N.B. I am using a TabActivity, the tabHost variable is the result of this.getTabHost().
R.id.errorText refers to a TextView I have in my layout xml.

Ohmage taking shape

Hey people,

the following is another screen shot. It shows how Ohmage is taking shape.
Ohmage can now go from text to color, as well as color to text.

In the screen you can see the auto-completion functionality in use.


Friday, February 27, 2009

Ohmage

Hey guys and gals,

I've been spending a little bit of time working with Android lately. The following is a sneak peek at an app I'm working on. I hope to have it added to the Android market place by the end of this weekend (free app). The app's name is Ohmage, however for stuff and giggles, I may change the name to Pure Ohmage. I should mention that I hope to also have several other features added before I release Ohmage.

These features include:
1. A "generate colors" based on resistance value (ohms).
2. A lookup formula/equation list. This list will initially be static.
3. A multi tab view to separate the ohmage view from the formula/equation list view.

N.B. You may notice the resistor image is very coder-art-ish. The image will be updated before I release Ohmage.






Update:

While I do only support 4 band resistors, it will be fairly simple to add support for 5 and 6 band resistors.

For anyone wanting to know what the learning curve for Android is like, the following is my take on learning Android. I have been working with Android for about 10 hours now. Android programming with the Eclipse plugin is... pleasant :D. Android programs are written in Java and the event system is fairly similar to Java Swing. A lot of the heavy lifting is performed by the Android View, Activity and Adapter classes.
The Api Demos included with the SDK are the holy grail for learning Android. They provide excellent examples and clearly show how to perform your layout in xml, rather than in the raw code.
If you are considering learning Android, I highly recommend making use of the eclipse plugin and the Api Demos.

Saturday, February 7, 2009

Working with SVN (Subversion) on a local network. Use hostnames when checking out.

Hello everybody,

Firstly, I am working on a new project, "Tiddly Speck" (Working Title), and will return to posting about "Using Ruby to easily scrape/search/spider a web page for "things"" once I have finished the "Tiddly Speck" project.

Now on with the post..

When working with subversion on a local network, I highly recommend using hostnames when performing checkouts.
You can add custom hostnames by configuring your system's hosts file.
If at any point the machine which you were using to checkout from changes its IP address, having used hostnames means you need only reconfigure the entry for the hostname in your hosts file.
If you didn't use hostnames you would either need to reconfigure your network to give the machine the original IP address it had, reconfigure all the .svn entries for the checkout, or re-checkout with the new IP address and merge all your old changes into your new checkout.

Example:
I use my mac mini as my subversion "server" at home.

My hosts file includes:
127.0.0.1     localhost
127.0.1.1 robert-ubuntu-desktop
192.168.0.9 macmini


This means that I can checkout my repository using a command similar to the following:
svn co http://macmini/svn/ my_new_work_area

If at any time my macmini changes its IP address, I only need to edit the macmini entry in my hosts file with the new IP address.

Thursday, February 5, 2009

Ruby - Regex Non-Greedy Operator (?)- Using Ruby to easily scrape/search/spider a web page for "things" - Part 2

Hello again,

This is part 2 of the "Using Ruby to easily scrape/search/spider a web page for "things"" multi-part post.

In this post I will explain the use of the non-greedy regex operator. In the spider example used in part 1, I scraped for all uses of the html heading element. The html heading element includes h1, h2, h3, etc. More specifically, I was looking for all matches of the regex: <h[0-9]>(.*?)</h[0-9]>

The following breaks down and explains the above regex:
<h[0-9]> → Find <h followed by one digit ([0-9]) followed by >
.*? → Find any character (.) zero or more times (*) non-greedily (?)
</h[0-9]> → Find </h followed by one digit ([0-9]) followed by >

The parentheses (), mark what I want to capture from my regex match. In this case, it is the actual heading, I don't want to capture the <h[0-9]> or the </h[0-9]>.

The non-greedy operator (?) means that the regex should not be greedy; it should look-ahead to see if it can break what it is currently looking at. In the above example, the non-greedy operator was used to prevent the .* from matching everything and thus never allowing the regex to match </h[0-9]>. The following examples demonstrate greedy vs non-greedy:

Example 1: Greedy:
Regex: <h[0-9]>.*</h[0-9]>
Input: <h3>My Title</h3>
<h[0-9]> matches: <h3>
.* matches: My Title</h3>

Example 2: Non-Greedy:
Regex: <h[0-9]>.*?</h[0-9]>
Input: <h3>My Title</h3>
<h[0-9]> matches: <h3>
.*? matches: My Title
</h[0-9]> matches: </h3>

Further Reading:
Ruby regex, quick reference guide
Ruby-doc, user's guide to regex
Ruby API: Regexp
Rubular: A Ruby regular expression editor (Interactive)

Wednesday, February 4, 2009

Ruby - Using Ruby to easily scrape/search/spider a web page for "things" - Part 1

Hello,

This is part 1 of the "Using Ruby to easily scrape/search/spider a web page for "things"" multi-part post.

This part contains the code necessary to scrape a web site for a specific thing. In the case of this example, a heading.

The other parts of this multi-part post will explain specific sections of the code and will demonstrate how to spider through a website by scraping for links.

The following code can be used to scrape/search/spider a web site for headings:

#!/sw/bin/ruby
require 'open-uri'
require 'pp'

spider_url = "http://robertpyke.com/"
pp "Looking up #{spider_url}"

# The parentheses mark what we want to capture
heading_pattern = /<h[0-9]>(.*?)<\/h[0-9]>/

headings = []
open(spider_url) do |f|
f.each do |line|
matchdata = line.scan(heading_pattern)
matchdata.each do |match| # Each match, match is an array
match.each do |string| # Each string within a match
headings << string # Store the heading we found
end
end
end
end
pp headings # Print the headings we found


Note: It should be noted that the regex used to capture the heading is by no means perfect. It has been provided as a simple starting point for people wanting to scrape web pages. Both the limitations of this regex, and a more advanced regex example, will be provided in a later part of this multi-part blog post.

Ruby - Declaring Strings using %{}

Hi guys/gals,

In Ruby, you can declare a string like follows:
string = "a string"

You can also declare a string with:
string = %{Another string}

The cool thing about the %{} declaration is that it is context aware. That is to say, it knows the depth of the curly stack. It knows when the next curly will end a start curly within the string and when the next curly will end the curly which started the string.

The following example demonstrates this:
my_string =
%{1st line, it is fairly normal.
2nd line , it contains "quotation marks".
3rd line, it contains one set of curlies, { a set }.
4th, it contains two sets of curlies, { first set }, { second set }.
5th line, this marks the end of my_string.}


The above works because the sets of curlies start and end within the string.


The following examples show what will happen if you incorrectly use %{}:

Input: string = %{hello}world}
Output: -:1: syntax error, unexpected tIDENTIFIER, expecting $end

Input: string = %{hello{world}
Output: -:1: unterminated string meets end of file


Update: As mentioned by Simon, you can use any non letter/number character to mark the start and end of a string. The character after the % marks the start of the String, if the character has an end pair character, the way in which you declare the String will work as I have described for %{}. If the character does not have an end pair character, the next use of the character will mark the end of the String.

Pair characters include: %{}, %[], %(), %<>

The following shows different examples of character combinations you can use to start and end a string:
Start: %{ end: }
Start: %} end: }
Start: %[ end: ]
Start: %] end: ]
Start: %! end: !

The following shows an example of a character combination you cannot use:
Start: %{ end: {

The following are examples of correct String declarations:
my_string1 =
%[My string, I have some other brackets in my String [a, b, c], yay
for context aware Strings.]

my_string2 = %$MyString which is ended by a dollar sign$
my_string3 = %<Yet another example of a String which is closed by it's sensible pair character>
my_string4 = %%Yes, even the percent sign can be used%

The following are examples of invalid/incorrect String declarations:
Input: my_string4 = %1String Content1
Output: -:1: unknown type of %string

Input: my_string5 = %aString Contenta
Output: -:1: unknown type of %string

Input: my_string6 = %[String Content[
Output: -:1: unterminated string meets end of file