09-29-2018, 01:44 PM | #1 |
Connoisseur
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\..*\.', '' ), Is what I'm trying to do possible? |
09-29-2018, 01:54 PM | #2 |
Connoisseur
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...
|
09-29-2018, 02:59 PM | #3 | |
Grand Sorcerer
Posts: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Are you asking how to extract the N values in a template function? If the following is true:
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 |
|
|
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 |