def __getitem__(self, symbol: Union['Symbol', 'Namespace', str]): try: if not symbol.ns: return self.__dict__[symbol_to_identifier(symbol.name)] else: return self.__dict__[ns_to_identifier( symbol.ns)][symbol_to_identifier(symbol.name)] except AttributeError: pass # if symbol lacks the ns attribute, it's not a Symbol # next, assume it's a Namespace try: name = ns_to_identifier(symbol.name) return self.__dict__[name] except AttributeError: pass # now only a str or descendant is applicable try: if "/" in symbol: ns, name = symbol.split("/") # noinspection PyProtectedMember return self.__dict__[ns_to_identifier(ns)].__dict__[ symbol_to_identifier(name)] else: return self.__dict__[symbol_to_identifier(symbol)] except (TypeError, AttributeError) as e: raise TypeError( "a namespace may only contain symbols (or string keys)") from e
def __contains__(self, symbol: Union['Symbol', 'Namespace', str]): try: if not symbol.ns: return symbol_to_identifier(symbol.name) in self.__dict__ try: return symbol_to_identifier( symbol.name) in self.__dict__[ns_to_identifier(symbol.ns)] except KeyError: return False except AttributeError: pass try: name = ns_to_identifier(symbol.name) return name in self.__dict__ except AttributeError: pass try: if "/" in symbol: ns, name = symbol.split("/") try: # noinspection PyProtectedMember return symbol_to_identifier(name) in self.__dict__[ ns_to_identifier(ns)] except KeyError: return False else: return symbol_to_identifier(symbol) in self.__dict__ except (TypeError, AttributeError) as e: raise TypeError( "a namespace may only contain symbols (or string keys)") from e
def __init__(self, name: str, doc: str = nil): self._initialized = False self._name = String(name, intern=True) super().__init__(name, doc) pyname = ns_to_identifier(self._name) self.__doc__ = doc if name in self._registry: raise DeclarationError("namespace '%s' is already defined" % name) self.load_parent_if_absent() if pyname not in sys.modules: sys.modules[pyname] = self
def __setitem__(self, symbol: Union['Symbol', 'Namespace', str], value): try: if symbol.ns: raise DeclarationError("cannot define ns-qualified var: '%s'" % symbol) pyname = symbol_to_identifier(symbol.name) if pyname in self.__dict__: raise DeclarationError( "symbol '%s' has already been declared in ns '%s'", symbol, self.name) self.__dict__[pyname] = value return except AttributeError: pass try: name = ns_to_identifier(symbol.name) self.__dict__[name] = value return except AttributeError: pass try: if "/" in symbol: raise DeclarationError("cannot define ns-qualified var: '%s'" % symbol) if isinstance(value, ModuleType): self.__dict__[ns_to_identifier(symbol)] = value else: pyname = symbol_to_identifier(symbol) if pyname in self.__dict__: raise DeclarationError( "symbol '%s' has already been declared in ns '%s'", symbol, self.name) self.__dict__[pyname] = value except (TypeError, AttributeError) as e: raise TypeError( "a namespace may only contain symbols (or string keys)") from e
def test_conversion(self): self.assertEqual("x", util.symbol_to_identifier("x")) self.assertEqual("ASTER", util.symbol_to_identifier("*")) self.assertEqual("BACKT", util.symbol_to_identifier("`")) self.assertEqual("COLON", util.symbol_to_identifier(":")) with self.assertRaises(ValueError): util.symbol_to_identifier("0") with self.assertRaises(ValueError): util.symbol_to_identifier("@") with self.assertRaises(ValueError): util.symbol_to_identifier("{") with self.assertRaises(ValueError): util.symbol_to_identifier(chr(0xbf)) self.assertEqual("x0123_QWEd", util.symbol_to_identifier("x0123_QWEd")) self.assertEqual("ASTERnsASTER", util.symbol_to_identifier("*ns*")) with self.assertRaises(ValueError): util.symbol_to_identifier("a{") with self.assertRaises(ValueError): util.symbol_to_identifier("{a") with self.assertRaises(ValueError): util.symbol_to_identifier(":x") with self.assertRaises(ValueError): util.symbol_to_identifier(chr(0xbf) + "qwre") with self.assertRaises(ValueError): util.symbol_to_identifier("qwre" + chr(0xbf)) self.assertEqual("_", util.ns_to_identifier("-")) self.assertEqual("hello.world", util.ns_to_identifier("hello.world")) self.assertEqual("x0", util.ns_to_identifier("x0")) with self.assertRaises(ValueError): util.ns_to_identifier(".") with self.assertRaises(ValueError): util.ns_to_identifier("0") with self.assertRaises(ValueError): util.ns_to_identifier("{") with self.assertRaises(ValueError): util.ns_to_identifier(chr(0xc0))