View Single Post
Old 01-29-2022, 03:45 PM   #5
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,200
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
It doesn't fail for me.

Is the 'P' in 'Petra' a non-Latin unicode character? I can see something like this happening if it is a unicode composed character.

FWIW: there is some strange (to me) code in your function:
Code:
def evaluate(self, formatter, kwargs, mi, locals, val, col_name, user_cat_prefix):
    new_val = ''
    if hasattr(mi, '_proxy_metadata'):
        # Below should be mi.user_categories
        all_cats = mi._proxy_metadata.user_categories
        cats = {k:v for k,v in all_cats.items() if k.startswith(user_cat_prefix)}
        SEP = mi.metadata_for_field(col_name)['is_multiple'].get('list_to_ui', '')
        new_val = set()
        if SEP:
            val_ = val.split(SEP)
        else:
            val_ = [val]  
        for user_cat, v in cats.items():
            repl = user_cat.lstrip(user_cat_prefix)
            for user_cat_item, src_cat in v:
                if src_cat == col_name:
                    # Why does the next line have the slice?
                    # I think it should be 
                    # for item in val_:
                    for item in val_[:]:
                        if item == user_cat_item:
                            new_val.add(repl)
                            # Why are you removing the item from the list?
                            # Sometimes that will mess up the for loop. Or
                            # at least it could in python 2.
                            val_.remove(item)
                        else:
                            new_val.add(item)
        if new_val:
            return ', '.join(list(new_val))
    return val
I wrote this a long time ago, so I don't remember the details. The slice was intentional to create a new list (a substitute for copy.copy()), because I needed to preserve the original one (I don't remember the details now).

Edit: Looking at the code now, I guess the slicing might have been an attempt to improve performance of the template function, by not having to iterate over items that have been already processed.

The mi._proxy_metadata.user_categories was the way I saw it done in one of the builtin template functions. But since you recommend mi.user_categories(), I will be using that instead.

Last edited by capink; 01-29-2022 at 04:30 PM.
capink is offline   Reply With Quote