![]() |
#1 |
Leftutti
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 549
Karma: 1717097
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
|
Display bug with User Category
There's a strange bug in the book details with my alias column.
The name is cutted off, and it happens with this name only. Edit: additional info Code:
def evaluate(self, formatter, kwargs, mi, locals, val, col_name, user_cat_prefix): new_val = '' if hasattr(mi, '_proxy_metadata'): 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: for item in val_[:]: if item == user_cat_item: new_val.add(repl) val_.remove(item) else: new_val.add(item) if new_val: return ', '.join(list(new_val)) return val Last edited by Wiggo; 01-29-2022 at 12:14 PM. |
![]() |
![]() |
![]() |
#2 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 892
Karma: 810834
Join Date: Sep 2017
Location: Buenos Aires, Argentina
Device: moon+ reader, kindle paperwhite
|
Where do you put the code, I find the idea interesting
|
![]() |
![]() |
Advert | |
|
![]() |
#3 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
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 |
|
![]() |
![]() |
![]() |
#4 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 892
Karma: 810834
Join Date: Sep 2017
Location: Buenos Aires, Argentina
Device: moon+ reader, kindle paperwhite
|
@Chaley where does the code go in the Pseudo custom column???
Last edited by dunhill; 01-29-2022 at 03:08 PM. |
![]() |
![]() |
![]() |
#5 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
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. |
|
![]() |
![]() |
Advert | |
|
![]() |
#7 | ||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Quote:
|
||
![]() |
![]() |
![]() |
#8 | |
Leftutti
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 549
Karma: 1717097
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
|
Quote:
It's the "P" only. I tried different entries changing e.g. "Robert" to "Pobert" and the same thing happened My problem is that I do not know where to look for the error. @capink: Will you update your template? Last edited by Wiggo; 01-30-2022 at 04:13 AM. |
|
![]() |
![]() |
![]() |
#9 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
The problem happens because the template function uses lstrip() to remove the prefix. That function doesn't look for a string but instead strips out each character (case sensitive). Your user category is Pseudo so if an author begins with any of those letters (note that P is one of them) then it will be stripped. If an author name started with d then the same thing will happen.
This fixes it by changing lstrip() to a slice, removing N characters from the beginning where N is the length of the outer user category name passed to the function (user_cat_prefix). As a side benefit it will be a bit faster. Code:
def evaluate(self, formatter, kwargs, mi, locals, val, col_name, user_cat_prefix): new_val = '' if hasattr(mi, '_proxy_metadata'): all_cats = mi.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] prefix_length = len(user_cat_prefix) for user_cat, v in cats.items(): repl = user_cat[prefix_length:] for user_cat_item, src_cat in v: if src_cat == col_name: for item in val_: if item == user_cat_item: new_val.add(repl) val_.remove(item) else: new_val.add(item) if new_val: return ', '.join(list(new_val)) return val |
![]() |
![]() |
![]() |
#10 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
I overlooked this, Thanks for the fix. I am thinking about going with re.sub() in case the user enters a wrong prefix. It will be slower though.
|
![]() |
![]() |
![]() |
#11 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
I looked at that code at least 20 times before I saw it.
![]() Quote:
|
|
![]() |
![]() |
![]() |
#12 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
|
Quote:
|
|
![]() |
![]() |
![]() |
#13 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Ahh, I see. That makes sense. However, don't you need to change/get rid of the dict comprehension on line 5? It ensures that only user categories with the prefix used are put into the "cats" dict, making it impossible for the re.sub() to fail.
|
![]() |
![]() |
![]() |
#14 |
Leftutti
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 549
Karma: 1717097
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
|
@chaley
I don't even know what to say - thank you so much! I think it's really great how you spend your time and help noobs like me out of trouble. |
![]() |
![]() |
![]() |
#15 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Sorry about the noise in the channel. You are welcome. And it was partly for me. I needed to be sure that there wasn't a strange bug somewhere in template processing. |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
[GUI Plugin] User Category | kiwidude | Plugins | 123 | 03-16-2024 11:59 PM |
Is there a way to add a boolean value to a User Category? | ownedbycats | Library Management | 13 | 06-13-2021 09:55 AM |
Move a user Sub-Category into another Category | groob | Library Management | 5 | 06-09-2021 10:22 AM |
User Category Help | rdyornot | Library Management | 0 | 10-10-2017 07:29 PM |
Need Help with User Category Plugin | ommaandnugs | Plugins | 0 | 05-11-2014 02:56 PM |