Ejemplo n.º 1
0
 def __getitem__(self, key):
     """access element of the shadow dict using the [] notation"""
     try:
         self.default
         return getvar(f"{self.tcl_array}({key})",
                       to=self.to_type,
                       default=self.default)
     except AttributeError:
         return getvar(f"{self.tcl_array}({key})", to=self.to_type)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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)