Switching to Python 3

Technical Note #59

By Tessa Hayman

May 2021

We introduced the option to choose to use Aimsun Next with Python 3 in the latest version, Aimsun Next 20. However, there are some important differences to be aware of between Python 2 and Python 3 when you begin to convert your models and scripts. This technical note will cover the key differences and how to update your Python 2 script to get it Python 3ready.  

 

In January 2020, Python 2 reached endof-life status, which means it will receive no further updates or bug fixes and, importantly, no security fixes. We therefore recommended that you use Python 3 for all new development. A lot of Python 3 code in Aimsun software is useable also in Python 2. However, you may find that your old script gives errors when used with Python 3 – particularly with respect to print statements! 

 

In addition to print statements, its good to be aware of the major differences so you can check what needs to be changed if you did wish to use the same script or cost function on multiple models as some changes may change your script without giving an error. 

 

Print 

The most common change you will see is with print statements. In Python 3, the print statement should be in brackets. 

Python 2
				
					print “hello” 
				
			
Python 3
				
					print (“hello”)  
				
			

Integer division 

In Python 2, when two integer variables were divided, an integer value would be generated. However, in Python 3 this has been changed so that a float value is given as an output, which protects against a common place of confusion for users.  

 

This could have a significant impact if you have integer division in your script. 

Python 2
				
					5/3=1 
				
			
Python 3
				
					5/3=1.6666... 
				
			

Unicode 

In Python 2, you had to specify if a string was to be stored as Unicode using “u” before the string. In Python 3, Unicode is now the default storage method of strings 

Python 2
				
					u“café” 
				
			
Python 3
				
					“café” 
				
			

Iterate over a range 

xrange() is often used in Python 2 to iterate over a set of objects in a list or set in a for-loop. It is useful for one-time operations but can become inefficient if its repeated multiple times as the generation of the range is repeated. It also can’t be used with functions applied to lists. Thereforeit has been replaced with range()which generates a range object which works more like a list. 

Python 2
				
					for x in xrange(): 
				
			
Python 3
				
					for x in range(): 
				
			

Iterate over dict 

Iteration over the keys of a dictionary has been aligned to be the same as iterating over a list. .iterkeys() is no longer needed and instead you can iterate directly over the keys. To iterate over the values, you can use .values() rather than itervalues(). 

Python 2
				
					for x in dict.iterkeys(): 
for x in dict.itervalues(): 
				
			
Python 3
				
					for x in dict: 
for x in dict.values(): 
				
			

Raise exceptions 

Exceptions can be raised to alert the user to an error and tell the script what to do in the case of an error. In Python 3 this exception must be put into brackets. 

Python 2
				
					raise IOError, “file error” 
				
			
Python 3
				
					raise IOError(“file error”) 
				
			
Handling exceptions 

Try and except are used to test and handle errors in code. The try block allows you to test a block of code for errors whereas the except block lets you handle the error. You can specify in the except to do a certain action if a named error is raised. The syntax for the except statement has been altered in Python 3 to include “as”.  

 

e.g.  

				
					try:  

print(x)  

except NameError as err:  

print("x is not defined")  

print(err) 

except:  

print("Something else")
				
			

The syntax of the except line has been altered to include an “as” and this example would print the following: 

 

x is not defined 

name ‘x’ is not defined 

Python 2
				
					except NameError, err: 

				
			
Python 3
				
					except NameError as err: 
				
			

Next() 

Next allows you to return the next item in an iterator. Previously in Python 2 it could be specified using List.next() and next(List) but in Python 3 only the latter syntax is possible. 

Python 2
				
					X=next(List) or 
X=List.next() 
				
			
Python 3
				
					X =next(List) 
				
			

Namespace leaks 

In Python 2, it was possible for the iterated variable used in a for-loop to leak into the global namespace. Or namely, if you used the same variable name before and after the for-loop, the for-loop variable could overwrite that value with the final value for the variable in the loop. 
 
e.g.  

				
					variable = 0 
print 'before: variable =', variable 
print 'for-loop: ', [variable for variable in range(5)] 
print 'after: variable =', variable 
				
			
Python 2
				
					before: variable = 0 
for-loop:  [0, 1, 2, 3, 4] 
after: variable = 4 
				
			
Python 3
				
					before: variable = 0 
comprehension: [0, 1, 2, 3, 4] 
after: variable = 0 
				
			

More technical notes

Scripting with time series

March 2021: Tessa Hayman guides you through the complexity of handling time series via Python scripting and explains how to create your custom time series in a way that can be visualized in the UI.

Macro Function Components

March 2016: Marga Delgado explains how to use macro function components to produce extra outputs when executing an assignment.

  • Got a question? Get in touch.

    We are here to help!

SHARE

Cite Aimsun Next

 

Aimsun Next 23

Aimsun (2023). Aimsun Next 23 User's Manual, Aimsun Next Version 23.0.0, Barcelona, Spain. Accessed on: July. 19, 2023. [Online].
Available: https://docs.aimsun.com/next/23.0.0/

 


 

Aimsun Next 20.0.5

Aimsun (2021). Aimsun Next 20.0.5 User's Manual, Aimsun Next Version 20.0.3, Barcelona, Spain. Accessed on: May. 1, 2021. [In software].
Available: qthelp://aimsun.com.aimsun.20.0/doc/UsersManual/Intro.html
 

Aimsun Next 23

@manual {​​​​​​​​AimsunManual,

title = {​​​​​​​​Aimsun Next 23 User's Manual}​​​​​,
author = {​​​​​​​​Aimsun}​​​​​​​​,
edition =  {​​​​​​​​​​​​​​​Aimsun Next 23.0.0}​​​​​​​​​​​​​​​,
address = {​​​​​​​​​​​​​​​Barcelona, Spain}​​​​​​​​​​​​​​​,
year  = {​​​​​​​​​​​​​​​2023. [Online]}​​​​​​​​​​​​​​​,
month = {​​​​​​​​​​​​​​​Accessed on: Month, Day, Year}​​​​​​​​​​​​​​​,
url = {​​​​​​​​​​​​​​​https://docs.aimsun.com/next/23.0.0/}​​​​​​​​​​​​​​​,
}​​​​​​​​​​​​​​​


Aimsun Next 20.0.5

@manual {​​​​​​​​AimsunManual,

title = {​​​​​​​​Aimsun Next 20.0.5 User's Manual}​​​​​​​​,
author = {​​​​​​​​Aimsun}​​​​​​​​,
edition =  {​​​​​​​​​​​​​​​Aimsun Next 20.0.5}​​​​​​​​​​​​​​​,
address = {​​​​​​​​​​​​​​​Barcelona, Spain}​​​​​​​​​​​​​​​,
year  = {​​​​​​​​​​​​​​​2021. [In software]}​​​​​​​​​​​​​​​,
month = {​​​​​​​​​​​​​​​Accessed on: Month, Day, Year}​​​​​​​​​​​​​​​,
url = {​​​​​​​​​​​​​​​qthelp://aimsun.com.aimsun.20.0/doc/UsersManual/Intro.html}​​​​​​​​​​​​​​​,
}​​​​​​​​​​​​​​​

Aimsun Next 23

TY  - COMP
T1  - Aimsun Next 23 User's Manual
A1  - Aimsun
ET - Aimsun Next Version 23.0.0
Y1  - 2023
Y2 - Accessed on: Month, Day, Year
CY  - Barcelona, Spain
PB  - Aimsun
UR  - [In software]. Available: https://docs.aimsun.com/next/23.0.0/


Aimsun Next 20.0.5

TY  - COMP
T1  - Aimsun Next 20.0.5 User's Manual
A1  - Aimsun
ET - Aimsun Next Version 20.0.5
Y1  - 2021
Y2 - Accessed on: Month, Day, Year
CY  - Barcelona, Spain
PB  - Aimsun
UR  - [In software]. Available: qthelp://aimsun.com.aimsun.20.0/doc/UsersManual/Intro.html