Skip to content

Instantly share code, notes, and snippets.

@C10udburst
Last active January 22, 2024 23:54
Show Gist options
  • Save C10udburst/f171e0bc334ce64b2ccd992cebe02f26 to your computer and use it in GitHub Desktop.
Save C10udburst/f171e0bc334ce64b2ccd992cebe02f26 to your computer and use it in GitHub Desktop.
Better namespace class that behaves similarly to a JS object. A cross between dict() and types.SimpleNamespace().
"""
A helper class that behaves similarly to a JS object. A cross between dict() and types.SimpleNamespace(). Can be cast into a dict.
Examples:
>>> data = ns(test=True, abc={"d":1})
>>> data.test
True
>>> data['test']
True
>>> data.abc.d
1
>>> data.non_existent.asdf = "test"
"test"
>>> data
{"test":True, "abc":{"d":1}, "non_existent": {"asdf":"test"}}
"""
import inspect
class ns(object):
def __init__(self, **kwargs):
self.push(**kwargs)
def __getitem__(self, k):
self.__getattr__(k)
return self.__getattribute__(str(k))
def __getattr__(self, k):
# ignore existing, dunder and "private" members, otherwise create new ns()
if k not in self.__dict__ and not k.startswith("_"):
self[k] = ns()
return self[k]
def getdoc(self):
return inspect.getdoc(self)
def __setitem__(self, k, v):
return self.__setattr__(str(k), v)
def __delitem__(self, k):
return self.__delattr__(str(k))
def keys(self):
return self.__dict__.keys()
def values(self):
return self.__dict__.values()
def items(self):
return self.__dict__.items()
def __contains__(self, k):
return self.__dict__.__contains__(k)
def __len__(self):
return self.__dict__.__len__()
def __repr__(self):
return self.__dict__.__repr__()
def __iadd__(self, other):
if isinstance(other, ns):
other = dict(other)
if not isinstance(other, dict):
raise TypeError(f"unsupported operand type(s) for +=: '{type(self)}' and '{type(other)}'")
self.push(**other)
return self
def __add__(self, other):
if isinstance(other, ns):
other = dict(other)
if not isinstance(other, dict):
raise TypeError(f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'")
out = ns()
out.push(**dict(self))
out.push(**other)
return out
def push(self, **kwargs):
for k, v in kwargs.items():
# if new item is a dict, convert it to ns()
if isinstance(v, dict):
self[k] = ns(**v)
else:
self[k] = v
from typing import Any
class OpStr(str):
def __or__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self} | {__value}")
def __and__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self} & {__value}")
def __xor__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self} ^ {__value}")
def __invert__(self) -> 'OpStr':
return OpStr(f"~{self}")
def __ror__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value} | {self}")
def __rand__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value} & {self}")
def __rxor__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value} ^ {self}")
def __rshift__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self} >> {__value}")
def __lshift__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self} << {__value}")
def __rrshift__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value} >> {self}")
def __rlshift__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value} << {self}")
def __add__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self}{__value}")
def __radd__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value}{self}")
def __sub__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self} - {__value}")
def __rsub__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value} - {self}")
def __mul__(self, __value: Any) -> 'OpStr':
if isinstance(__value, int):
return OpStr(str(self) * __value)
return OpStr(f"{self} * {__value}")
def __rmul__(self, __value: Any) -> 'OpStr':
if isinstance(__value, int):
return OpStr(str(self) * __value)
return OpStr(f"{__value} * {self}")
def __truediv__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self} / {__value}")
def __rtruediv__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value} / {self}")
def __floordiv__(self, __value: Any) -> 'OpStr':
return OpStr(f"{self} // {__value}")
def __rfloordiv__(self, __value: Any) -> 'OpStr':
return OpStr(f"{__value} // {self}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment