My stuff http://michelhollands.net/blog Tech and travel tidbits Tue, 06 Jul 2010 19:45:55 +0000 http://wordpress.org/?v=2.8.4 en hourly 1 The __enter__ method in the with statement http://michelhollands.net/blog/2010/07/06/the-__enter__-method-in-the-with-statement/ http://michelhollands.net/blog/2010/07/06/the-__enter__-method-in-the-with-statement/#comments Tue, 06 Jul 2010 19:44:04 +0000 Administrator http://michelhollands.net/blog/?p=194 The with statement is a great addition to Python. One thing that might not be immediately clear from the documentation is how the __enter__ method has to be implemented. If you want to use the object containing __enter__ then you have to return self there. You should not create a new object and return it. Here’s an example :

class example():
  def __init__(self, login=False):
      self.setting = 'A'
      print '__init__'
 
  def __enter__(self):
      print 'Entering '
      return self
 
  def __exit__(self, exctype, value, traceback):
      print 'Exiting '
 
with example() as e:
   print e.setting

The output of this script is :

__init__
Entering
A
Exiting

So __init__ is called first and then __enter__. Then the block after the with statement is executed, followed by __exit__ .

Of course you can return another object in __enter__ if necessary. In a lot of cases that might make more sense.

]]>
http://michelhollands.net/blog/2010/07/06/the-__enter__-method-in-the-with-statement/feed/ 0
Podcasts http://michelhollands.net/blog/2010/03/14/188/ http://michelhollands.net/blog/2010/03/14/188/#comments Sun, 14 Mar 2010 17:44:45 +0000 Administrator http://michelhollands.net/blog/?p=188 Here are two great podcasts that make my ride into work more enjoyable :

Well worth subscribing to.

]]>
http://michelhollands.net/blog/2010/03/14/188/feed/ 0
Useless http://michelhollands.net/blog/2010/01/05/useless/ http://michelhollands.net/blog/2010/01/05/useless/#comments Tue, 05 Jan 2010 20:35:42 +0000 Administrator http://michelhollands.net/blog/?p=184 This is great :



Some people have too much spare time though.

]]>
http://michelhollands.net/blog/2010/01/05/useless/feed/ 0
New job http://michelhollands.net/blog/2009/12/17/new-job/ http://michelhollands.net/blog/2009/12/17/new-job/#comments Thu, 17 Dec 2009 20:55:14 +0000 Administrator http://michelhollands.net/blog/?p=181 It’s been nearly 4 weeks now that I got a new job. The company is called Velocix. We make content delivery network software. Wikipedia explains that a lot better than me here.

The nice thing about the job is that I get to program Python for a living now :) .

]]>
http://michelhollands.net/blog/2009/12/17/new-job/feed/ 0
Dublin http://michelhollands.net/blog/2009/11/22/dublin/ http://michelhollands.net/blog/2009/11/22/dublin/#comments Sun, 22 Nov 2009 16:36:15 +0000 Administrator http://michelhollands.net/blog/?p=176 Visiting Dublin in November isn’t perhaps the best time. And indeed, one of the days there was a bit of a wash-out. It’s still a beautiful small city though. Here are the pictures :


Customs house

This is Customs House, on the bank of the river Liffey.

]]>
http://michelhollands.net/blog/2009/11/22/dublin/feed/ 0
Andalucia http://michelhollands.net/blog/2009/10/28/andalucia/ http://michelhollands.net/blog/2009/10/28/andalucia/#comments Wed, 28 Oct 2009 17:26:47 +0000 Administrator http://michelhollands.net/blog/?p=156 Great weather, great food and great architecture : there is a lot to enjoy in Andalucia. Recently I went on leisurely trip through Seville, Cordoba and Granada. Click on the following picture too see the photos :

mezquita_small

BTW : The picture shows the interior of the Mezquita in Cordoba.

]]>
http://michelhollands.net/blog/2009/10/28/andalucia/feed/ 0
Bath and Bristol http://michelhollands.net/blog/2009/08/19/bath-and-bristol/ http://michelhollands.net/blog/2009/08/19/bath-and-bristol/#comments Wed, 19 Aug 2009 18:38:01 +0000 Administrator http://michelhollands.net/blog/2009/08/19/bath-and-bristol/ The Banksy exhibition in Bristol was an opportunity to visit Bath and Bristol. Here are the photos :


Clifton suspension bridge

The photo is of the Clifton suspension bridge.

]]>
http://michelhollands.net/blog/2009/08/19/bath-and-bristol/feed/ 0
Python logging output file change http://michelhollands.net/blog/2009/08/12/python-logging-output-file-change/ http://michelhollands.net/blog/2009/08/12/python-logging-output-file-change/#comments Wed, 12 Aug 2009 16:29:02 +0000 Administrator http://michelhollands.net/blog/2009/08/12/python-logging-output-file-change/ I run unit tests in Python using the standard unittest module. The output should be logged to a file. For this Python has a standard logging library, which is somewhat similar to the log4j library for Java.

The problem is that there is no easy way to direct the logging to a new file for every test case. Here’s how I got around it. Firstly the setup method of my testcase object contains the following :

def setUp(self):
    self.logger = logging.getLogger('MyLogger')
    self.logger.setLevel(logging.DEBUG)
    self.log_handler = None
    self.formatter = logging.Formatter("%(asctime)s - %(name)s - %(message)s")

A logger is created and the default level is set. Then variables are set for the log_handler and formatter. These are used in the settestcasename method found below. This gets called in every test method of the testcase class. First it removes the old handler. The testcase name given as parameter is then used to create a new FileHandler. This will create and log to a new file when something is logged. This new handler is then added to the logger.

def settestcasename(self,giventestcasename,description=""):
    now=datetime.datetime.today().strftime('%Y%m%d_%H%M%S')
    if self.log_handler is not None:
        self.log_handler.close()
        self.logger.removeHandler(self.log_handler)
    filename='%s_%s.log' % (giventestcasename.replace(' ','_'), now)
    self.log_handler=logging.FileHandler(filename,'w')
    self.log_handler.setFormatter(self.formatter)
    self.logger.addHandler(self.log_handler)

The fact that the settestcasename has to be called is not ideal. but it works me. Something evil like this could be used to get the name of the current method, which can then be used as a filename. A decorator can possibly be created as well.

]]>
http://michelhollands.net/blog/2009/08/12/python-logging-output-file-change/feed/ 0
Using map with a dictionary in Python http://michelhollands.net/blog/2009/07/21/using-map-with-a-dictionary-in-python/ http://michelhollands.net/blog/2009/07/21/using-map-with-a-dictionary-in-python/#comments Tue, 21 Jul 2009 10:08:07 +0000 Administrator http://michelhollands.net/blog/2009/07/21/using-map-with-a-dictionary-in-python/ When you have a dictionary in Python like this :

   mydict={'first':1, 'second':2, 'third':3}

and you want to get the values into a list with a certain order you can do this :

   mylist=[mydict['first'], mydict['second'], mydict['third']]

However, that can get a bit verbose if there are a lot of entries. For every entry you have to repeat ‘mydict’. An elegant way of solving this is using the map function :

   mylist=map(mydict.get,['first', 'second', 'third'])

This applies to ‘get’ function of mydict to each of the arguments given in the list. This ‘get’ function is the equivalent of calling mydict['some index'] .

List comprehensions can be used a lot of times instead of map. That’s the case here as well :

   mylist=[mydict[x] for x in ['first','second','third']]

This is arguably easier to understand than the version using map.

]]>
http://michelhollands.net/blog/2009/07/21/using-map-with-a-dictionary-in-python/feed/ 0
Windsor, Stonehenge and Blenheim http://michelhollands.net/blog/2009/06/16/windsor-stonehenge-and-blenheim/ http://michelhollands.net/blog/2009/06/16/windsor-stonehenge-and-blenheim/#comments Tue, 16 Jun 2009 18:49:18 +0000 Administrator http://michelhollands.net/blog/2009/06/16/windsor-stonehenge-and-blenheim/ A few weeks ago, I took my parents on a trip to Windsor castle, Stonehenge and Blenheim. The weather was great and the traffic was fine as well. Here are the pictures :


Windsor

This picture is Windsor Castle, by the way.

]]>
http://michelhollands.net/blog/2009/06/16/windsor-stonehenge-and-blenheim/feed/ 0