def save_data(self): """Dumps data structures to JSON files""" with open(f'{self.data_file}/clocks.json', 'wb') as file: file.write(jsons.dumpb(self.clocks)) with open(f'{self.data_file}/contacts.json', 'wb') as file: file.write(jsons.dumpb(self.contacts)) with open(f'{self.data_file}/memories.json', 'wb') as file: file.write(jsons.dumpb(self.memories)) with open(f'{self.data_file}/dead_characters.json', 'wb') as file: file.write(jsons.dumpb(self.dead_characters))
def test_dumpb(self): class A: def __init__(self): self.name = 'A' class B: def __init__(self, a: A): self.a = a self.name = 'B' dumped = jsons.dumpb(B(A()), jdkwargs={'sort_keys': True}) b = json.dumps({ 'a': { 'name': 'A' }, 'name': 'B' }, sort_keys=True).encode() self.assertEqual(b, dumped)
"""Pickle2Json Converts old pickled data into the newer JSON format Author: Josh Rogers (2021) """ from pbabot.main import Clock, Contact import pickle import jsons try: with open('data.pickle', 'rb') as file: pickled_data = pickle.loads(file) except FileNotFoundError: print('Pickled data not found.') else: with open('clocks.json', 'wb') as file: file.write(jsons.dumpb(pickled_data['clocks'])) with open('contacts.json', 'wb') as file: file.write(jsons.dumpb(pickled_data['contacts'])) with open('memories.json', 'wb') as file: file.write(jsons.dumpb(pickled_data['memories'])) with open('/dead_characters.json', 'wb') as file: file.write(jsons.dumpb(pickled_data['dead_characters']))
def _rewrite_db(self): self._db.put(self._list_name, jsons.dumpb(self))
def encode(self): return jsons.dumpb(self)