View Single Post
Old 02-22-2012, 02:50 AM   #8
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,734
Karma: 6690881
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Here is a general program mode template that does what I think you want. It puts all values into one custom column, not three
Code:
program:
	categories = 'Fiction, Non-Fiction, Fanfiction, Magazine';
	types = list_intersection(field('tags'), categories, ',');
	genres = list_re(list_difference(field('tags'), categories, ','), ',', '^', 'Genre: ') ;
	series = test(field('series'), strcat('Series: ' , field('series'), ' [', field('series_index'), ']'), '');
	list_union(types, list_union(genres, series, ','), ',')
Here is a custom template function that does the same thing, except that the series index is formatted n.n.
Code:
Function: myFunc or whatever you want
Arg count: 0
Documentation: whatever you want
Program code:
def evaluate(self, formatter, kwargs, mi, locals):
    # create a set of categories
    categories = {'fiction', 'non-fiction', 'fanfiction', 'magazine'}
    # get a list of non-empty tags
    tags = [v.strip() for v in kwargs.get('tags') if v.strip()]
    res = []
    for v in tags:
        if v.lower() in categories:
            # tag is in categories. Add it 'as is' to the result list
            res.append(v)
        else:
            # tag is not in categories. Add it as a genre to the result list
            res.append('Genre: ' + v)
    # add the series if there is one
    series = kwargs.get('series')
    if series:
        res.append('Series: ' + series + (' [%3.1f]'%kwargs.get('series_index')))
    return ', '.join(res)
You call it using the template
Code:
{:'myFunc()'}
Note that if the only thing that varies are the categories, you could pass these in as an argument instead of building them into the function. The function would look something like:
Code:
Function: myFunc or whatever you want
Arg count: 0
Documentation: whatever you want
Program code:
def evaluate(self, formatter, kwargs, mi, locals, cats):
    categories = set([v.strip() for v in cats.split(',') if v.strip()])
    tags = [v.strip() for v in kwargs.get('tags') if v.strip()]
    res = []
    for v in tags:
        if v.lower() in categories:
            res.append(v)
        else:
            res.append('Genre: ' + v)
    series = kwargs.get('series')
    if series:
        res.append('Series: ' + series + (' [%3.1f]'%kwargs.get('series_index')))
    return ', '.join(res)
You call it using the template
Code:
{:'myFunc('fiction, non-fiction, fanfiction, magazine')'}

Last edited by chaley; 02-22-2012 at 10:29 AM. Reason: fix typo in second template function
chaley is offline   Reply With Quote