Conversion – Core Functionality

While the IDL and Python languages deliver similar syntax and features, core differences exists between the languages. Some core difference are abstract, and easily handled by Pike, but others require translation time logic adaptation. Details on the unique translation time modifications Pike performs is outlined in the following sections.

Data Types

While IDL provides a rich, extensive set of data types to support scientific computing operations, core Python provides three: integers, floating point and string data types. A full suite of data type is support is delivered to Python via the NumPy package.

Pike maps IDL data types to those provided by NumPy. The following table outlines the type matching used.

IDL Python
Undefined None
byte uint8
integer int16
long int32
float float32
double float64
complex complex64
string string_
struct PikeStructure
dcomplex complex128
ptr object
object object
uint uint16
ulong uint32
l64 int64
ul64 uint64

These NumPy types are used in all the type conversion and type informational routines provided by Pike.

Note: Pike does not currently support IDL Object or Pointer types.

Numeric Constants

Numeric constants provide a unique challenge, since the Python compiler only recognizes the three aforementioned numeric types: integer and floating, but IDL supports a wide variety of constant number types. To ensure the correct numeric type at runtime, Pike wraps specifically typed numeric constants with the appropriate Numpy numeric conversion function.

Examples of this conversion

In IDL:

   a = 0b
   b = 0
   c = 0l
   d = 1.2
   e = 1.2d
   f = 0u
   g = 0ul
   h = 0ll
   i = 0ull

In Python:

    a = np.uint8(0)
    b = 0
    c = np.int32(0)
    d = 1.2
    e = np.float64(1.2e0)
    f = np.uint16(0)
    g = np.uint32(0)
    h = np.int64(0)
    i = np.uint64(0)

Octal and Hexadecimal Constants

Octal and Hexadecimal constants are expressed differently by IDL and Python. Pike performs the necessary conversion during translation.

In IDL:

    num_oct = "15
    num_hex = ‘0FF11'x

In Python:

    num_oct = 015
    num_hex = 0x0ff11

Undefined Variables

An undefined variable is just another type in IDL and commonly used when implementing a function with optional arguments. In Python, an undefined variable in not a valid state, and if an undefined variable is provided as an argument in a function call, an error is thrown by the Python runtime.

Pike detects undefined variables during the conversion process and sets them to a value of zero at the beginning of the function. Pike also labels these variables with a comment, indicating their undefined state.

It should also be noted that calling a function with an undefined variable often denotes that the value is an output parameter. Pike uses this as a signal during the conversion process, helping to determine output variables.

Control Statements

While most control statements that IDL implements are supported in Python, several are not. This section outlines these statements and how Pike translates them.

Repeat Statement

The structure of an IDL repeat statement is not supported in Python. To support the logical flow of a repeat statement in Python, Pike uses an infinite while statement that uses an if statement to exit the loop.

In IDL:

     tmp = 0
     repeat begin
        tmp = tmp + 1
     endrep until tmp gt 10

In Python:

    tmp = 0
    while True:
        tmp = tmp + 1
        if tmp > 10:  break

Case and Switch Statements

Python provides no intrinsic Case or Switch statement support, eliminating the possibility of a direct conversion from IDL to a native Python solution. However, a common implementation pattern does exist for implementing case and switch statements in Python and Pike utilizes this pattern to deliver a solution a case/switch implementation that resembles IDLs structure.

While the specific details of Pike’s implementation are beyond the scope of this discussion, further information is available in Pike’s source code, or at this location.

Example of a case statement conversion:

In IDL:

    case input of
        1:     return, 1
        2:     return, 2
        else:  return, 4
    endcase

In Python:

    for __case in pike.case(input): 
         if __case(1):    
             return 1
         if __case(2):    
             return 2
         if __case(): # else clause     
             return 4

Example of a switch statement conversion.

In IDL:

    tmp = 0 
    switch input of
     1:   tmp= tmp + 1
     2:   tmp= tmp + 1
     3:   begin
          tmp= tmp + 1
          break
       end
     else:  return,0
    endswitch
se

In Python:

    tmp = 0
    for __switch in pike.switch(input): # A switch statement
         if __switch(1):    
             tmp = tmp + 1
         if __switch(2):    
             tmp = tmp + 1
         if __switch(3):    
             tmp = tmp + 1
             break
         if __switch(): # else clause     
             return 0

Miscellaneous Items

NOTs

To support both the logical and bitwise not operators that IDL implements via syntax, Pike converts the operators to their NumPy counterparts.

IDL Python
Logical Not not numpy.local_not()
Bitwise Not ~ numpy.bitwise_not()

Include Files

IDL allow for the inclusion of a common set of source code through it’s include file capability (@<filename>). A similar functionality isn’t implemented in Python.

To provide this functionality, Pike inlines the include file contents into the conversion file input text before the conversion process. This maintains the original IDL functionality, while conforming to the limitations of Python.

Symbol Case

IDL is a case insensitive language, while Python is case sensitive. As such, changes in the case used to identify a symbol (variable, function) has no effect in IDL, but will lead to error in Python.

To ensure proper operation of the resultant Python code, Pike performs the following:

  • Pike converts all functions to lower case.
  • In a function, the first use of a variable defines the symbols case for the entire routine.
  • System variables are converted to upper case.