<commit_before># -*- coding: utf-8 -*-

import json

class DataHandler(object):

    def __init__(self):
        self.data_path = "../data/"

    def read_file(self, fname):
        try:
            with open(self.data_path + fname, 'r') as infile:
                return json.loads(infile.read())
        except:
            return {}

    def write_file(self, fname, data):
        with open(self.data_path + fname, 'w') as outfile:
            json.dump(data, outfile)<commit_msg>Add: add, edit, delete function<commit_after># -*- coding: utf-8 -*-

import json

class DataHandler(object):

    def __init__(self):
        self.data_path = "../data/"

    def read_file(self, fname):
        try:
            with open(self.data_path + fname, 'r') as infile:
                return json.loads(infile.read())
        except:
            return {}

    def write_file(self, fname, data):
        with open(self.data_path + fname, 'w') as outfile:
            json.dump(data, outfile)

    def read_json_then_add_data(self, fname, category, input_data):
        total_data = self.read_file(fname)
        category_data = total_data.get(category, {})

        if category_data == {}:
            total_data[category]= category_data
            c_index = 1
        else:
            c_index = category_data['index'] + 1
        category_data["index"] = c_index
        c_index = "#" + str(c_index)
        category_data[c_index] = input_data

        self.write_file(fname, total_data)

        return total_data, c_index

    def read_json_then_edit_data(self, fname, category, c_index, input_data):
        total_data = self.read_file(fname)
        category_data = total_data.get(category, {})

        if c_index not in category_data:
            return "not exist"

        category_data[c_index] = input_data
        self.write_file(fname, total_data)

        return "sucess"

    def read_json_then_delete(self, fname, category, index):
        total_data = self.read_file(fname)
        category_data = total_data.get(category, {})
        category_data.pop(index, None)
        self.write_file(fname, total_data)
{'fraction_non_alphanumeric': 0.11367924528301887, 'fraction_numerical': 0.0018867924528301887, 'mean_word_length': 2.31924882629108, 'pattern_counts': {'":': 0, '<': 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': '4302107', 'n_tokens_mistral': 682, 'n_tokens_neox': 666, 'n_words': 144}