<commit_before>import json
import os.path

class JDict(dict):
    def __init__(self, filepath, *args, **kwargs):
        assert filepath
        
        if str(filepath).split('.')[-1] == 'json':
            self.filepath = str(filepath)
        else:
            self.filepath = str('{}.json'.format(filepath))
            
        if os.path.isfile(self.filepath):
            super(JDict, self).__init__(self.read())
        else:
            super(JDict, self).__init__(*args, **kwargs)
            self.write()
        
    def __setitem__(self, key, value):
        assert key
        assert value
        
        dict.__setitem__(self, key, value)
        self.write()
      
    def write(self):
        with open(self.filepath, 'w') as outfile:
            json.dump(self, outfile, sort_keys = True, indent = 4,
                ensure_ascii=False)
        
    def read(self):
        with open(self.filepath, 'r') as infile:
            jsonData = json.load(infile)
        self = jsonData
        return self
        
        
if __name__ == '__main__':
    #print ('hola')
    myDict = JDict('test.json',{
        'blah': 'ha',
        'blo': 'so',
        'folo': 'bobo'
    })
    '''
    myDict['blah'] = 'ha'
    myDict['blo'] = 'so'
    myDict['folo'] = 'bobo'
    '''
    '''
    myDict = {
        'blah': 'ha',
        'blo': 'so',
        'folo': 'bobo'
    }
    '''
    
    myDict['blah'] = 'za'
    #myDict.write()
    
    #print (myDict.read())
    print (myDict)
        <commit_msg>General code cleanup<commit_after>import json
import os.path

class JDict(dict):
    def __init__(self, filepath, *args, **kwargs):
        assert filepath
        
        if str(filepath).split('.')[-1] == 'json':
            self.filepath = str(filepath)
        else:
            self.filepath = str('{}.json'.format(filepath))
            
        if os.path.isfile(self.filepath):
            super(JDict, self).__init__(self.read())
        else:
            super(JDict, self).__init__(*args, **kwargs)
            self.write()
        
    def __setitem__(self, key, value):
        assert key
        assert value
        
        dict.__setitem__(self, key, value)
        self.write()
      
    def write(self):
        with open(self.filepath, 'w') as outfile:
            json.dump(self, outfile, sort_keys = True, indent = 4,
                ensure_ascii=False)
        
    def read(self):
        with open(self.filepath, 'r') as infile:
            jsonData = json.load(infile)
        self = jsonData
        return self
{'fraction_non_alphanumeric': 0.1410105757931845, 'fraction_numerical': 0.0015667841754798276, 'mean_word_length': 1.6940928270042195, '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': '4352069', 'n_tokens_mistral': 819, 'n_tokens_neox': 811, 'n_words': 173}