<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

scanner/database/tables.py
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from database import Connect

#-----------------------------------------------------------------------------
# Params Table class
#-----------------------------------------------------------------------------
class ParamsTable:
    """
    This class handles :
    - Load and read parameters from the Params table as attributes
    Usage :
    - ParamsTable()
    - ParamsTable.PARAM_NAME
    """
    def __init__(self):
        """
        Constructor
        :rtype: ParamsTable
        """
        # Is the database connexion initialized ?
        if not Connect.is_ready():
            Connect.on()

        # Get all parameters
        Connect.CURSOR.execute("SELECT * FROM Params;")
        items = Connect.CURSOR.fetchall()

        # Store parameters as attributes in lower caps as they are not constants
        for item in items:
            setattr(self, item['key'].lower(), item['value'])

        # Cast some parameters
        self.camera_res_x = int(self.camera_res_x)
        self.camera_res_y = int(self.camera_res_y)

#-----------------------------------------------------------------------------
# Groceries Table class
#-----------------------------------------------------------------------------
class GroceriesTable:
    pass

#-----------------------------------------------------------------------------
# Products Table class
#-----------------------------------------------------------------------------
class ProductsTable:
    """
    This class handles :
    - Add / Get items from the Products table, which is a cache for products info
    Usage :
    - products = ProductsTable()
    - product = products.get_one(barcode)
    """
    def __init__(self):
        """
        Constructor
        :rtype: ProductsTable
        """
        # Is the database connexion initialized ?
        if not Connect.is_ready():
            Connect.on()

    def get_item(self, barcode):
        """
        Get a product from its barcode
        :param barcode: barcode to lookup for
        :type barcode: string
        :rtype: tuple
        """
        query = "SELECT * FROM Products WHERE barcode = ?;"
        params = (barcode,)

        Connect.CURSOR.execute(query, params)
        product = Connect.CURSOR.fetchone()

        if product:
            return product
        else:
            return False

    def add_item(self, barcode, name):
        """
        Adds a product
        :param barcode: barcode to lookup for
        :type barcode: string
        :param name: name of the product
        :type name: string
        :rtype: bool
        """
        query = "INSERT INTO Products VALUES (?, ?);"
        params = (barcode, name)

        Connect.CURSOR.execute(query, params)
        Connect.LINK.commit()

        return True<commit_msg>SQL typo<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

scanner/database/tables.py
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from database import Connect

#-----------------------------------------------------------------------------
# Params Table class
#-----------------------------------------------------------------------------
class ParamsTable:
    """
    This class handles :
    - Load and read parameters from the Params table as attributes
    Usage :
    - ParamsTable()
    - ParamsTable.PARAM_NAME
    """
    def __init__(self):
        """
        Constructor
        :rtype: ParamsTable
        """
        # Is the database connexion initialized ?
        if not Connect.is_ready():
            Connect.on()

        # Get all parameters
        Connect.CURSOR.execute("SELECT * FROM Params;")
        items = Connect.CURSOR.fetchall()

        # Store parameters as attributes in lower caps as they are not constants
        for item in items:
            setattr(self, item['key'].lower(), item['value'])

        # Cast some parameters
        self.camera_res_x = int(self.camera_res_x)
        self.camera_res_y = int(self.camera_res_y)

#-----------------------------------------------------------------------------
# Groceries Table class
#-----------------------------------------------------------------------------
class GroceriesTable:
    pass

#-----------------------------------------------------------------------------
# Products Table class
#-----------------------------------------------------------------------------
class ProductsTable:
    """
    This class handles :
    - Add / Get items from the Products table, which is a cache for products info
    Usage :
    - products = ProductsTable()
    - product = products.get_one(barcode)
    """
    def __init__(self):
        """
        Constructor
        :rtype: ProductsTable
        """
        # Is the database connexion initialized ?
        if not Connect.is_ready():
            Connect.on()

    def get_item(self, barcode):
        """
        Get a product from its barcode
        :param barcode: barcode to lookup for
        :type barcode: string
        :rtype: tuple
        """
        query = "SELECT * FROM Products WHERE barcode = ?;"
        params = (barcode,)

        Connect.CURSOR.execute(query, params)
        product = Connect.CURSOR.fetchone()

        if product:
            return product
        else:
            return False

    def add_item(self, barcode, name):
        """
        Adds a product
        :param barcode: barcode to lookup for
        :type barcode: string
        :param name: name of the product
        :type name: string
        :rtype: bool
        """
        query = "INSERT INTO Products VALUES (?, ?, ?);"
        params = (barcode, name, "")

        Connect.CURSOR.execute(query, params)
        Connect.LINK.commit()

        return True
{'fraction_non_alphanumeric': 0.2720518495099589, 'fraction_numerical': 0.0006323110970597534, 'mean_word_length': 1.1817241379310346, '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': '633902', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1451, 'n_words': 440}