Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
Old 10-16-2011, 08:24 AM   #16
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,374
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Thanks. I will look.
Quote:
Originally Posted by Noughty View Post
I just changed this one to, #length wasn't needed here, only word Short:
Code:
program:
list_sort(
   list_difference(
      list_union(field('#shelf'), field('tags'), ','),
      list_union(strcat('Short', ',',
        'delete'),
         strcat(field('#genre'), ',',
                field('#mode'), ',', 
                field('#character')),
         ','), 
      ','),
   0, ',')
You can improve this by eliminating the first strcat, because all the arguments are constants. Try:
Code:
program:
list_sort(
   list_difference(
      list_union(field('#shelf'), field('tags'), ','),
      list_union('Short, delete',
         strcat(field('#genre'), ',',
                field('#mode'), ',', 
                field('#character')),
         ','), 
      ','),
   0, ',')
chaley is offline   Reply With Quote
Old 10-16-2011, 08:31 AM   #17
Noughty
Addict
Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.
 
Posts: 352
Karma: 103850
Join Date: Apr 2011
Device: Kindle NT
It worked. Thanks.

Now it takes 2 mins and a few seconds to write all the data.

I'm thinking of another cheat. Just rename shelves according to the category. If it belongs to genre just add word genre/g or something in the beginning so when viewing the list it would show it alphabetically so all genres would be there. I can do the same with tags. A little work while renaming and after that it should look ok. Not as nice as having separate columns but it would work without any freezes as before. I'm not sure I would like how it looks. Having tags like g-action, g-adventure etc.

I guess I will keep it as an option if freezing up makes it too much trouble working with metadata.

Would making the list for list_intersection shorter make it easier for calibre? I could remove a lot from category character since there are way too many detail tags.

Last edited by Noughty; 10-16-2011 at 08:51 AM.
Noughty is offline   Reply With Quote
Advert
Old 10-16-2011, 09:50 AM   #18
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,374
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Noughty View Post
Would making the list for list_intersection shorter make it easier for calibre? I could remove a lot from category character since there are way too many detail tags.
A bit, but I think not enough to justify the cost.

If you don't mind getting more complicated, we *can* justify the cost of using a python evaluation function. For example, for the template we have been playing with in the last 2 posts, a custom function could easily be 100 times faster to evaluate. That won't change the time needed to build the categories for the tag browser, but it would drastically reduce the amount of time needed to build the columns so the categories can be built.

Here is the last template rewritten as a template function. To put this into your library, go to preferences -> advanced -> template functions. You will see some boxes. Fill them in as follows:
Code:
Function: myListMerge (you can use any name here you want, as 
          long as it is alphabetic. You might consider using the column name, 
          such as myWhateverItIs.)

Arg count: 0 (ignore the warning that will appear later)

Documentation: whatever you want to put here

Program Code:
def evaluate(self, formatter, kwargs, mi, locals):
	tags = set(mi.get('tags', []))
	shelf = set(mi.get('#shelf', []))
	genre = set(mi.get('#genre', []))
	mode = set(mi.get('#mode', []))
	charac = set(mi.get('#character', []))

	st = tags | shelf
	gmc = set(['Short', 'delete']) | genre | mode | charac
	res = st - gmc
	return ', '.join(res)
Press 'create' (or modify if you are changing it later), then apply.

You would use this new function in your composite column template as follows, and assuming it is named myListMerge:
Code:
{:'myListMerge()'}
Given you current experience, I think you can see how this function works. It first gets all the fields from the book metadata information (mi). It then does a union of tags and shelf (the '|' operator), and then a union of genre, mode, charac, and the constant set containing 'Short' and 'delete'. Finally it does the difference (the '-' operator), then turns the result back into a comma-separated string (the 'join').

Do note that this function assumes that the case of all items is identical and uses a case-sensitive compare. For example, it thinks that foo and Foo are different. Fixing this is possible, but it would slow things down somewhat.

Last edited by chaley; 10-16-2011 at 10:41 AM. Reason: Fix mistake in composite template to call custom function
chaley is offline   Reply With Quote
Old 10-16-2011, 10:03 AM   #19
Noughty
Addict
Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.
 
Posts: 352
Karma: 103850
Join Date: Apr 2011
Device: Kindle NT
I created it named as otherts but I don't get any result. I need to write:

{'otherts()'} in custom column made from other tags (behaves like tags)?

I get error:
EXCEPTION: Value: unknown field 'otherts()'
Noughty is offline   Reply With Quote
Old 10-16-2011, 10:48 AM   #20
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,374
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Noughty View Post
I need to write:

{'otherts()'} in custom column made from other tags (behaves like tags)?

I get error:
EXCEPTION: Value: unknown field 'otherts()'
Ahh, my mistake, which I fixed in my example above. You should all {:'otherts()'}. I left out the colon (the second character).

For completeness, the :' tells the formatter that what follows is in template program mode, which allows use of functions as arguments and zero-parameter functions. The program goes up to the '}. Writing "{:'foobar()'}" is exactly the same as writing "program:foobar()".

You might then ask "why use program:"? That construct allows multiple lines and multiple statements, and is (I think) easier to read. On the other hand, template program mode expressions can be combined together in a single template, such as "{title} {:'foobar()'|- | }"
chaley is offline   Reply With Quote
Advert
Old 10-16-2011, 10:53 AM   #21
Noughty
Addict
Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.
 
Posts: 352
Karma: 103850
Join Date: Apr 2011
Device: Kindle NT
It gives wrong result

Nothing gets taken away. It just merges lists (tags, shelf) and deletes 'Short', 'delete', but not other lists

Last edited by Noughty; 10-16-2011 at 10:59 AM.
Noughty is offline   Reply With Quote
Old 10-16-2011, 11:03 AM   #22
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,374
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Did I spell the lookup names of the columns correctly?

Are any of #shelf, #mode, #genre, or #character composite columns? I assumed that they were all text, like tags.

Do all the words have the same case?

If you add 'Short' as a tag, it appears in the output of the custom function? If so, that is odd because it does not for me. Note that if you add 'short' it will appear, but 'Short' should not.
chaley is offline   Reply With Quote
Old 10-16-2011, 11:08 AM   #23
Noughty
Addict
Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.
 
Posts: 352
Karma: 103850
Join Date: Apr 2011
Device: Kindle NT
Everything is spelled correctly as far as I can tell.
They are composite columns using code like this:
Code:
program:
	list_union(
		list_intersection(field('tags'), 'values listed, ','),
		list_intersection(field('#shelf'), 'values listed', ','), 
		',')
It deletes Short, delete but not custom columns. I guess because they are custom. All values are with first letter uppercase, others lowercase.

Maybe I could just paste all these values from these into python code like 'short, delete' are? The ones I want to remove.

What function I would use in python to write the values I want to keep, not add. Like extract only those values? I'll try to write it myself using your example. I just need a function so I could use it for genre, character,mode columns. It would be more efficient as far as I understand.

Last edited by Noughty; 10-16-2011 at 11:17 AM.
Noughty is offline   Reply With Quote
Old 10-16-2011, 11:28 AM   #24
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,374
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Noughty View Post
Everything is spelled correctly as far as I can tell.
They are composite columns using code like this:
Code:
program:
	list_union(
		list_intersection(field('tags'), 'values listed, ','),
		list_intersection(field('#shelf'), 'values listed', ','), 
		',')
It deletes Short, delete but not custom columns. I guess because they are custom. All values are with first letter uppercase, others lowercase.
Try this program. Replace the Program Code of your otherts function with the code above.
Code:
def evaluate(self, formatter, kwargs, mi, locals):
	# Tags is a multiple text field, so it is already split
	tags = set(mi.get('tags', []))
	# Shelf is a composite column, so we must split it
	shelf = set([l.strip() for l in mi.get('#shelf', "").split(',')])
	# Genre is a composite column, so we must split it
	genre = set([l.strip() for l in mi.get('#genre', "").split(',')])
	# Mode is a composite column, so we must split it
	mode = set([l.strip() for l in mi.get('#mode', "").split(',')])
	# Character is a composite column, so we must split it
	charac = set([l.strip() for l in mi.get('#character', "").split(',')])

	st = tags | shelf
	gmc = set(['Short', 'delete']) | genre | mode | charac
	res = st - gmc
	return ', '.join(res)
Quote:
Maybe I could just paste all these values from these into python code like 'short, delete' are? The ones I want to remove.
If you know the list and it isn't too long, that isn't a bad solution at all.
Quote:
What function I would use in python to write the values I want to keep, not add. Like extract only those values? I'll try to write it myself using your example. I just need a function so I could use it for genre, character,mode columns. It would be more efficient as far as I understand.
If I understand your question, it would be something like:
Code:
def evaluate(self, formatter, kwargs, mi, locals):
	# Tags is a multiple text field, so it is already split
	tags = set(mi.get('tags', []))
	# Shelf is a composite column, so we must split it
	shelf = set([l.strip() for l in mi.get('#shelf', "").split(',')])
	
	# items to remove from the result
	to_remove = set(['Short', 'delete', 'foo', 'bar', somethingElse',
		 'andSoOn', 'until I get tired', 'of adding things']) 

	# combine the two fields containing items we might want to keep.
	st = tags | shelf
	# now remove the items we don't want in the result
	res = st - to_remove
	return ', '.join(res)
chaley is offline   Reply With Quote
Old 10-16-2011, 11:31 AM   #25
Noughty
Addict
Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.
 
Posts: 352
Karma: 103850
Join Date: Apr 2011
Device: Kindle NT
I'll try it but shelf isn't composite column. Tags and shelves are simple tag columns.
I edited it and it seems to work

It doesn't seem to sort list. Is it possible to use {:'otherts()'} like program? I could use sort function when.

Last edited by Noughty; 10-16-2011 at 11:36 AM.
Noughty is offline   Reply With Quote
Old 10-16-2011, 11:33 AM   #26
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,374
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Noughty View Post
I'll try it but shelf isn't composite column. Tags and shelves are simple tag columns.
I wasn't sure, which is why I added the comment. Use the original line of code for shelf instead of the new line.
Code:
	shelf = set(mi.get('#shelf', []))
chaley is offline   Reply With Quote
Old 10-16-2011, 11:41 AM   #27
Noughty
Addict
Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.
 
Posts: 352
Karma: 103850
Join Date: Apr 2011
Device: Kindle NT
Thanks for the comments with them I managed to make edit easily. Now just to figure out how to sort the list

Made it
Just added sort for the result. I might start to get python too... Well, a little

Last edited by Noughty; 10-16-2011 at 11:52 AM.
Noughty is offline   Reply With Quote
Old 10-16-2011, 11:49 AM   #28
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,374
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Noughty View Post
Thanks for the comments with them I managed to make edit easily. Now just to figure out how to sort the list
That is easy (if you know how ). Replace the "return ..." line with the following two lines.
Code:
	from calibre.utils.icu import sort_key
	return ', '.join(sorted(res, key=sort_key))
This sorts the list in order respecting the current language rules, then converts it to a string.
chaley is offline   Reply With Quote
Old 10-16-2011, 11:55 AM   #29
Noughty
Addict
Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.Noughty is cognizant of many things which escape those who dream only by night.
 
Posts: 352
Karma: 103850
Join Date: Apr 2011
Device: Kindle NT
And I just wrote

Code:
return ', '.join(sorted(res))
Is it wrong? The result seems to be correct...

Where could I read more about functions in python?

Seems the speed increased by half a minute. Maybe I could make other columns like this too (genre, character). Since they are basically the same it might not be hard.

Code:
def evaluate(self, formatter, kwargs, mi, locals):
	# Tags is a multiple text field, so it is already split
	tags = set(mi.get('tags', []))
	# Shelf is a composite column, so we must split it
	shelf = set([l.strip() for l in mi.get('#shelf', "").split(',')])
	
	# items to remove from the result
	to_remove = set(['Short', 'delete', 'foo', 'bar', somethingElse',
		 'andSoOn', 'until I get tired', 'of adding things']) 

	# combine the two fields containing items we might want to keep.
	st = tags | shelf
	# now remove the items we don't want in the result
	res = st - to_remove
	return ', '.join(res)
I need another function. I need to leave the values I list, not remove them. For example I am doing the genre column, so I would list all genre values I have in my library, so in the returned list only they remain.

I need to extract specific values

Last edited by Noughty; 10-16-2011 at 12:05 PM.
Noughty is offline   Reply With Quote
Old 10-16-2011, 11:56 AM   #30
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,374
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Noughty View Post
And I just wrote

Code:
return ', '.join(sorted(res))
Is it wrong? The result seems to be correct...
That works fine as long as the words do not contain non-English letters. If they do, then the words will probably sort incorrectly.
chaley is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tag browser searching Stampercam Calibre 2 06-25-2011 04:42 AM
Browser freezes loading main forum page Ripplinger Feedback 5 04-20-2011 04:24 PM
Calibre 7.36 Author Fields in Tag Browser is weird dfad1469 Library Management 44 01-24-2011 04:47 AM
Tag Browser partitioning dasimser Calibre 2 01-03-2011 12:18 PM
Empty tag browser Rachel Calibre 18 07-26-2010 11:42 AM


All times are GMT -4. The time now is 03:50 PM.


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