Table of Contents
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.
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
Description | Address |
---|---|
Official Python website | http://www.python.org/doc/ |
Full Python 2.1 documentation | http://python.org/doc/2.1/index.html |
Python 2.1 tutorial | http://python.org/doc/2.1/tut/tut.html |
A description of all available Python libraries | http://pydoc.org/2.1 |
Information on Python and Java integration | http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html |
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.
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
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
Table 14.3. Python Logical operators and values
Value or Operator | Evaluates 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 True | 1 |
constant False | 0 |
built-in function
bool(expr) | True if expr is true, False otherwise. |
None, numeric zeros, empty sequences and mappings | False |
all other values | True |
The if statement is used for conditional code execution. It may contain the optional elif (else if) and else clauses.
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.
The for loop iterates through a list of values. The continue and break statements described above can also be used with for loops.
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
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.
Objects are commonly used in Gython scripts. This section explains how to create objects and access it is attributes.