Skip to content

Instantly share code, notes, and snippets.

@Jiler01
Created December 10, 2023 15:13
Show Gist options
  • Save Jiler01/496162b73c9c3c54883f9cf7e1abe61c to your computer and use it in GitHub Desktop.
Save Jiler01/496162b73c9c3c54883f9cf7e1abe61c to your computer and use it in GitHub Desktop.
Some easy-to-use json dict storage system
import os,json
class DATA:
def __init__(self,name:str,*,default:dict={}):
"""
name: the name of the file which will be stored in the DATA folder (without extension)
default: default value which will be used only on file initialization. Use overwrite to modify the stored value.
"""
self.path = "DATA/"+name+".json"
if not os.path.exists(self.path):
if not os.path.exists("DATA"):
os.makedirs("DATA")
self.overwrite(default)
self.update()
def update(self):
self.value = json.load(open(self.path , "r"))
def overwrite(self,value:dict):
json.dump(value, open(self.path , "w"), indent=4, sort_keys=True)
self.update()
def push(self):
self.overwrite(self.value)
def delete(self):
os.remove(self.path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment