View Single Post
Old 10-07-2014, 01:52 PM   #609
MiniMouse
Groupie
MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.MiniMouse ought to be getting tired of karma fortunes by now.
 
MiniMouse's Avatar
 
Posts: 197
Karma: 318144
Join Date: Jul 2013
Location: Well, there will always be a big time delay because I'm not living in the US! And there could be some communication difficulties too because I'm not a native speaker!
Device: Sony PRS-T3S
Quote:
Originally Posted by chaley View Post
Yet another alternate approach would be to write a calibre python script that processes the relationship column to create another tags-like column containing the values you want. This would have great performance and be somewhat easier to maintain, but still suffers from having to do it in python. An example of what this might look like is under the spoiler.
Spoiler:
Code:
def init_cache(library_path):
	from calibre.db.backend import DB
	from calibre.db.cache import Cache
	backend = DB(library_path)
	cache = Cache(backend)
	cache.init()
	return cache

from collections import defaultdict
	
cache = init_cache(library_path = sys.argv[1])

transformation_dict = {
	'Sherlock Holmes': 'Holmes',
	'S. Holmes': 'Holmes',
	'John Watson': 'Watson',
	'J. Watson': 'Watson',
	'Sebastian Moran': 'Moran',
	'S. Moran': 'Moran',
	'Jim Moriarty': 'Moriarty',
	'J. Moriarty': 'Moriarty'
	}

values_to_write = {}
for id_ in cache.all_book_ids():
	relationships = cache.field_for('#mytextmult', id_)
	new_relationships = []
	if relationships:
		for relationship in relationships:
			pair = relationship.split('/')
			if len(pair) == 2:
				left, right = pair
				newleft = transformation_dict.get(left, left)
				newright = transformation_dict.get(right, right)
				new_relationships.append(newleft + '/' + newright)
			else:
				new_relationships.append(relationship)
		values_to_write[id_] = new_relationships
print values_to_write

cache.set_field('#mytextmult', values_to_write)
Dealing with templates and regex I learned by now.
Now it seems I have to learn something new... again. I think the last option could be more my solution BUT where do I put something like this? Never have done that before. Now I need a bit more information

Mini
MiniMouse is offline   Reply With Quote