import os

class SavedInteger:
    dirty = False
    def __init__(self,location,default):
        self.location = location
        self.value = default
        self.load()
    def get(self):
        if self.value is None: return self.default
        return self.value
    def set(self,value):
        self.dirty = True
        self.value = value
        self.save()
    def __gt__(self,other):
        return self.value > other
    def __eq__(self,other):
        return self.value == other
    def __int__(self):
        return self.value
    def __repr__(self):
        s = 's('+str(self.value)
        if self.dirty:
            s = s + '*)'
        else:
            s = s + ')'
        return s
    def load(self):
        try:
            with open(self.location,'rb') as inp:
                self.value = int(inp.read().decode())
            self.dirty = False
        except IOError as e:
            pass
    def save(self):
        if not self.dirty: return
        with open(self.location+'.tmp','wb') as out:
            out.write((str(self.value)).encode())
        try: os.unlink(self.location)
        except OSError: pass
        os.rename(self.location+'.tmp',self.location)
        self.dirty = False
