![]() |
#1 |
e^bored
![]() Posts: 44
Karma: 10
Join Date: Jun 2010
Device: Kobo Aura HD, BeBook Neo
|
Loop over list
Calibre already has the list_re function to be able to apply a regex to every element of a list, but is there any way of applying other functions to list elements?
(Specifically I want to be able to apply shorten to every element of a list; I want to be able to achieve something like the sample program listing in the Calibre template language reference but my series column is often a `.' separated list of nested series.) |
![]() |
![]() |
![]() |
#2 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
No, there isn't a way to do that in the template language. For various technical reasons it isn't possible (requires deferred evaluation).
You can of course do this in a custom template function (Preferences / Template Functions) where you have full python available. You can call standard template functions in a custom function, but it is enough of a pain that you might be better off doing your own shorten. |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
e^bored
![]() Posts: 44
Karma: 10
Join Date: Jun 2010
Device: Kobo Aura HD, BeBook Neo
|
That's what I was afraid of.
Are custom template functions stored in the library or in the preferences? (That is, if I just copy my ~/Calibre Library/ directory to another computer, will that include the custom function?) |
![]() |
![]() |
![]() |
#4 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Another way to do this is to write a "calibre" command line python script that does the work on all the books. That way you could run it on any library you want. There is an example of such a script in this thread. |
|
![]() |
![]() |
![]() |
#5 |
e^bored
![]() Posts: 44
Karma: 10
Join Date: Jun 2010
Device: Kobo Aura HD, BeBook Neo
|
So I've tried writing my own function that applies that entire shortening/initialisation process over each element of a series list, but I when I try and use it I get the error "EXCEPTION: global name re is not defined"; adding "import re" at the top of the definition didn't work.
Alternatively, how do I call standard template functions in a custom function? The documentation just says that all instructions are in the dialog, and it just says the required syntax, not how to call other functions. (I haven't done much Python for a while; I usually use Haskell, so I'm not used to trying to find dynamically-typed values in an object ![]() |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Here is a template function that uses re. Don't worry about what it does.
![]() Code:
def evaluate(self, formatter, kwargs, mi, locals, val, is_read_pct, is_reading_str, no_page_read_str): try: test_val = int(is_read_pct) except: return 'is_read_pct is not a number' import re pattern = u'.*(\d+[-/]\d+[-/]\d+).*?Dernière page lue : Emplacement \d+ \((\d+)%\)' mg = re.match(pattern, val, re.U + re.I + re.DOTALL); if mg is None: return no_page_read_str date = mg.group(1) pct = mg.group(2) try: f = int(pct) if f > test_val: return date elif f > 0: return is_reading_str + ': ' + pct + '%' except: pass return no_page_read_str |
![]() |
![]() |
![]() |
#7 |
e^bored
![]() Posts: 44
Karma: 10
Join Date: Jun 2010
Device: Kobo Aura HD, BeBook Neo
|
Wait, you can do an import mid-code in Python?
![]() shakes head at the silly language Anyway, ta muchly chaley! |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Kobo Touch in an on-going loop. | Mohan-V-Allied | Kobo Reader | 8 | 06-28-2014 08:34 AM |
Edge Reboot Loop | nikkie | enTourage eDGe | 22 | 10-08-2011 07:12 PM |
Caught in registration loop | sma | enTourage eDGe | 2 | 09-24-2011 11:48 AM |
Out of the loop and need help! | mklynds | Which one should I buy? | 26 | 05-24-2011 10:58 PM |