details about coded lines

Asked by Richi

Hello, well i have tested the code below which does the following :

 the data.configure.camera=true argument is changed into data.configure.camera=false in an Instructions file.

However , i would like to know if someone can describe the code lines below in order that i can understand properly which coded line does what please?

Thanx.

Here is the code below which i used:

def getInstructions(inp): --line 1 does what ?

    store = {} --line 2 does what ?

    try: -- line 3 does what ?

      configInput = open(inp) --line 4 does what ?

      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)

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Eugene S (shragovich) said :
#1

Hi,

Line 1:
This is a definition of "getInstructions" function that gets "inp" as a parameter. I assume that "inp" is a file path.

Line 2:
Defines a Python(or this case Jython) dictionary and stores in in variable named "store"

Line 3:
Try statement is an exception handler for a group of statements. In this case, if an error will occur in any of the lines between "try" and except, the code execution will skip to "except" part instead of just failing.

Line 4:
Looks like opening a file which path is defined by "inp". File handler is then saved in "configInput".

Cheers,
Eugene

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

@Eugene
well done ;-)
thanks for taking over (see question https://answers.launchpad.net/sikuli/+question/252800 ;-)

I think Richi should learn some basic Python using one of the excellent tutorials in the net.

I am offline from tomorrow for 2 weeks vacation - at least very low Sikuli activity ;-)

Revision history for this message
Eugene S (shragovich) said :
#3

@RaiMan

Oh wow, now I see where it's coming from :)

Enjoy your vacation!
I will try to help as much as my knowledge lets me.

Can you help with this problem?

Provide an answer of your own, or ask Richi for more information if necessary.

To post a message you must log in.