Store raw trie data to db from a dict
961611
def persist_trie_data_dict(self, trie_data_dict: Dict[Hash32, bytes]) -> None:
        
        with self.db.atomic_batch() as db:
            for key, value in trie_data_dict.items():
                db[key] = value
571676
def storedata(filename=None):
    """"""
    if filename is None:
        filename = 'credolib_state.pickle'
    ns = get_ipython().user_ns
    with open(filename, 'wb') as f:
        d = {}
        for var in ['_headers', '_loaders', '_data1d', '_data2d', '_data1dunited',
                    'allsamplenames', '_headers_sample', 'badfsns', '_rowavg',
                    'saveto_dir',
                    'badfsns_datcmp', 'auximages_dir', 'subtractedsamplenames', 'outputpath', 'saveto_dir_rel',
                    'auximages_dir_rel', 'crd_prefix']:
            try:
                d[var] = ns[var]
            except KeyError:
                warnings.warn('Skipping storage of unavailable variable "%s"' % var)
        pickle.dump(d, f)
29925
def _dict_to_bson(doc, check_keys, opts, top_level=True):
    """"""
    if _raw_document_class(doc):
        return doc.raw
    try:
        elements = []
        if top_level and "_id" in doc:
            elements.append(_name_value_to_bson(b"_id\x00", doc["_id"],
                                                check_keys, opts))
        for (key, value) in iteritems(doc):
            if not top_level or key != "_id":
                elements.append(_element_to_bson(key, value,
                                                 check_keys, opts))
    except AttributeError:
        raise TypeError("encoder expected a mapping type but got: %r" % (doc,))

    encoded = b"".join(elements)
    return _PACK_INT(len(encoded) + 5) + encoded + b"\x00"
815471
def save_dict(self, key: str, my_dict: dict, hierarchical: bool = False):
        """"""
        for _key, _value in my_dict.items():
            if isinstance(_value, dict):
                if not hierarchical:
                    self._db.hmset(key, {_key: json.dumps(_value)})
                else:
                    self.save_dict(key + ':' + _key, _value, hierarchical)
            elif isinstance(_value, list):
                if not hierarchical:
                    self._db.hmset(key, {_key: str(_value)})
                else:
                    print('saving list at ', key + ':' + _key)
                    self._db.lpush(key + ':' + _key, *_value[::-1])
            elif isinstance(_value, bool):
                self._db.hmset(key, {_key: str(_value)})
            else:
                self._db.hmset(key, {_key: _value})
333749
def from_dict(raw_data):
        """"""
        try:
            set_id = raw_data['id']
            title = raw_data['title']
            terms = [Term.from_dict(term) for term in raw_data['terms']]
            return WordSet(set_id, title, terms)
        except KeyError:
            raise ValueError('Unexpected set json structure')
715058
def sql_to_dict(self, query, param):
        
        cursor = connection.cursor()
        cursor.execute(query, param)
        fieldnames = [name[0] for name in cursor.description]
        result = []
        for row in cursor.fetchall():
            rowset = []
            for field in zip(fieldnames, row):
                rowset.append(field)
            result.append(dict(rowset))
        return result
1145348
def set_from_dict(self, data, source=None):
        
        if source:
            self._source = source

        if not data:
            return

        for key, value in data.items():
            key = key.replace("-", "_")
            if not hasattr(self, key):
                LOG.debug("%s is not an attribute of %s", key, self.__class__.__name__)
                continue

            attr = getattr(self, key)
            if attr is not None and not same_type(value, attr):
                source = getattr(self, "_source", None)
                origin = " in %s" % source if source else ""
                LOG.debug("Wrong type '%s' for %s.%s%s, expecting '%s'", type_name(value), type_name(self), key, origin, type_name(attr))
                continue

            setattr(self, key, value)
900353
def save_object_to_db(self,
                          obj: Any,
                          table: str,
                          fieldlist: Sequence[str],
                          is_new_record: bool) -> None:
        """"""
        if is_new_record:
            pkvalue = getattr(obj, fieldlist[0])
            if pkvalue is None:
                self.insert_object_into_db_pk_unknown(obj, table, fieldlist)
            else:
                self.insert_object_into_db_pk_known(obj, table, fieldlist)
        else:
            self.update_object_in_db(obj, table, fieldlist)
949255
def from_raw_data(cls, item_raw):
        
        deserialized = {}
        for name, value in item_raw.items():
            attr = getattr(cls, name, None)
            if attr is None:
                continue
            deserialized[name] = attr.decode(value)
        return cls(**deserialized)
613615
def load_trie(filename):
    
    trie = marisa_trie.BytesTrie()
    # marisa_trie raises warnings that make no sense. Ignore them.
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        trie.load(filename)
    return trie
1216598
def dict_to_unicode(raw_dict):
    
    decoded = {}
    for key, value in raw_dict.items():
        decoded[to_unicode(key)] = map(
            to_unicode, value)
    return decoded
738401
def _parse_text_DB(self, s):
        """"""
        dic = self.decode_json_str(s)
        new_dic = {}
        for table_name, (header, rows) in dic.items():
            newl = [{c: ligne[i]
                     for i, c in enumerate(header)} for ligne in rows]
            new_dic[table_name] = newl
        return new_dic
1139761
def dictionize(fields: Sequence, records: Sequence) -> Generator:
    """"""

    return (dict(zip(fields, rec)) for rec in records)
202559
def save_data(uniq_keys, values_array, tbl_name)
      values_array = [values_array].flatten(1) # coerce to an array
      all_field_names = values_array.map(&:keys).flatten.uniq
      all_field_names_as_string = format_field_names_as_string(all_field_names)
      all_field_names_as_symbol_string = all_field_names.map{ |k| ":#{k}" }.join(',') # need to appear as symbols
      begin
        values_array.each do |values_hash|
          # mustn't use nil value in unique value due to fact that SQLite considers NULL values to be different from
          # each other in UNIQUE indexes. See http://www.sqlite.org/lang_createindex.html
          raise DatabaseError.new("Data has nil value for unique key. Unique keys are #{uniq_keys}. Offending data: #{values_hash.inspect}") unless uniq_keys.all?{ |k| values_hash[k] }
          sql_query =  "INSERT OR REPLACE INTO #{tbl_name} (#{all_field_names_as_string}) VALUES (#{all_field_names_as_symbol_string})"
          database.execute(sql_query, values_hash)
        end
      rescue SQLite3::SQLException => e
        puts "Exception (#{e.inspect}) raised" if verbose?
        case e.message
        when /no such table/
          create_table(tbl_name, all_field_names, uniq_keys)
          retry
        when /has no column/
          add_columns(tbl_name, all_field_names)
          retry
        else
          raise e
        end
      end
    end
585896
def dump_conndata(self):
        """"""
        attrs = vars(self)
        return ', '.join("%s: %s" % item for item in attrs.items())
1301938
def SaveDataToFD(self, raw_data, fd):
    """"""
    for key, value in iteritems(raw_data):
      # TODO(hanuszczak): Incorrect type specification for `set`.
      # pytype: disable=wrong-arg-types
      self.set("", key, value=value)
      # pytype: enable=wrong-arg-types

    self.write(fd)
1188681
def from_dict(self, data, recursive=1):
        
        # Find other linked resources
        data = self._encode_resource(data, recursive=recursive)
        self.data = data