From 90e7f6b6e1bc562423a90f214f595391f3aac068 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 17 Oct 2020 17:56:21 -0700 Subject: [PATCH] Rewrite tag_export.easybake as a generator. --- etiquette/photodb.py | 3 +++ etiquette/tag_export.py | 18 ++++++++---------- .../backend/endpoints/tag_endpoints.py | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 06e98d2..7569335 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -6,6 +6,7 @@ import random import sqlite3 import tempfile import time +import types from voussoirkit import cacheclass from voussoirkit import configlayers @@ -268,6 +269,8 @@ class PDBCacheManagerMixin: return exp except KeyError: exp = function(**kwargs) + if isinstance(exp, types.GeneratorType): + exp = tuple(exp) self.caches['tag_exports'][key] = exp return exp diff --git a/etiquette/tag_export.py b/etiquette/tag_export.py index b279928..06133c8 100644 --- a/etiquette/tag_export.py +++ b/etiquette/tag_export.py @@ -12,19 +12,20 @@ def easybake(tags, include_synonyms=True, with_objects=False): people.family.mother people.family.mother+mom ''' - lines = [] + tags = sorted(tags) for tag in tags: if with_objects: my_line = (tag.name, tag) else: my_line = tag.name - lines.append(my_line) + + yield my_line if include_synonyms: - syn_lines = [f'{tag.name}+{syn}' for syn in tag.get_synonyms()] + syn_lines = [f'{tag.name}+{syn}' for syn in sorted(tag.get_synonyms())] if with_objects: syn_lines = [(line, tag) for line in syn_lines] - lines.extend(syn_lines) + yield from syn_lines child_lines = easybake( tag.get_children(), @@ -32,13 +33,10 @@ def easybake(tags, include_synonyms=True, with_objects=False): with_objects=with_objects, ) if with_objects: - child_lines = [(f'{tag.name}.{line[0]}', line[1]) for line in child_lines] + child_lines = ((f'{tag.name}.{line[0]}', line[1]) for line in child_lines) else: - child_lines = [f'{tag.name}.{line}' for line in child_lines] - lines.extend(child_lines) - - lines.sort() - return lines + child_lines = (f'{tag.name}.{line}' for line in child_lines) + yield from child_lines def flat_dict(tags, include_synonyms=True): ''' diff --git a/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py b/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py index a59eff8..8d31d9c 100644 --- a/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py @@ -93,12 +93,12 @@ def get_tags_html(specific_tag_name=None): tags = [specific_tag] tag_count = sum(1 for child in specific_tag.walk_children()) - tags = list(common.P.get_cached_tag_export( + tags = common.P.get_cached_tag_export( 'easybake', tags=tags, include_synonyms=False, with_objects=True, - )) + ) response = common.render_template( request,