iterations-xdex/functions.py

38 lines
667 B
Python
Raw Normal View History

2020-02-28 13:09:29 +01:00
import json
# different useful functions
# line = '-----------'
# def write_to_file(filename, string):
# path = './'+filename
# f = open(path,'a+')
# f.write(string)
# f.write('\n')
# if 'mark' in filename:
# f.write(line)
# f.write('\n')
# f.close()
def load_db(filename):
try:
open(filename,'r')
except:
open(filename,'w+')
f = open(filename,'r').read()
if f == '':
f = '{}'
db = json.loads(f)
# find last_id used in db
keys = [int(x) for x in db.keys()]
keys.sort()
if len(keys) == 0:
x = 0
else:
x = int(keys[-1])
return db, x
def write_db(filename, db):
f = open(filename, 'w')
f.write(json.dumps(db, indent=4))
f.close()