DateTime TypeError – Unicode

Ran into an error running Justin’s code (

TypeError: can’t compare datetime.datetime to unicode

(When I uploaded a newly edited PNG from GIMP and ‘scan’ updated my cache).

So I did a quick bit of debugging:

PhotoFloat/scanner# python -m pdb ./ ../web/albums/ ../web/cache/
> PhotoFloat/scanner/
-> from TreeWalker import TreeWalker
(Pdb) b
Breakpoint 1 at PhotoFloat/scanner/
(Pdb) r
2014-04-05T01:46:12.970163 [walking] albums
2014-04-05T01:46:12.970884 |–[walking] Months
2014-04-05T01:46:13.000626 |–[full cache] Months
2014-04-05T01:46:13.001210 |–[caching] Months
2014-04-05T01:46:13.010905 |–[metainfo] free-bsd-wallpaperEllie.png
2014-04-05T01:46:13.019080 |–[thumbing] free-bsd-wallpaperEllie.png -> 75px, square
2014-04-05T01:46:13.019647 |–[thumbing] free-bsd-wallpaperEllie.png -> 150px, square
2014-04-05T01:46:13.020161 |–[thumbing] free-bsd-wallpaperEllie.png -> 640px
2014-04-05T01:46:13.020739 |–[thumbing] free-bsd-wallpaperEllie.png -> 800px
2014-04-05T01:46:13.021310 |–[thumbing] free-bsd-wallpaperEllie.png -> 1024px
2014-04-05T01:46:13.021684 |–[walking] Funny
2014-04-05T01:46:13.022194 |–[empty] Funny
2014-04-05T01:46:13.022380 [caching] albums
> /usr/local/www/nginx-dist/ellie/photoalbum/PhotoFloat/scanner/
-> return cmp(,
u’2014:04:04 15:33:38′
datetime.datetime(1900, 1, 1, 0, 0)
(Pdb) print
1900-01-01 00:00:00

So we need to convert the Unicode date sting to a proper datetime object:

(Pdb) print datetime.strptime(, ‘%Y:%m:%d %H:%M:%S’).strftime(‘%Y-%m-%d %H:%M:%S’)
2014-04-04 15:33:38

And we need __cmp__() to handle it too:
def __cmp__(self, other):
if isinstance(, unicode):
return cmp(, datetime.strptime(, ‘%Y:%m:%d %H:%M:%S’))
return cmp(,

Which brought on the same error from the function above it in line 40, So I added:
elif isinstance(self._photos[-1].date, unicode) and isinstance(self._albums[-1].date, unicode):
return max(datetime.strptime(self._photos[-1].date, ‘%Y:%m:%d %H:%M:%S’), datetime.strptime(self._albums[-1].date, ‘%Y:%m:%d %H:%M:%S’))
elif isinstance(self._photos[-1].date, unicode):
return max(datetime.strptime(self._photos[-1].date, ‘%Y:%m:%d %H:%M:%S’), self._albums[-1].date)
elif isinstance(self._albums[-1].date, unicode):
return max(self._photos[-1].date, datetime.strptime(self._albums[-1].date, ‘%Y:%m:%d %H:%M:%S’))

Which brought on the same error from the function below it in line 346, So I added:
def __cmp__(self, other):
if isinstance(, unicode) and isinstance(, unicode):
date_compare = cmp(datetime.strptime(, ‘%Y:%m:%d %H:%M:%S’), datetime.strptime(, ‘%Y:%m:%d %H:%M:%S’))
elif isinstance(, unicode):
date_compare = cmp(datetime.strptime(, ‘%Y:%m:%d %H:%M:%S’),
elif isinstance(, unicode):
date_compare = cmp(, datetime.strptime(, ‘%Y:%m:%d %H:%M:%S’))
date_compare = cmp(,
if date_compare == 0:
return cmp(,
return date_compare

After that, ‘scan’ ran without issue.
Looking back, this probably could have been handled more elegantly utilizing Decorators or something, but – It works!

Leave a Reply

Your email address will not be published. Required fields are marked *