Skip to content

Commit

Permalink
Another checkpoint: got sorting working.
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jun 21, 2010
1 parent a223a83 commit a473a42
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
50 changes: 48 additions & 2 deletions photosorter-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def direction():
photos = [1, 2, 3]

for b in p.next_bucket():
b.unsorted = photos
b.unsorted = set(photos)
break

for b in p.next_bucket():
Expand All @@ -147,11 +147,57 @@ def direction():
self.assertEquals(len(b.unknown), 1)
break

p.reconcile_buckets()
self.assertEquals(p.buckets[1].before, p.buckets[0].unsorted)
self.assertEquals(p.buckets[1].after, p.buckets[2].unsorted)
self.assertEquals(p.buckets[1].unknown, set([2]))

def test_mediumReconcileBuckets(self):
"""More advanced case of reconciling several buckets"""
def direction():
values = [-1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1]
for i in values:
yield i

def photosByBucket(p, bucket):
retv = []
for i in p:
if p[i] == bucket:
retv.append(i)
return retv

p = PhotoSorter(loadFromDisk=False, dumpToDisk=False)
p.buckets = [Bucket(i) for i in [1, 2, 3, 4]]
photos = {
# photo number: bucket
1: 3,
2: 2,
3: 4,
4: 2,
5: 1,
6: 4,
7: 1,
8: 4,
9: 4,
}

for b in p.next_bucket():
b.unsorted = set(photos)
break

for b in p.next_bucket():
for photo in p.next_photo():
if photos[photo] >= int(b.name):
p.sort_photo(photo, b, 1)
elif photos[photo] < int(b.name):
p.sort_photo(photo, b, -1)
else:
p.sort_photo(photo, b, 0)

for b in p.next_bucket():
self.assertEquals(sorted(list(b.during)), sorted(photosByBucket(photos, int(b.name))))




if __name__ == "__main__":
unittest.main()
Expand Down
60 changes: 26 additions & 34 deletions photosorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,24 @@ def reconcile_buckets(self):
move those into the appropriate bucket. If a photo is after and before
adjacent buckets, add it to the "during" list. Kind of an expensive
operation and there's probably a better way to do this in-line, especially
since this is called on every sort operation."""
since this is called on every sort operation.
Subtle thing: photos can be before the 1st bucket, but can't be after the
last bucket. They can only be during the last bucket, since there's no
upper bound to it really."""

for i in range(0, len(self.buckets)-1):
for i in range(0, len(self.buckets)):
# be careful at the beginning and end of the list
if i == 0:
self.buckets[i].unsorted |= self.buckets[i+1].before
for item in self.buckets[i].after:
if item in self.buckets[i+1].before:
self.buckets[i].during.add(item)
self.buckets[i+1].before.remove(item)

elif i == len(self.buckets)-1:
self.buckets[i].unsorted |= self.buckets[i-1].after
self.buckets[i].during |= self.buckets[i].after

else:
# prep adjacent buckets for sorting; elements before
Expand All @@ -244,6 +253,11 @@ def reconcile_buckets(self):
self.buckets[i-1].unsorted |= self.buckets[i].before
self.buckets[i+1].unsorted |= self.buckets[i].after

for item in self.buckets[i].after:
if item in self.buckets[i+1].before:
self.buckets[i].during.add(item)
self.buckets[i+1].before.remove(item)



class PhotoSorterGui(object):
Expand Down Expand Up @@ -323,56 +337,34 @@ def redraw_window(self):
def keyboard_command(self, widget, event):
try:
if chr(event.keyval).upper() == "A":
print self.properties
pass

if chr(event.keyval).upper() == "B":
self.update_bucket()
pass

if chr(event.keyval).upper() == "N":
self.image.set_from_file(self.next_file(filter=False))
pass

if chr(event.keyval).upper() == "P":
self.image.set_from_file(self.prev_file(filter=False))
pass

if chr(event.keyval) == "1":
self.properties[current]["sort"][bucket] = "Before"
next = self.next_file()
if next is not None:
self.image.set_from_file(next)
else:
self.update_bucket()
self.image.set_from_file(self.next_file(filter=False))
pass

if chr(event.keyval) == "2":
self.properties[current]["sort"][bucket] = "After"
next = self.next_file()
if next is not None:
self.image.set_from_file(next)
else:
self.update_bucket()
self.image.set_from_file(self.next_file(filter=False))
pass

if chr(event.keyval) == "3":
self.properties[current]["sort"][bucket] = "Unknown"
next = self.next_file()
if next is not None:
self.image.set_from_file(next)
else:
self.update_bucket()
self.image.set_from_file(self.next_file(filter=False))
pass

if chr(event.keyval).upper() == "D":
try:
print self.properties[current]
except KeyError:
print "No properties for %s" % current
pass

if chr(event.keyval).upper() == "L":
self.rotate_clockwise(CURRENT_IMAGE)

pass

if chr(event.keyval).upper() == "H":
self.flip_horizontal(CURRENT_IMAGE)
pass

if chr(event.keyval).upper() == "Q":
self.quit(None, None)
Expand Down

0 comments on commit a473a42

Please sign in to comment.