Save and Restore Script Variables

Function Description

The OPS provides the function of saving and restoring script variables in Python scripts.

A maximum of 100 script variables can be stored. A variable with the same name as one that has been stored will replace the stored one.

Command Prototype

# Save script variables.

opsObj.context.save(varName, value)

# Restore script variables.

opsObj.context.retrieve(varName)

Parameter Description

Table 1 describes the parameters supported by APIs for saving and restoring script variables.
Table 1 Parameters supported by APIs for saving and restoring script variables

Method

Description

opsObj

Specifies an OPS object. It is obtained through ops.ops() instantiation.

varName

Specifies the name of a variable. The value is a string of a maximum of 16 characters.

value

Specifies the value of a variable. The value can be a string of a maximum of 1024 characters or an integer ranging from –2147483648 to 2147483647.

Description of Return Values

  • Return values of opsObj.context.save():
    • First return value: The value 0 indicates a success, and the value 1 indicates a failure.
    • Second return value: This value describes success or failure reasons, expressed in a character string.
  • Return values of opsObj.context.retrieve():
    • First return value: If None is returned, restoring a specified user-defined environment variable fails. Otherwise, user-defined environment variable values are returned.
    • Second return value: This value describes success or failure reasons, expressed in a character string.

Example

# Save script variables.
test.py

import ops

test = ops.ops()
print 'test context save'
a, des= test.context.save("varInt1", 111)
print 'save varInt1 return' , a
a, des= test.context.save("varStr2", 'testString')
print 'save varStr2 return' , a
print 'test context save over'
# Restore script variables.
test.py

import ops

test = ops.ops()
print 'test context retrieve'
a, des = test.context.retrieve("varInt1")
print 'retrieve varInt1 = ', a
a, des = test.context.retrieve("varStr2")
print 'retrieve varStr2 = ', a
print 'test context retrieve over'
Copyright © Huawei Technologies Co., Ltd.
Copyright © Huawei Technologies Co., Ltd.
< Previous topic Next topic >