Elron commited on
Commit
7c32c70
1 Parent(s): 401a1f1

Upload collections.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. collections.py +14 -2
collections.py CHANGED
@@ -1,8 +1,8 @@
1
- import random
2
  from abc import abstractmethod
3
  from dataclasses import field
4
 
5
  from .artifact import Artifact
 
6
 
7
 
8
  class Collection(Artifact):
@@ -20,6 +20,15 @@ class ListCollection(Collection):
20
  def __len__(self):
21
  return len(self.items)
22
 
 
 
 
 
 
 
 
 
 
23
 
24
  class DictCollection(Collection):
25
  items: dict = field(default_factory=dict)
@@ -32,7 +41,10 @@ class ItemPicker(Artifact):
32
  item: object = None
33
 
34
  def __call__(self, collection: Collection):
35
- return collection[self.item]
 
 
 
36
 
37
 
38
  class RandomPicker(Artifact):
 
 
1
  from abc import abstractmethod
2
  from dataclasses import field
3
 
4
  from .artifact import Artifact
5
+ from .random_utils import random
6
 
7
 
8
  class Collection(Artifact):
 
20
  def __len__(self):
21
  return len(self.items)
22
 
23
+ def append(self, item):
24
+ self.items.append(item)
25
+
26
+ def extend(self, other):
27
+ self.items.extend(other.items)
28
+
29
+ def __add__(self, other):
30
+ return ListCollection(self.items.__add__(other.items))
31
+
32
 
33
  class DictCollection(Collection):
34
  items: dict = field(default_factory=dict)
 
41
  item: object = None
42
 
43
  def __call__(self, collection: Collection):
44
+ try:
45
+ return collection[int(self.item)]
46
+ except (SyntaxError, KeyError, ValueError) as e: # in case picking from a dictionary
47
+ return collection[self.item]
48
 
49
 
50
  class RandomPicker(Artifact):