-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcollections.py
28 lines (23 loc) · 935 Bytes
/
collections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
try:
ModuleNotFoundError
except NameError:
ModuleNotFoundError = ImportError
try:
from six import itervalues
except (ImportError, ModuleNotFoundError):
from ._six import itervalues
class UnifiVideoCollection(dict):
def __init__(self, collection_type, *args, **kwargs):
self._collection_type = collection_type
self.update(*args, **kwargs)
def __iter__(self, *args, **kwargs):
return itervalues(self, **kwargs)
def add(self, single_dict):
if not isinstance(single_dict, self._collection_type):
raise ValueError('Cannot add items other than of type {}'.format(
self._collection_type.__name__))
self[single_dict._id] = single_dict
def __contains__(self, item):
if isinstance(item, self._collection_type) and hasattr(item, '_id'):
item = item._id
return super(UnifiVideoCollection, self).__contains__(item)