<commit_before>import sqlite3
import logging

class Database(object):
    def on_start(self):
        self.last_value = None
        db_types = {'numeric': 'real',
                'string': 'text',
                'boolean': 'boolean'}
        try:
            type_str = db_types[self.get_info()['type']]
        except KeyError:
            type_str = db_types['string']

        self.db_name = self.module.module_name + '.db'
        db_conn = sqlite3.connect(self.db_name)

        # Create a new table for the field
        try:
            with db_conn:
                db_conn.execute(
                        'CREATE TABLE %s (time real primary key, value %s)'
                        % (self.field_name, type_str))
        except sqlite3.OperationalError as e:
            logging.exception(e)
            pass
        finally:
            db_conn.close()

    def _get_value(self):
        db_conn = sqlite3.connect(self.db_name)
        query = 'SELECT time, value FROM %s \
                ORDER BY time DESC LIMIT 1' \
                % (self.field_name,)
        try:
            c = db_conn.cursor()
            c.execute(query)
            re = c.fetchall()
            if re:
                return re[0]
        except Exception as e:
            logging.exception(e)
            return None
        finally:
            db_conn.close()
        return None

    def _get_value_at(self, t):
        db_conn = sqlite3.connect(self.db_name)
        query = 'SELECT time, value FROM %s \
                WHERE time < ? ORDER BY time DESC LIMIT 1' \
                % (self.field_name,)
        try:
            c = db_conn.cursor()
            c.execute(query, (t,))
            re = c.fetchall()
            if re:
                return re[0]
        except Exception as e:
            logging.exception(e)
            return None
        finally:
            db_conn.close()
        return None

    def _get_value_from_to(self, fr, to):
        db_conn = sqlite3.connect(self.db_name)
        query = 'SELECT time, value FROM %s \
                WHERE time < ?  AND time > ? ORDER BY time DESC' \
                % (self.field_name,)
        fr = int(fr)
        to = int(to)
        try:
            c = db_conn.cursor()
            c.execute(query, (to, fr))
            re = c.fetchall()
            return re
        except Exception as e:
            logging.exception(e)
            return []
        finally:
            db_conn.close()
        return []

    def set_value(self, t, value):
        db_conn = sqlite3.connect(self.db_name)
        query = 'INSERT INTO %s (time, value) VALUES (?, ?)' \
                % (self.field_name,)
        try:<commit_msg>Résolution de la persistance db (prob d'encodage)<commit_after>import sqlite3
import logging

def get_type(f, default='string'):
    try:
        return f.get_info()['type']
    except (KeyError, TypeError):
        return default

class Database(object):
    def on_start(self):
        self.last_value = None
        db_types = {'numeric': 'real',
                'string': 'text',
                'boolean': 'boolean'}
        try:
            type_str = db_types[self.get_info()['type']]
        except KeyError:
            type_str = db_types['string']

        self.db_name = self.module.module_name + '.db'
        db_conn = sqlite3.connect(self.db_name)

        # Create a new table for the field
        try:
            with db_conn:
                db_conn.execute(
                        'CREATE TABLE %s (time real primary key, value %s)'
                        % (self.field_name, type_str))
        except sqlite3.OperationalError as e:
            logging.exception(e)
            pass
        finally:
            db_conn.close()

    def _get_value(self):
        db_conn = sqlite3.connect(self.db_name)
        query = 'SELECT time, value FROM %s \
                ORDER BY time DESC LIMIT 1' \
                % (self.field_name,)
        try:
            c = db_conn.cursor()
            c.execute(query)
            re = c.fetchall()
            if re:
                return re[0]
        except Exception as e:
            logging.exception(e)
            return None
        finally:
            db_conn.close()
        return None

    def _get_value_at(self, t):
        db_conn = sqlite3.connect(self.db_name)
        query = 'SELECT time, value FROM %s \
                WHERE time < ? ORDER BY time DESC LIMIT 1' \
                % (self.field_name,)
        try:
            c = db_conn.cursor()
            c.execute(query, (t,))
            re = c.fetchall()
            if re:
                return re[0]
        except Exception as e:
            logging.exception(e)
            return None
        finally:
            db_conn.close()
        return None

    def _get_value_from_to(self, fr, to):
        db_conn = sqlite3.connect(self.db_name)
        query = 'SELECT time, value FROM %s \
                WHERE time < ?  AND time > ? ORDER BY time DESC' \
                % (self.field_name,)
        fr = int(fr)
        to = int(to)
        try:
            c = db_conn.cursor()
            c.execute(query, (to, fr))
            re = c.fetchall()
            return re
        except Exception as e:
            logging.exception(e)
            return []
        finally:
            db_conn.close()
        return []

    def set_value(self, t, value):
        db_conn = sqlite3.connect(self.db_name)
        query = 'INSERT INTO %s (time, value) VALUES (?, ?)' \
                % (self.field_name,)

        if get_type(self) == 'string':
            value = value.decode('utf-8')

        try:
{'fraction_non_alphanumeric': 0.10221827861579415, 'fraction_numerical': 0.004081632653061225, 'mean_word_length': 1.381073088297423, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5969956', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1618, 'n_words': 417}