def update(self, dictionary): """Extend dict.update() to handle *named tags*.""" for key, value in dictionary.items(): if is_stringlike(key): setattr(self, key, value) else: self[Tag(key)] = value
def __contains__(self, name): """Extend dict.__contains__() to handle *named tags*. This is called for code like: ``if 'SliceLocation' in dataset``. """ if is_stringlike(name): tag = TagForName(name) else: try: tag = Tag(name) except: return False if tag: return dict.__contains__(self, tag) else: return dict.__contains__(self, name) # will no doubt raise an exception
def get(self, key, default=None): """Extend dict.get() to handle *named tags*.""" if is_stringlike(key): try: return getattr(self, key) except AttributeError: return default else: # is not a string, try to make it into a tag and then hand it # off to the underlying dict if not isinstance(key, BaseTag): try: key = Tag(key) except: raise TypeError("Dataset.get key must be a string or tag") try: return_val = self.__getitem__(key) except KeyError: return_val = default return return_val