Archive for August 2010


Serving images dynamically with CherryPy (on Google AppEngine)

August 13th, 2010 — 10:22 am

Google AppEngine(GAE) is great for hosting Python (or Java)  Web-Applications. They offer 1.3mio hits/d and 1GB up- and downstream/d for free. Considering that you will get access to Google infrastructure that let’s you crawl the web as fast as Google does itself, choosing GAE is a no-brainer for applications doing a lot of web-crawling, screen scraping or web-indexing. You can even do cron-jobs to get your job done periodically.

I won’t elaborate on how to get an account, download the SDK and get started, because Google hosts great tutorials for these itself. If you are already familiar with Python web development this will get you started in a matter of minutes.

I personally chose not to use the Google webapp framework, because I’m quite familiar with CherryPy. I fell in love with it, because it feels very sleek – very Zen-like. This comes to no surprise, because it was a deliberate design decision as can be read in The Zen of CherryPy.

Getting started with CherryPy on GAE is no trouble, either. GAE supports any Python framework that is WSGI-compliant. Those include Django, CherryPy, web.py and Pylons. Google doesn’t host these frameworks themselves, so all you have to do is copy the whole framework into your GAE project to get the import to work. That’s it. Same counts for any 3rd party module. Need BeautifulSoup? Just copy the py-file to your project. Easy as cake.

Now, if you want to serve images dynamically, you don’t have to store them on harddisk to link to them. Just save them in the Google Datastore and serve whenever needed.

Using the following snippet you will be able to dynamically serve images with URLs like this:

http://application/handler_name/index/[0-9]*

import cherrypy
from cherrypy import expose
import wsgiref.handlers
import DynamicImage
class Root:
  @expose
  def index(self):
    return ""
class GetImage():
  """ GetImage provides a handler for dynamic images """
  def __init__(self):
    """
      Mockup for getting some images. Datastore or live
      scraping could be done here
    """
    # Note: DynamicImage is just a mockup.
    # There is no such module.
    dynamic_image = DynamicImage.DynamicImage()
    self.pictures = dynamic_image.getImages()
  @expose
  def index(self, num=None):
    """
      Provides the handler for urls:
        application/handler_name/index/[0-9]*
    """
    return self._convert_to_image(self.pictures[0][int(num)])
  def _convert_to_image(self, picture):
    cherrypy.response.headers['Content-Type'] = "image/jpg"
    return picture
# Root() doesn't do anything here. It normally serves your index page.
root = Root()
# Generate route http://app/img/
root.img = GetImage()
# Start CherryPy app in wsgi mode
app = cherrypy.tree.mount(root, "/")
wsgiref.handlers.CGIHandler().run(app)

One last note: Processes running longer than 15-30s will be cut off from GAE with the DeadlineExceededError exception. You can catch this exception and try to divide your workload into smaller pieces.

2 comments » | articles

Business cards draft #1

August 5th, 2010 — 04:33 pm

A first draft of my new business card including a test print at the local copy shop.It’s not finished yet. People won’t get from it what dispatched is ought to do.

Many thanks to my great designer Katrin from http://rocketship.cc

Comments?

business_card_draft

Picture 1 of 2

6 comments » | personal

Petitions I signed today

August 3rd, 2010 — 10:31 am

Normally, I’m not much of a petition signing person. But looking on how this world runs, I probably should step up a bit.The petitions I signed today were:

Stop the construction of Europe’s biggest slaughterhouse
http://www.vebu.de/aktuelles/petitionen/465-aktionen-und-petitionen/?pet_id=582

This new slaughterhouse would run at 7.5 deaths per second. Seriously, do you people need even cheaper meat? At a discounter, a chicken already costs about 2€. There
is absolutely no margin for a cruel free life or any decent quality in the “production process”. This is madness.

Stop a new animal testing lab in Malaysia
http://www.thepetitionsite.com/1/StopPlansForAnimalTestingMalaysia

We know how cruel animal testing is in Europe. How bad will it be in a lesser developed country with even fewer laws concerning animal rights? Besides, there’s lots of proof that animal testing is not only cruel, but completely unnecessary (read for example “Doctors speak out against animal testing”).

End the brutal killing of more than 20,000 dolphins every year in Japan
http://apps.facebook.com/causes/petitions/252

Japan is known for killing whales to study them. Afterwards they sell their meat on the market. The profits therein are of course completely unrelated to their will of studying so many whales. Due to international pressure, Japan begins to slaughter dolphins instead. 20’000 a year as a new documentary shows.

As human being and a vegetarian, probably it is not only my right, but also an obligation to make this world a better place. There is no god to do it for us. And mankind itself often is incompetent or incapable of action. I, on the other hand, have the needed time and resources to do my part. I honestly feel bad for not doing enough.

I want to finish with a small side story. Today, during my tea break, a smoker came up to me and asked me what I drank. I had peppermint tea, because I’m still suffering a little from the flew. His reaction was completely gross. He said “No wonder you’re ill. You should live more hardcore! Eat more sausage and stuff. This makes you strong.”

I will remind him later this year.  In Winter he will regularly go outside to have a smoke, coughing and freezing at -10C. He is a real hardcore guy. He eats sausage and stuff.

The pigs inside the cage are brought down in groups to a space full of CO2. Taken from http://www.flickr.com/photos/igualdadanimal/3806411367/

Comment » | personal

« Previous Entries     Next Entries »