Illusory Follies Sed quis debuget ipsos debugatores?

17May/091

Unicorns?

I don't know how many people reading this may know, but you can use Google as a calculator. Doing a search for "4 * pi" will in fact return a special "first result" that is the calculation. This is handy as it also does unit conversions like in order to convert 22 km to furlongs. There are some easter eggs though, and this is definitely the best: number of horns on a unicorn. Maybe it's just me, but this is incredibly funny to me.

I've been playing with the very impressive WolframAlpha and wondered if they had done anything similar. Sure enough "number of horns on a unicorn" return the expected result. Even more amusingly, my second guess, number of horns on a horse shows that they really having some fun:

Number of horns on a horse

5May/090

Yikes

It's been a month again without any updates. I'm not good at this blogging thing. It's difficult because I think I haven't defined my purpose very clearly. From my second blog entry way back in January 2005, I said that I would have this site "chronicle my hobbies" and might also include "items that I find amusing". I feel a little cheap simply rehashing the million-and-one sites out there that have funny things on them so I tend to not include unless I find them extremely funny and I think that my blog readers likely won't see the source site. With regards to my hobbies, it's hard because my availability for hobbies ebbs and flows so when I'm not posting it's usually because I'm not doing much.SMile

The thing I find funny about that original post is that I said: I know [my blog topic] is not original, but then again not many people are interested if your hobby is photographing sumo wrestlers."

I think in fact that I would have more visitors if I had chosen sumo wrestler photography. I would have a very real chance of becoming the biggest, most popular sumo wrestler photography site. It would be part of that "long tail" that may not be as popular overall but with global availability could dominate the niche.

The problem with talking about just my hobbies and things that I find funny is that everyone does this and although it's more interesting than journaling my diet (hey, that's what Twitter is for!) it's not terribly thought provoking. I don't have a budget in cash or time to do anything that's too thrilling or novel so I'm mostly just talking about my little 25 minutes of coding adventure in Groovy or my weekend of wiring the house (which I just did this last weekend!)

So I've decided to branch out. I think two things would help. First: posting more regularly. I need to get in the habit of posting at least every other day. That's a lot of posting, but when I'm busy, I will content myself with rehashing other sites that I've read and digested. I usually have thoughts about posts on Coding Horror or similar sites and could easily indulge in 20 minutes of logorrhoea and save Sarah from having to hear me talk about some software methodology and why I think it's terrible/wonderful.

So that's the attempt. More posts, more content, less junk, less stuff about boring hobbies.

30Mar/090

C++ from Python

I was impressed today to see how easy it was to call a C++ DLL from Python. I got the following information from another site:

1. Create a file called dlltest.cpp and write a function that sums two numbers and returns the result:

      //dlltest.cpp
      #define DLLEXPORT extern "C" __declspec(dllexport)
 
      DLLEXPORT int sum(int a, int b) {
          return a + b;
      }

The extern "C" construct tells the compiler that the function is a C function. It also removes the decorations from the functions names in the DLL.
__declspec(dllexport) adds the export directive to the object file so you do not need to use a .def file.
2. Include the header of the function in dlltest.h:

      //dlltest.h
      int sum(int, int);

3. Create a new Dinamic-Link Library project and include the two files, compile, and create the DLL.
4. You can now use Dependency Walker to see the list of the exported functions. You should see here the sum function.
5. Move the DLL in the Python folder or use

      >>> import sys
      >>> sys.path.append(r"C:\path\of\dll")

to include the DLL folder in the list of Python folders.

6. Use the ctypes module to access the DLL:

      >>> from ctypes import *
      >>>mydll = cdll.dlltest
      >>> mydll

Note: ctype module is already included from Python 2.5. If you are using an older version you can download ctypes here.
7. Now call the function:

      >>> sum = mydll.sum
      >>> sum
      <_FuncPtr object at 0x0097DBE8>
      >>> sum(5, 3)
      8

Reposted from here... (Thanks!)

I need to get into Python more -- I've used Ruby a bit but have tended to ignore Python simply because I've not seen it is needed. Evidently, I need more side projects.

30Mar/090

Google CTemplate

I just realized that I reinvented the wheel a month or so... I had a need for a nice templating language and although I did look for a solution that already existed, I didn't look hard enough. Google CTemplate is exactly what I wanted. My solution was basically to create this from scratch while ignoring all the complexities that I thought I wouldn't need. The result works (and may even be faster) but it feels clunky and it's not very flexible. You learn something new every day. Maybe with Rev2 of our release I'll start using this -- it certainly would be easier and the API is already documented.

From their website:

Here is a simple template file:

Hello {{NAME}},
You have just won ${{VALUE}}!
{{#IN_CA}}Well, ${{TAXED_VALUE}}, after taxes.{{/IN_CA}}

Here is a C++ program to fill in the template, which we assume is stored in the file 'example.tpl':

#include <stdlib.h>
#include <string>
#include <iostream>  
#include <google/template.h>  
int main(int argc, char** argv) {
  google::TemplateDictionary dict("example");
  dict.SetValue("NAME", "John Smith");
  int winnings = rand() % 100000;
  dict.SetIntValue("VALUE", winnings);
  dict.SetFormattedValue("TAXED_VALUE", "%.2f", winnings * 0.83);
  // For now, assume everyone lives in CA.
  // (Try running the program with a 0 here instead!)
  if (1) {
    dict.ShowSection("IN_CA");
  }
  google::Template* tpl = google::Template::GetTemplate("example.tpl",
                                                        google::DO_NOT_STRIP);
  std::string output;
  tpl->Expand(&output, &dict);
  std::cout << output;
  return 0;
}
Filed under: Geekiness No Comments
25Mar/091

So Pretty

It's truly amazing what beauty people can create. The Internet can be a fun place.

Wait for it to load and then move your cursor around and see what happens.

Via Dark Roasted Blend

20Mar/093

Because Perl is Too Straightforward…

...someone created Perligata.

Here's an example:

#! /usr/local/bin/perl -w
 
use Lingua::Romana::Perligata;
 
maximum inquementum tum biguttam egresso scribe.
meo maximo vestibulo perlegamentum da.
da duo tum maximum conscribementa meis listis.
 
dum listis decapitamentum damentum nexto
    fac sic
        nextum tum novumversum scribe egresso.
        lista sic hoc recidementum nextum cis vannementa da listis.
    cis.

is equivalent to:

#! /usr/local/bin/perl -w
 
print STDOUT 'maximum:';                  
my $maxim = <STDIN>;                     
my (@list) = (2..$maxim);
 
while ($next = shift @list)             
    {
        print STDOUT $next, "\n";
        @list = grep {$_ % $next} @list; 
    }
Tagged as: , 3 Comments
19Mar/090

No Way…

I think I actually got a snippet of Symbian code to work on the first attempt! This is a first... Maybe I'm actually getting the hang of this. I just find the whole "descriptor" concept very odd.

Anyway, all I was trying to do was replace all plus signs with spaces. I normally wrestle with descriptor nonsense for a while but this time, I got it on the first try!

_LIT(TestData, "THIS+IS+A+TEST");
HBufC* heapBuf = HBufC::NewLC(255);
*heapBuf = TestData;
TPtr pHeapBuf(heapBuf->Des());
while (heapBuf->Find(_L("+")) > 0)
{
pHeapBuf.Replace(heapBuf->Find(_L("+")), 1, _L(" "));
}
 
CleanupStack::PopAndDestroy(heapBuf); //Don't forget!

Bleh... stupid Symbian. Thank goodness I didn't have to change the length of the descriptor...

18Mar/091

Malaise

...not to be confused with mayonnaise (although both tend to ruin an otherwise tasty sandwich).

I've had a lot recently (again, malaise, not mayonnaise). There's a lot going on and I empathize with Alice:

"Well, in our country," said Alice, still panting a little, "you'd generally get to somewhere else — if you run very fast for a long time, as we've been doing."

"A slow sort of country!" said the Queen. "Now, here, you see, it takes all the running you can do, to keep in the same place. If you want to get somewhere else, you must run at least twice as fast as that!"

Red QueenI'm way behind on reading, way behind on chores around the house (although I did finally wrap up my inspirational Plant Ledge Project). not keeping up with my supposedly rigorous exercise routine, behind on a huge bunch of personal geeky projects. Today I stayed home from work since I wasn't feeling so hot. I feel like I'm in continual deadlock as I analyze new tasks that I feel may be higher priority than my current tasks. There's no reason that I can't accomplish what I have in front of me, I just keep thinking that among the continual stream of tasks flowing at me, there might be a more critical task that I should immediately stick at the front of my queue. The problem is that except for very small tasks, almost all of my time is spent analyzing the incoming tasks. So even if I do identify the critical tasks and properly prioritize them, it doesn't really matter because unless a task can be done quickly, it will never actually have any opportunity to run despite its priority.

This causes feelings of guilt and malaise. Also, my cold doesn't help.

Filed under: Geekiness 1 Comment
23Jan/093

O’Reilly Books

home_tarsier (Copyright O'Reilly Media)I love almost all the O'Reilly books that I've read. The content is great. The style is readable. The quality is excellent.

I've heard of Safari Books Online over the last year or two and I've been thinking about it more recently. Safari Books offers unlimited access for $42.99/month to all the O'Reilly books. At first blush, it seems expensive. Yes, it's nice to be able to do a full-text search through a printed book that's sitting in front of you -- those "index" things in the back are so hard to use! ;-) but at the same time, I don't typically enjoy reading things on the computer for extended periods if time.

...But I'm trying it anyway. My thought is that over the last 12 months I think I've spent at least $50/month on technical books. Most of them (all but 3 I think) have been O'Reilly.

oreilly_large (Copyright O'Reilly Media)

Anybody reading this used Safari Books? Any opinions? I'll post my thoughts in the next week when I make the decision whether to keep the subscription or not.

Pictures used in this post are owned by O'Reilly.

21Jan/091

Variable Naming

Some in computer programming have insisted on using the prefix is for all boolean data types. I've been bumping against this lately. I think it's silly. It's a form of Hungarian notation which seems unnecessary considering that the compiler/interpreter in almost all cases will help us deal with type issues. For readabilities' sake, wouldn't it make sense to name something what it represents? For example, if the boolean variable represents the state of being done, I suppose isDone may be an OK name. But if it represents the state of something that may or may not have been done 3 years ago, a better name might be wasDone. What if we want special checking to take place if a flag is set? Should that flag be called isCheck? It seems silly -- maybe shouldCheck would work. What if we're talking about ownership or class relationship. isChild works as a name to indicate class relation, but hasChildren is a perfectly logical name to define the inverse relationship. I saw a few places that advocate the use of helping verbs (have, has, had, do, does, did, shall, should, will, would, may, might, can, could, and must) or verbs of being (am, is, are, was, were, be, being, and been) to prefix these names. This makes sense. However, we speak English and sometimes in English we drop helping verbs (think of did for example). Is something.didSucceed better or worse than something.succeeded? There are numerous similar examples.

I think in the end, naming variables to make them readable is much more important than following some convention. Or perhaps I should rephrase that: the convention we ought to follow is the convention of written English, not some tightly defined arbitrary subset.