changes from True to False

Asked by Richi

Hello,

I have a question.

Actually, i have three instructions by default which are saved in a txt.file named Instructions.txt on my desktop.

the path to reach the Instructions.txt file is : "C:\\Users\\Richi\\Desktop\\Instructions.txt"

the instructions saved in the Instructions.txt file are :

configure.camera=true
configure.mobile=false
configure.radio=true

 i am looking for a function /code with SIKULI which can do the following scenario:

 the instruction "configure.camera=true" is changed into "configure.camera=false" when i call that function, and the 2 instructions namely; configure.mobile=false and configure.radio=true remain unchanged.

For the above scenario, I tried the following code below but it is not working.

import fileinput
def changecameraParameter(status):
def mobileParameter(status):
def radioParameter(status):

     for line input.input("C:\\Users\\Richi\\Desktop\\Instructions.txt", inplace = True, inplace=False, inplace = true):
          if appPattern in line:
                  print "configure.camera=status”
         else:
               print line,
changeCameraParameter("False")

Can someone help?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Richi
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

according to the definition of the file input module (that is not needed here though):

import fileinput
def changecameraParameter(status):
    pass # should be some code here
def mobileParameter(status):
    pass # should be some code here
def radioParameter(status):
    pass # should be some code here

for line in fileinput.input("C:\\Users\\Richi\\Desktop\\Instructions.txt"):
    (key, val) = line.strip().split("=")
    if key.strip().startsWith("configure.camera"):
        val = True if val.strip == "true" else False
        changeCameraParameter(val)

--- no need for fileinput

for line in open("C:\\Users\\Richi\\Desktop\\Instructions.txt").readlines():
    (key, val) = line.strip().split("=")
    if key.strip().startsWith("configure.camera"):
        val = True if val.strip == "true" else False
        changeCameraParameter(val)
    # …. same for the other options

Revision history for this message
RaiMan (raimund-hocke) said :
#2

But this is a rather clumsy solution.

Since your config file already has key/value pairs, just read the file into a dictionary and use this during your script run:

config = {} # empty dictionary
configInput = open("C:\\Users\\Richi\\Desktop\\Instructions.txt")
for line in configInput.readlines():
    (key, val) = line.strip().split("=")
    config[key.strip()] = val.strip()
configInput.close()

now you know your settings:
if config["configure.camera"] == "true":
    pass # do something that should be done in this case

if you "pythonize" your file like this:

configure.camera=True
configure.mobile=False
configure.radio=True

its even easier:

config = {} # empty dictionary
configInput = open("C:\\Users\\Richi\\Desktop\\Instructions.txt")
for line in configInput.readlines():
    (key, val) = line.strip().split("=")
    config[key.strip()] = eval(val.strip()) # evaluates the given strings to the Python value True or False
configInput.close()

# now you know your settings:
if config["configure.camera"] : # now this is sufficient
    pass # do something that should be done in this case

Be aware:
- there is currently no error checking or exception handling, so if the Instructions file does not exactly contain what is expected, it might crash

allowed already now: leading and trailing whitespace for the line and the key and value:
so this is ok:
    configure.camera = True

because of the use of strip()

Revision history for this message
Richi (ateeshr) said :
#3

hi, I thank you for your reply.

Actually the instructions saved in the Instructions.txt file are :

data.configure.camera=true
data.configure.mobile=false
data.configure.radio=true

As wanted to change the data.configure.camera=true into data.configure.camera=false only in the Instructions file,

I followed your arguments and I use the script below with sikuli but the change from true to false has not been done:

config = {} # empty dictionary
configInput = open("C:\\Users\\Richi\\Desktop\\Instructions.txt")
for line in configInput.readlines():
    (key, val) = line.strip().split("=")
    config[key.strip()] = eval(val.strip())
configInput.close()

if config["data.configure.camera"] :
    pass
  data.configure.camera = True

can you help me to change the above code please?

Revision history for this message
RaiMan (raimund-hocke) said :
#4

Then this is the most flexible solution:

step 1: read the current content of Instructions to the dictionary
step 2: modify the dictionary as needed
step 3: write the dictionary back to Instructions.txt

def getInstructions(inp):
    store = {}
    configInput = open(inp)
    for line in configInput.readlines():
        (key, val) = line.strip().split("=")
        config[key.strip()] = val.strip()
    configInput.close()
    return store

def putInstructions(out, store):
    configOutput = open(out, "w")
    for key in store.keys():
        line = key + "=" + store[key] + "\n"
        out.write(line)
    configInput.close()

# step 1: fetch current instructions
instructions = getInstructions("C:\\Users\\Richi\\Desktop\\Instructions.txt")

# step 2: modify instructions
instructions["data.configure.camera"] = "false"

# one might add additional instructions
instructions["data.configure.newoption"] = "true"

# step 3: write the instructions back to file
putInstructions("C:\\Users\\Richi\\Desktop\\Instructions.txt", instructions)

Revision history for this message
Richi (ateeshr) said :
#5

Hi i have tried the following code with sikuli :

def getInstructions(inp):
    store = {}
    configInput = open(inp)
    for line in configInput.readlines():
        (key, val) = line.strip().split("=")
        config[key.strip()] = val.strip()
    configInput.close()
    return store

def putInstructions(out, store):
    configOutput = open(out, "w")
    for key in store.keys():
        line = key + "=" + store[key] + "\n"
        out.write(line)
    configInput.close()

# step 1: fetch current instructions
instructions = getInstructions("C:\\Users\\Richi\\Desktop\\Instructions.txt")

# step 2: modify instructions
instructions["data.configure.camera"] = "false"

# one might add additional instructions
instructions["data.configure.newoption"] = "true"

# step 3: write the instructions back to file
putInstructions("C:\\Users\\Richi\\Desktop\\Instructions.txt", instructions)

But the above code does not work.

The error message which is displayed by Sikuli is the following:

[error] script [ ConfigDR ] stopped with error in line 16
[error] NameError ( global name 'config' is not defined )
[error] --- Traceback --- error source first line: module ( function ) statement 6: main ( getInstructions ) config[key.strip()] = val.strip()
[error] --- Traceback --- end --------------

line 16 is :
 # step 1: fetch current instructions
instructions = getInstructions("C:\\Users\\Richi\\Desktop\\Instructions.txt")

can you please help?

Revision history for this message
RaiMan (raimund-hocke) said :
#6

ok, my bad - consequences of scripting without testing ;-)

in
def getInstructions(inp):
    store = {}
    configInput = open(inp)
    for line in configInput.readlines():
        (key, val) = line.strip().split("=")
        config[key.strip()] = val.strip() <---- this line
    configInput.close()
    return store

… must be
        store[key.strip()] = val.strip()

Revision history for this message
Richi (ateeshr) said :
#7

Hi, i have tried your correction but still i have problem:

the new path to reach the Instructions.txt file is : "C:\\Users\\Richi\\Desktop\\Instructions.txt"

the code used is:

def getInstructions(inp):
    store = {}
    configInput = open(inp)
    for line in configInput.readlines():
        (key, val) = line.strip().split("=")
        store[key.strip()] = val.strip()
    configInput.close()
    return store
def putInstructions(out, store):
    configOutput = open(out, "w")
    for key in store.keys():
        line = key + "=" + store[key] + "\n"
        out.write(line)
    configInput.close()
# step 1: fetch current instructions
instructions = getInstructions("C:\\Users\\Astrator\\Desktop\\Instructions.txt")
# step 2: modify instructions
instructions["data.configure.camera"] = "false"
# one might add additional instructions
instructions["data.configure.newoption"] = "true"
# step 3: write the instructions back to file
putInstructions(" C:\\Users\\Astrator\\Desktop\\Instructions.txt", instructions) <---- line 22

the error message is :

[error] script [ ConfigDR ] stopped with error in line 22
[error] IOError ( (13, 'Permission denied', ' C:\\Users\\Astrator\\Desktop\\Instructions.txt') )
[error] --- Traceback --- error source first line: module ( function ) statement 10: main ( putInstructions ) configOutput = open(out, "w")
[error] --- Traceback --- end --------------

can u help me out to change the above code lines , hence , the data.configure.camera=true argument is changed into data.configure.camera=false in the Instructions file please.

thanx

Revision history for this message
Richi (ateeshr) said :
#8

Im sorry,

as i said above in the beginning of my question was wrong because :

the new path to reach the Instructions.txt file is : "C:\\Users\\Richi\\Astrator\\Instructions.txt"

not : "C:\\Users\\Richi\\Desktop\\Instructions.txt"

thank you

Revision history for this message
RaiMan (raimund-hocke) said :
#9

I have revised and tested the snippet. It works for me.
(putInstructions had 2 errors, but had nothing to do with your problem)

now a workflow
step 1
step 2
step 3

will create an instructions file with the given options in step2, if the named instructions file does not exist yet.

def getInstructions(inp):
    store = {}
    try:
      configInput = open(inp)
      for line in configInput.readlines():
          (key, val) = line.strip().split("=")
          store[key.strip()] = val.strip()
      configInput.close()
    except: pass
    return store

def putInstructions(out, store):
    configOutput = open(out, "w")
    for key in store.keys():
        line = key + "=" + store[key] + "\n"
        configOutput.write(line)
    configOutput.close()

fInstructions = "C:\\Users\\Richi\\Astrator\\Instructions.txt"

# step 1: fetch current instructions
instructions = getInstructions(fInstructions)
# step 2: modify instructions
instructions["data.configure.camera"] = "false"
# one might add additional instructions
instructions["data.configure.newoption"] = "true"
# step 3: write the instructions back to file
putInstructions(fInstructions, instructions)

so please try again with this version

Revision history for this message
Richi (ateeshr) said :
#10

hello,

its working and its FANTASTIC!!

thanx a lot...

Cheers

Revision history for this message
RaiMan (raimund-hocke) said :
#11

Thanks for feedback.

happy scripting