Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 04-13-2023, 01:31 PM   #571
Levinas2021
Junior Member
Levinas2021 began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Jan 2021
Device: Kobo Clara
Ranking Position

Hello everyone,

I'm using Calibre Portable to manage my ebooks and I have a column named Ranking with a numeric value for each book.
I would like to create a new column called Ranking Position that displays the position of each book based on their value in the Ranking column (book with highest value has position number 1).
I'm not very familiar with Calibre's functions, so I was wondering if anyone could help me with the steps to populate this new column automatically.

Thank you in advance!



Last edited by Levinas2021; 04-13-2023 at 01:54 PM.
Levinas2021 is offline   Reply With Quote
Old 04-13-2023, 01:50 PM   #572
DNSB
Bibliophagist
DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.DNSB ought to be getting tired of karma fortunes by now.
 
DNSB's Avatar
 
Posts: 46,313
Karma: 169098492
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
Quote:
Originally Posted by Levinas2021 View Post
Hello everyone,

I'm using Calibre Portable to manage my ebooks and I have a column named Ranking with a numeric value for each book.
I would like to create a new column called Ranking Position that displays the position of each book based on their value in the Ranking column (book with highest value has position number 1).
I'm not very familiar with Calibre's functions, so I was wondering if anyone could help me with the steps to populate this new column automatically.

Thank you in advance!

You need to use the image link not the webpage link to show the image.

i.e. [IMG]https://i.imgur.com/gr3wMI2.jpeg[/IMG] instead of [IMG]https://i.imgur.com/gr3wMI2[/IMG]

DNSB is offline   Reply With Quote
Old 04-13-2023, 02:05 PM   #573
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,993
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Small unrelated question:

{field:|prefix_text|suffix_text}

Seeing as it's used for the separator, it possible to use a pipe as either prefix_ or suffix_? I got an error even when I enclosed it in single or double-quotes.
ownedbycats is offline   Reply With Quote
Old 04-13-2023, 03:25 PM   #574
isarl
Addict
isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.
 
Posts: 287
Karma: 2534928
Join Date: Nov 2022
Location: Canada
Device: Kobo Aura 2
Quote:
Originally Posted by Levinas2021 View Post
Hello everyone,

I'm using Calibre Portable to manage my ebooks and I have a column named Ranking with a numeric value for each book.
I would like to create a new column called Ranking Position that displays the position of each book based on their value in the Ranking column (book with highest value has position number 1).
I'm not very familiar with Calibre's functions, so I was wondering if anyone could help me with the steps to populate this new column automatically.

Thank you in advance!


I don't believe Calibre's built-in functions have a useful way of ranking values, but Python does, if you're willing to bust out the big guns. Here is an example which I think will do approximately what you are after. I am assuming that your “ranking” is better the higher it is. If I've made the wrong assumption here, and a lower “ranking” value means a better-ranked book, then simply change reverse=True to reverse=False. The title:true argument is merely meant to represent a search query which will return all books. The ampersand may be replaced with any arbitrary value that will never appear within the values you are looking up; make sure to replace it in both places on that first (3rd) line of code. Finally, if you wish to restrict your search to the current virtual library, replace that 0 with a 1. Finally, this code will assign the top rank a value of 1 – without the “1 +” on the last line, the top-ranked book would have a rank of 0.

Code:
python:
def evaluate(book, context):
	vals = context.funcs.book_values('#ranking', 'title:true', '&', 0).split('&')
	vals = sorted([float(v) for v in vals], reverse=True)
	return str(1 + vals.index(float(book.get('#ranking'))))
Warning: if you have multiple books with an identical ranking, the above code will NOT correctly count ties. For example, if your top rank is shared by two books, the next rank will be 2, not 3. If you wish to fix this error then you will have to modify the code; the error arises because the book_values() function returns unique values. In order to have it count correctly in the case of ties, you will need to count the multiplicity of each value in your library, and add that many copies of the value to the vals list. I'm ignoring this because it seems like your ranking value has enough precision to easily avoid duplicates, but that assumption may be incorrect.
isarl is offline   Reply With Quote
Old 04-13-2023, 06:48 PM   #575
Levinas2021
Junior Member
Levinas2021 began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Jan 2021
Device: Kobo Clara
Hi everyone,

I'm having trouble with adding code to the "model" field (column Ranking Position) in my application.
I've tried adding the code to the field, but it keeps giving me an error.
I'm not sure what I'm doing wrong.

I've attached some screenshots to show what I've tried.
When I test the function in the "Edit Model" option, it works fine and returns the value "1" (Screenshot 1).
However, when I try to use it in the "model" field for the column Ranking Position, it returns an error (Screenshots 2 and 3).

Can anyone help me understand what I'm doing wrong and how I can successfully add my code to the "model" field?
I would greatly appreciate any guidance or suggestions.

Thank you!

Screenshot 1



Screenshots 2 and 3

Click image for larger version

Name:	uIJgCLW.jpg
Views:	435
Size:	59.8 KB
ID:	201024
Click image for larger version

Name:	S5uDpsN.jpg
Views:	457
Size:	22.2 KB
ID:	201025

Last edited by issybird; 04-13-2023 at 07:44 PM.
Levinas2021 is offline   Reply With Quote
Old 04-14-2023, 10:02 AM   #576
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Levinas2021 View Post
Hi everyone,

I'm having trouble with adding code to the "model" field (column Ranking Position) in my application.
I've tried adding the code to the field, but it keeps giving me an error.
I'm not sure what I'm doing wrong.

I've attached some screenshots to show what I've tried.
When I test the function in the "Edit Model" option, it works fine and returns the value "1" (Screenshot 1).
However, when I try to use it in the "model" field for the column Ranking Position, it returns an error (Screenshots 2 and 3).

Can anyone help me understand what I'm doing wrong and how I can successfully add my code to the "model" field?
I would greatly appreciate any guidance or suggestions.

Thank you!
The error tells you that the book_values() function can't be used in a composite column. See https://www.mobileread.com/forums/sh...35#post4235735 for how to get around this.

The problem is that those functions can be slow. Since you are writing in Python you would be better served using the calibre database API, which is much faster. Glancing at your template I think you want
Code:
context.db.new_api.get_custom_book_data('#ranking')
chaley is offline   Reply With Quote
Old 04-14-2023, 01:08 PM   #577
isarl
Addict
isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.
 
Posts: 287
Karma: 2534928
Join Date: Nov 2022
Location: Canada
Device: Kobo Aura 2
Quote:
Originally Posted by chaley View Post
The error tells you that the book_values() function can't be used in a composite column. See https://www.mobileread.com/forums/sh...35#post4235735 for how to get around this.

The problem is that those functions can be slow. Since you are writing in Python you would be better served using the calibre database API, which is much faster. Glancing at your template I think you want
Code:
context.db.new_api.get_custom_book_data('#ranking')
Thanks for this Charles, I'm not very experienced at writing templates which query the rest of the library, so I haven't encountered this yet and just tinkered in the template tester until it appeared to work well enough.
isarl is offline   Reply With Quote
Old 04-15-2023, 05:08 PM   #578
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,993
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quote:
Originally Posted by ownedbycats View Post
Small unrelated question:

{field:|prefix_text|suffix_text}

Seeing as it's used for the separator, it possible to use a pipe as either prefix_ or suffix_? I got an error even when I enclosed it in single or double-quotes.
Since this slightly got buried in the other conversation, any idea? I also tried using escape character on the pipe but that didn't seem to help.
ownedbycats is offline   Reply With Quote
Old 04-15-2023, 11:11 PM   #579
isarl
Addict
isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.
 
Posts: 287
Karma: 2534928
Join Date: Nov 2022
Location: Canada
Device: Kobo Aura 2
Quote:
Originally Posted by ownedbycats View Post
Since this slightly got buried in the other conversation, any idea? I also tried using escape character on the pipe but that didn't seem to help.
Use general program mode instead? I can't find anything in the docs about escaping pipe characters.
isarl is offline   Reply With Quote
Old 04-15-2023, 11:17 PM   #580
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,993
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Yes, I think thsts probably the only way.
ownedbycats is offline   Reply With Quote
Old 04-16-2023, 05:41 AM   #581
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Since this slightly got buried in the other conversation, any idea? I also tried using escape character on the pipe but that didn't seem to help.
Quote:
Originally Posted by isarl View Post
Use general program mode instead? I can't find anything in the docs about escaping pipe characters.
Quote:
Originally Posted by ownedbycats View Post
Yes, I think thsts probably the only way.
Do it in GPM using finish_formatting().

Example:
Code:
program: finish_formatting($series_index, '02s', ' |', '| ')
Make it a stored template if you want to use it in SFM or TPM templates.
chaley is offline   Reply With Quote
Old 04-16-2023, 07:44 AM   #582
Levinas2021
Junior Member
Levinas2021 began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Jan 2021
Device: Kobo Clara
The code would be something like this?

python:
def evaluate(book, context):
context.db.new_api.get_custom_book_data('#ranking' )
vals = sorted([float(v) for v in vals], reverse=True)
return str(1 + vals.index(float(book.get('#ranking'))))
Levinas2021 is offline   Reply With Quote
Old 04-16-2023, 05:38 PM   #583
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,993
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quote:
Originally Posted by chaley View Post
Do it in GPM using finish_formatting().

Example:
Code:
program: finish_formatting($series_index, '02s', ' |', '| ')
Make it a stored template if you want to use it in SFM or TPM templates.
Thanks. How do I get a stored template into an SFM template, anyways?

Example: I had this SFM template for a save path:

Code:
{#kobopath}/{author_sort:sublist(0,1,&)}/{title} - {authors}
I copied the GPM template for #kobopath to a new template, device_path().

{device_path()} isn't a field, and entering it without the squiggly brackets just adds it literally

I looked through the template page but couldn't find this
ownedbycats is offline   Reply With Quote
Old 04-16-2023, 05:47 PM   #584
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
How do I get a stored template into an SFM template, anyways?

Example: I had this SFM template for a save path:

Code:
{#kobopath}/{author_sort:sublist(0,1,&)}/{title} - {authors}
I copied the GPM template for #kobopath to a new template, device_path().

{device_path()} isn't a field, and entering it without the squiggly brackets just adds it literally

I looked through the template page but couldn't find this
To call stored templates that want no arguments, use this TPM utterance:
Code:
{:'device_path()'}
This says that for the "null" field, call the function device_path() in TPM mode.
chaley is offline   Reply With Quote
Old 04-16-2023, 05:57 PM   #585
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,993
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Thanks

I did a ctrl+F for "{:" and found only a brief example buried in the middle of the approximate_formats() entry. No wonder I missed it
ownedbycats is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Library Management: various questions not worth their own thread ownedbycats Library Management 224 07-25-2025 02:16 PM
[Metadata Source Plugin] Questions regarding parse select, docs and ref templates Boilerplate4U Development 13 07-07-2020 02:35 AM
Questions on Kobo [Interfered with another thread topic] spdavies Kobo Reader 8 10-12-2014 11:37 AM
[OLD Thread] Some questions before buying the fire. darthreader13 Kindle Fire 7 05-10-2013 09:19 PM
Thread management questions meme Feedback 6 01-31-2011 05:07 PM


All times are GMT -4. The time now is 12:25 PM.


MobileRead.com is a privately owned, operated and funded community.