[Tutor] How to create array of variants?

Alan Gauld alan.gauld at btinternet.com
Fri Sep 12 01:11:28 CEST 2008


"Krasyn" <mndrk at mail.ru> wrote

>>
>> I'm trying to translate the following VB code into Python and not 
>> sure how
>> to create an array of variants.

All variables in Python are effectively variants - variables that can
store any type. So an array of variants equates to a Python list

>> VB Code:
>> Sub SetXdata()

def SetXData():

>>     Dim lineObj As AcadLine
>>     Set lineObj = ThisDrawing.ModelSpace.Item(0)


lineObj = ThisDrawing.ModelSpace.Item(0)

Where ThisDrawing.ModelSpace.Item(0) is s0ome kind of
data structure you have defined elsewhere. Or fetch using COM.

>>     Dim DataType(0 To 1) As Integer
>>     Dim Data(0 To 1) As Variant

DataType = []
Data = []

But it looks like you are trying to fake a dictionary - although VB
has dictionaries!

>>     DataType(0) = 1001: Data(0) = "Test_Application"
>>     DataType(1) = 1070: Data(1) = 600

Data = {'Test_application' : 1001, 600 : 1070}

>>     lineObj.SetXdata DataType, Data

lineObj.SetXdata( Data )

>> Python code
>> import array
>> import comtypes.client
>>
>> def SetXData():
>>     activedoc =
>> comtypes.client.GetActiveObject("AutoCAD.Application").ActiveDocument
>>     line = activedoc.ModelSpace.Item(0)
>>
>>     dataType = array.array('i', [1001, 1070])
>>     dataValue = array.array('?', ['Test_Application', 600]) #What 
>> should I
>> use
>> for the type code?
>>
>>     line.SetXData(dataType, dataValue)

Here's the snag with the dictionary approach so its back to
two lists.  I wouldn't use the array module just lists.

It might be enough to just pass DataType and Data as two lists
into the COM object. I don't know enough about Python's COM
integration to be sure that it will sort it all out though. But I 
suspect
it will.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list