# Path: stagehand/toolbox/utils.py
# def tostr(value, encoding=None, desperate=True, coerce=False, fs=False):
#     """
#     Convert (if necessary) the given value to a unicode string.
# 
#     :param value: the value to be converted to a unicode string
#     :param encoding: the character set to first try to decode as; if None, will
#                      use the system default (from the locale).
#     :type encoding: str
#     :param desperate: if True and decoding to the given (or default) charset
#                       fails, will also try utf-8 and latin-1 (in that order),
#                       and if those fail, will decode as the preferred charset,
#                       replacing unknown characters with \\\\uFFFD.
#     :type desperate: bool
#     :param coerce: if True, will coerce numeric types to a unicode string; if
#                    False, such values will be returned untouched.
#     :type coerce: bool
#     :param fs: indicates value is a file name or other environment string; if True,
#                the encoding (if not explicitly specified) will be the encoding
#                given by ``sys.getfilesystemencoding()`` and the error handler
#                used will be ``surrogateescape`` if supported.
#     :type fs: bool
#     :returns: the value as a unicode string, or the original value if coerce is
#               False and the value was not a bytes or string.
#     """
#     if isinstance(value, str):
#         # Nothing to do.
#         return value
#     elif isinstance(value, (int, float)):
#         return str(value) if coerce else value
#     elif not isinstance(value, (bytearray, bytes)):
#         # Need to coerce this value.  Try the direct approach.
#         try:
#             return str(value)
#         except UnicodeError:
#             # Could be that value.__repr__ returned a non-unicode and
#             # non-8bit-clean string.  Be a bit more brute force about it.
#             return tostr(repr(value), desperate=desperate)
# 
#     errors = 'strict'
#     if fs:
#         if not encoding:
#             encoding = sys.getfilesystemencoding()
#         errors = 'surrogateescape'
# 
#     # We now have a bytes object to decode.
#     for c in (encoding or ENCODING, 'utf-8', 'latin-1'):
#         try:
#             return value.decode(c, errors)
#         except UnicodeError:
#             pass
#         if not desperate:
#             raise UnicodeError("Couldn't decode value to unicode (and not desperate enough to keep trying)")
# 
#     return value.decode(encoding or ENCODING, 'replace')


import sys
import os
import time
import re
import logging
import math
import copyreg
import pickle
import struct
import _weakref
import threading
import functools
import string
import sqlite3 as sqlite
import asyncio
            import io
    import _objectrow
from .utils import tostr
        the name of inverted index we're scoring for.

        Terms are either unicode objects or strings, or sequences of unicode or
        string objects.  In the case of strings, they are passed through
        tostr() to try to decode them intelligently.

        Each term T is given the score:
             sqrt( (T coeff * T count) / total term count )

        Counts are relative to the given object, not all objects in the
        database.

        Returns a dict of term->score.  Terms (the keys) are converted to
        unicode objects, but their case is preserved as given (if term is
        given more than once, the case of the first occurence is used), and
        score (the values) are calculated as described above.
        """
        terms_scores = {}
        total_terms = 0

        for terms, coeff, split, ivtidx in terms_list:
            if not terms:
                continue
            # Swap ivtidx name for inverted index definition dict
            ivtidx = self._inverted_indexes[ivtidx]
            if not isinstance(terms, (str, list, tuple)):
                raise ValueError("Invalid type (%s) for ATTR_INVERTED_INDEX attribute. " \
                                 "Only sequence, unicode or str allowed." % str(type(terms)))

            if isinstance(terms, (list, tuple)):

# Path: stagehand/toolbox/utils.py
# def tostr(value, encoding=None, desperate=True, coerce=False, fs=False):
#     """
#     Convert (if necessary) the given value to a unicode string.
# 
#     :param value: the value to be converted to a unicode string
#     :param encoding: the character set to first try to decode as; if None, will
#                      use the system default (from the locale).
#     :type encoding: str
#     :param desperate: if True and decoding to the given (or default) charset
#                       fails, will also try utf-8 and latin-1 (in that order),
#                       and if those fail, will decode as the preferred charset,
#                       replacing unknown characters with \\\\uFFFD.
#     :type desperate: bool
#     :param coerce: if True, will coerce numeric types to a unicode string; if
#                    False, such values will be returned untouched.
#     :type coerce: bool
#     :param fs: indicates value is a file name or other environment string; if True,
#                the encoding (if not explicitly specified) will be the encoding
#                given by ``sys.getfilesystemencoding()`` and the error handler
#                used will be ``surrogateescape`` if supported.
#     :type fs: bool
#     :returns: the value as a unicode string, or the original value if coerce is
#               False and the value was not a bytes or string.
#     """
#     if isinstance(value, str):
#         # Nothing to do.
#         return value
#     elif isinstance(value, (int, float)):
#         return str(value) if coerce else value
#     elif not isinstance(value, (bytearray, bytes)):
#         # Need to coerce this value.  Try the direct approach.
#         try:
#             return str(value)
#         except UnicodeError:
#             # Could be that value.__repr__ returned a non-unicode and
#             # non-8bit-clean string.  Be a bit more brute force about it.
#             return tostr(repr(value), desperate=desperate)
# 
#     errors = 'strict'
#     if fs:
#         if not encoding:
#             encoding = sys.getfilesystemencoding()
#         errors = 'surrogateescape'
# 
#     # We now have a bytes object to decode.
#     for c in (encoding or ENCODING, 'utf-8', 'latin-1'):
#         try:
#             return value.decode(c, errors)
#         except UnicodeError:
#             pass
#         if not desperate:
#             raise UnicodeError("Couldn't decode value to unicode (and not desperate enough to keep trying)")
# 
#     return value.decode(encoding or ENCODING, 'replace')


# Path: stagehand/toolbox/db.py
import sys
import os
import time
import re
import logging
import math
import copyreg
import pickle
import struct
import _weakref
import threading
import functools
import string
import sqlite3 as sqlite
import asyncio
            import io
    import _objectrow
from .utils import tostr

        the name of inverted index we're scoring for.

        Terms are either unicode objects or strings, or sequences of unicode or
        string objects.  In the case of strings, they are passed through
        tostr() to try to decode them intelligently.

        Each term T is given the score:
             sqrt( (T coeff * T count) / total term count )

        Counts are relative to the given object, not all objects in the
        database.

        Returns a dict of term->score.  Terms (the keys) are converted to
        unicode objects, but their case is preserved as given (if term is
        given more than once, the case of the first occurence is used), and
        score (the values) are calculated as described above.
        """
        terms_scores = {}
        total_terms = 0

        for terms, coeff, split, ivtidx in terms_list:
            if not terms:
                continue
            # Swap ivtidx name for inverted index definition dict
            ivtidx = self._inverted_indexes[ivtidx]
            if not isinstance(terms, (str, list, tuple)):
                raise ValueError("Invalid type (%s) for ATTR_INVERTED_INDEX attribute. " \
                                 "Only sequence, unicode or str allowed." % str(type(terms)))

            if isinstance(terms, (list, tuple)):

                terms = [tostr(term) for term in terms]