<commit_before>#! /usr/bin/env python3
# coding: utf-8
"""
Jean-Pierre [Prototype]
A Raspberry Pi robot helping people to build groceries list.
<NAME> - github.com/matteocargnelutti

utils/cls.py - SQlite connector
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import re
import sqlite3

#-----------------------------------------------------------------------------
# Database class
#-----------------------------------------------------------------------------
class Database:
    """
    This class handles :
    - Provides a link and a cursor to the database as class attributes
    Usage :
    - Database.on()
    - Database.CURSOR.execute(query, params)
    - ... etc
    - Database.off()
    Available class attributes :
    - LINK
    - CURSOR
    - FILE (path to the database file)
    """
    DATABASE_PRODUCTION = 'database.db'
    """ Normal database filename """
    DATABASE_TEST = 'database_test.db'
    """ Test database filename : this database is temporary """

    @classmethod
    def on(cls, is_test=False):
        """
        Connect to the database
        :param alternate_file: Allows for use of an alternative database file
        :rtype: bool
        """
        # Don't reconnect if already connected
        if hasattr(cls, 'LINK'):
            return True

        # Path to the database
        path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        path = path.replace('/utils', '')
        cls.PATH = path + '/'

        # Filename
        if not is_test:
            cls.FILE = cls.DATABASE_PRODUCTION
        else:
            cls.FILE = cls.DATABASE_TEST

        # Connect
        cls.LINK = sqlite3.connect(cls.PATH + cls.FILE)
        cls.LINK.row_factory = sqlite3.Row

        # Cursor
        cls.CURSOR = cls.LINK.cursor()

        return True

    @classmethod
    def off(cls):
        """
        Ends connection with the cls.
        :rtype: bool
        """
        cls.LINK.close()
        cls.LINK = None
        cls.CURSOR = None
        return True

    @classmethod
    def is_ready(cls):
        """
        Is the connection open ?
        :rtype: bool
        """
        return hasattr(cls, 'LINK')<commit_msg>Progress : unit tests<commit_after>#! /usr/bin/env python3
# coding: utf-8
"""
Jean-Pierre [Prototype]
A Raspberry Pi robot helping people to build groceries list.
<NAME> - github.com/matteocargnelutti

utils/cls.py - SQlite connector
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import re
import sqlite3

#-----------------------------------------------------------------------------
# Database class
#-----------------------------------------------------------------------------
class Database:
    """
    This class handles :
    - Provides a link and a cursor to the database as class attributes
    Usage :
    - Database.on()
    - Database.CURSOR.execute(query, params)
    - ... etc
    - Database.off()
    Available class attributes :
    - LINK
    - CURSOR
    - FILE (path to the database file)
    """
    DATABASE_PRODUCTION = 'database.db'
    """ Normal database filename """
    DATABASE_TEST = 'database_test.db'
    """ Test database filename : this database is temporary """

    @classmethod
    def on(cls, is_test=False):
        """
        Connect to the database
        :param alternate_file: Allows for use of an alternative database file
        :rtype: bool
        """
        # Don't reconnect if already connected
        if cls.is_ready():
            return False

        # Path to the database
        path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        path = path.replace('/utils', '')
        cls.PATH = path + '/'

        # Filename
        if not is_test:
            cls.FILE = cls.DATABASE_PRODUCTION
        else:
            cls.FILE = cls.DATABASE_TEST

        # Connect
        cls.LINK = sqlite3.connect(cls.PATH + cls.FILE)
        cls.LINK.row_factory = sqlite3.Row

        # Cursor
        cls.CURSOR = cls.LINK.cursor()

        return True

    @classmethod
    def off(cls):
        """
        Ends connection with the cls.
        :rtype: bool
        """
        cls.LINK.close()
        cls.LINK = None
        cls.CURSOR = None
        return True

    @classmethod
    def is_ready(cls):
        """
        Is the connection open ?
        :rtype: bool
        """
        return hasattr(cls, 'LINK') and cls.LINK != None
{'fraction_non_alphanumeric': 0.2230308219178082, 'fraction_numerical': 0.0021404109589041095, 'mean_word_length': 1.3423558897243109, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2050178', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1221, 'n_words': 339}