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 02-22-2024, 07:46 PM   #1
calibricious
Junior Member
calibricious began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Feb 2024
Device: Kobo Aura One
Incorrect number of arguments for function

Hello everyone,

Let me preface this with: I have about 24 hours experience with Calibre templating language, so forgive me if I am missing something obvious. I have tried to search the forum and the internet, but found nothing.

The problem

I have written a function maketitle which takes in 6 arguments. For almost all books, it works perfectly, but for a few select cases I get the error
Code:
EXCEPTION: Incorrect number of arguments for function maketitle
Details

Code:
def evaluate(self, formatter, kwargs, mi, locals, title, author, series, series_index, category, pairing):
[...]
  • The function takes in 6 arguments: title, author, series, series_index, category, pairing
    • "category" and "pairing" are custom created columns (values only ever contain 1 word with [A-Za-z]*)
  • I wrote the function in Python, but am trying to use it for formatting book titles when I send them to my Kobo. So I guess I'm using it in basic template mode (?)

Debugging

Since it is a basic template, I am running it on the field title
Code:
{title:maketitle({author},{series},{series_index},{#category},{#pairing})}
I am getting the following error code in some instances:
Code:
EXCEPTION: Incorrect number of arguments for function maketitle
I saw a file named after this error on my Kobo, so I decided to create a column with the same exact template, to visualise where it was going wrong.

I have noticed 2 things:
  1. It fails on 3 books (out of over 100) in the column
    • What these books have in common is that their author field contains commas (to be precise, exactly 1 comma)
  2. It fails on other books on the Kobo
    • I am unsure of how many or which, because every new error overwrites the previously generated file with the error title
    • I am sure these are files OTHER than the aforementioned 3 books, because I noticed it when I tried to send all files except for the ones with errors

I have also tested removing and adding fields, to see how many arguments it was effectively taking in. Removing 1 argument solved the issue for the 3 books.

My conclusion was that, at least for the column in Calibre, the comma in the authors list is being interpreted not literally but as part of the code. Therefore it is separating "authors" into multiple arguments sent into the function.

What I am looking for

1. Fields as literals (escaping)?

My first instinct was that I needed to pass the field names as literals, so they don't get split into multiple arguments.

I've tried the syntax
Code:
{title:maketitle(`{author}`,{series},{series_index},{#category},{#pairing})}
Nothing really changed. I also tried escaping everything (except for title). Still the same error
Code:
Incorrect number of arguments for function maketitle

2. Alternative way to use my Python functions in templates

I don't know much about Calibre's templating system, so maybe there's an easier way to do this. I'm open to suggestions.

All I want is to have my makefile function do its job, which is to determine the subdirectory where the book will be saved and establish which data goes into the title and which doesn't. I also want this to work for saving books to disk/device.
calibricious is offline   Reply With Quote
Old 02-23-2024, 06:25 AM   #2
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I strongly recommend you look at the template language manual.

You are breaking a major rule for Single Function Mode:
Quote:
Do not use subtemplates (`{ … }`) as function arguments. Instead, use Template Program Mode and General Program Mode.
In addition, the Single Function Mode documentation says:
Quote:
Important: If you have programming experience, please note that the syntax in Single Function Mode is not what you expect. Strings are not quoted and spaces are significant. All arguments are considered to be constants; there are no expressions.
My suspicion is that in your situation using Template Program Mode is easiest. Something like
Code:
{title:'maketitle($, $author, $series, $series_index, $#category, $#pairing)'}
chaley is offline   Reply With Quote
Old 02-23-2024, 04:02 PM   #3
calibricious
Junior Member
calibricious began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Feb 2024
Device: Kobo Aura One
First and foremost, THANK YOU for taking the time to help me.

Quote:
Originally Posted by chaley View Post
I strongly recommend you look at the template language manual.

You are breaking a major rule for Single Function Mode:
Believe it or not, I DID look at the manual a bunch of times.
The problem is: I didn't really understand that rule, specifically on how I could avoid doing that, until you showed me the alternative.


Quote:
Originally Posted by chaley View Post
My suspicion is that in your situation using Template Program Mode is easiest. Something like
Code:
{title:'maketitle($, $author, $series, $series_index, $#category, $#pairing)'}
Your suggestion has worked like a charm! Once again, thank you so much!



I also must admit that, although I understand there are 3 different modes, I don't fully understand:
  1. How to specify which mode I'm using (is it just syntax?)
  2. General Programming Mode. Honestly, I just didn't understand the grammar code block in the man page at all
  3. What use cases are adequate for Template Program Mode and which ones are for General Programming Mode
  4. When I can and can't use which of them (and whether I can mix them)
calibricious is offline   Reply With Quote
Old 02-23-2024, 04:41 PM   #4
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by calibricious View Post
I also must admit that, although I understand there are 3 different modes, I don't fully understand:
  • How to specify which mode I'm using (is it just syntax?)
Yes. TPM is differentiated from SFM by using quotes in specific places (see the manual). GPM templates start with the string "program:". Python templates (PTM) start with the string "python:"
Quote:
  • General Programming Mode. Honestly, I just didn't understand the grammar code block in the man page at all.
It is a fairly standard programming language syntax, with some stuff borrowed from Algol68 to disambiguate block structure. The grammar defines precedence and nesting.

The manual has lots of examples of GPM templates that should help. Look at those and use the calibre Template tester to experiment.
Quote:
  • What use cases are adequate for Template Program Mode and which ones are for General Programming Mode
GPM is best when the work to be done is complex. TPM is best if the work is simple and you need nesting, you want to use conditional text, or the template exists as part of a larger template expression. PTM is used when you need performance or want to use calibre's APIs.

Personally, I always use GPM or PTM to avoid syntax traps and task complexity outgrowing the other modes. Sometimes I use TPM that calls stored templates (Preferences / Advanced / Template functions) for the complex stuff and where I want to reuse the computation.
Quote:
  • When I can and can't use which of them (and whether I can mix them)
You can use any mode where you can use a template. You can't use GPM or python templates in template expressions, e.g.,
Code:
{tags} {#some_column:some_function()} {title:'maketitle($, $author, $series, $series_index, $#category, $#pairing)'}
There are many examples of templates in the manual, in MobileRead Template recipies thread, in Templates: various questions not worth their own thread, and under several of the spoilers the Template language changes thread's first post.
chaley is offline   Reply With Quote
Old 02-23-2024, 05:29 PM   #5
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: 8,639
Karma: 61234567
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
@calibricious - just want to mention I love that username
ownedbycats is offline   Reply With Quote
Old 02-23-2024, 05:43 PM   #6
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
BTW: the grammar is written in Extended Backus–Naur form.

Last edited by chaley; 02-23-2024 at 05:46 PM.
chaley is offline   Reply With Quote
Old 02-23-2024, 06:16 PM   #7
calibricious
Junior Member
calibricious began at the beginning.
 
Posts: 3
Karma: 10
Join Date: Feb 2024
Device: Kobo Aura One
Talking

Quote:
Originally Posted by chaley View Post
BTW: the grammar is written in Extended Backus–Naur form.

Thank you so much for the answers! I really appreciate all the guidance (and special thanks for mentioning the Algol background + EBNF, it tickled my brain in all the right ways).


Quote:
Originally Posted by ownedbycats View Post
@calibricious - just want to mention I love that username

Thank you As someone who is also owned by a cat, I must say: right back at ya
calibricious is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenate arguments in a replace function - Save to disk Horus68 Library Management 2 10-03-2019 12:13 PM
Random number in Regex Function? nqk Editor 2 05-23-2017 11:47 PM
Calibre 1.33 - incorrect version number? massenz Calibre 1 04-20-2014 01:16 AM
Nesting Function arguments in custom columns da_jane Calibre 1 11-21-2012 02:48 PM
Agency (ebook) pricing gone from arguments on MR to arguments in court. RichL News 3 12-07-2011 07:40 AM


All times are GMT -4. The time now is 10:18 AM.


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