Custom CacheMiddleware that tells Javascript a page is cached in Django

August 24, 2009
1 comment Django

Here I'm going to explain a solution I had to make for a site I recently launched. Basically, I wanted to cache the whole page in memcache and set the appropriate Expires and Cache-Control headers so that my view was only rendered once an hour and parts of the page needs to be unique (i.e. "Hi, logged in as xxxx")

The advantages is great: The page loads fast, content is stored in memcache every hour, page still appears to be dynamic.

The disadvantages are not so great: the AJAX loads fast but causes a flicker

Basically, I wrote a custom decorator called custom_cache_page(<delay in seconds>) that works like the normal cache_page(<delay in seconds>) decorator available in stock Django. However, my decorator inserts a piece of HTML into the rendered HTML (before it's stored in memcache) that I later use to update certain elements of the page with AJAX instead of server side.

Truncated! Read the rest by clicking the link below.

The Secret to SEO Search Engine Optimization

August 20, 2009
0 comments Web development

"SEO is not about creating a website sausage overstuffed with key words and phrases. It’s all about creating relevant and compelling content that transforms you into a thought leader who can become a trusted provider That is ultimately how you convert visitors into buyers"

Calling all kung fu people - kungfupeople.com

August 19, 2009
0 comments Kung Fu, Django

Calling all kung fu people - kungfupeople.com Tonight we're launching our new Kung Fu website: kungfupeople.com

My friend Chris and I have been busy building a website where people who do kung fu can put themselves on a map to say where they train kung fu, what style they do and what kung fu club they belong to. The site is very much centred on having a world map and each little pin on the map is one kung fu martial artist.

This site is build in Django and is based on work that was done to build Django People originally developed by Simon Willison. We took his original code and revamped it almost completely.

Our goal is to slowly build up a world map of people from all sorts of clubs and styles and hopefully one day become the best place on the Internet for understanding what clubs are available where and what styles different people do. The site has been in an "alpha testing" phase now for a couple of weeks and even though we still have lots of ideas and cool features to add we believe it's ready to go live.

So if you train kung fu or know someone who trains kung fu go to our website and add yourself to the map

Google Reverse Geocoding vs. GeoNames

August 17, 2009
3 comments Python

I've been experimenting with the new Google Reverse Geocoding which allows you to get the location name and country and stuff from a latitude/longitude coordinate.

What I've been doing is comparing this with GeoNames. GeoNames is available from geopy in the reverse-geocode branch.

I wrote down a list of about 15 lat/long points and the result I expect from them (taken from an existing app I'm contemplating switching to Google Reverse Geocoding for) and ran a batch of timed tests on. These results might satisfy the impatient:


FAILURES:
geonames_json        0
google               0
geonames             12

TOTAL TIMES:
geonames_json        2.43582677841        0.143283928142 seconds/request
google               2.24999976158        0.132352927152 seconds/request
geonames             1.78063511848        0.104743242264 seconds/request

Truncated! Read the rest by clicking the link below.

gg - wrapping git-grep

August 11, 2009
0 comments Linux

I've grown quite addicted to this and finding that it's saving me tonnes of milliseconds every day. First of all, I've made this little script and put it in my bin directory called '~/bin/gg':


#!/usr/bin/python
import sys, os
args = sys.argv[1:]
i = False
if '-i' in args:
    i = True
    args.remove('-i')
pattern = args[-1]
extra_args = ''
if len(args) > 1:
    extra_args = ' '.join(args[:-1])
if i:
    param = "-in"
else:
    param = "-n"
cmd = "git grep %s %s '%s'" % (param, extra_args, pattern)
os.system(cmd)

Basically, it's just a lazy short hand for git grep ("Look for specified patterns in the working tree files"). Now I can do this:


peterbe@trillian:~/MoneyVillage2 $ gg getDIYPackURL
Homesite.py:526:    def getDIYPackURL(self):
zpt/homepage/index_html.zpt:78:       tal:attributes="href here/getDIYPackURL">Get your free trial here</
zpt/moneyconcerns/index_html.zpt:36:       tal:attributes="href here/getDIYPackURL">Get your free trial h
zpt/moneyconcerns/index_html.zpt:50:          <p><a tal:attributes="href here/getDIYPackURL" class="makea
(END) 

It's not much faster than normal grep but it automatically filters out junk. Obviously doesn't help you when searching in files you haven't added yet.

Public calendars on Google Calendar

August 8, 2009
0 comments Misc. links

Public calendars on Google Calendar I've been looking for something like this but thought I had to find an external VCAL file to import into my Google Calendar. Apparently Google Calendar has a whole bunch of useful public calendars. For example, UK Holidays.

Public calendars on Google Calendar To enable any of these click the little down arrow next to the word "Add" on the left hand side under the list of all calendars you have enabled.

The rest is easy and needs no explanation. I did however spot one "bug" or perhaps I just confused myself. When I went in the second time to take a screenshot of the list of public calendars available it had mistakenly unselected the ones I selected a few seconds ago. Perhaps a caching problem.

More optimization of Peterbe.com - CSS sprites

August 5, 2009
0 comments Web development

I have now made the menu images on this site into a CSS sprite. Basically, instead of loading 6 different images totaling 10Kb it now only needs 1 image which is 7Kb! The difference in filesize isn't the big win here but the reduced number of requests is the big deal.

The number one tip from the Yahoo! Performance people is reducing the number of requests and this is what I've done.

To generate the sprite image I use the CSS Sprite Generator and then fiddled it a bit to make it work for this site. Thanks guys!

The major drawbacks of CSS Sprite images isn't really technical but it's just that it's an optimization hack. The next time I need to change any of the images I have to reapply the hack and there's a risk that by the time I need to get back into it I will have forgotten how to do it or where to go to do it. Anyway, I'm really pleased with the performance of this site now.

The 4-hour Work Week by Timothy Ferris

July 29, 2009
0 comments Books

An American businessman took a vacation to a small coastal Mexican village on doctor's orders. Unable to sleep after an urgent phone call from the office the first morning, he walked out to the pier to clear his head. A small boat with just one fisherman had docked, and inside the boat were several large yellowfin tuna. The American complimented the Mexican on the quality of his fish.
  "How long did it take you to catch them?" the American asked.
  "Only a little while," the Mexican replied in surprisingly good English.
  "Why don't you stay out longer and catch more fish?" the American then asked.
  "I have enough to support my family and give a few to friends," the Mexican said as he unloaded them into a basket.
  "But... What do you do with the rest of your time?"
  The Mexican looked up and smiled. "I sleep late, fish a little, play with my children, take a siesta with my wife, Julia, and stroll into the village each evening, where I sip wine and play guitar with my amigos. I have a full and busy life, senor."
  The American laughed and stool tall. "Sir, I'm a Harvard M.B.A. and can help you. You should spend more time fishing, and with the proceeds, buy a bigger boat. In no time, you could buy several boats with the increased haul. Eventually, you would have a fleet of fishing boats."
  He continued, "Instead of selling your catch to a middleman, you would sell directly to the consumers, eventually opening your own cannery. You would control the product, processing, and distribution, and move to Mexico City, then to Los Angeles, and eventually New York City, where you could run your expanding enterprise with proper management."
  The Mexican fisherman asked, "But senor, how long will all this take?"
  To which the American replied, "15-20 years. 25 tops."
  "But what then, senor?"
  The American laughed and said, "That's the best part. When the time is right, you would announce an IPO and sell your company stock to the public and become very rich. You would make millions."
  "Millions, senor? Then what?"
  "Then you would retire and move to a small coastal fishing village, where you would sleep late, fish a little, play with your kids, take a siesta with your wife, and stroll to the village in the evenings where you could sip wine and play your guitar with your amigos..."
(The 4-hour Work Week, Timothy Ferriss, page 231-232)

The 4-hour Work Week by Timothy Ferris This quote is ripped from a book called The 4-hour Work Week by nutty young entrepreneurial American man called Timothy Ferriss. Just finished the book I have to admit being quite hit by it. It says on the back "WARNING: Don't read this book if you don't want to change your life". I don't want to radically change my life but a bit wouldn't hurt.

If your an office guy working for the man (or as the Japanese call it "salary men") reading this book will probably leave a bad taste of guilt and trembling eager to take his advice on board.

My take is to not take the book on at either 100% (what the author wants) or 0% (what almost all readers will do) but instead do some cherry picking of ideas and concepts that I like. I've actually already started to change a few things in my life all thanks to inspiration in the book.