Skip to content

Instantly share code, notes, and snippets.

@mindforger
Created July 16, 2017 09:40
Show Gist options
  • Save mindforger/f137ee6d59dd1ebb68d2394619be54e7 to your computer and use it in GitHub Desktop.
Save mindforger/f137ee6d59dd1ebb68d2394619be54e7 to your computer and use it in GitHub Desktop.
python circular buffer log file
import random
indicator = 0
maxline = 30
def readSeek(file_ptr):
file_ptr.seek(0,0)
ind_str = file.readline()
return int(ind_str)
def calcSeek():
return (indicator*8)
def writeSeek(file_ptr, ind):
file_ptr.seek(0,0)
line = "%6d\n" % (ind)
file.write(line)
print("opening file")
file = open("test.txt","r+")
indicator = readSeek(file)
print(str(indicator))
indicator += 1
if indicator > maxline:
print("indicator overflow, wrappning around!")
indicator = 0
else:
print(str(indicator))
file.seek(calcSeek(),0)
line = "%6d\n" % (random.randint(0,0xFFFF))
file.write(line)
writeSeek(file,indicator)
print("closing file")
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment