View Single Post
Old 11-10-2011, 06:03 AM   #1
smoothrolla
Member
smoothrolla began at the beginning.
 
Posts: 13
Karma: 10
Join Date: Nov 2011
Device: kindle
using templates/pyhon and custom columns to extract specific data from tags

Hi Guys

I recently found out how to copy all data from one column (tags) to another customer column by using search and replace, thanks Chaley!

However i only want to copy specific data from the tags, using a template/python function so i dont have to do it manually.

I started to learn about templates and python last night and got pretty far:

ie

i created a column called #testcomposite

first i tried templates to extract only known genres from the tags column:

Code:
{#testcomposite:'list_intersection(field('tags'),'Adult, Adventure, Anthologies, Biography, Childrens, Classics, Drugs, Fantasy, Food, Football, Health, History, Historical, Horror, Humour, Inspirational, Modern, Music, Mystery, Non-Fiction, Poetry, Political, Philosophy, Psychological, Reference, Religion, Romance, Science, Science Fiction, Self Help, Short Stories, Sociology, Spirituality, Suspense, Thriller, Travel, Vampires, War, Western, Writing, Young Adult',',')'}
That worked, but this was slow, so i figured out how to create a python function by adapting the list_intersection function:

function:getgenre, 1 param
Code:
def evaluate(self, formatter, kwargs, mi, locals, val):
list1 = val
list2 = 'Adult, Adventure, Anthologies, Biography, Childrens, Classics, Drugs, Fantasy, Food, Football, Health, History, Historical, Horror, Humour, Inspirational, Modern, Music, Mystery, Non-Fiction, Poetry, Political, Philosophy, Psychological, Reference, Religion, Romance, Science, Science Fiction, Self Help, Short Stories, Sociology, Spirituality, Suspense, Thriller, Travel, Vampires, War, Western, Writing, Young Adult'
separator = ','
l1 = [l.strip() for l in list1.split(separator) if l.strip()]
l2 = [icu_lower(l.strip()) for l in list2.split(separator) if l.strip()]
res = []
for i in l1:
if icu_lower(i) in l2:
res.append(i)
return ', '.join(res)
called with template:
Code:
{#testcomposite:'getgenre(field('tags'))'}
So thats great, runs real quick, was quite amazed i got this far

However, what i would like is something like this:
Extract all the known genres from the tags (like above)
but also if i come across a tag which contains *mystery* (like Mystery & Detective) then add genre "Mystery" to the #testcomposite column

so something like this
if tag item like '*horror*' or tag item='Scarey' or tag item='Spooky' then add 'Horror' etc

Any help is appreicated, in either template or python (or both!)

PS, i am a programmer, but python and calibre is all very new to me and a little lower level language than im used to.

PPS, im amazed at home flexable this program is, hats off to the creator(s)!

Thanks very much!

Last edited by smoothrolla; 11-10-2011 at 11:38 AM.
smoothrolla is offline   Reply With Quote