View Single Post
Old 07-18-2010, 07:23 AM   #15
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,489
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Starson17 View Post
Charles, two questions:

Code:
from_record_value = db.get_custom(from_id, num=col_num)
set_custom(to_id, val, num=col_num)
How do I get/set the series_index associated with a custom field of datatype 'series'?
Get the value with db.get_custom_extra(...)

Set the value with db.set_custom(..., extra=series_index). You set the series and the series_index together. They cannot be set separately.
Quote:
Is there an easy way to merge tag-like text from two records when is_multiple is True? With tags, it's just:
Code:
for tag in from_mi.tags:
   to_mi.tags.append(tag)
Duplicate tags are automatically removed.
Thanks.
Two things:

1) your code does not remove duplicates. The append() method adds the tag to the end of the list (array), with no check for duplicates. I assume that later you call db.set_tags(). That method checks for duplicate tags in the input list, which is why your code works.

There are two ways to deal with duplicates, one with a set and one with 'in' check. The set code works because sets automatically remove duplicates, and would be something like:
Code:
to_mi.tags = list(set(to_mi.tags) | set(from_mi.tags))
The list code would look something like:
Code:
for tag in from_mi.tags:
    if tag not in to_mi.tags:
       to_mi.tags.append(tag)
If you are willing to let the db code cull the duplicates, then you can use
Code:
to_mi.tags.extend(from_mi.tags)
2) The custom code also removes duplicates, so you can use the similar code. It would be something like:
Code:
# assume that label contains the column label.
# id must be the id, not the index
tags = db.get_custom(to_mi.id, label=label, index_is_id=True)
tags.extend(db.get_custom(from_mi.id, label=label, index_is_id=True))
db.set_custom(to_mi.id, tags, label=label)
chaley is offline   Reply With Quote