12
12
Pattern ,
13
13
Tuple ,
14
14
Type ,
15
+ TypeVar ,
15
16
Union ,
16
17
)
17
18
18
19
from numpy import bool_
19
20
20
- from cobra .core .object import Object
21
+ from .object import Object
22
+
23
+
24
+ Object_T = TypeVar ("Object_T" , bound = Object )
21
25
22
26
23
27
class DictList (list ):
@@ -50,12 +54,12 @@ def __init__(self, *args):
50
54
self .extend (other )
51
55
52
56
# noinspection PyShadowingBuiltins
53
- def has_id (self , id : Union [Object , str ]) -> bool :
57
+ def has_id (self , id : Union [Object_T , str ]) -> bool :
54
58
"""Check if id is in DictList."""
55
59
return id in self ._dict
56
60
57
61
# noinspection PyShadowingBuiltins
58
- def _check (self , id : Union [Object , str ]) -> None :
62
+ def _check (self , id : Union [Object_T , str ]) -> None :
59
63
"""Make sure duplicate id's are not added.
60
64
61
65
This function is called before adding in elements.
@@ -69,15 +73,15 @@ def _generate_index(self) -> None:
69
73
self ._dict = {v .id : k for k , v in enumerate (self )}
70
74
71
75
# noinspection PyShadowingBuiltins
72
- def get_by_id (self , id : Union [Object , str ]) -> Object :
76
+ def get_by_id (self , id : Union [Object_T , str ]) -> Object_T :
73
77
"""Return the element with a matching id."""
74
78
return list .__getitem__ (self , self ._dict [id ])
75
79
76
80
def list_attr (self , attribute : str ) -> list :
77
81
"""Return a list of the given attribute for every object."""
78
82
return [getattr (i , attribute ) for i in self ]
79
83
80
- def get_by_any (self , iterable : List [Union [str , Object , int ]]) -> list :
84
+ def get_by_any (self , iterable : List [Union [str , Object_T , int ]]) -> list :
81
85
"""Get a list of members using several different ways of indexing.
82
86
83
87
Parameters
@@ -168,29 +172,29 @@ def select_attribute(x: Optional[Any]) -> Any:
168
172
results ._extend_nocheck (matches )
169
173
return results
170
174
171
- def _replace_on_id (self , new_object : Object ) -> None :
175
+ def _replace_on_id (self , new_object : Object_T ) -> None :
172
176
"""Replace an object by another with the same id."""
173
177
the_id = new_object .id
174
178
the_index = self ._dict [the_id ]
175
179
list .__setitem__ (self , the_index , new_object )
176
180
177
181
# overriding default list functions with new ones
178
- def append (self , entity : Object ) -> None :
182
+ def append (self , entity : Object_T ) -> None :
179
183
"""Append object to end."""
180
184
the_id = entity .id
181
185
self ._check (the_id )
182
186
self ._dict [the_id ] = len (self )
183
187
list .append (self , entity )
184
188
185
- def union (self , iterable : Iterable [Object ]) -> None :
189
+ def union (self , iterable : Iterable [Object_T ]) -> None :
186
190
"""Add elements with id's not already in the model."""
187
191
_dict = self ._dict
188
192
append = self .append
189
193
for i in iterable :
190
194
if i .id not in _dict :
191
195
append (i )
192
196
193
- def extend (self , iterable : Iterable [Object ]) -> None :
197
+ def extend (self , iterable : Iterable [Object_T ]) -> None :
194
198
"""Extend list by appending elements from the iterable.
195
199
196
200
Sometimes during initialization from an older pickle, _dict
@@ -223,7 +227,7 @@ def extend(self, iterable: Iterable[Object]) -> None:
223
227
f"Is it present twice?"
224
228
)
225
229
226
- def _extend_nocheck (self , iterable : Iterable [Object ]) -> None :
230
+ def _extend_nocheck (self , iterable : Iterable [Object_T ]) -> None :
227
231
"""Extend without checking for uniqueness.
228
232
229
233
This function should only be used internally by DictList when it
@@ -245,7 +249,7 @@ def _extend_nocheck(self, iterable: Iterable[Object]) -> None:
245
249
for i , obj in enumerate (islice (self , current_length , None ), current_length ):
246
250
_dict [obj .id ] = i
247
251
248
- def __sub__ (self , other : Iterable [Object ]) -> "DictList" :
252
+ def __sub__ (self , other : Iterable [Object_T ]) -> "DictList" :
249
253
"""Remove a value or values, and returns the new DictList.
250
254
251
255
x.__sub__(y) <==> x - y
@@ -265,7 +269,7 @@ def __sub__(self, other: Iterable[Object]) -> "DictList":
265
269
total .remove (item )
266
270
return total
267
271
268
- def __isub__ (self , other : Iterable [Object ]) -> "DictList" :
272
+ def __isub__ (self , other : Iterable [Object_T ]) -> "DictList" :
269
273
"""Remove a value or values in place.
270
274
271
275
x.__sub__(y) <==> x -= y
@@ -279,7 +283,7 @@ def __isub__(self, other: Iterable[Object]) -> "DictList":
279
283
self .remove (item )
280
284
return self
281
285
282
- def __add__ (self , other : Iterable [Object ]) -> "DictList" :
286
+ def __add__ (self , other : Iterable [Object_T ]) -> "DictList" :
283
287
"""Add item while returning a new DictList.
284
288
285
289
x.__add__(y) <==> x + y
@@ -295,7 +299,7 @@ def __add__(self, other: Iterable[Object]) -> "DictList":
295
299
total .extend (other )
296
300
return total
297
301
298
- def __iadd__ (self , other : Iterable [Object ]) -> "DictList" :
302
+ def __iadd__ (self , other : Iterable [Object_T ]) -> "DictList" :
299
303
"""Add item while returning the same DictList.
300
304
301
305
x.__iadd__(y) <==> x += y
@@ -337,7 +341,7 @@ def __setstate__(self, state: dict) -> None:
337
341
self ._generate_index ()
338
342
339
343
# noinspection PyShadowingBuiltins
340
- def index (self , id : Union [str , Object ], * args ) -> int :
344
+ def index (self , id : Union [str , Object_T ], * args ) -> int :
341
345
"""Determine the position in the list.
342
346
343
347
Parameters
@@ -361,7 +365,7 @@ def index(self, id: Union[str, Object], *args) -> int:
361
365
except KeyError :
362
366
raise ValueError (f"{ str (id )} not found" )
363
367
364
- def __contains__ (self , entity : Union [str , Object ]) -> bool :
368
+ def __contains__ (self , entity : Union [str , Object_T ]) -> bool :
365
369
"""Ask if the DictList contain an entity.
366
370
367
371
DictList.__contains__(entity) <==> entity in DictList
@@ -385,7 +389,7 @@ def __copy__(self) -> "DictList":
385
389
the_copy ._dict = self ._dict .copy ()
386
390
return the_copy
387
391
388
- def insert (self , index : int , entity : Object ) -> None :
392
+ def insert (self , index : int , entity : Object_T ) -> None :
389
393
"""Insert entity before index."""
390
394
self ._check (entity .id )
391
395
list .insert (self , index , entity )
@@ -396,7 +400,7 @@ def insert(self, index: int, entity: Object) -> None:
396
400
_dict [i ] = j + 1
397
401
_dict [entity .id ] = index
398
402
399
- def pop (self , * args ) -> Object :
403
+ def pop (self , * args ) -> Object_T :
400
404
"""Remove and return item at index (default last)."""
401
405
value = list .pop (self , * args )
402
406
index = self ._dict .pop (value .id )
@@ -410,11 +414,11 @@ def pop(self, *args) -> Object:
410
414
_dict [i ] = j - 1
411
415
return value
412
416
413
- def add (self , x : Object ) -> None :
417
+ def add (self , x : Object_T ) -> None :
414
418
"""Opposite of `remove`. Mirrors set.add."""
415
419
self .extend ([x ])
416
420
417
- def remove (self , x : Union [str , Object ]) -> None :
421
+ def remove (self , x : Union [str , Object_T ]) -> None :
418
422
""".. warning :: Internal use only.
419
423
420
424
Each item is unique in the list which allows this
@@ -446,8 +450,8 @@ def key(i):
446
450
self ._generate_index ()
447
451
448
452
def __getitem__ (
449
- self , i : Union [int , slice , Iterable , Object , "DictList" ]
450
- ) -> Union ["DictList" , Object ]:
453
+ self , i : Union [int , slice , Iterable , Object_T , "DictList" ]
454
+ ) -> Union ["DictList" , Object_T ]:
451
455
"""Get item from DictList."""
452
456
if isinstance (i , int ):
453
457
return list .__getitem__ (self , i )
@@ -466,7 +470,7 @@ def __getitem__(
466
470
else :
467
471
return list .__getitem__ (self , i )
468
472
469
- def __setitem__ (self , i : Union [slice , int ], y : Union [list , Object ]) -> None :
473
+ def __setitem__ (self , i : Union [slice , int ], y : Union [list , Object_T ]) -> None :
470
474
"""Set an item via index or slice.
471
475
472
476
Parameters
@@ -512,7 +516,7 @@ def __getslice__(self, i: int, j: int) -> "DictList":
512
516
"""Get a slice from it to j of DictList."""
513
517
return self .__getitem__ (slice (i , j ))
514
518
515
- def __setslice__ (self , i : int , j : int , y : Union [list , Object ]) -> None :
519
+ def __setslice__ (self , i : int , j : int , y : Union [list , Object_T ]) -> None :
516
520
"""Set slice, where y is an iterable."""
517
521
self .__setitem__ (slice (i , j ), y )
518
522
0 commit comments