@@ -89,7 +89,7 @@ def _new_from_string_as_bytes_on_client(self, client, str):
8989 else :
9090 return self ._api .any_new_from_string_on_client (client , str )
9191
92- def _type_to_new_from_get_as_method (self ):
92+ def _type_to_new_from_get_as_method (self , obj ):
9393 from ansys .dpf .core import (
9494 field ,
9595 property_field ,
@@ -100,74 +100,72 @@ def _type_to_new_from_get_as_method(self):
100100 custom_type_field ,
101101 collection ,
102102 )
103-
104- return [
105- (
106- int ,
103+ if issubclass (obj , int ):
104+ return (
107105 self ._api .any_new_from_int ,
108106 self ._api .any_get_as_int ,
109107 self ._api .any_new_from_int_on_client ,
110- ),
111- (
112- str ,
108+ )
109+ elif issubclass ( obj , str ):
110+ return (
113111 self ._new_from_string ,
114112 self ._get_as_string ,
115113 self ._new_from_string_on_client ,
116- ),
117- (
118- float ,
114+ )
115+ elif issubclass ( obj , float ):
116+ return (
119117 self ._api .any_new_from_double ,
120118 self ._api .any_get_as_double ,
121119 self ._api .any_new_from_double_on_client ,
122- ),
123- (
124- bytes ,
120+ )
121+ elif issubclass ( obj , bytes ):
122+ return (
125123 self ._new_from_string_as_bytes ,
126124 self ._get_as_string_as_bytes ,
127125 self ._new_from_string_as_bytes_on_client ,
128- ),
129- (field .Field , self ._api .any_new_from_field , self ._api .any_get_as_field ),
130- (
131- property_field .PropertyField ,
126+ )
127+ elif issubclass (obj , field .Field ):
128+ return self ._api .any_new_from_field , self ._api .any_get_as_field
129+ elif issubclass (obj , property_field .PropertyField ):
130+ return (
132131 self ._api .any_new_from_property_field ,
133132 self ._api .any_get_as_property_field ,
134- ),
135- (
136- string_field . StringField ,
133+ )
134+ elif issubclass ( obj , string_field . StringField ):
135+ return (
137136 self ._api .any_new_from_string_field ,
138137 self ._api .any_get_as_string_field ,
139- ),
140- (
141- generic_data_container . GenericDataContainer ,
138+ )
139+ elif issubclass ( obj , generic_data_container . GenericDataContainer ):
140+ return (
142141 self ._api .any_new_from_generic_data_container ,
143142 self ._api .any_get_as_generic_data_container ,
144- ),
145- (
146- scoping . Scoping ,
143+ )
144+ elif issubclass ( obj , scoping . Scoping ):
145+ return (
147146 self ._api .any_new_from_scoping ,
148147 self ._api .any_get_as_scoping ,
149- ),
150- (
151- data_tree . DataTree ,
148+ )
149+ elif issubclass ( obj , data_tree . DataTree ):
150+ return (
152151 self ._api .any_new_from_data_tree ,
153152 self ._api .any_get_as_data_tree ,
154- ),
155- (
156- custom_type_field . CustomTypeField ,
153+ )
154+ elif issubclass ( obj , custom_type_field . CustomTypeField ):
155+ return (
157156 self ._api .any_new_from_custom_type_field ,
158157 self ._api .any_get_as_custom_type_field ,
159- ),
160- (
161- collection . Collection ,
158+ )
159+ elif issubclass ( obj , collection . Collection ):
160+ return (
162161 self ._api .any_new_from_any_collection ,
163162 self ._api .any_get_as_any_collection ,
164- ),
165- (
166- dpf_vector . DPFVectorInt ,
163+ )
164+ elif issubclass ( obj , dpf_vector . DPFVectorInt ):
165+ return (
167166 self ._api .any_new_from_int_collection ,
168167 self ._api .any_get_as_int_collection ,
169- ),
170- ]
168+ )
171169
172170 @staticmethod
173171 def new_from (obj , server = None ):
@@ -190,30 +188,31 @@ def new_from(obj, server=None):
190188
191189 any_dpf = Any (server = inner_server )
192190
193- for type_tuple in any_dpf ._type_to_new_from_get_as_method ():
194- if isinstance (obj , type_tuple [0 ]):
195- # call respective new_from function
196- if isinstance (server , ansys .dpf .core .server_types .InProcessServer ) or not (
197- isinstance (obj , (int , str , float , bytes ))
198- ):
199- any_dpf ._internal_obj = type_tuple [1 ](obj )
200- else :
201- any_dpf ._internal_obj = type_tuple [3 ](inner_server .client , obj )
202- # store get_as & type for casting back to original type
203- any_dpf ._internal_type = type_tuple [0 ]
204- any_dpf ._get_as_method = type_tuple [2 ]
205-
191+ type_tuple = any_dpf ._type_to_new_from_get_as_method (type (obj ))
192+ if type_tuple is not None :
193+ # call respective new_from function
194+ if isinstance (server , ansys .dpf .core .server_types .InProcessServer ) or not (
195+ isinstance (obj , (int , str , float , bytes ))
196+ ):
197+ any_dpf ._internal_obj = type_tuple [0 ](obj )
198+ else :
199+ any_dpf ._internal_obj = type_tuple [2 ](inner_server .client , obj )
200+ # store get_as & type for casting back to original type
201+ any_dpf ._internal_type = type (obj )
202+ any_dpf ._get_as_method = type_tuple [1 ]
203+
204+ return any_dpf
205+ elif isinstance (obj , (list , np .ndarray )):
206+ type_tuple = any_dpf ._type_to_new_from_get_as_method (dpf_vector .DPFVectorInt )
207+ from ansys .dpf .core import collection
208+ if server_meet_version_and_raise ("9.0" , inner_server , "Creating an Any from a list is only supported "
209+ "with"
210+ "server versions starting at 9.0" ):
211+ inpt = collection .CollectionBase .integral_collection (obj , inner_server )
212+ any_dpf ._internal_obj = type_tuple [0 ](inpt )
213+ any_dpf ._internal_type = dpf_vector .DPFVectorInt
214+ any_dpf ._get_as_method = type_tuple [1 ]
206215 return any_dpf
207- if isinstance (obj , (list , np .ndarray )) and type_tuple [0 ]== dpf_vector .DPFVectorInt :
208- from ansys .dpf .core import collection
209- if server_meet_version_and_raise ("9.0" , inner_server , "Creating an Any from a list is only supported "
210- "with"
211- "server versions starting at 9.0" ):
212- inpt = collection .CollectionBase .integral_collection (obj , inner_server )
213- any_dpf ._internal_obj = type_tuple [1 ](inpt )
214- any_dpf ._internal_type = type_tuple [0 ]
215- any_dpf ._get_as_method = type_tuple [2 ]
216- return any_dpf
217216
218217 raise TypeError (f"{ obj .__class__ } is not currently supported by the Any class." )
219218
@@ -258,22 +257,21 @@ def cast(self, output_type=None):
258257
259258 self ._internal_type = output_type if output_type is not None else self ._internal_type
260259
261- for type_tuple in Any ._type_to_new_from_get_as_method (self ):
262- if issubclass (self ._internal_type , type_tuple [0 ]):
263- # call the get_as function for the appropriate type
264- internal_obj = type_tuple [2 ](self )
265- if (
266- self ._internal_type is int
267- or self ._internal_type is str
268- or self ._internal_type is float
269- or self ._internal_type is bytes
270-
271- ):
272- obj = internal_obj
273- else :
274- return create_dpf_instance (self ._internal_type , internal_obj , self ._server )
275-
276- return obj
260+ type_tuple = self ._type_to_new_from_get_as_method (self ._internal_type )
261+ if type_tuple is not None :
262+ internal_obj = type_tuple [1 ](self )
263+ if (
264+ self ._internal_type is int
265+ or self ._internal_type is str
266+ or self ._internal_type is float
267+ or self ._internal_type is bytes
268+
269+ ):
270+ obj = internal_obj
271+ else :
272+ return create_dpf_instance (self ._internal_type , internal_obj , self ._server )
273+
274+ return obj
277275
278276 raise TypeError (f"{ output_type } is not currently supported by the Any class." )
279277
0 commit comments