The strange beast of PyTZ and datetime

Update A good explaintation of what is happening is provided here. In this day and age dateutil.tz is the better solution to timezones, as it works as expected with datetime. TL;DR The crux of this post is, make sure to never use datetime.replace(tzinfo=...) when working with PyTZ, use tz.localize(...) instead, otherwise you’ll end up with some very strange times. The PyTZ docs do mention this helper method as a way to fix incorrect conversion across timezones, but out of the box PyTZ timezones seem odd. Consider this simple code that takes both the native datetime.replace approach, and the localize approach: ...

November 11, 2014 · Nigel Sim

Using MapMessage with ActiveMQ with a Python Stomp.py consumer

Out of the box a STOMP consumer on an ActiveMQ broker will be able to receive TextMessages, but MapMessages will arrive without content. This is because we need to specify a converter to ActiveMQ(?) which we can do in the subscription setup, in a similar way that this thread discusses sending MapMessages from the Python end. Unfortunately there is a little bit of manual handling, because the best the client library is do is deliver a string, so you’ll have to handle the deserialisation. ...

December 11, 2013 · Nigel Sim

Simple type checking using property()

Python usually relies of duck typing for type safety, but from time to time it can be handy to enforce some type checking, particularly when new users are going to be using your objects. The following are three utility methods for applying type checking to class properties, using the new style object property() method. def deleter(attr): """Deleter closure, used to remove the inner variable""" def deleter_real(self): return delattr(self, attr) return deleter_real def getter(attr): """Getter closure, used to simply return the inner variable""" def getter_real(self): return getattr(self, attr) return getter_real def setter(attr, valid_types): """Setter closure, used to do type checking before storing var""" def setter_real(self, var): if not isinstance(var, valid_types): raise TypeError("Not of required type: "+str(valid_types)) setattr(self,attr,var) return setter_real def typed(attr, valid_types, docs=""): """Wrapper around property() so that we can easily apply type checking to properties""" return property(getter(attr), setter(attr, valid_types), deleter(attr), docs) # Example class class A(object): a = typed("_a", int) # Testing output a1 = A() a1.a = 1 print "Got stored value = " + str(a1.a) a1.a = "1" The results are: ...

January 8, 2013 · Nigel Sim

ZSI -> CXF: Parameters coming in as NULL

Recounting a strange little compatibility issue I had between ZSI 2.0 and CXF 2.0.x. I was using CXF as the server, running from Maven using Jetty, and ZSI as the client. The parameters from the ZSI were arriving at the service implementation as null. With the web services logging turned on I could see that the SOAP packet was arriving OK, and looked good. A little bit of digging round the web turned up this message. The issue was that ZSI wasn’t explicitly namespacing the elements, and so CXF was not seeing them. The solution was to add elementFormDefault=“qualified” to my WSDL definition, and rebuild the ZSI stubs. ...

November 17, 2008 · Nigel Sim

Trac taking a hammering

At work we have one VM which hosts all our project management software like Git, SVN, Trac and Bugzilla. However, recently it has been taking a hammering and essentially crashing. The issue was it was running out of RAM, and swapping like crazy. A little investigation into the situation uncovered that there were some Trac 0.10 CGI processes which were using >500MB of memory. Using the ps ewww -p <pid> command to look at the environment variables of the processes we determined that each of them was serving the same kind of request, SVN changesets. And essentially all of them were being hit by spiders. ...

October 15, 2008 · Nigel Sim

Date sweep

On more than one occasion I've needed to iterate over all dates between two dates, and although the code and concept are trivial it does normally take up 20 minutes to find the exact classes to use, simply because it is a long time between uses. So here's the snippet, more for my own reference, but it will probably be useful to others too. from datetime import date, timedelta step = timedelta(1) # one day at a time start = date(2005,1,1) end = date.today() d = start while d <= end: print d d = d + step

May 17, 2006 · Nigel Sim

HTML table parser using Python

More on the python front. This is my first cut at extracting HTML tables from webpages, and returning them in arrays, using Python. (Files here) The usage would look something like this: import urllib from table_parser import * f = urllib.urlopen('http://myweb.com/data.html') p = TableParser() p.feed(f.read()) f.close() print p.doc # Get to the data

May 17, 2006 · Nigel Sim