Skip to main content

Print E-Z

This project had a very unique requirement: They wanted to dynamically serve up their products without augmenting their application with a server-side technology like PHP or Java. More specifically, they wanted their products to be pulled from a datasource instead of being statically specified in HTML.


What if we used a data source that could be hosted elsewhere and served over Javascript?

GoogleBase to the rescue!

In this project, we used GoogleBase to store their products and then javascript to read them from GoogleBase and render them on the correct pages.

It was tricky, but we were successful.


There was just one problem that we found during implementation, and that was that Google Base just wasn't prepared to have many integer values assigned to a single attribute. We needed this in order to map each product to the pages that it ought to appear on.

To make a long story short, we tried countless strategies over the course of a couple of weeks until the following thought dawned on me:

What if we assigned a prime number to each value and created composite numbers for each product's unique set of values, which we could then extract back out using modular arithmetic?

I'm sure that the thought occurred to you right away. Of course, it took me a couple of weeks to see it.

So here is what I did in the end:

Each page number would be assigned a unique prime number. So "Page 1" would be assigned the number "2", "Page 2" would be assigned "3", "Page 3" would be assigned "5", etc. The set of pages that an individual product should appear on can then be expressed as a set of those prime numbers:

Product A's 'page_numbers'-> {17, 3, 93, 41}

"But wait," you say. "Google Base won't let us specify a list of integers as a value in the data feed!" You speak well. What we will do now is multiple those prime numbers together to get a composite number, which will become the value for the "page_numbers" attribute.

17*3*93*41 = 194463

Product A's 'page_numbers' -> 194463

Now, if you stretch back to high school math, you'll remember that ever composite number is a product of primes (remember what they called "prime factorization"). This concept ensures that there is only one prime factorization per integer and thus, will not be divisible by any other primes than those that make up its prime factorization.

This is important because now I can say with confidence that if product xyz's 'page_numbers' attribute is divisible by 43, then it must belong on the page that is assigned to the prime number '43'.

Modular arithmetic is perfect for such a calculation. The question "Does x mod y equal 0?" is the same as asking "Does y divide evenly into x?"

So, the math works. Now, will Google Base support it?

It turns out that their Ranking Language does support modular arithmetic and can act as a secondary filtering mechanism after its Query Language. Yes!

Upon reading the documentation, I use the following Ranking Language code:

orderby=[x = page_number(int) : if ( x % 43 == 0 ) then x else fail()]

Drum roll.....


...It fails. Expected an 'int' for the '%' operator, but found 'page_number(int)'.

What? **Heart sinks**

Well, I won't tell you about the resulting melancholy that weighed on my stricken heart for the next 48 hours while I stewed over what had gone wrong.

Instead, I'll just tell you that I found out that since each attribute is able to have multiple values (just not necessarily through a data feed), I needed to apply an aggregating function on the value before applying the mod.

So, then I tried:

orderby=[x = page_number(int) : if ( max(x) % 43 ) then max(x) else fail()]

And it worked!

It's always fun running into problems like that because there is an interesting solution hanging right around the corner.


Popular posts from this blog

How Many Teeth Does The Tooth Fairy Pick Up Each Night in Utah?

Somebody asked me a question about my Tooth Fairy post the other day that got me thinking. How many baby teeth are lost every day in Utah?

I began with Googling. Surely someone else has thought of this and run some numbers, right? Lo, there is a tooth fairy site that claims that the Tooth Fairy collects 300,000 teeth per night.

That's a lot; however, when I ran the numbers, it started to seem awfully low.

Let's assume that the Tooth Fairy collects all baby teeth regardless of quality and we assume that all children lose all their baby teeth. The world population of children sits at 2.2 billion, with 74.2 million of them in the United States. Of those, approximately 896,961 of them are in Utah. This means that somewhere around .04077% of the world's children are in Utah.

If we assume that kids in Utah lose teeth at the same rate as all other children in the world and that each day in the year is just as likely as the rest to lose a tooth, then we have that of the alleged …

Five Reasons Serving on the Athlos Board is Fun Right Now

About 18 months ago, a friend of mine, Bethany Zeyer, let me know about an open position on the Athlos Academy of Utah school board. I've always had a passion for education, and my kids' school seemed like a place where I could have a positive effect on the community.

Also, I'd just finished reading "The Smartest Kids in the World" by Amanda Ripley and, based on Amanda's advice, interviewed the school's director.

I was in the mood to contribute!

I applied and was accepted, and I've been serving on the board for a little over a year now.

Since then, I've learned a whole lot about how a school is run.

I've learned that someone needs to determine the school guidelines for pesticide usage.

And that someone needs to be thinking about the long-term future of the school, whether or not to increase grade capacity, whether or not to match the pay increases big school districts are giving, and most importantly, evaluate whether or not the school is achi…

I don't know you from Adam OR How to Tie Yourself Back to Adam in 150 Easy Steps

Last Sunday, I was working on my genealogy on, a free site provided by The Church of Jesus Christ of Latter-Day Saints for doing pretty extensive family history. While looking for information about a Thomas Neal, I found an individual who had done a bunch of work on his family including is tie into the Garland family, which tied in through Thomas's wife.

So, while I was pondering what to do about Thomas Neal (who's parents I still haven't found), I clicked up the Garland line. It was pretty cool because it went really far back; it's always fun to see that there were real people who you are really related to back in the 14th century or what not.
As I worked my way back through the tree, I noticed it dead-ended at Sir Thomas Morieux, who, according to the chart, was the maternal grandfather-in-law of Humphy Garland (b. 1376).  The name sounded pretty official, so I thought I'd Google him. I learned from Wikipedia that Sir Thomas Morieux married Blanc…