Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
Old 04-10-2015, 06:37 AM   #1
ivanm
e^bored
ivanm began at the beginning.
 
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.)
ivanm is offline   Reply With Quote
Old 04-10-2015, 07:33 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: 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.
chaley is offline   Reply With Quote
Advert
Old 04-10-2015, 07:48 AM   #3
ivanm
e^bored
ivanm began at the beginning.
 
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?)
ivanm is offline   Reply With Quote
Old 04-10-2015, 08:17 AM   #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: 12,443
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ivanm View Post
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?)
They are in the library, specifically in the metadata.db.

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.
chaley is offline   Reply With Quote
Old 04-10-2015, 09:55 AM   #5
ivanm
e^bored
ivanm began at the beginning.
 
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 )
ivanm is offline   Reply With Quote
Advert
Old 04-10-2015, 10:03 AM   #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: 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
chaley is offline   Reply With Quote
Old 04-10-2015, 10:08 AM   #7
ivanm
e^bored
ivanm began at the beginning.
 
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!
ivanm is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
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


All times are GMT -4. The time now is 11:35 PM.


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