X
12

Passing named structures between IDL and Python using the IDL Python bridge

 

Named structures cannot be passed directly between IDL and Python when using the IDL-Python bridge. Therefore, the transfer of named structures from IDL to Python, and vice versa, must be handled manually.

One Python equivalent of an IDL named structure is a dataclass. The first example below shows how to create an IDL named structure and pass it to Python to create the corresponding Python dataclass, using the Python-to-IDL bridge. The second example demonstrates the reverse process, that is, how to recreate an IDL named structure from a Python dataclass, still using the Python-to-IDL bridge.

 

Launching the Python to IDL bridge (Windows-based example)

(py313) C:\Users\berangere.casson>python

Python 3.13.15 | packaged by Anaconda, Inc. | (main, Aug 14 2026, 17:17:48) [MSC v.1942 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import sys

>>> sys.path.append('C:\\Program Files\\NV5\\ENVI62\\IDL92\\lib\\bridges')

>>> from idlpy import *

IDL 9.2.0 (Win32 x86_64 m64).

(c) 2025, NV5 Geospatial Solutions, Inc.

Licensed for use by: NV5 Geospatial Internal Use Only

License: xxxxxxx-internal

>>> 

 

Creating an IDL named structure in IDL

>>> IDL.run('a={test_str,aa:2,bb:2.0,st:"str"}')

>>> IDL.run('st_name=typename(a)')

>>> IDL.run('help,a')

** Structure TEST_STR, 3 tags, length=24, data length=22:

   AA              INT              2

   BB              FLOAT           2.00000

   ST              STRING    'str'

>>> IDL.run('print,a.aa')

       2

>>> IDL.run('print,a.bb')

      2.00000

>>> IDL.run('print,a.st')

str

>>>

 

Example 1: converting an IDL named structure to a Python dataclass, using the Python-to-IDL bridge

>>> a=IDL.a

>>> st_name=IDL.st_name

>>> print(st_name)

TEST_STR

>>> print(a)

OrderedDict({'AA': np.int16(2), 'BB': np.float32(2.0), 'ST': 'str'})

>>> from dataclasses import make_dataclass

>>> fields = [(k, type(v)) for k, v in a.items()]

>>> StructClass = make_dataclass(st_name, fields)

>>> obj = StructClass(**a)

>>> print(obj)

TEST_STR(AA=np.int16(2), BB=np.float32(2.0), ST='str')

>>> print(obj.AA)

2

>>> print(obj.BB)

2.0

>>> print(obj.ST)

str

>>> 

 

Example 2: converting a Python dataclass to an IDL named structure, using the Python-to-IDL bridge

>>> from dataclasses import fields

>>> from collections import OrderedDict

>>> od = OrderedDict((f.name, getattr(obj, f.name)) for f in fields(obj))

>>> print(od)

OrderedDict({'AA': np.int16(2), 'BB': np.float32(2.0), 'ST': 'str'})

>>> struct_name = obj.__class__.__name__

>>> IDL.struct=od

>>> IDL.name_st=struct_name

>>> IDL.run('struct = create_struct(name=name_st, struct.toStruct())')

>>> IDL.run('help,struct')

** Structure TEST_STR, 3 tags, length=24, data length=22:

   AA              INT              2

   BB              FLOAT           2.00000

   ST              STRING    'str'

>>> IDL.run('print,struct.AA')

       2

>>> IDL.run('print,struct.BB')

      2.00000

>>> IDL.run('print,struct.ST')

str

>>>

 

 

 

---------------------------------------------------------

created by BC (EU) on 9/9/26

reviewed by BC (US) on 9/9/26