Ran into an error running Justin’s code (http://git.zx2c4.com/PhotoFloat/about/):
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 ./main.py ../web/albums/ ../web/cache/
> PhotoFloat/scanner/main.py(3)
-> from TreeWalker import TreeWalker
(Pdb) b PhotoAlbum.py:42
Breakpoint 1 at PhotoFloat/scanner/PhotoAlbum.py:42
(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/PhotoAlbum.py(42)__cmp__()
-> return cmp(self.date, other.date)
(Pdb) other.date
u’2014:04:04 15:33:38′
(Pdb) self.date
datetime.datetime(1900, 1, 1, 0, 0)
(Pdb) print self.date
1900-01-01 00:00:00
So we need to convert the Unicode date sting to a proper datetime object:
(Pdb) print datetime.strptime(other.date, ‘%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:
#PhotoAlbum.py:41:
def __cmp__(self, other):
if isinstance(other.date, unicode):
return cmp(self.date, datetime.strptime(other.date, ‘%Y:%m:%d %H:%M:%S’))
return cmp(self.date, other.date)
Which brought on the same error from the function above it in PhotoAlbum.py line 40, So I added:
#PhotoAlbum.py:40:
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 PhotoAlbum.py line 346, So I added:
#PhotoAlbum.py:325:
def __cmp__(self, other):
if isinstance(self.date, unicode) and isinstance(other.date, unicode):
date_compare = cmp(datetime.strptime(self.date, ‘%Y:%m:%d %H:%M:%S’), datetime.strptime(other.date, ‘%Y:%m:%d %H:%M:%S’))
elif isinstance(self.date, unicode):
date_compare = cmp(datetime.strptime(self.date, ‘%Y:%m:%d %H:%M:%S’), other.date)
elif isinstance(other.date, unicode):
date_compare = cmp(self.date, datetime.strptime(other.date, ‘%Y:%m:%d %H:%M:%S’))
else:
date_compare = cmp(self.date, other.date)
if date_compare == 0:
return cmp(self.name, other.name)
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!