Upload collections.py with huggingface_hub
Browse files- collections.py +11 -11
collections.py
CHANGED
@@ -1,21 +1,24 @@
|
|
1 |
-
|
2 |
from dataclasses import field
|
|
|
3 |
|
4 |
from .artifact import Artifact
|
|
|
5 |
from .random_utils import random
|
6 |
|
7 |
|
8 |
class Collection(Artifact):
|
9 |
-
|
|
|
10 |
def __getitem__(self, key):
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
class ListCollection(Collection):
|
15 |
-
items:
|
16 |
-
|
17 |
-
def __getitem__(self, index):
|
18 |
-
return self.items[index]
|
19 |
|
20 |
def __len__(self):
|
21 |
return len(self.items)
|
@@ -31,10 +34,7 @@ class ListCollection(Collection):
|
|
31 |
|
32 |
|
33 |
class DictCollection(Collection):
|
34 |
-
items:
|
35 |
-
|
36 |
-
def __getitem__(self, key):
|
37 |
-
return self.items[key]
|
38 |
|
39 |
|
40 |
class ItemPicker(Artifact):
|
|
|
1 |
+
import typing
|
2 |
from dataclasses import field
|
3 |
+
from typing import Dict, List
|
4 |
|
5 |
from .artifact import Artifact
|
6 |
+
from .dataclass import AbstractField
|
7 |
from .random_utils import random
|
8 |
|
9 |
|
10 |
class Collection(Artifact):
|
11 |
+
items: typing.Collection = AbstractField()
|
12 |
+
|
13 |
def __getitem__(self, key):
|
14 |
+
try:
|
15 |
+
return self.items[key]
|
16 |
+
except LookupError:
|
17 |
+
raise LookupError(f"Cannot find item {repr(key)} in {repr(self)}")
|
18 |
|
19 |
|
20 |
class ListCollection(Collection):
|
21 |
+
items: List[Artifact] = field(default_factory=list)
|
|
|
|
|
|
|
22 |
|
23 |
def __len__(self):
|
24 |
return len(self.items)
|
|
|
34 |
|
35 |
|
36 |
class DictCollection(Collection):
|
37 |
+
items: Dict[str, Artifact] = field(default_factory=dict)
|
|
|
|
|
|
|
38 |
|
39 |
|
40 |
class ItemPicker(Artifact):
|