Fix the sorting issue I thought was fixed years ago.

I've got to re-write this software someday :/
This commit is contained in:
Colin Davis 2015-09-02 03:00:32 -04:00
parent 63e9f3f69c
commit 049fa5aabb
2 changed files with 13 additions and 7 deletions

View File

@ -1,2 +1,3 @@
pillow pillow
tornado tornado
natsort

View File

@ -2,6 +2,7 @@
import os import os
import hashlib import hashlib
from PIL import Image from PIL import Image
import natsort
class Robohash(object): class Robohash(object):
""" """
@ -73,7 +74,7 @@ class Robohash(object):
self.hasharray.append(int(self.hexdigest[currentstart:currentend],16)) self.hasharray.append(int(self.hexdigest[currentstart:currentend],16))
def _listdirs(self,path): def _listdirs(self,path):
return [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))] return [d for d in natsort.natsorted(os.listdir(path)) if os.path.isdir(os.path.join(path, d))]
def _get_list_of_files(self,path): def _get_list_of_files(self,path):
""" """
@ -84,17 +85,19 @@ class Robohash(object):
# Get a list of all subdirectories # Get a list of all subdirectories
directories = [] directories = []
for root, dirs, files in os.walk(path, topdown=False): for root, dirs, files in natsort.natsorted(os.walk(path, topdown=False)):
for name in dirs: for name in dirs:
if name[:1] is not '.': if name[:1] is not '.':
directories.append(os.path.join(root, name)) directories.append(os.path.join(root, name))
directories = natsort.natsorted(directories)
# Go through each directory in the list, and choose one file from each. # Go through each directory in the list, and choose one file from each.
# Add this file to our master list of robotparts. # Add this file to our master list of robotparts.
for directory in directories: for directory in directories:
files_in_dir = [] files_in_dir = []
for imagefile in os.listdir(directory): for imagefile in natsort.natsorted(os.listdir(directory)):
files_in_dir.append(os.path.join(directory,imagefile)) files_in_dir.append(os.path.join(directory,imagefile))
files_in_dir = natsort.natsorted(files_in_dir)
# Use some of our hash bits to choose which file # Use some of our hash bits to choose which file
element_in_list = self.hasharray[self.iter] % len(files_in_dir) element_in_list = self.hasharray[self.iter] % len(files_in_dir)
@ -152,13 +155,15 @@ class Robohash(object):
# First, we'll get a list of parts of our robot. # First, we'll get a list of parts of our robot.
roboparts = self._get_list_of_files(self.resourcedir + 'sets/' + roboset) roboparts = self._get_list_of_files(self.resourcedir + 'sets/' + roboset)
print(roboparts)
# Now that we've sorted them by the first number, we need to sort each sub-category by the second. # Now that we've sorted them by the first number, we need to sort each sub-category by the second.
roboparts.sort(key=lambda x: x.split("#")[1]) roboparts.sort(key=lambda x: x.split("#")[1])
print(roboparts)
if bgset is not None: if bgset is not None:
bglist = [] bglist = []
backgrounds = os.listdir(self.resourcedir + 'backgrounds/' + bgset) backgrounds = natsort.natsorted(os.listdir(self.resourcedir + 'backgrounds/' + bgset))
backgrounds.sort() backgrounds.sort()
for ls in backgrounds: for ls in backgrounds:
if not ls.startswith("."): if not ls.startswith("."):