Chapter 14. Gython – the AdvancedMiner Scripting Language

Table of Contents

Python quick reference
Syntax
Variables
Operators
Flow control
Working with objects
Defining and calling functions
Gython methods for different types of variables
String methods
List methods
Dictionary methods
Python Library functions
Built-in functions
String functions
Mathematical functions
Random functions
Date/time objects
Managing Gython objects
Constructing and accessing objects
Saving objects
Loading objects
Renaming objects
Executing tasks
Deleting objects
Checking object existence
Task termination
Saving script environment
Loading script environment
Setting alias to the metadata repository
Sending messages to the log
Registry Repository
Project path
Context Scripts
How do context scripts work?
Where can I find context scripts?
Writing context scripts
Requesting user input using InputDialog

Gython is a scripting language used in AdvancedMiner for data processing and manipulation of DataMining objects. Gython is derived from the Python language, so it is strongly recommend for the user to get familiar with the Python language. After reading this chapter we suggest you look through numerous example scripts in the Client/scripts directory.

Python quick reference

This chapter covers the basics of Python. The topics covered in this chapter are sufficient to work with AdvancedMiner and understand Gython scripts. For more advanced reference visit the official Python documentation (see the links below). It is strongly recommended to read the Python tutorial. The scripting language Gython used in AdvancedMiner is compatible with Python 2.1. All libraries (modules) provided with Python 2.1 are provided with Gython, but not all were tested.

There are numerous resources on Pythen available on the web. The table below lists some of them.

Table 14.1. Online Python resources

DescriptionAddress
Official Python websitehttp://www.python.org/doc/
Full Python 2.1 documentationhttp://python.org/doc/2.1/index.html
Python 2.1 tutorialhttp://python.org/doc/2.1/tut/tut.html
A description of all available Python librarieshttp://pydoc.org/2.1
Information on Python and Java integrationhttp://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html

Syntax

Python instructions are separated by a new line without a semicolon. Scope is marked with indentations by tabs or spaces. The number of spaces or tabs does not matter as long as it is equal for all statements in a block. No braces are required to separate blocks of code.

Comments in Python begin with the '#' character. All text in a line after the '#' symbol is ignored by the Python interpreter.

Example 14.1. Syntax example:

# this is a comment

number=1
character='c'   # this is also a comment
if number == 1 :
     print character
number = 2
    

Output:

c
    

Variables

Variable names in Python are case sensitive and must start with a letter or the underscore character. Except for the first character, names can contain digits.

Python does not require explicit data type declarations for variables. Furthermore, the data type of an already used variable can be changed during the execution of a script.

The most often used Python data types are:

  • Numbers (including integer and floating point numbers)

  • Strings

  • Lists - the variables of this type are ordered sequences of elements. In scripts a list is created by enclosing a comma-separated sequence of elements in square brackets. See the example below.

  • Dictionaries - the variables of this type are collections of key : value pairs separated by commas and enclosed within braces. See the example below.

Example 14.2.  Variable usage:

  # Numbers
print "Numbers:"
a = 10                                             # integer number
b = 1.5                                            # floating point number
i, j, k = 1.5, 2.6, 3.7                            # quick variable assignment

print a
print b
b = 3                                              # b is an integer number now
print b
print i, j, k

  # Strings
print "String:"
str = "This is a example string"
print str

  # lists
print "Lists:"
l1 = ['circle', 'triangle', 'box']                # a simple list with 3 strings
l2 = [l1, 'point']                                # a list containing another list and one string

print l2
print l2[0]
print l2[0][1]                                    # accessing elements of a nested list
print l2[1]

  # dictionaries
print "Dictionaries:"
dict = {'Mary': 'cat', 'John': 'dog', 'Bob': 'chicken'}

print "Mary has a", dict['Mary']
    

Output:

Numbers:
10
1.5
3
1.5 2.6 3.7
String:
This is a example string
Lists:
[['circle', 'triangle', 'box'], 'point']
['circle', 'triangle', 'box']
triangle
point
Dictionaries:
Mary has a cat
    

Operators

Arithmetic and string operators

assignment( = ):

Example 14.3. Assignment operator:

number = 10                  # a simple assignment
str = 'a string'
var1 = var2 = var3 = 3
var4, var5 = 4, 5

print "This is "+str
print "var1 =", var1,"var2 =",var2,"var3 =",var3

print "before swap: var4=",var4,"var5=",var5
(var4, var5) = (var5, var4)    # swap variables
print "after swap: var4=",var4,"var5=",var5

sum = var4 + var5
print "Sum of var4 and var5 equals",sum
    

Output:

This is a string
var1 = 3 var2 = 3 var3 = 3
before swap: var4= 4 var5= 5
after swap: var4= 5 var5= 4
Sum of var4 and var5 equals 9
    
addition and subtraction:

Example 14.4. Addition and substraction:

a=2
b=3
sum = a + b
sub = b - a
print "a + b =", sum
print "b - a =", sub
    

Output:

a + b = 5
b - a = 1
    
division ( / ) and modulus division ( % ):

Example 14.5. Division and modulus division:

a = 7.0
b = 3
c = a / b
d= a % b

print "a / b =", c
print "a % b =", d
    

Output:

a / b = 2.3333333333333335
a % b = 1.0
    
multiplication ( * ) and exponentiation ( ** ):

Example 14.6. Multiplication and exponentiation:

a = 2
b = 4
c = a * b
d = a ** 2

print "a * b =",c
print "a ** 2 =",d
    

Output:

a * b = 8
a ** 2 = 4
    
string concatenation ( + ) and repetition ( * ):

Example 14.7. String concatenation and repetition:

str1 = "AdvancedMiner"
str2 = "System"

name = str1 + " " + str2
print "." * 20
print name
print "." * 20
    

Output:

....................
AdvancedMiner System
....................
    

Comparison operators

Table 14.2. Python Comparison operators

Comparison typeOperator
Less than<
Greater than>
Less then or equal<=
Greater than or equal>=
Equal==
Not equal!=

Logical operators and values

Table 14.3. Python Logical operators and values

Value or OperatorEvaluates as
boolean negation (not x) True if x is False, False otherwise
boolean and (x and y) if x is False then x, otherwise y.
boolean or (x or y) if x is False then y, otherwise x
constant True1
constant False0
built-in function
bool(expr)
True if expr is true, False otherwise.
None, numeric zeros, empty sequences and mappings False
all other valuesTrue

Flow control

if statement

The if statement is used for conditional code execution. It may contain the optional elif (else if) and else clauses.

Example 14.8. If statement:

a=1
b=2
if a > b :
   print "a is greater than b"
elif a < b:
   print "b is greater than a"
else:
   print "a is equal to b"
    

Output:

b is greater than a
    
while loop

The while loop is used to repeat a block of code more then once. The code inside the while loop is executed as long as the specified condition is true. It is possible to exit the loop earlier by using the break statement. It is possible to jump to the beginning of the loop with the continue statement.

Example 14.9. While loop:

i = 0
while i<10 :
   i = i + 1
   if i == 8 :
      print "breaking the loop"
      break
   if i == 3 :
      print "jump to the beginning of the loop"
      continue
   print i
    

Output:

1
2
jump to the beginning of the loop
4
5
6
7
breaking the loop
    
for

The for loop iterates through a list of values. The continue and break statements described above can also be used with for loops.

Note

The range statement is commonly used with for loops. It returns a list containing integer values. The range statement accepts one, two or three parameters.

Syntax:

range(n)       # returns a list of integers from 0 to n
range(m, n)    # returns a list of integers from m to n
range(m, n, k) # returns a list of integers from m to n in steps of k
                            

Example 14.10. For loop:

for i in range(10,200,10):
   if i == 90 :
      print "breaking the loop"
      break
   if i == 50 :
      print "jump to the beginning of the loop"
      continue
   print i
    

Output:

10
20
30
40
jump to the beginning of the loop
60
70
80
breaking the loop
    
pass statement

Sometimes it is necessary to create an empty code block, for example an empty for loop or an empty if block. In such case Python syntax requires the pass statement, which does nothing.

Example 14.11. Pass statement:

for i in range(1,10):
   if i == 4:
      pass    #do nothing when i is equal 4, pass statement is
              #required because of python syntax
   else:
      print i,
    

Output:

1 2 3 5 6 7 8 9
    

Working with objects

Objects are commonly used in Gython scripts. This section explains how to create objects and access it is attributes.

Example 14.12. Object creation:

# create a new instance of ClassificationFunctionSettings class
cfs = ClassificationFunctionSettings()

# some classes require a parameter
pd = PhysicalData('train_data_name')
                

Example 14.13. Creating an object and accessing its attributes:

#set the value of the attribute 
cfs = ClassificationFunctionSettings()
cfs.setName('classFunSet_1')

# access the value of the attribute
print cfs.getName()
    

Output:

classFunSet_1
    

Defining and calling functions

Functions are blocks of code identified by a unique name. Functions can have parameters passed to then and can return values. Even if a function does not take parameters, parentheses are still required. Functions are defined using the def statement:

Example 14.14. Defining and calling functions

def getCubic(x):
   print "Computing x*x*x"
   return x * x * x

print getCubic(3)
    

Output:

Computing x*x*x
27