# Programming for Scripts¶

Extensive resources relating to Python can be found at http://www.python.org. This section is intended as a very brief introduction to the language to help a user to read and understand this manual section.

## Python ¶

Python is a scripting language. Scripts are interpreted by the Python engine directly, without the need for compilation and linking steps common to compiled languages such as C or Java, so editing and execution can be carried out directly within Aimsun Next. Python’s other advantages over C++ include the fact there is no need to worry about pointers and memory management.

One feature of Python which can cause problems, particularly to programmers from other less strict languages, is the importance of indentation to program structure. Where other languages use braces, {}, Python relies on strict indentation to mark out sections of code, ends of loops, etc. On the plus side, this does in theory at least make the code more readable, particularly between authors.

### Basics¶

Anything to the right of a hash (sometimes called pound sign) is a comment, and will not be interpreted on execution.

# This is a comment. Python will not attempt to interpret this
# print "Hello World"
# nothing happens, the print statement is behind a comment


There is no need to declare a variable: it is created assigning a value. Moreover, Python is a dynamically typed language, so different data types can be assigned to the same variable.

myInteger = 42 # an integer
myString = "Hello World" # a string
myPi = 3.142 # a float
myEmpty = None # a null
myInteger = myPi # now myInteger is a float


Python contains the following basic data structures:

• Numbers, including integers and floating point.
• Strings, enclosed by " " or ' ', and including special characters e.g. \n to start a newline.
• Boolean, True or False.
• Lists e.g. myList = [item1, item2, item 3] where items are accessed by index in the range 0 to (n-1), eg myList[1] would return item2.
• Dictionaries, in which values are stored with a key, and accessed by the key which maps to the value.

For example, the following command:

myAddresses = {'grail': 'Castle Aargh', 'arthur': 'Camelot', 'knight ni': 'shrubbery'}


would create a map as follows

Key Value
grail Castle Aargh
arthur Camelot
knight ni shrubbery

While myAddresses['arthur'] would return 'Camelot'

Strings can be treated as lists of characters. The main operations which will work with any list but are most useful for strings are:

• myString = myString1 + myString2 : Creates myString as a concatenation of two strings.
• myString[i] : Returns character at index i.
• myString[i,j] : Returns a substring from index i to j-1.
• len(myString) :Returns the length of myString.

## Object Oriented concepts¶

This is a general, and by no means complete, introduction to object oriented concepts to help a user read and understand this manual section.

### Classes¶

A class is a blueprint, or prototype, that defines variables and methods common to all objects of a certain kind. Objects are concrete instances of a class, a bundle of related methods and attributes.

Examples of classes and potential objects are given below:

Class Objects
Vehicle type car, lgv, hgv, bus, coach
Car type focus, golf, mini
Car chittyChittyBangBang, knight 2000, M_33EJB

Note the convention for naming classes with the first letter in upper case and objects with a first letter in lower case.

What constitutes a class and what constitutes an object can be application specific.The table above shows how a class scheme can be used to describe vehicle types, sub types of a vehicle type and finally specific vehicles.

### Attributes¶

Attributes are values that define the object. Attributes are common to all objects of the same class, although their values are different

Class Attributes
Vehicle route, start_time
Vehicle type travel_mode, pcus
PrivateCarType manufacturer, model, weight, power
IndividualCar owner, passengers, registration_number, color, age

The attributes of an object, if publicly accessible, can be accessed by the convention object.attribute, e.g. to obtain the age of a specific car, we would use M_33EJB.age, or to find the weight of a Mini we would use mini.weight.

## Methods¶

A method is a function the object is capable of performing. Objects of the same class share the same class methods. Methods can take several parameters as inputs and often return a value. Methods can be used to:

• Get a value from an object: myCar.getNumberOfWheels().
• Set a value for an object: myCar.setMaximumPassengers(6).
• Make the object carry out some operation or calculation:
• myCar.accelerate(rateOfAcceleration, TargetSpeed).
• myCar.calculateMaximumSpeed().