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 09-29-2018, 01:44 PM   #1
sweth
Connoisseur
sweth began at the beginning.
 
Posts: 67
Karma: 10
Join Date: May 2015
Device: Kobo Aura H20, Kindle Fire 7 (7G), Kindle PW (2G), Nook GlowLight Plus
Arrays, capturing parens in GPM?

Is there any support for arrays in general program mode, to allow for doing things like summing multiple values without having the chain together a bunch of add()s (especially if the total number of values to add isn't known)? And, related, is there any way to use capturing parens in regexes (to populate an array)?

For a specific use case: whenever someone recommends a book to me, I create a stub entry in Calibre and tag it "recommended.persons-name.N" where N is a number reflecting how enthusiastic the recommendation was. Rather than having to periodically look through those formatless records periodically to see which ones were highly recommended, and go get those books, I'd love to have a custom column that could just show the sum of all of the Ns for that book. The closest I've been able to come up with, though, is

Code:
                        re(
                            list_re(
                                tags,
                                ',',
                                'recommended\..*\.[\d.]+',
                                ''
                            ),
                            'recommended\..*\.', ''
                        ),
which extracts the N from the first tag of that sort, but completely ignores subsequent ones. From the descriptions of re_group and list_re_group, I think those *might* allow some type of use of capturing parens that might obviate that re(list_re()) ugliness, but I can't for the life of me figure either of those functions out, and in either case it seems like they would only return one value.

Is what I'm trying to do possible?
sweth is offline   Reply With Quote
Old 09-29-2018, 01:54 PM   #2
sweth
Connoisseur
sweth began at the beginning.
 
Posts: 67
Karma: 10
Join Date: May 2015
Device: Kobo Aura H20, Kindle Fire 7 (7G), Kindle PW (2G), Nook GlowLight Plus
Hmm. Is the answer maybe template functions? I was able to define a sum() function that appears to work (once I realized that args are passed in as unicode and needed to be converted before being added), but am a python newbie so the extraction of the Ns from an arbitrary number of tags is still befuddling me...
sweth is offline   Reply With Quote
Old 09-29-2018, 02:59 PM   #3
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 sweth View Post
Hmm. Is the answer maybe template functions? I was able to define a sum() function that appears to work (once I realized that args are passed in as unicode and needed to be converted before being added), but am a python newbie so the extraction of the Ns from an arbitrary number of tags is still befuddling me...
Given your use case, a template function is definitely the way to go.

Are you asking how to extract the N values in a template function? If the following is true:
  • The source of the values is always the field "tags".
  • The pattern is always 'recommended\..*\.[\d.]+'
Then something like the following should work for you:
Code:
def evaluate(self, formatter, kwargs, mi, locals):
	import re
	tags = mi.get('tags')
	v = 0
	if tags:
		for t in tags:
			m = re.match('recommended\..*\.([\d.]+)', t)
			if m:
				v = v + int(m.group(1))
	return v
chaley is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom column GPM foobarius Library Management 3 09-04-2016 07:20 PM
Capturing a GUI event AlPe Development 4 08-15-2013 12:37 PM
Parens after folder/file names BrownSamurai Calibre 2 12-07-2011 02:45 AM
Capturing screen shot from NC Rebo Nook Color & Nook Tablet 3 11-26-2010 03:06 PM
Regex capturing variables Lonas Sigil 9 08-14-2010 03:05 PM


All times are GMT -4. The time now is 07:38 AM.


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