<commit_before>import redis
import config
from serializer import Serializer
from redis_connection import Redis


class Cart(object):

    """docstring for Cart"""

    def __init__(self, user_id):
        # Init function to check weather the cart dictionary exists in redis
        # this can also be done via user every time he uses the library
        self.user_id = user_id
        self.redis_connection = Redis().get_connections()
        self.redis_cart_init(user_id)

    def redis_cart_init(self, user_id):
        if not self.redis_connection.hexists(config.CART_CONFIG["hash_name"], self.user_id):
            self.redis_connection.hset(
                config.CART_CONFIG["hash_name"], self.user_id, {})

    def __cart_dict(self):
        cart_string = self.redis_connection.hget(
            config.CART_CONFIG["hash_name"], self.user_id)
        return Serializer.loads(cart_string)

    def __product_dict(self, product_price, qty, extra_data_dict):
        product_dict = {
            "product_price": product_price,
            "qty": qty
        }
        if extra_data_dict:
            product_dict.update(extra_data_dict)
        return product_dict

    def add(self, product_id, product_price, qty=1, **extra_data_dict):
        cart_dict = self.__cart_dict()
        cart_dict[product_id] = self.__product_dict(
            product_price, qty, extra_data_dict)
        self.redis_connection.hset(
            config.CART_CONFIG["hash_name"], self.user_id, Serializer.dumps(cart_dict))

    def get(self, product_id):
        cart_dict = self.__cart_dict()
        return cart_dict.get(str(product_id))

    def remove(self, product_id):
        cart_dict = self.__cart_dict()
        if product_id in cart_dict:
            del cart_dict[product_id]
            self.redis_connection.hset(
                config.CART_CONFIG["hash_name"], self.user_id, Serializer.dumps(cart_dict))
            return True
        else:
            return False<commit_msg>Added Destroy cart but needs improvements<commit_after>import redis
import config
from serializer import Serializer
from redis_connection import Redis


class Cart(object):

    """docstring for Cart"""

    def __init__(self, user_id):
        # Init function to check weather the cart dictionary exists in redis
        # this can also be done via user every time he uses the library
        self.user_id = user_id
        self.redis_connection = Redis().get_connections()
        self.redis_cart_init(user_id)
        self.destroy = self.__del__

    def redis_cart_init(self, user_id):
        if not self.redis_connection.hexists(config.CART_CONFIG["hash_name"], self.user_id):
            self.redis_connection.hset(
                config.CART_CONFIG["hash_name"], self.user_id, {})

    def __cart_dict(self):
        cart_string = self.redis_connection.hget(
            config.CART_CONFIG["hash_name"], self.user_id)
        return Serializer.loads(cart_string)

    def __product_dict(self, product_price, qty, extra_data_dict):
        product_dict = {
            "product_price": product_price,
            "qty": qty
        }
        if extra_data_dict:
            product_dict.update(extra_data_dict)
        return product_dict

    def add(self, product_id, product_price, qty=1, **extra_data_dict):
        cart_dict = self.__cart_dict()
        cart_dict[product_id] = self.__product_dict(
            product_price, qty, extra_data_dict)
        self.redis_connection.hset(
            config.CART_CONFIG["hash_name"], self.user_id, Serializer.dumps(cart_dict))

    def get(self, product_id):
        cart_dict = self.__cart_dict()
        return cart_dict.get(str(product_id))

    def remove(self, product_id):
        cart_dict = self.__cart_dict()
        if product_id in cart_dict:
            del cart_dict[product_id]
            self.redis_connection.hset(
                config.CART_CONFIG["hash_name"], self.user_id, Serializer.dumps(cart_dict))
            return True
        else:
            return False

    def __del__(self):
        self.redis_connection.hdel(
            config.CART_CONFIG["hash_name"], self.user_id)
{'fraction_non_alphanumeric': 0.08858315230509292, 'fraction_numerical': 0.0004827419744146754, 'mean_word_length': 2.6770186335403725, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '440616', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1249, 'n_words': 263}