Rewrite tag_export.easybake as a generator.

This commit is contained in:
voussoir 2020-10-17 17:56:21 -07:00
parent c278f3e0af
commit 90e7f6b6e1
3 changed files with 13 additions and 12 deletions

View file

@ -6,6 +6,7 @@ import random
import sqlite3 import sqlite3
import tempfile import tempfile
import time import time
import types
from voussoirkit import cacheclass from voussoirkit import cacheclass
from voussoirkit import configlayers from voussoirkit import configlayers
@ -268,6 +269,8 @@ class PDBCacheManagerMixin:
return exp return exp
except KeyError: except KeyError:
exp = function(**kwargs) exp = function(**kwargs)
if isinstance(exp, types.GeneratorType):
exp = tuple(exp)
self.caches['tag_exports'][key] = exp self.caches['tag_exports'][key] = exp
return exp return exp

View file

@ -12,19 +12,20 @@ def easybake(tags, include_synonyms=True, with_objects=False):
people.family.mother people.family.mother
people.family.mother+mom people.family.mother+mom
''' '''
lines = [] tags = sorted(tags)
for tag in tags: for tag in tags:
if with_objects: if with_objects:
my_line = (tag.name, tag) my_line = (tag.name, tag)
else: else:
my_line = tag.name my_line = tag.name
lines.append(my_line)
yield my_line
if include_synonyms: 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: if with_objects:
syn_lines = [(line, tag) for line in syn_lines] syn_lines = [(line, tag) for line in syn_lines]
lines.extend(syn_lines) yield from syn_lines
child_lines = easybake( child_lines = easybake(
tag.get_children(), tag.get_children(),
@ -32,13 +33,10 @@ def easybake(tags, include_synonyms=True, with_objects=False):
with_objects=with_objects, with_objects=with_objects,
) )
if 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: else:
child_lines = [f'{tag.name}.{line}' for line in child_lines] child_lines = (f'{tag.name}.{line}' for line in child_lines)
lines.extend(child_lines) yield from child_lines
lines.sort()
return lines
def flat_dict(tags, include_synonyms=True): def flat_dict(tags, include_synonyms=True):
''' '''

View file

@ -93,12 +93,12 @@ def get_tags_html(specific_tag_name=None):
tags = [specific_tag] tags = [specific_tag]
tag_count = sum(1 for child in specific_tag.walk_children()) 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', 'easybake',
tags=tags, tags=tags,
include_synonyms=False, include_synonyms=False,
with_objects=True, with_objects=True,
)) )
response = common.render_template( response = common.render_template(
request, request,