class dataBase :
def __init__ (self,base_name):
self.base_name=str(base_name)
def read (self):
base = open(str(self.base_name), 'r')
base_r = base.readlines()
print ('!!!!!!!!!!!!!!!!!!!!!!!!!!!!',base_r)
st1 = ''
dict_r = ''
dict_r = {}
tx=0
while tx <len(base_r) :
print ('tx :',tx,'of ',len(base_r))
base_re = base_r[tx]
st1 = base_re.split (':')
print(st1[0],st1[1])
dict_r [st1[0]] = st1[1][0]
tx+=1
print('resulr READING - ', dict_r)
global stick
if str(self.base_name) == "dataSti.txt" :
print ('reading ',self.base_name,' end!')
stick = dict_r
base.close()
return dict_r
def write (self,id,val):
self.id = id
self.val=val
print('write id - ',self.id)
self.val = self.val
print('write val - ', self.val, type(self.val))
dat1= dataBase (self.base_name)
dat1.read()
global stick
if str(self.id) in stick:
# изменяем имеюoийся айди
stick [stick[self.id]] = self.val #======================эта строка================#
print ('STICK ', stick, self.val)
base = open(self.base_name, 'w')
for k,v in stick.items() :
if k == '0':
continue
base.write(str(k +':'+v+'\n'))
base.close()
else:
# добаляем айди
base = open(self.base_name, 'a+')
base_r = base.read()
newT = base_r +str(id) + ' : ' + str(val)
base.write(newT)
base.close()