def name(self, name): '''Set the name, unless already registered. @arg name: New name (C{str}). ''' if self._enum: raise _NameError(str(name), self, txt='registered') # XXX _TypeError self._name = str(name)
def __new__(cls, *args): '''New L{_NamedTuple} initialized with B{C{positional}} arguments. ''' self = tuple.__new__(cls, args) ns = self._Names_ if not (isinstance(ns, tuple) and len(ns) > 1): # XXX > 0 raise _TypeError(_dot_(self.classname, _Names_), ns) if len(ns) != len(args) or not ns: raise LenError(cls, args=len(args), ns=len(ns)) if _name_ in ns: t = unstr(_dot_(self.classname, _Names_), *ns) raise _NameError(_name_, _name_, txt=t) return self
def register(self, item): '''Registed a new item. @arg item: The item (any C{type}). @return: The item name (C{str}). @raise NameError: An B{C{item}} already registered with that name or the B{C{item}} has no, an empty or an invalid name. @raise TypeError: The B{C{item}} type invalid. ''' try: n = item.name if not (n and n.replace(_UNDERSCORE_, NN).isalnum() and isstr(n)): raise ValueError except (AttributeError, ValueError, TypeError) as x: raise _NameError(_dot_('item', _name_), item, txt=str(x)) if n in self: raise _NameError(self._dot_(n), item, txt='exists') if not (self._item_Classes and isinstance(item, self._item_Classes)): raise _TypesError(self._dot_(n), item, *self._item_Classes) self[n] = item
def register(self, item): '''Registed a new item. @arg item: The item (any C{type}). @return: The item name (C{str}). @raise NameError: An B{C{item}} already registered with that name or the B{C{item}} has no, an empty or an invalid name. @raise TypeError: The B{C{item}} type invalid. ''' try: n = item.name if not (n and isstr(n) and isidentifier(n)): raise ValueError except (AttributeError, ValueError, TypeError) as x: raise _NameError(_DOT_(_item_, _name_), item, txt=str(x)) if n in self: raise _NameError(self._DOT_(n), item, txt=_exists_) if not (self._item_Classes and isinstance(item, self._item_Classes)): raise _TypesError(self._DOT_(n), item, *self._item_Classes) self[n] = item
def unregister(self, name_or_item): '''Remove a registered item. @arg name_or_item: Name (C{str}) of or the item (any C{type}). @return: The unregistered item. @raise NameError: No item with that B{C{name}}. @raise ValueError: No such item. ''' name = self.find(name_or_item) if name is None: if not isstr(name_or_item): raise _ValueError(name_or_item=name_or_item) name = name_or_item try: item = dict.pop(self, name) except KeyError: raise _NameError(item=self._dot_(name), txt=_doesn_t_exist_) item._enum = None return item