sikuli-driver team mailing list archive
-
sikuli-driver team
-
Mailing list archive
-
Message #03803
Re: [Question #142149]: [Python] How to save/reload regions using a file
Question #142149 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/142149
RaiMan proposed the following answer:
This is a basic setup with Python marshal, that uses a dictionary, to store the Region position/size.
This should run as is in Sikuli IDE.
import marshal
class dictRegions:
def __init__(self):
self.d = {}
def getRegs(self): # returns the dict
return self.d
def setRegs(self, d): # sets the dict based on the given dict
self.d = d.copy()
def getReg(self, r): # returns a region
rx = self.d.get(r, None)
if not rx: return None
return Region(rx[0], rx[1], rx[2], rx[3])
def addReg(self, n, r): # adds a region to dict
self.d[n] = (r.x, r.y, r.w, r.h)
def saveRegs(self, f): # saves the dict to file
save = file(f, "wb")
marshal.dump(self.d, save)
save.close()
def loadRegs(self, f): # reloads the dict from file
save = file(f, "rb")
self.d = marshal.load(save)
save.close()
# ****** usage as examples
print "\n**** step 1: original"
d = dictRegions()
d.addReg("r1", Region(100,200,300,400))
reg1 = d.getReg("r1")
print "reg1=", reg1
reg2 = reg1.right(200)
d.addReg("r2", reg2)
d.saveRegs("/Users/rhocke/saveregs.txt")
print "saved dictRegions=", d.getRegs()
print "\n**** step 2: modified"
dnew = {"r1":(101, 201, 301, 401)}
d.setRegs(dnew)
print "r1=", d.getReg("r1")
print "modified dictRegions=", d.getRegs()
print "\n**** step 3: reloaded"
d.loadRegs("/Users/rhocke/saveregs.txt")
reg1 = d.getReg("r1")
print "reg1=", reg1
print "reloaded dictRegions=", d.getRegs()
--
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.