def get(self, key, default=None, *, to=None): """return the value of an element of the shadow dict, with conversion to the specified type, if key is present in the tcl array. if default is not given, it defaults to None, so this method never raises a KeyError.""" if to is None: to = self.to_type if exists(f"{self.tcl_array}({key})"): return getvar(f"{self.tcl_array}({key})", to=to) return convert(default, to=to)
def __init__(self, result, return_options): self.result = result for key in return_options: value = return_options[key] key = key[1:] # print(f"setting attribute '{key}' to '{value}'") if key in ("code", "errorline", "level"): value = int(value) elif key == "errorcode": value = convert(value, to=list) self.__setattr__(key, value)
def pop(self, key, *args, to=None): """if key is in the dictionary, remove it and return its value, else return default. if default is not given and key is not in the dictionary, a KeyError is raised.""" if len(args) > 1: raise TypeError(f"function takes one or two position arguments") if to is None: to = self.to_type var = f"{self.tcl_array}({key})" if exists(var): val = getvar(var, to=to) call("unset", var) return val if len(args) == 0: raise KeyError(key) if args[0] is None: return None return convert(args[0], to=to)