def __getitem__(self, key): if key == None: return Null if key == ".": output = _get(self, "_dict") if isinstance(output, Mapping): return self else: return output if isinstance(key, str): key = key.decode("utf8") elif not isinstance(key, unicode): get_logger().error("only string keys are supported") d = _get(self, "_dict") if key.find(".") >= 0: seq = _split_field(key) for n in seq: if isinstance(d, NullType): d = NullType(d, n) # OH DEAR, Null TREATS n AS PATH, NOT LITERAL elif isinstance(d, list): d = [_getdefault(dd, n) for dd in d] else: d = _getdefault(d, n) # EVERYTHING ELSE TREATS n AS LITERAL return wrap(d) else: o = d.get(key) if o == None: return NullType(d, key) return wrap(o)
def __init__(self, *args, **kwargs): """ CALLING Data(**something) WILL RESULT IN A COPY OF something, WHICH IS UNLIKELY TO BE USEFUL. USE wrap() INSTEAD """ if DEBUG: d = self._internal_dict for k, v in kwargs.items(): d[literal_field(k)] = unwrap(v) else: nargs = len(args) if nargs == 1: args0 = args[0] class_ = _get(args0, CLASS) if class_ is dict: _set(self, SLOT, args0) elif class_ is Data: _set(self, SLOT, _get(args0, SLOT)) else: _set(self, SLOT, dict(args0)) elif nargs > 1: get_logger().error("Expecting only one argument, not {{num}}", num=nargs) elif kwargs: _set(self, SLOT, unwrap(kwargs)) else: _set(self, SLOT, {})
def __setitem__(self, key, value): if key == "": get_logger().error("key is empty string. Probably a bad idea") if isinstance(key, str): key = key.decode("utf8") d=self try: value = unwrap(value) if key.find(".") == -1: if value is None: dict.pop(d, key, None) else: dict.__setitem__(d, key, value) return self seq = _split_field(key) for k in seq[:-1]: d = _getdefault(d, k) if value == None: dict.pop(d, seq[-1], None) else: dict.__setitem__(d, seq[-1], value) return self except Exception as e: raise e
def __getitem__(self, key): if key == None: return Null if key == ".": output = _get(self, "_dict") if isinstance(output, Mapping): return self else: return output if isinstance(key, str): key = key.decode("utf8") elif not isinstance(key, unicode): get_logger().error("only string keys are supported") d = _get(self, "_dict") if key.find(".") >= 0: seq = _split_field(key) for n in seq: if isinstance(d, NullType): d = NullType(d, n) # OH DEAR, Null TREATS n AS PATH, NOT LITERAL elif isinstance(d, list): d = [_getdefault(dd, n) for dd in d] else: d = _getdefault(d, n) # EVERYTHING ELSE TREATS n AS LITERAL return wrap(d) else: o = d.get(key) if o == None: return NullType(d, key) return wrap(o)
def __setitem__(self, key, value): if key == "": get_logger().error("key is empty string. Probably a bad idea") if isinstance(key, str): key = key.decode("utf8") d=self try: value = unwrap(value) if key.find(".") == -1: if value is None: dict.pop(d, key, None) else: dict.__setitem__(d, key, value) return self seq = _split_field(key) for k in seq[:-1]: d = _getdefault(d, k) if value == None: dict.pop(d, seq[-1], None) else: dict.__setitem__(d, seq[-1], value) return self except Exception as e: raise e
def wrapper(*args, **kwargs): try: func_name = get_function_name(func) if func_name in ("__init__", "__new__") and "kwargs" in kwargs: packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), kwargs["kwargs"], defaults) return func(args[0], **packed) elif func_name in ("__init__", "__new__") and len( args) == 2 and len(kwargs) == 0 and isinstance( args[1], Mapping): # ASSUME SECOND UNNAMED PARAM IS kwargs packed = params_pack(params, args[1], defaults) return func(args[0], **packed) elif func_name in ("__init__", "__new__"): # DO NOT INCLUDE self IN kwargs packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), defaults) return func(args[0], **packed) elif params[0] == "self" and "kwargs" in kwargs: packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), kwargs["kwargs"], defaults) return func(args[0], **packed) elif params[0] == "self" and len(args) == 2 and len( kwargs) == 0 and isinstance(args[1], Mapping): # ASSUME SECOND UNNAMED PARAM IS kwargs packed = params_pack(params, args[1], defaults) return func(args[0], **packed) elif params[0] == "self": packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), defaults) return func(args[0], **packed) elif len(args) == 1 and len(kwargs) == 0 and isinstance( args[0], Mapping): # ASSUME SINGLE PARAMETER IS A SETTING packed = params_pack(params, args[0], defaults) return func(**packed) elif "kwargs" in kwargs and isinstance(kwargs["kwargs"], Mapping): # PUT args INTO kwargs packed = params_pack(params, kwargs, dict_zip(params, args), kwargs["kwargs"], defaults) return func(**packed) else: # PULL kwargs OUT INTO PARAMS packed = params_pack(params, kwargs, dict_zip(params, args), defaults) return func(**packed) except TypeError as e: e = Except.wrap(e) if e.message.startswith(func_name) and "takes at least" in e: missing = [p for p in params if str(p) not in packed] get_logger().error( "Problem calling {{func_name}}: Expecting parameter {{missing}}, given {{given}}", func_name=func_name, missing=missing, given=packed.keys(), stack_depth=1) get_logger().error("Error dispatching call", e)
def __ror__(self, other): """ RECURSIVE COALESCE OF DATA PROPERTIES """ if not _get(other, CLASS) in data_types: get_logger().error("Expecting Data") return wrap(other).__or__(self)
def __ior__(self, other): if not _get(other, CLASS) in data_types: get_logger().error("Expecting a Mapping") d = self._internal_dict for ok, ov in other.items(): sv = d.get(ok) d[ok] = sv | ov return d
def wrapper(*args, **kwargs): try: if func.func_name in ("__init__", "__new__") and "kwargs" in kwargs: packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), kwargs["kwargs"], defaults) return func(args[0], **packed) elif func.func_name in ("__init__", "__new__") and len( args) == 2 and len(kwargs) == 0 and isinstance( args[1], Mapping): # ASSUME SECOND UNNAMED PARAM IS kwargs packed = params_pack(params, args[1], defaults) return func(args[0], **packed) elif func.func_name in ("__init__", "__new__"): # DO NOT INCLUDE self IN kwargs packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), defaults) return func(args[0], **packed) elif params[0] == "self" and "kwargs" in kwargs: packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), kwargs["kwargs"], defaults) return func(args[0], **packed) elif params[0] == "self" and len(args) == 2 and len( kwargs) == 0 and isinstance(args[1], Mapping): # ASSUME SECOND UNNAMED PARAM IS kwargs packed = params_pack(params, args[1], defaults) return func(args[0], **packed) elif params[0] == "self": packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), defaults) return func(args[0], **packed) elif len(args) == 1 and len(kwargs) == 0 and isinstance( args[0], Mapping): # ASSUME SINGLE PARAMETER IS A SETTING packed = params_pack(params, args[0], defaults) return func(**packed) elif "kwargs" in kwargs and isinstance(kwargs["kwargs"], Mapping): # PUT args INTO kwargs packed = params_pack(params, kwargs, dict_zip(params, args), kwargs["kwargs"], defaults) return func(**packed) else: # PULL kwargs OUT INTO PARAMS packed = params_pack(params, kwargs, dict_zip(params, args), defaults) return func(**packed) except TypeError, e: if e.message.find("takes at least") >= 0: missing = [p for p in params if str(p) not in packed] get_logger().error( "Problem calling {{func_name}}: Expecting parameter {{missing}}", func_name=func.func_name, missing=missing, stack_depth=1) get_logger().error("Unexpected", e)
def __or__(self, other): """ RECURSIVE COALESCE OF DATA PROPERTIES """ if not _get(other, CLASS) in data_types: get_logger().error("Expecting Data") d = self._internal_dict output = Data(**d) output.__ior__(other) return output
def __or__(self, other): if not _get(other, CLASS) in data_types: get_logger().error("Expecting a Mapping") output = object.__new__(Data) output._internal_dict = {} d = self._internal_dict for ok, ov in other.items(): sv = d.get(ok) output[ok] = sv | ov return output
def raise_error(e, packed): err = text_type(e) e = Except.wrap(e) if err.startswith(func_name) and ("takes at least" in err or "required positional argument" in err): missing = [p for p in params if str(p) not in packed] given = [p for p in params if str(p) in packed] get_logger().error( "Problem calling {{func_name}}: Expecting parameter {{missing}}, given {{given}}", func_name=func_name, missing=missing, given=given, stack_depth=2 ) get_logger().error("Error dispatching call", e)
def _parse_traceback(tb): if is_many(tb): get_logger().error("Expecting a tracback object, not a list") trace = [] while tb is not None: f = tb.tb_frame trace.append({ "file": f.f_code.co_filename, "line": tb.tb_lineno, "method": f.f_code.co_name, }) tb = tb.tb_next trace.reverse() return trace
def raise_error(e, packed): err = text_type(e) e = Except.wrap(e) if err.startswith(func_name) and ("takes at least" in err or "required positional argument" in err): missing = [p for p in params if str(p) not in packed] given = [p for p in params if str(p) in packed] get_logger().error( "Problem calling {{func_name}}: Expecting parameter {{missing}}, given {{given}}", func_name=func_name, missing=missing, given=given, stack_depth=2) get_logger().error("Error dispatching call", e)
def wrapper(*args, **kwargs): try: if func.func_name in ("__init__", "__new__") and "kwargs" in kwargs: packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), kwargs["kwargs"], defaults) return func(args[0], **packed) elif func.func_name in ("__init__", "__new__") and len(args) == 2 and len(kwargs) == 0 and isinstance(args[1], Mapping): # ASSUME SECOND UNNAMED PARAM IS kwargs packed = params_pack(params, args[1], defaults) return func(args[0], **packed) elif func.func_name in ("__init__", "__new__"): # DO NOT INCLUDE self IN kwargs packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), defaults) return func(args[0], **packed) elif params[0] == "self" and "kwargs" in kwargs: packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), kwargs["kwargs"], defaults) return func(args[0], **packed) elif params[0] == "self" and len(args) == 2 and len(kwargs) == 0 and isinstance(args[1], Mapping): # ASSUME SECOND UNNAMED PARAM IS kwargs packed = params_pack(params, args[1], defaults) return func(args[0], **packed) elif params[0] == "self": packed = params_pack(params, kwargs, dict_zip(params[1:], args[1:]), defaults) return func(args[0], **packed) elif len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], Mapping): # ASSUME SINGLE PARAMETER IS A SETTING packed = params_pack(params, args[0], defaults) return func(**packed) elif "kwargs" in kwargs and isinstance(kwargs["kwargs"], Mapping): # PUT args INTO kwargs packed = params_pack(params, kwargs, dict_zip(params, args), kwargs["kwargs"], defaults) return func(**packed) else: # PULL kwargs OUT INTO PARAMS packed = params_pack(params, kwargs, dict_zip(params, args), defaults) return func(**packed) except TypeError, e: if e.message.find("takes at least") >= 0: missing = [p for p in params if str(p) not in packed] get_logger().error( "Problem calling {{func_name}}: Expecting parameter {{missing}}", func_name=func.func_name, missing=missing, stack_depth=1 ) get_logger().error("Unexpected", e)
def raise_error(e, packed): err = text(e) e = Except.wrap(e) if err.startswith(func_name) and ( "takes at least" in err or "takes exactly " in err or "required positional argument" in err): missing = [p for p in params if str(p) not in packed] given = [p for p in params if str(p) in packed] if not missing: raise e else: get_logger().error( "Problem calling {{func_name}}: Expecting parameter {{missing}}, given {{given}}", func_name=func_name, missing=missing, given=given, stack_depth=2, cause=e) raise e
def leaves(value, prefix=None): """ LIKE items() BUT RECURSIVE, AND ONLY FOR THE LEAVES (non dict) VALUES SEE wrap_leaves FOR THE INVERSE :param value: THE Mapping TO TRAVERSE :param prefix: OPTIONAL PREFIX GIVEN TO EACH KEY :return: Data, WHICH EACH KEY BEING A PATH INTO value TREE """ prefix = coalesce(prefix, "") output = [] for k, v in value.items(): try: if isinstance(v, Mapping): output.extend(leaves(v, prefix=prefix + literal_field(k) + ".")) else: output.append((prefix + literal_field(k), unwrap(v))) except Exception as e: get_logger().error("Do not know how to handle", cause=e) return output
def leaves(value, prefix=None): """ LIKE items() BUT RECURSIVE, AND ONLY FOR THE LEAVES (non dict) VALUES SEE wrap_leaves FOR THE INVERSE :param value: THE Mapping TO TRAVERSE :param prefix: OPTIONAL PREFIX GIVEN TO EACH KEY :return: Data, WHICH EACH KEY BEING A PATH INTO value TREE """ prefix = coalesce(prefix, "") output = [] for k, v in value.items(): try: if isinstance(v, Mapping): output.extend(leaves(v, prefix=prefix + literal_field(k) + ".")) else: output.append((prefix + literal_field(k), unwrap(v))) except Exception as e: get_logger().error("Do not know how to handle", cause=e) return output
def __ior__(self, other): """ RECURSIVE COALESCE OF DATA PROPERTIES """ if not _get(other, CLASS) in data_types: get_logger().error("Expecting Data") d = self._internal_dict for ok, ov in other.items(): if ov == None: continue sv = d.get(ok) if sv == None: d[ok] = ov elif isinstance(sv, Data): sv |= ov elif is_data(sv): wv = object.__new__(Data) _set(wv, SLOT, sv) wv |= ov return self
def raise_error(e, a, k): packed = k.copy() packed.update(dict(zip(params, a))) err = text(e) if err.startswith(func_name) and ( "takes at least" in err or "takes exactly " in err or "required positional argument" in err): missing = [p for p in params if str(p) not in packed] given = [p for p in params if str(p) in packed] if not missing: raise e else: get_logger().error( "Problem calling {{func_name}}: Expecting parameter {{missing}}, given {{given}}", func_name=func_name, missing=missing, given=given, stack_depth=2, cause=e, ) raise e
def _iadd(self, other): """ RECURSIVE ADDITION OF DATA PROPERTIES * LISTS ARE CONCATENATED * SETS ARE UNIONED * NUMBERS ARE ADDED """ if not _get(other, CLASS) in data_types: get_logger().error("Expecting Data") d = unwrap(self) for ok, ov in other.items(): sv = d.get(ok) if sv == None: d[ok] = deepcopy(ov) elif isinstance(ov, (Decimal, float, long, int)): if _get(sv, CLASS) in data_types: get_logger().error( "can not add {{stype}} with {{otype}", stype=_get(sv, CLASS).__name__, otype=_get(ov, CLASS).__name__ ) elif is_list(sv): d[ok].append(ov) else: d[ok] = sv + ov elif is_list(ov): d[ok] = listwrap(sv) + ov elif _get(ov, CLASS) in data_types: if _get(sv, CLASS) in data_types: _iadd(sv, ov) elif is_list(sv): d[ok].append(ov) else: get_logger().error( "can not add {{stype}} with {{otype}", stype=_get(sv, CLASS).__name__, otype=_get(ov, CLASS).__name__ ) else: if _get(sv, CLASS) in data_types: get_logger().error( "can not add {{stype}} with {{otype}", stype=_get(sv, CLASS).__name__, otype=_get(ov, CLASS).__name__ ) else: d[ok].append(ov) return self
def __setitem__(self, key, value): if key == "": get_logger().error("key is empty string. Probably a bad idea") if key == None: return Null if key == ".": # SOMETHING TERRIBLE HAPPENS WHEN value IS NOT A Mapping; # HOPEFULLY THE ONLY OTHER METHOD RUN ON self IS unwrap() v = unwrap(value) _set(self, SLOT, v) return v try: d = self._internal_dict value = unwrap(value) if key.find(".") == -1: if value is None: d.pop(key, None) else: d[key] = value return self seq = _split_field(key) for k in seq[:-1]: d = _getdefault(d, k) if value == None: try: d.pop(seq[-1], None) except Exception as _: pass elif d == None: d[literal_field(seq[-1])] = value else: d[seq[-1]] = value return self except Exception as e: from mo_logs import Log Log.error("can not set key={{key}}", key=key, cause=e)
def __setitem__(self, key, value): if key == "": get_logger().error("key is empty string. Probably a bad idea") if key == None: return Null if key == ".": # SOMETHING TERRIBLE HAPPENS WHEN value IS NOT A Mapping; # HOPEFULLY THE ONLY OTHER METHOD RUN ON self IS unwrap() v = unwrap(value) _set(self, SLOT, v) return v try: d = self._internal_dict value = unwrap(value) if key.find(".") == -1: if value is None: d.pop(key, None) else: d[key] = value return self seq = _split_field(key) for k in seq[:-1]: d = _getdefault(d, k) if value == None: try: d.pop(seq[-1], None) except Exception as _: pass elif d==None: d[literal_field(seq[-1])] = value else: d[seq[-1]] = value return self except Exception as e: raise e
def __setitem__(self, key, value): if key == "": get_logger().error("key is empty string. Probably a bad idea") if key == None: return Null if key == ".": # SOMETHING TERRIBLE HAPPENS WHEN value IS NOT A Mapping; # HOPEFULLY THE ONLY OTHER METHOD RUN ON self IS unwrap() v = unwrap(value) _set(self, "_dict", v) return v if isinstance(key, str): key = key.decode("utf8") try: d = _get(self, "_dict") value = unwrap(value) if key.find(".") == -1: if value is None: d.pop(key, None) else: d[key] = value return self seq = _split_field(key) for k in seq[:-1]: d = _getdefault(d, k) if value == None: d.pop(seq[-1], None) elif d==None: d[literal_field(seq[-1])] = value else: d[seq[-1]] = value return self except Exception as e: raise e
def _iadd(self, other): if not isinstance(other, Mapping): get_logger().error("Expecting a Mapping") d = unwrap(self) for ok, ov in other.items(): sv = d.get(ok) if sv == None: d[ok] = deepcopy(ov) elif isinstance(ov, (Decimal, float, long, int)): if isinstance(sv, Mapping): get_logger().error( "can not add {{stype}} with {{otype}", stype=sv.__class__.__name__, otype=ov.__class__.__name__ ) elif isinstance(sv, list): d[ok].append(ov) else: d[ok] = sv + ov elif isinstance(ov, list): d[ok] = listwrap(sv) + ov elif isinstance(ov, Mapping): if isinstance(sv, Mapping): _iadd(sv, ov) elif isinstance(sv, list): d[ok].append(ov) else: get_logger().error( "can not add {{stype}} with {{otype}", stype=sv.__class__.__name__, otype=ov.__class__.__name__ ) else: if isinstance(sv, Mapping): get_logger().error( "can not add {{stype}} with {{otype}", stype=sv.__class__.__name__, otype=ov.__class__.__name__ ) else: d[ok].append(ov) return self
def _iadd(self, other): if not _get(other, CLASS) in data_types: get_logger().error("Expecting a Mapping") d = unwrap(self) for ok, ov in other.items(): sv = d.get(ok) if sv == None: d[ok] = deepcopy(ov) elif isinstance(ov, (Decimal, float, long, int)): if _get(sv, CLASS) in data_types: get_logger().error( "can not add {{stype}} with {{otype}", stype=_get(sv, CLASS).__name__, otype=_get(ov, CLASS).__name__ ) elif is_list(sv): d[ok].append(ov) else: d[ok] = sv + ov elif is_list(ov): d[ok] = listwrap(sv) + ov elif _get(ov, CLASS) in data_types: if _get(sv, CLASS) in data_types: _iadd(sv, ov) elif is_list(sv): d[ok].append(ov) else: get_logger().error( "can not add {{stype}} with {{otype}", stype=_get(sv, CLASS).__name__, otype=_get(ov, CLASS).__name__ ) else: if _get(sv, CLASS) in data_types: get_logger().error( "can not add {{stype}} with {{otype}", stype=_get(sv, CLASS).__name__, otype=_get(ov, CLASS).__name__ ) else: d[ok].append(ov) return self
def clear(self): get_logger().error("clear() not supported")
def __ror__(self, other): if not _get(other, CLASS) in data_types: get_logger().error("Expecting a Mapping") return wrap(other).__or__(self)
def clear(self): get_logger().error("clear() not supported")