def test_sort(self): forwards = indexed.IndexedOrderedDict([("a", 1), ("b", -1)]) backwards = indexed.IndexedOrderedDict([("b", -1), ("a", 1)]) self.assertNotEqual(forwards, backwards) backwards.sort() self.assertEqual(forwards, backwards) backwards.sort(reverse=True) self.assertNotEqual(forwards, backwards) forwards.sort(key=lambda k: forwards[k]) self.assertEqual(forwards, backwards)
def test_repr(self): d = indexed.IndexedOrderedDict() d["key"] = "value" d["recursive"] = d self.assertEqual( d.__repr__(), "IndexedOrderedDict([('key', 'value'), ('recursive', ...)])")
class CopterData: class_basic_attrs = indexed.IndexedOrderedDict([('copter_id', None), ('git_ver', None), ('anim_id', None), ('battery', None), ('sys_status', None), ('cal_status', None), ('mode', None), ('selfcheck', None), ('position', None), ('start_pos', None), ('time_delta', None), ('client', None)]) def __init__(self, **kwargs): self.attrs_dict = self.class_basic_attrs.copy() self.attrs_dict.update(kwargs) for attr, value in self.attrs_dict.items(): setattr(self, attr, value) def __getitem__(self, key): return getattr(self, self.attrs_dict.keys()[key]) def __setitem__(self, key, value): setattr(self, self.attrs_dict.keys()[key], value)
def test_set_default(self): d = indexed.IndexedOrderedDict() d["a"] = "set" self.assertEqual(d.setdefault("a", "not-set"), "set") self.assertEqual(d.setdefault("b", "not-set"), "not-set") self.assertEqual(d.setdefault("b", "still-not-set"), "not-set")
def setUp(self): self.d = indexed.IndexedOrderedDict() self.d["key-zero"] = "zero" self.d["key-one"] = "one" self.d["key-two"] = "two" self.d["key-three"] = "three" self.d["key-four"] = "four" self.d["key-five"] = "five"
def test_pop(self): d = indexed.IndexedOrderedDict() d["foo"] = "bar" self.assertTrue("foo" in d) self.assertEqual(d.pop("foo"), "bar") self.assertFalse("foo" in d) self.assertEqual(d.pop("hello", "default"), "default")
def test_reversed(self): d = indexed.IndexedOrderedDict() d["a"] = "b" d["b"] = "a" it = d.__reversed__() self.assertEqual(next(it), "b") self.assertEqual(next(it), "a")
def test_pickle(self): d = indexed.IndexedOrderedDict() d["foo"] = "bar" d["bar"] = "baz" pickled = pickle.dumps(d) unpickled = pickle.loads(pickled) self.assertEqual(d, unpickled)
def test_clear(self): d = indexed.IndexedOrderedDict() d["foo"] = "bar" self.assertEqual(len(d), 1) self.assertEqual(len(d.values()), 1) d.clear() self.assertEqual(len(d), 0) self.assertEqual(len(d.values()), 0)
def test_popitem(self): d = indexed.IndexedOrderedDict() d["first-key"] = "first" d["middle-key"] = "middle" d["last-key"] = "last" self.assertEqual(d.popitem(), ("last-key", "last")) self.assertEqual(d.popitem(False), ("first-key", "first")) self.assertEqual(len(d), 1) self.assertEqual(d["middle-key"], "middle")
def test_iter(self): d = indexed.IndexedOrderedDict() d[8] = "8" d[5] = "5" d[9] = "9" it = d.__iter__() self.assertEqual(next(it), 8) self.assertEqual(next(it), 5) self.assertEqual(next(it), 9)
def test_del_item(self): d = indexed.IndexedOrderedDict() keys = d.keys() d["key-a"] = "a" d["key-b"] = "b" d["key-c"] = "c" del d["key-a"] self.assertFalse("key-a" in d) self.assertEqual(keys.index("key-b"), 0) self.assertEqual(keys.index("key-c"), 1)
def test_move_to_end(self): d = indexed.IndexedOrderedDict() d["first-key"] = "first" d["middle-key"] = "middle" d["last-key"] = "last" d.move_to_end("middle-key") self.assertEqual(d.keys()[2], "middle-key") self.assertEqual(d.values()[2], "middle") self.assertEqual(d.keys()[1], "last-key") d.move_to_end("last-key", False) self.assertEqual(d.keys()[0], "last-key") self.assertEqual(d.values()[0], "last") self.assertEqual(len(d), 3)
def test_equality(self): a = indexed.IndexedOrderedDict() a["foo"] = "bar" a["baz"] = "zab" b = a.copy() self.assertTrue(a == b) self.assertFalse(a != b) b["zip"] = "zap" self.assertFalse(a == b) self.assertTrue(a != b) std_dict = {"foo": "bar", "baz": "zab"} self.assertEqual(std_dict, a) reordered_a = a.copy() reordered_a.move_to_end("foo") self.assertNotEqual(a, reordered_a)
class StatedCopterData(CopterData): class_basic_states = indexed.IndexedOrderedDict([ ("checked", 0), ("selfchecked", None), ("takeoff_ready", None), ("copter_id", True), ]) def __init__(self, checks_class=ModelChecks, **kwargs): self.states = CopterData(**self.class_basic_states) self.checks = ModelChecks super(StatedCopterData, self).__init__(**kwargs) def __setattr__(self, key, value): self.__dict__[key] = value if key in self.class_basic_attrs.keys(): try: self.states.__dict__[key] = \ ModelChecks.checks_dict[self.attrs_dict.keys().index(key)](value) if key == 'start_pos': if (self.__dict__['position'] is not None) and (self.__dict__['start_pos'] is not None): current_pos = get_position(self.__dict__['position']) start_pos = get_position(self.__dict__['start_pos']) delta = get_position_delta(current_pos, start_pos) if delta != 'NO_POS': self.states.__dict__[key] = (delta < start_pos_delta_max) except KeyError: # No check present for that col pass else: # update selfchecked and takeoff_ready self.states.__dict__["selfchecked"] = all( [self.states[i] for i in ModelChecks.checks_dict.keys()]) self.states.__dict__["takeoff_ready"] = all( [self.states[i] for i in ModelChecks.takeoff_checklist])
def __init__(self, annotation_file=None, root_path=None, split_file=None, load_anno=True): """ GluonCVMotionDataset :param annotation_file: The path to the annotation file, either a full path or a path relative to the root annotation path (root_path/annotation/), defaults to 'anno.json' :param root_path: The root path of the dataset, containing the 'annotation', 'cache', and 'raw_data' folders. If left empty it will be inferred from the annotation_file path by searching up until the 'annotation' folder is found, then going one more level up :param split_file: The path to the split file relative to the annotation file. It will be relative to the root annotation path instead if it starts with './' :param load_anno: Whether to load the annotation file, will cause an exception if it is true and file does not exist. Set this to false if you are just trying to write a new annotation file for example in an ingestion script """ # a dict of DataSample instances import indexed self._samples = indexed.IndexedOrderedDict() self._splits = {} self._metadata = {} if annotation_file is None: annotation_file = self._DEFAULT_ANNO_FILE log.info("Annotation file not provided, defaulting to '{}'".format(annotation_file)) self._root_path = self._get_root_path(root_path, annotation_file) if self._root_path: if not os.path.isdir(self._root_path): raise ValueError("Expected root folder but was not found at: {}".format(self._root_path)) self._anno_path = os.path.join(self._root_path, self.ANNO_DIR, annotation_file) self._data_path = self.get_data_path_from_root(self._root_path) self._cache_path = self.get_cache_path_from_root(self._root_path) if not os.path.isdir(self._data_path): raise ValueError("Expected data folder but was not found at: {}".format(self._data_path)) else: log.warning('Root path was not set for dataset, this should only happen when loading a lone annotation' ' file for inspection') self._anno_path = annotation_file self._data_path = None self._cache_path = None if load_anno: if os.path.exists(self._anno_path): log.info('Loading annotation file {}...'.format(self._anno_path)) # load annotation file if self._get_pickle_path().exists(): log.info('Found pickle file, loading this instead') loaded_pickle = self._load_pickle() if not loaded_pickle: self._parse_anno(self._anno_path) self._split_path = self._get_split_path(split_file, self._anno_path) self._load_split() else: raise ValueError( "load_anno is true but the anno path does not exist at: {}".format(self._anno_path)) else: log.info('Skipping loading for annotation file {}'.format(self._anno_path)) self._split_path = self._get_split_path(split_file, self._anno_path)