From dd38db4c4d47b773995d92d60f12c58e05c9927d Mon Sep 17 00:00:00 2001 From: Richard Taylor Date: Tue, 16 Jan 2007 22:25:29 +0000 Subject: [PATCH] faster name display svn: r7919 --- ChangeLog | 5 ++- src/NameDisplay.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 9484e7a40..f88f43852 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ -2007-01-05 Richard Taylor +2007-01-17 Richard Taylor + * src/NameDisplay.py: fast implementation + +2007-01-16 Richard Taylor * src/Filters/_GenericFilter.py: file get_cursor funcs 2007-01-16 Don Allingham diff --git a/src/NameDisplay.py b/src/NameDisplay.py index 54ea09372..0fc232185 100644 --- a/src/NameDisplay.py +++ b/src/NameDisplay.py @@ -30,6 +30,7 @@ Class handling language-specific displaying of names. # #------------------------------------------------------------------------- from gettext import gettext as _ +import re #------------------------------------------------------------------------- # @@ -75,6 +76,8 @@ class NameDisplay: Base class for displaying of Name instances. """ + format_funcs = {} + # FIXME: Is this used anywhere? I cannot see that it is. sort_field = (Name.get_surname, Name.get_surname, Name.get_first_name, Name.get_patronymic, @@ -257,6 +260,87 @@ class NameDisplay: namestr += output[i] return namestr.strip() + + def _gen_func(self, format_str): + d = {"%t":"title", + "%f":"first", + "%p":"prefix", + "%l":"surname", + "%s":"suffix", + "%y":"patronymic", + "%c":"call", + "%T":"title.upper()", + "%F":"first.upper()", + "%P":"prefix.upper()", + "%L":"surname.upper()", + "%S":"suffix.upper()", + "%Y":"patronymic.upper()", + "%C":"call.upper()", + "%%":"'%'"} + + + new_fmt = format_str + + new_fmt = new_fmt.replace("%t","%s") + new_fmt = new_fmt.replace("%f","%s") + new_fmt = new_fmt.replace("%p","%s") + new_fmt = new_fmt.replace("%l","%s") + new_fmt = new_fmt.replace("%s","%s") + new_fmt = new_fmt.replace("%y","%s") + new_fmt = new_fmt.replace("%c","%s") + + new_fmt = new_fmt.replace("%T","%s") + new_fmt = new_fmt.replace("%F","%s") + new_fmt = new_fmt.replace("%P","%s") + new_fmt = new_fmt.replace("%L","%s") + new_fmt = new_fmt.replace("%S","%s") + new_fmt = new_fmt.replace("%Y","%s") + new_fmt = new_fmt.replace("%C","%s") + new_fmt = new_fmt.replace("%%",'%') + + pat = re.compile("%.") + + mat = pat.search(format_str) + + param = () + while mat: + param = param + (d[mat.group(0)],) + mat = pat.search(format_str,mat.end()) + + s = 'def fn(first,surname,prefix,suffix,patronymic,title,call,):\n'\ + ' return "%s" %% (%s)' % (new_fmt,",".join(param)) + + c = compile(s,'','exec') + exec(c) + + return fn + + def _format_str_base1(self,first,surname,prefix,suffix,patronymic, + title,call,format_str): + """ + Generates name from a format string. + + The following substitutions are made: + %t -> title + %f -> given name (first name) + %p -> prefix + %s -> suffix + %l -> family name (last name, surname) + %y -> patronymic + %c -> call name + The capital letters are substituted for capitalized name components. + The %% is substituted with the single % character. + All the other characters in the fmt_str are unaffected. + + """ + + func = self.__class__.format_funcs.get(format_str) + if func == None: + func = self._gen_func(format_str) + self.__class__.format_funcs[format_str] = func + + s = func(first,surname,prefix,suffix,patronymic,title,call) + return ' '.join([ x for x in s.split(" ") if x != '']) #-------------------------------------------------------------------------