7
7
from ansys .dpf .core .scoping import Scoping , scoping_pb2
8
8
from ansys .dpf .core .field import Field , field_pb2
9
9
from ansys .dpf .core .time_freq_support import TimeFreqSupport
10
+ from ansys .dpf .core .errors import protect_grpc
11
+
10
12
11
13
class Collection :
12
14
"""A class used to represent a Collection which contains
@@ -18,19 +20,19 @@ class Collection:
18
20
Create a collection from a Collection message.
19
21
20
22
channel : channel, optional
21
- Channel connected to the remote or local instance. Defaults to the global channel.
23
+ Channel connected to the remote or local instance. When
24
+ ``None``, attempts to use the the global channel.
22
25
23
26
"""
24
27
25
- def __init__ (self ,dpf_type , collection = None , channel = None ):
26
-
28
+ def __init__ (self , dpf_type , collection = None , channel = None ):
27
29
if channel is None :
28
30
channel = dpf .core ._global_channel ()
29
31
30
32
self ._channel = channel
31
33
self ._stub = self ._connect ()
32
34
self ._type = dpf_type
33
-
35
+
34
36
if collection is None :
35
37
request = collection_pb2 .CollectionRequest ()
36
38
if hasattr (dpf_type , 'name' ):
@@ -39,6 +41,8 @@ def __init__(self,dpf_type, collection=None, channel=None):
39
41
stype = dpf_type
40
42
request .type = base_pb2 .Type .Value (stype .upper ())
41
43
self ._message = self ._stub .Create (request )
44
+ elif hasattr (collection , '_message' ):
45
+ self ._message = collection ._message
42
46
else :
43
47
self ._message = collection
44
48
@@ -52,14 +56,14 @@ def set_labels(self, labels):
52
56
['time','complex']
53
57
54
58
"""
55
- if len (self .__info__ () ['labels' ])!= 0 :
56
- print ("the collection has already labels :" ,self .__info__ () ['labels' ],'deleting existing abels is not implemented yet' )
59
+ if len (self ._info ['labels' ])!= 0 :
60
+ print ("the collection has already labels :" ,self ._info ['labels' ],'deleting existing abels is not implemented yet' )
57
61
return
58
62
request = collection_pb2 .UpdateLabelsRequest ()
59
63
request .collection .CopyFrom (self ._message )
60
64
request .labels .labels .extend (labels )
61
65
self ._stub .UpdateLabels (request )
62
-
66
+
63
67
def add_label (self , label ):
64
68
"""add the requested label to scope the collection
65
69
@@ -70,12 +74,11 @@ def add_label(self, label):
70
74
'time'
71
75
72
76
"""
73
-
74
77
request = collection_pb2 .UpdateLabelsRequest ()
75
78
request .collection .CopyFrom (self ._message )
76
79
request .labels .labels .extend ([label ])
77
80
self ._stub .UpdateLabels (request )
78
-
81
+
79
82
def _get_labels (self ):
80
83
"""get the labels scoping the collection
81
84
@@ -86,10 +89,10 @@ def _get_labels(self):
86
89
['time','complex']
87
90
88
91
"""
89
- return self .__info__ () ['labels' ]
90
-
92
+ return self ._info ['labels' ]
93
+
91
94
labels = property (_get_labels , set_labels , "labels" )
92
-
95
+
93
96
def _get_entries (self , label_space_or_index ):
94
97
"""Returns the entry at a requested index or label space
95
98
@@ -162,10 +165,11 @@ def get_label_space(self, index):
162
165
dictOut [key ]= out [key ]
163
166
164
167
return dictOut
165
-
168
+
166
169
def get_ids (self , label = "time" ):
167
-
168
- """
170
+ """Get the IDs corresponding to the input label.
171
+
172
+
169
173
Parameters
170
174
----------
171
175
label : str
@@ -176,13 +180,13 @@ def get_ids(self, label="time"):
176
180
ids : list of int
177
181
ids corresponding to the input label
178
182
"""
179
- ids = []
180
- for i in range (0 , self . __len__ ( )):
181
- current_scop = self .get_label_space (i )
182
- if label in current_scop and current_scop [label ] not in ids :
183
+ ids = []
184
+ for i in range (len ( self )):
185
+ current_scop = self .get_label_space (i )
186
+ if label in current_scop and current_scop [label ] not in ids :
183
187
ids .append (current_scop [label ])
184
188
return ids
185
-
189
+
186
190
def __getitem__ (self , key ):
187
191
"""Returns the entry at a requested index
188
192
@@ -222,7 +226,7 @@ def _add_entry(self, label_space, entry):
222
226
for key in label_space :
223
227
request .label_space .label_space [key ] = label_space [key ]
224
228
self ._stub .UpdateEntry (request )
225
-
229
+
226
230
def _get_time_freq_support (self ):
227
231
"""
228
232
Returns
@@ -235,19 +239,20 @@ def _get_time_freq_support(self):
235
239
message = self ._stub .GetSupport (request )
236
240
return TimeFreqSupport (time_freq_support = message )
237
241
238
-
239
242
def _connect (self ):
240
243
"""Connect to the grpc service"""
241
244
return collection_pb2_grpc .CollectionServiceStub (self ._channel )
242
245
243
- def __info__ (self ):
244
- list = self ._stub .List (self ._message )
245
- out = {"len" :list .count_entries , "labels" :list .labels .labels }
246
- return out
247
-
246
+ @property
247
+ @protect_grpc
248
+ def _info (self ):
249
+ """Length and labels of this container"""
250
+ list_stub = self ._stub .List (self ._message )
251
+ return {"len" : list_stub .count_entries , "labels" : list_stub .labels .labels }
252
+
248
253
def __len__ (self ):
249
254
"""Return number of entries"""
250
- return self .__info__ () ["len" ]
255
+ return self ._info ["len" ]
251
256
252
257
def __del__ (self ):
253
258
try :
@@ -258,7 +263,3 @@ def __del__(self):
258
263
def __iter__ (self ):
259
264
for i in range (len (self )):
260
265
yield self [i ]
261
-
262
-
263
- # -*- coding: utf-8 -*-
264
-
0 commit comments