diff --git a/etiquette/constants.py b/etiquette/constants.py index 89efb10..33a6669 100644 --- a/etiquette/constants.py +++ b/etiquette/constants.py @@ -147,7 +147,15 @@ for statement in DB_INIT.split(';'): column_names = [x.strip().split(' ')[0] for x in column_names] SQL_COLUMNS[table_name] = column_names -_sql_dictify = lambda columns: {key:index for (index, key) in enumerate(columns)} +def _sql_dictify(columns): + ''' + A dictionary where the key is the item and the value is the index. + Used to convert a stringy name into the correct number to then index into + an sql row. + ['test', 'toast'] -> {'test': 0, 'toast': 1} + ''' + return {key: index for (index, key) in enumerate(columns)} + SQL_INDEX = {key: _sql_dictify(value) for (key, value) in SQL_COLUMNS.items()} diff --git a/etiquette/decorators.py b/etiquette/decorators.py index 2613672..046c59d 100644 --- a/etiquette/decorators.py +++ b/etiquette/decorators.py @@ -60,7 +60,8 @@ def time_me(function): start = time.time() result = function(*args, **kwargs) end = time.time() - print('%s: %0.8f' % (function.__name__, end-start)) + duration = end - start + print('%s: %0.8f' % (function.__name__, duration)) return result return timed_function diff --git a/etiquette/exceptions.py b/etiquette/exceptions.py index 119499f..95e0ca8 100644 --- a/etiquette/exceptions.py +++ b/etiquette/exceptions.py @@ -34,6 +34,7 @@ class EtiquetteException(Exception, metaclass=ErrorTypeAdder): Exception's constructor arguments. ''' error_message = '' + def __init__(self, *args, **kwargs): super().__init__() self.given_args = args @@ -77,6 +78,7 @@ class Exists(EtiquetteException): class AlbumExists(Exists): error_message = 'Album "{}" already exists.' + def __init__(self, album): self.album = album EtiquetteException.__init__(self, album) @@ -86,18 +88,21 @@ class GroupExists(Exists): class PhotoExists(Exists): error_message = 'Photo "{}" already exists.' + def __init__(self, photo): self.photo = photo EtiquetteException.__init__(self, photo) class TagExists(Exists): error_message = 'Tag "{}" already exists.' + def __init__(self, tag): self.tag = tag EtiquetteException.__init__(self, tag) class UserExists(Exists): error_message = 'User "{}" already exists.' + def __init__(self, user): self.user = user EtiquetteException.__init__(self, user) diff --git a/etiquette/helpers.py b/etiquette/helpers.py index b5ac6f1..800e30f 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -140,7 +140,9 @@ def fit_into_bounds(image_width, image_height, frame_width, frame_height): (1920, 1080, 400, 400) -> (400, 225) ''' - ratio = min(frame_width/image_width, frame_height/image_height) + width_ratio = frame_width / image_width + height_ratio = frame_height / image_height + ratio = min(width_ratio, height_ratio) new_width = int(image_width * ratio) new_height = int(image_height * ratio) @@ -193,10 +195,10 @@ def hms_to_seconds(hms): hms = hms.split(':') seconds = 0 if len(hms) == 3: - seconds += int(hms[0])*3600 + seconds += int(hms[0]) * 3600 hms.pop(0) if len(hms) == 2: - seconds += int(hms[0])*60 + seconds += int(hms[0]) * 60 hms.pop(0) if len(hms) == 1: seconds += float(hms[0]) @@ -229,13 +231,11 @@ def read_filebytes(filepath, range_min, range_max, chunk_size=2 ** 20): ''' range_span = range_max - range_min - #print('read span', range_min, range_max, range_span) f = open(filepath, 'rb') f.seek(range_min) sent_amount = 0 with f: while sent_amount < range_span: - #print(sent_amount) chunk = f.read(chunk_size) if len(chunk) == 0: break diff --git a/etiquette/objects.py b/etiquette/objects.py index 830cb63..fb6c6f1 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -762,12 +762,11 @@ class Photo(ObjectBase): size=size, time=timestamp, ) - except: + except Exception: traceback.print_exc() else: return_filepath = hopeful_filepath - if return_filepath != self.thumbnail: data = { 'id': self.id, @@ -1331,7 +1330,7 @@ class Tag(ObjectBase, GroupableMixin): return try: - existing_tag = self.photodb.get_tag(name=new_name) + self.photodb.get_tag(name=new_name) except exceptions.NoSuchTag: pass else: diff --git a/etiquette/photodb.py b/etiquette/photodb.py index b70a9ef..4f34130 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -1313,6 +1313,7 @@ class PhotoDB( etc ''' output_notes = [] + def create_or_get(name): #print('cog', name) try: diff --git a/etiquette/searchhelpers.py b/etiquette/searchhelpers.py index 975a2f1..088eed5 100644 --- a/etiquette/searchhelpers.py +++ b/etiquette/searchhelpers.py @@ -55,7 +55,6 @@ def build_query( if column != 'RANDOM()': notnulls.add(column) - if minimums: for (column, value) in minimums.items(): wheres.add(column + ' >= ' + str(value)) @@ -73,7 +72,7 @@ def build_query( wheres.add(column + ' IS NULL') if wheres: - wheres = 'WHERE ' + ' AND '.join(wheres) + wheres = 'WHERE ' + ' AND '.join(wheres) query.append(wheres) if orderby: