Reputation: 91
I am using python to access results from another program. The program has a specific module in order to so. Unfortunately I do not understand the tuple format ("stuff") that comes out as a result.
I am familiar with looking up keys/values in dictionaries, but not how this would work. I am new to tuples and I am using 2.7.5 - any help would be great!
>>> import OrcFxAPI
>>> model = model.OrcFxAPI('C:\#17.sim')
>>> stuff = model.objects
>>> print stuff
(<General Data: 'General'>, <Environment Data: 'Environment'>, <Line Contact Data: 'Line Contact Data'>, <Vessel: 'vesselA'>, <Vessel: 'vesselB'>, <Line: 'A'>, <Line: 'B'>, <Line: 'C'>, <Line: 'D'>, <Line: 'E'>, <Line: 'F'>)
>>> print type(stuff)
<type 'tuple'>
>>> print map(type, stuff)
[<class 'OrcFxAPI.OrcaFlexObject'>, <class 'OrcFxAPI.OrcaFlexObject'>, <class 'OrcFxAPI.OrcaFlexObject'>, <class 'OrcFxAPI.OrcaFlexVesselObject'>, <class 'OrcFxAPI.OrcaFlexVesselObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>, <class 'OrcFxAPI.OrcaFlexLineObject'>]
I want to lookup instances of Line, and output 'A','B','C','D','E','F':
>>> for thing in stuff:
if isinstance(thing, Line) and thing.name == 'C':
line_c = thing
break
else:
raise ValueError('No Line C in stuff!')
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'Line' is not defined
Upvotes: 0
Views: 476
Reputation: 2925
OrcFxAPI objects of type OrcaFlexObject
and those with that base e.g. OrcaFlexLineObject
have attributes called typeName
, Name
and type
(the upper/lower cases of those is correct and a proper PITA). So, if I understand your question you can do this:
>>> import OrcFxAPI
>>> model = model.OrcFxAPI('C:\#17.sim')
>>> lines = filter(lambda o: o.typeName=="Line",model.objects)
>>> for line in lines:
print line.Name
A
B
C
D
E
F
Upvotes: 0
Reputation: 365925
It's hard to tell from your vague question, but I think what you want is something like this:
for thing in stuff:
if isinstance(thing, OrcFxAPI.OrcaFlexLineObject):
print thing.name,
print
Or, equivalently:
lines = (thing for thing in stuff
if isinstance(thing, OrcFxAPI.OrcaFlexLineObject))
names = (line.name for line in lines)
print ' '.join(names)
If you're looking for the Line
object that has a specific name, that's just as easy:
for thing in stuff:
if isinstance(thing, OrcFxAPI.OrcaFlexLineObject) and thing.name == 'C':
line_c = thing
break
else:
raise ValueError('No Line C in stuff!')
… or:
line_c = next(thing for thing in stuff
if isinstance(thing, OrcFxAPI.OrcaFlexLineObject) and thing.name == 'C')
But in general, you don't want to do this like of "type switching" code. It would be better to store this information in some way that kept all the lines separate from all the other things. Maybe this:
{'general': <General Data: 'General'>,
'environment': <Environment Data: 'Environment'>,
'line contact': <Line Contact Data: 'Line Contact Data'>,
'code checks': <Code Checks: 'Code Checks'>,
'shear7': <SHEAR7 Data: 'SHEAR7 Data'>,
'vessels': (<Vessel: 'vesselA'>, <Vessel: 'vesselB'>),
'lines': (<Line: 'A'>, <Line: 'B'>, <Line: 'C'>, <Line: 'D'>, <Line: 'E'>, <Line: 'F'>)
}
And then it would be a matter of just using a dict lookup (which you already know how to do) and iterating over a sequence (which you already know how to do):
for line in stuff['lines']:
print line.name,
print
Or, maybe even better, use a class instead of a dict, so the object has a lines
attribute that you can use like this:
for line in stuff.lines:
print line.name,
print
Upvotes: 3