Sunday, 22 June 2014

Metasequoia Script – Import one of your own modules

Sometimes a Metasequoia Python script may contain many lines of code.

To keep the script simple you can split the script into several smaller scripts.

For instance you could have all your function definitions in a script of their own.

To access those functions you would import the script containing your functions into your main script the same as we have been doing for Python’s inbuilt modules such as “math”.

The script you import must have a filename that is a valid Python identifier so cannot start with a number and use only letters or numbers or the underscore, ‘_’.

The script file must have the extension ".py".

The script you import must not have any executing code or only have executing code in an “if __name__== “__main__”:” block.

For Metasequoia 4 the script or scripts you import must be in the same folder with the main script.

If you use Metasequoia’s MQSystem classes in the script you import, in that script you must explicitly import the MQSystem module as follows or you will get errors.

import MQSystem


Here is a script that defines a function named “adder” to add two numbers and then print the sum to the output.

'''
Metasequoia Python script


type a description here

'''

# imports


# global variables
doc = MQSystem.getDocument()


# function definitions
def mqprint(message):
 try:
  print(message)
 except:
  MQSystem.println(repr(message))
 return
 
def adder(x, y):
 return x + y
 
# main script function
def main():
 a = 3
 b = 7
 total = adder(a, b)
 mqprint(total)
 return 
 

if __name__ == "__main__":
 # clear output window
 MQSystem.clearLog()
 
 # run main function
 main()
 
 # let user know script finished
 mqprint("Script finished")

The output from the script is shown below.

10
Script finished

We can write the “adder” function in another Python script file named “mymodule.py”.

# mymodule.py

def adder(x, y):
 return x + y

We delete the “adder” function definition from the main script and instead import it from “mymodule.py”.

To use the function notice how we have to prefix it with “mymodule” and then a full stop (dot) ‘.’.

'''
Metasequoia Python script


type a description here

'''

# imports
# import mymodule module to use adder function 
# when importing a module omit the .py extension
import mymodule ## new

# global variables
doc = MQSystem.getDocument()


# function definitions
def mqprint(message):
 try:
  print(message)
 except:
  MQSystem.println(repr(message))
 return
 
# main script function
def main():
 a = 3
 b = 7
 total = mymodule.adder(a, b) ## new
 mqprint(total)
 return 
 

if __name__ == "__main__":
 # clear output window
 MQSystem.clearLog()
 
 # run main function
 main()
 
 # let user know script finished
 mqprint("Script finished")

The output from the new main script is the same as before.

10
Script finished

Remember to import Python modules such as "math" in the script you import if it needs them.

This post [link] explains some of the basics of using Metasequoia’s Script Editor.

No comments:

Post a Comment