Coverage for pygeodesy/named.py: 96%
507 statements
« prev ^ index » next coverage.py v7.2.2, created at 2024-06-01 11:43 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2024-06-01 11:43 -0400
2# -*- coding: utf-8 -*-
4u'''(INTERNAL) Nameable class instances.
6Classes C{_Named}, C{_NamedDict}, C{_NamedEnum}, C{_NamedEnumItem} and
7C{_NamedTuple} and several subclasses thereof, all with nameable instances.
9The items in a C{_NamedDict} are accessable as attributes and the items
10in a C{_NamedTuple} are named to be accessable as attributes, similar to
11standard Python C{namedtuple}s.
13@see: Module L{pygeodesy.namedTuples} for (most of) the C{Named-Tuples}.
14'''
16from pygeodesy.basics import isclass, isidentifier, iskeyword, isstr, issubclassof, \
17 itemsorted, len2, _xcopy, _xdup, _zip
18from pygeodesy.errors import _AssertionError, _AttributeError, _incompatible, \
19 _IndexError, _IsnotError, _KeyError, LenError, \
20 _NameError, _NotImplementedError, _TypeError, \
21 _TypesError, UnitError, _ValueError, _xattr, _xkwds, \
22 _xkwds_item2, _xkwds_pop2
23from pygeodesy.internals import _caller3, _dunder_nameof, _isPyPy, _sizeof, _under
24from pygeodesy.interns import MISSING, NN, _AT_, _COLON_, _COLONSPACE_, _COMMA_, \
25 _COMMASPACE_, _doesn_t_exist_, _DOT_, _DUNDER_, \
26 _dunder_name_, _EQUAL_, _exists_, _immutable_, _name_, \
27 _NL_, _NN_, _no_, _other_, _s_, _SPACE_, _std_, \
28 _UNDER_, _valid_, _vs_
29from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _getenv
30from pygeodesy.props import _allPropertiesOf_n, deprecated_method, _hasProperty, \
31 _update_all, property_doc_, Property_RO, property_RO, \
32 _update_attrs
33from pygeodesy.streprs import attrs, Fmt, lrstrip, pairs, reprs, unstr
35__all__ = _ALL_LAZY.named
36__version__ = '24.05.31'
38_COMMANL_ = _COMMA_ + _NL_
39_COMMASPACEDOT_ = _COMMASPACE_ + _DOT_
40_del_ = 'del'
41_item_ = 'item'
42_MRO_ = 'MRO'
43# __DUNDER gets mangled in class
44_name = _under(_name_)
45_Names_ = '_Names_'
46_registered_ = 'registered' # PYCHOK used!
47_std_NotImplemented = _getenv('PYGEODESY_NOTIMPLEMENTED', NN).lower() == _std_
48_such_ = 'such'
49_Units_ = '_Units_'
50_UP = 2
53class ADict(dict):
54 '''A C{dict} with both key I{and} attribute access to
55 the C{dict} items.
56 '''
57 _iteration = None # Iteration number (C{int}) or C{None}
59 def __getattr__(self, name):
60 '''Get the value of an item by B{C{name}}.
61 '''
62 try:
63 return self[name]
64 except KeyError:
65 if name == _name_:
66 return NN
67 raise self._AttributeError(name)
69 def __repr__(self):
70 '''Default C{repr(self)}.
71 '''
72 return self.toRepr()
74 def __str__(self):
75 '''Default C{str(self)}.
76 '''
77 return self.toStr()
79 def _AttributeError(self, name):
80 '''(INTERNAL) Create an C{AttributeError}.
81 '''
82 if _DOT_ not in name: # NOT classname(self)!
83 name = _DOT_(self.__class__.__name__, name)
84 return _AttributeError(item=name, txt=_doesn_t_exist_)
86 @property_RO
87 def iteration(self): # see .named._NamedBase
88 '''Get the iteration number (C{int}) or
89 C{None} if not available/applicable.
90 '''
91 return self._iteration
93 def set_(self, iteration=None, **items): # PYCHOK signature
94 '''Add one or several new items or replace existing ones.
96 @kwarg iteration: Optional C{iteration} (C{int}).
97 @kwarg items: One or more C{name=value} pairs.
98 '''
99 if iteration is not None:
100 self._iteration = iteration
101 if items:
102 dict.update(self, items)
103 return self # in RhumbLineBase.Intersecant2, _PseudoRhumbLine.Position
105 def toRepr(self, **prec_fmt):
106 '''Like C{repr(dict)} but with C{name} prefix and with
107 C{floats} formatted by function L{pygeodesy.fstr}.
108 '''
109 n = _xattr(self, name=NN) or self.__class__.__name__
110 return Fmt.PAREN(n, self._toT(_EQUAL_, **prec_fmt))
112 def toStr(self, **prec_fmt):
113 '''Like C{str(dict)} but with C{floats} formatted by
114 function L{pygeodesy.fstr}.
115 '''
116 return Fmt.CURLY(self._toT(_COLONSPACE_, **prec_fmt))
118 def _toT(self, sep, **kwds):
119 '''(INTERNAL) Helper for C{.toRepr} and C{.toStr}.
120 '''
121 kwds = _xkwds(kwds, prec=6, fmt=Fmt.F, sep=sep)
122 return _COMMASPACE_.join(pairs(itemsorted(self), **kwds))
125class _Named(object):
126 '''(INTERNAL) Root class for named objects.
127 '''
128 _iteration = None # iteration number (C{int}) or C{None}
129 _name = NN # name (C{str})
130 _classnaming = False # prefixed (C{bool})
131# _updates = 0 # OBSOLETE Property/property updates
133 def __imatmul__(self, other): # PYCHOK no cover
134 '''Not implemented.'''
135 return _NotImplemented(self, other) # PYCHOK Python 3.5+
137 def __matmul__(self, other): # PYCHOK no cover
138 '''Not implemented.'''
139 return _NotImplemented(self, other) # PYCHOK Python 3.5+
141 def __repr__(self):
142 '''Default C{repr(self)}.
143 '''
144 return Fmt.repr_at(self)
146 def __rmatmul__(self, other): # PYCHOK no cover
147 '''Not implemented.'''
148 return _NotImplemented(self, other) # PYCHOK Python 3.5+
150 def __str__(self):
151 '''Default C{str(self)}.
152 '''
153 return self.named2
155 def attrs(self, *names, **sep_Nones_pairs_kwds):
156 '''Join named attributes as I{name=value} strings, with C{float}s formatted by
157 function L{pygeodesy.fstr}.
159 @arg names: The attribute names, all positional (C{str}).
160 @kwarg sep_Nones_pairs_kwds: Keyword arguments for function L{pygeodesy.pairs},
161 except C{B{sep}=", "} and C{B{Nones}=True} to in-/exclude missing
162 or C{None}-valued attributes.
164 @return: All C{name=value} pairs, joined by B{C{sep}} (C{str}).
166 @see: Functions L{pygeodesy.attrs}, L{pygeodesy.pairs} and L{pygeodesy.fstr}
167 '''
168 sep, kwds = _xkwds_pop2(sep_Nones_pairs_kwds, sep=_COMMASPACE_)
169 return sep.join(attrs(self, *names, **kwds))
171 @Property_RO
172 def classname(self):
173 '''Get this object's C{[module.]class} name (C{str}), see
174 property C{.classnaming} and function C{classnaming}.
175 '''
176 return classname(self, prefixed=self._classnaming)
178 @property_doc_(''' the class naming (C{bool}).''')
179 def classnaming(self):
180 '''Get the class naming (C{bool}), see function C{classnaming}.
181 '''
182 return self._classnaming
184 @classnaming.setter # PYCHOK setter!
185 def classnaming(self, prefixed):
186 '''Set the class naming for C{[module.].class} names (C{bool})
187 to C{True} to include the module name.
188 '''
189 b = bool(prefixed)
190 if self._classnaming != b:
191 self._classnaming = b
192 _update_attrs(self, *_Named_Property_ROs)
194 def classof(self, *args, **kwds):
195 '''Create another instance of this very class.
197 @arg args: Optional, positional arguments.
198 @kwarg kwds: Optional, keyword arguments.
200 @return: New instance (B{self.__class__}).
201 '''
202 return _xnamed(self.__class__(*args, **kwds), self.name)
204 def copy(self, deep=False, **name):
205 '''Make a shallow or deep copy of this instance.
207 @kwarg deep: If C{True} make a deep, otherwise
208 a shallow copy (C{bool}).
209 @kwarg name: Optional, non-empty C{B{name}=NN} (C{str}).
211 @return: The copy (C{This class}).
212 '''
213 c = _xcopy(self, deep=deep)
214 if name:
215 _ = c.rename(name)
216 return c
218 def _DOT_(self, *names):
219 '''(INTERNAL) Period-join C{self.name} and C{names}.
220 '''
221 return _DOT_(self.name, *names)
223 def dup(self, deep=False, **items):
224 '''Duplicate this instance, replacing some attributes.
226 @kwarg deep: If C{True} duplicate deep, otherwise shallow.
227 @kwarg items: Attributes to be changed (C{any}), including
228 optional C{B{name}} (C{str}).
230 @return: The duplicate (C{This class}).
232 @raise AttributeError: Some B{C{items}} invalid.
233 '''
234 n = self.name
235 m, items = _xkwds_pop2(items, name=n)
236 d = _xdup(self, deep=deep, **items)
237 if m != n:
238 d.rename(m) # zap _Named_Property_ROs
239# if items:
240# _update_all(d)
241 return d
243 def _instr(self, name, prec, *attrs, **fmt_props_kwds):
244 '''(INTERNAL) Format, used by C{Conic}, C{Ellipsoid}, C{Transform}, C{Triaxial}.
245 '''
246 def _fmt_props_kwds(fmt=Fmt.F, props=(), **kwds):
247 return fmt, props, kwds
249 fmt, props, kwds = _fmt_props_kwds(**fmt_props_kwds)
251 t = () if name is None else (Fmt.EQUAL(name=repr(name or self.name)),)
252 if attrs:
253 t += pairs(((a, getattr(self, a)) for a in attrs),
254 prec=prec, ints=True, fmt=fmt)
255 if props:
256 t += pairs(((p.name, getattr(self, p.name)) for p in props),
257 prec=prec, ints=True)
258 if kwds:
259 t += pairs(kwds, prec=prec)
260 return _COMMASPACE_.join(t)
262 @property_RO
263 def iteration(self): # see .karney.GDict
264 '''Get the most recent iteration number (C{int}) or C{None}
265 if not available or not applicable.
267 @note: The interation number may be an aggregate number over
268 several, nested functions.
269 '''
270 return self._iteration
272 def methodname(self, which):
273 '''Get a method C{[module.]class.method} name of this object (C{str}).
275 @arg which: The method (C{callable}).
276 '''
277 return _DOT_(self.classname, which.__name__ if callable(which) else _NN_)
279 @property_doc_(''' the name (C{str}).''')
280 def name(self):
281 '''Get the name (C{str}).
282 '''
283 return self._name
285 @name.setter # PYCHOK setter!
286 def name(self, name):
287 '''Set the C{B{name}=NN} (C{str}).
289 @raise NameError: Can't rename, use method L{rename}.
290 '''
291 m, n = self._name, _name__(name)
292 if not m:
293 self._name = n
294 elif n != m:
295 n = repr(n)
296 c = self.classname
297 t = _DOT_(c, Fmt.PAREN(self.rename.__name__, n))
298 n = _DOT_(c, Fmt.EQUALSPACED(name=n))
299 m = Fmt.PAREN(_SPACE_('was', repr(m)))
300 n = _SPACE_(n, m)
301 raise _NameError(n, txt=_SPACE_('use', t))
302 # to set the name from a sub-class, use
303 # self.name = name or
304 # _Named.name.fset(self, name), but NOT
305 # _Named(self).name = name
307 def _name__(self, name): # usually **name
308 '''(INTERNAL) Get the C{name} or this C{name}.
309 '''
310 return _name__(name, _or_nameof=self) # nameof(self)
312 @Property_RO
313 def named(self):
314 '''Get the name I{or} class name or C{""} (C{str}).
315 '''
316 return self.name or self.classname
318# @Property_RO
319# def named_(self):
320# '''Get the C{class} name I{and/or} the str(name) or C{""} (C{str}).
321# '''
322# return _xjoined_(self.classname, self.name, enquote=False)
324 @Property_RO
325 def named2(self):
326 '''Get the C{class} name I{and/or} the repr(name) or C{""} (C{str}).
327 '''
328 return _xjoined_(self.classname, self.name)
330 @Property_RO
331 def named3(self):
332 '''Get the I{prefixed} C{class} name I{and/or} the name or C{""} (C{str}).
333 '''
334 return _xjoined_(classname(self, prefixed=True), self.name)
336 @Property_RO
337 def named4(self):
338 '''Get the C{package.module.class} name I{and/or} the name or C{""} (C{str}).
339 '''
340 return _xjoined_(_DOT_(self.__module__, self.__class__.__name__), self.name)
342 def _notImplemented(self, *args, **kwds):
343 '''(INTERNAL) See function L{notImplemented}.
344 '''
345 notImplemented(self, *args, **_xkwds(kwds, up=_UP + 1))
347 def _notOverloaded(self, *args, **kwds):
348 '''(INTERNAL) See function L{notOverloaded}.
349 '''
350 notOverloaded(self, *args, **_xkwds(kwds, up=_UP + 1))
352 def rename(self, name):
353 '''Change the name.
355 @arg name: The new name (C{str}).
357 @return: The previous name (C{str}).
358 '''
359 m, n = self._name, _name__(name)
360 if n != m:
361 self._name = n
362 _update_attrs(self, *_Named_Property_ROs)
363 return m
365 def renamed(self, name):
366 '''Change the name.
368 @arg name: The new name (C{str}).
370 @return: This instance (C{str}).
371 '''
372 _ = self.rename(name)
373 return self
375 @property_RO
376 def sizeof(self):
377 '''Get the current size in C{bytes} of this instance (C{int}).
378 '''
379 return _sizeof(self)
381 def toRepr(self, **unused): # PYCHOK no cover
382 '''Default C{repr(self)}.
383 '''
384 return repr(self)
386 def toStr(self, **unused): # PYCHOK no cover
387 '''Default C{str(self)}.
388 '''
389 return str(self)
391 @deprecated_method
392 def toStr2(self, **kwds): # PYCHOK no cover
393 '''DEPRECATED on 23.10.07, use method C{toRepr}.'''
394 return self.toRepr(**kwds)
396# def _unstr(self, which, *args, **kwds):
397# '''(INTERNAL) Return the string representation of a method
398# invokation of this instance: C{str(self).method(...)}
399#
400# @see: Function L{pygeodesy.unstr}.
401# '''
402# return _DOT_(self, unstr(which, *args, **kwds))
404 def _xnamed(self, inst, name=NN, **force):
405 '''(INTERNAL) Set the instance' C{.name = self.name}.
407 @arg inst: The instance (C{_Named}).
408 @kwarg name: The name (C{str}).
409 @kwarg force: If C{True}, force rename (C{bool}).
411 @return: The B{C{inst}}, renamed if B{C{force}}d
412 or if not named before.
413 '''
414 return _xnamed(inst, name, **force)
416 def _xrenamed(self, inst):
417 '''(INTERNAL) Rename the instance' C{.name = self.name}.
419 @arg inst: The instance (C{_Named}).
421 @return: The B{C{inst}}, named if not named before.
423 @raise TypeError: Not C{isinstance(B{inst}, _Named)}.
424 '''
425 if not isinstance(inst, _Named):
426 raise _IsnotError(_valid_, inst=inst)
428 _ = inst.rename(self.name)
429 return inst
431_Named_Property_ROs = _allPropertiesOf_n(5, _Named, Property_RO) # PYCHOK once
434class _NamedBase(_Named):
435 '''(INTERNAL) Base class with name.
436 '''
437 def __repr__(self):
438 '''Default C{repr(self)}.
439 '''
440 return self.toRepr()
442 def __str__(self):
443 '''Default C{str(self)}.
444 '''
445 return self.toStr()
447 def others(self, *other, **name_other_up):
448 '''Refined class comparison, invoked as C{.others(other)},
449 C{.others(name=other)} or C{.others(other, name='other')}.
451 @arg other: The other instance (any C{type}).
452 @kwarg name_other_up: Overriding C{name=other} and C{up=1}
453 keyword arguments.
455 @return: The B{C{other}} iff compatible with this instance's
456 C{class} or C{type}.
458 @raise TypeError: Mismatch of the B{C{other}} and this
459 instance's C{class} or C{type}.
460 '''
461 if other: # most common, just one arg B{C{other}}
462 other0 = other[0]
463 if isinstance(other0, self.__class__) or \
464 isinstance(self, other0.__class__):
465 return other0
467 other, name, up = _xother3(self, other, **name_other_up)
468 if isinstance(self, other.__class__) or \
469 isinstance(other, self.__class__):
470 return _xnamed(other, name)
472 raise _xotherError(self, other, name=name, up=up + 1)
474 def toRepr(self, **kwds): # PYCHOK expected
475 '''(INTERNAL) I{Could be overloaded}.
477 @kwarg kwds: Optional, C{toStr} keyword arguments.
479 @return: C{toStr}() with keyword arguments (as C{str}).
480 '''
481 t = lrstrip(self.toStr(**kwds))
482# if self.name:
483# t = NN(Fmt.EQUAL(name=repr(self.name)), sep, t)
484 return Fmt.PAREN(self.classname, t) # XXX (self.named, t)
486# def toRepr(self, **kwds)
487# if kwds:
488# s = NN.join(reprs((self,), **kwds))
489# else: # super().__repr__ only for Python 3+
490# s = super(self.__class__, self).__repr__()
491# return Fmt.PAREN(self.named, s) # clips(s)
493 def toStr(self, **kwds): # PYCHOK no cover
494 '''I{Must be overloaded}.'''
495 notOverloaded(self, **kwds)
497# def toStr(self, **kwds):
498# if kwds:
499# s = NN.join(strs((self,), **kwds))
500# else: # super().__str__ only for Python 3+
501# s = super(self.__class__, self).__str__()
502# return s
504 def _update(self, updated, *attrs, **setters):
505 '''(INTERNAL) Zap cached instance attributes and overwrite C{__dict__} or L{Property_RO} values.
506 '''
507 u = _update_all(self, *attrs) if updated else 0
508 if setters:
509 d = self.__dict__
510 # double-check that setters are Property_RO's
511 for n, v in setters.items():
512 if n in d or _hasProperty(self, n, Property_RO):
513 d[n] = v
514 else:
515 raise _AssertionError(n, v, txt=repr(self))
516 u += len(setters)
517 return u
520class _NamedDict(ADict, _Named):
521 '''(INTERNAL) Named C{dict} with key I{and} attribute access
522 to the items.
523 '''
524 def __init__(self, *args, **kwds):
525 if args: # is args[0] a dict?
526 if len(args) != 1: # or not isinstance(args[0], dict)
527 kwds = _name1__(kwds)
528 t = unstr(self.classname, *args, **kwds) # PYCHOK no cover
529 raise _ValueError(args=len(args), txt=t)
530 kwds = _xkwds(dict(args[0]), **kwds) # args[0] overrides kwds
531 n, kwds = _name2__(**kwds)
532 if n:
533 _Named.name.fset(self, n) # see _Named.name
534 ADict.__init__(self, kwds)
536 def __delattr__(self, name):
537 '''Delete an attribute or item by B{C{name}}.
538 '''
539 if name in self: # in ADict.keys(self):
540 ADict.pop(self, name)
541 elif name in (_name_, _name):
542 # ADict.__setattr__(self, name, NN)
543 _Named.rename(self, NN)
544 else:
545 ADict.__delattr__(self, name)
547 def __getattr__(self, name):
548 '''Get the value of an item by B{C{name}}.
549 '''
550 try:
551 return self[name]
552 except KeyError:
553 if name == _name_:
554 return _Named.name.fget(self)
555 raise ADict._AttributeError(self, self._DOT_(name))
557 def __getitem__(self, key):
558 '''Get the value of an item by B{C{key}}.
559 '''
560 if key == _name_:
561 raise self._KeyError(key)
562 return ADict.__getitem__(self, key)
564 def _KeyError(self, key, *value): # PYCHOK no cover
565 '''(INTERNAL) Create a C{KeyError}.
566 '''
567 n = self.name or self.__class__.__name__
568 t = Fmt.SQUARE(n, key)
569 if value:
570 t = Fmt.EQUALSPACED(t, *value)
571 return _KeyError(t)
573 def __setattr__(self, name, value):
574 '''Set attribute or item B{C{name}} to B{C{value}}.
575 '''
576 if name in self: # in ADict.keys(self)
577 ADict.__setitem__(self, name, value) # self[name] = value
578 else:
579 ADict.__setattr__(self, name, value)
581 def __setitem__(self, key, value):
582 '''Set item B{C{key}} to B{C{value}}.
583 '''
584 if key == _name_:
585 raise self._KeyError(key, repr(value))
586 ADict.__setitem__(self, key, value)
589class _NamedEnum(_NamedDict):
590 '''(INTERNAL) Enum-like C{_NamedDict} with attribute access
591 restricted to valid keys.
592 '''
593 _item_Classes = ()
595 def __init__(self, Class, *Classes, **name):
596 '''New C{_NamedEnum}.
598 @arg Class: Initial class or type acceptable as items
599 values (C{type}).
600 @arg Classes: Additional, acceptable classes or C{type}s.
601 '''
602 self._item_Classes = (Class,) + Classes
603 n = _name__(**name) or NN(Class.__name__, _s_) # _dunder_nameof
604 if n and _xvalid(n, underOK=True):
605 _Named.name.fset(self, n) # see _Named.name
607 def __getattr__(self, name):
608 '''Get the value of an attribute or item by B{C{name}}.
609 '''
610 return _NamedDict.__getattr__(self, name)
612 def __repr__(self):
613 '''Default C{repr(self)}.
614 '''
615 return self.toRepr()
617 def __str__(self):
618 '''Default C{str(self)}.
619 '''
620 return self.toStr()
622 def _assert(self, **kwds):
623 '''(INTERNAL) Check attribute name against given, registered name.
624 '''
625 pypy = _isPyPy()
626 _isa = isinstance
627 for n, v in kwds.items():
628 if _isa(v, _LazyNamedEnumItem): # property
629 assert (n == v.name) if pypy else (n is v.name)
630 # assert not hasattr(self.__class__, n)
631 setattr(self.__class__, n, v)
632 elif _isa(v, self._item_Classes): # PYCHOK no cover
633 assert self[n] is v and getattr(self, n) \
634 and self.find(v) == n
635 else:
636 raise _TypeError(v, name=n)
638 def find(self, item, dflt=None, all=False):
639 '''Find a registered item.
641 @arg item: The item to look for (any C{type}).
642 @kwarg dflt: Value to return if not found (any C{type}).
643 @kwarg all: Use C{True} to search I{all} items or C{False} only
644 the currently I{registered} ones (C{bool}).
646 @return: The B{C{item}}'s name if found (C{str}), or C{{dflt}}
647 if there is no such B{C{item}}.
648 '''
649 for k, v in self.items(all=all): # or ADict.items(self)
650 if v is item:
651 return k
652 return dflt
654 def get(self, name, dflt=None):
655 '''Get the value of a I{registered} item.
657 @arg name: The name of the item (C{str}).
658 @kwarg dflt: Value to return (any C{type}).
660 @return: The item with B{C{name}} if found, or B{C{dflt}} if
661 there is no I{registered} item with that B{C{name}}.
662 '''
663 # getattr needed to instantiate L{_LazyNamedEnumItem}
664 return getattr(self, name, dflt)
666 def items(self, all=False, asorted=False):
667 '''Yield all or only the I{registered} items.
669 @kwarg all: Use C{True} to yield I{all} items or C{False} for
670 only the currently I{registered} ones (C{bool}).
671 @kwarg asorted: If C{True}, yield the items in I{alphabetical,
672 case-insensitive} order (C{bool}).
673 '''
674 if all: # instantiate any remaining L{_LazyNamedEnumItem}
675 _isa = isinstance
676 for n, p in tuple(self.__class__.__dict__.items()):
677 if _isa(p, _LazyNamedEnumItem):
678 _ = getattr(self, n)
679 return itemsorted(self) if asorted else ADict.items(self)
681 def keys(self, **all_asorted):
682 '''Yield the name (C{str}) of I{all} or only the currently I{registered}
683 items, optionally sorted I{alphabetically, case-insensitively}.
685 @kwarg all_asorted: See method C{items}.
686 '''
687 for k, _ in self.items(**all_asorted):
688 yield k
690 def popitem(self):
691 '''Remove I{an, any} currently I{registed} item.
693 @return: The removed item.
694 '''
695 return self._zapitem(*ADict.popitem(self))
697 def register(self, item):
698 '''Registed one new item or I{all} or I{any} unregistered ones.
700 @arg item: The item (any C{type}) or B{I{all}} or B{C{any}}.
702 @return: The item name (C{str}) or C("all") or C{"any"}.
704 @raise NameError: An B{C{item}} with that name is already
705 registered the B{C{item}} has no or an
706 invalid name.
708 @raise TypeError: The B{C{item}} type invalid.
709 '''
710 if item is all or item is any:
711 _ = self.items(all=True)
712 n = item.__name__
713 else:
714 try:
715 n = item.name
716 if not (n and isstr(n) and isidentifier(n)):
717 raise ValueError()
718 except (AttributeError, ValueError, TypeError) as x:
719 raise _NameError(_DOT_(_item_, _name_), item, cause=x)
720 if n in self:
721 t = _SPACE_(_item_, self._DOT_(n), _exists_)
722 raise _NameError(t, txt=repr(item))
723 if not isinstance(item, self._item_Classes):
724 raise _TypesError(self._DOT_(n), item, *self._item_Classes)
725 self[n] = item
726 return n
728 def unregister(self, name_or_item):
729 '''Remove a I{registered} item.
731 @arg name_or_item: Name (C{str}) or the item (any C{type}).
733 @return: The unregistered item.
735 @raise AttributeError: No such B{C{item}}.
737 @raise NameError: No item with that B{C{name}}.
738 '''
739 if isstr(name_or_item):
740 name = name_or_item
741 else:
742 name = self.find(name_or_item, dflt=MISSING) # all=True?
743 if name is MISSING:
744 t = _SPACE_(_no_, _such_, self.classname, _item_)
745 raise _AttributeError(t, txt=repr(name_or_item))
746 try:
747 item = ADict.pop(self, name)
748 except KeyError:
749 raise _NameError(item=self._DOT_(name), txt=_doesn_t_exist_)
750 return self._zapitem(name, item)
752 pop = unregister
754 def toRepr(self, prec=6, fmt=Fmt.F, sep=_COMMANL_, **all_asorted): # PYCHOK _NamedDict, ADict
755 '''Like C{repr(dict)} but C{name}s optionally sorted and
756 C{floats} formatted by function L{pygeodesy.fstr}.
757 '''
758 t = ((self._DOT_(n), v) for n, v in self.items(**all_asorted))
759 return sep.join(pairs(t, prec=prec, fmt=fmt, sep=_COLONSPACE_))
761 def toStr(self, *unused, **all_asorted): # PYCHOK _NamedDict, ADict
762 '''Return a string with all C{name}s, optionally sorted.
763 '''
764 return self._DOT_(_COMMASPACEDOT_.join(self.keys(**all_asorted)))
766 def values(self, **all_asorted):
767 '''Yield the value (C{type}) of all or only the I{registered} items,
768 optionally sorted I{alphabetically} and I{case-insensitively}.
770 @kwarg all_asorted: See method C{items}.
771 '''
772 for _, v in self.items(**all_asorted):
773 yield v
775 def _zapitem(self, name, item):
776 # remove _LazyNamedEnumItem property value if still present
777 if self.__dict__.get(name, None) is item:
778 self.__dict__.pop(name) # [name] = None
779 item._enum = None
780 return item
783class _LazyNamedEnumItem(property_RO): # XXX or descriptor?
784 '''(INTERNAL) Lazily instantiated L{_NamedEnumItem}.
785 '''
786 pass
789def _lazyNamedEnumItem(name, *args, **kwds):
790 '''(INTERNAL) L{_LazyNamedEnumItem} property-like factory.
792 @see: Luciano Ramalho, "Fluent Python", O'Reilly, Example
793 19-24, 2016 p. 636 or Example 22-28, 2022 p. 869+
794 '''
795 def _fget(inst):
796 # assert isinstance(inst, _NamedEnum)
797 try: # get the item from the instance' __dict__
798 # item = inst.__dict__[name] # ... or ADict
799 item = inst[name]
800 except KeyError:
801 # instantiate an _NamedEnumItem, it self-registers
802 item = inst._Lazy(*args, **_xkwds(kwds, name=name))
803 # assert inst[name] is item # MUST be registered
804 # store the item in the instance' __dict__ ...
805 # inst.__dict__[name] = item # ... or update the
806 inst.update({name: item}) # ... ADict for Triaxials
807 # remove the property from the registry class, such that
808 # (a) the property no longer overrides the instance' item
809 # in inst.__dict__ and (b) _NamedEnum.items(all=True) only
810 # sees any un-instantiated ones yet to be instantiated
811 p = getattr(inst.__class__, name, None)
812 if isinstance(p, _LazyNamedEnumItem):
813 delattr(inst.__class__, name)
814 # assert isinstance(item, _NamedEnumItem)
815 return item
817 p = _LazyNamedEnumItem(_fget)
818 p.name = name
819 return p
822class _NamedEnumItem(_NamedBase):
823 '''(INTERNAL) Base class for items in a C{_NamedEnum} registery.
824 '''
825 _enum = None
827# def __ne__(self, other): # XXX fails for Lcc.conic = conic!
828# '''Compare this and an other item.
829#
830# @return: C{True} if different, C{False} otherwise.
831# '''
832# return not self.__eq__(other)
834 @property_doc_(''' the I{registered} name (C{str}).''')
835 def name(self):
836 '''Get the I{registered} name (C{str}).
837 '''
838 return self._name
840 @name.setter # PYCHOK setter!
841 def name(self, name):
842 '''Set the name, unless already registered (C{str}).
843 '''
844 name = _name__(name) or _NN_
845 if self._enum:
846 raise _NameError(name, self, txt=_registered_) # XXX _TypeError
847 if name:
848 self._name = name
850 def _register(self, enum, name):
851 '''(INTERNAL) Add this item as B{C{enum.name}}.
853 @note: Don't register if name is empty or doesn't
854 start with a letter.
855 '''
856 name = _name__(name)
857 if name and _xvalid(name, underOK=True):
858 self.name = name
859 if name[:1].isalpha(): # '_...' not registered
860 enum.register(self)
861 self._enum = enum
863 def unregister(self):
864 '''Remove this instance from its C{_NamedEnum} registry.
866 @raise AssertionError: Mismatch of this and registered item.
868 @raise NameError: This item is unregistered.
869 '''
870 enum = self._enum
871 if enum and self.name and self.name in enum:
872 item = enum.unregister(self.name)
873 if item is not self:
874 t = _SPACE_(repr(item), _vs_, repr(self)) # PYCHOK no cover
875 raise _AssertionError(t)
878class _NamedTuple(tuple, _Named):
879 '''(INTERNAL) Base for named C{tuple}s with both index I{and}
880 attribute name access to the items.
882 @note: This class is similar to Python's C{namedtuple},
883 but statically defined, lighter and limited.
884 '''
885 _Names_ = () # item names, non-identifier, no leading underscore
886 '''Tuple specifying the C{name} of each C{Named-Tuple} item.
888 @note: Specify at least 2 item names.
889 '''
890 _Units_ = () # .units classes
891 '''Tuple defining the C{units} of the value of each C{Named-Tuple} item.
893 @note: The C{len(_Units_)} must match C{len(_Names_)}.
894 '''
895 _validated = False # set to True I{per sub-class!}
897 def __new__(cls, arg, *args, **iteration_name):
898 '''New L{_NamedTuple} initialized with B{C{positional}} arguments.
900 @arg arg: Tuple items (C{tuple}, C{list}, ...) or first tuple
901 item of several more in other positional arguments.
902 @arg args: Tuple items (C{any}), all positional arguments.
903 @kwarg iteration_name: Only keyword arguments C{B{iteration}=None}
904 and C{B{name}=NN} are used, any other are
905 I{silently} ignored.
907 @raise LenError: Unequal number of positional arguments and
908 number of item C{_Names_} or C{_Units_}.
910 @raise TypeError: The C{_Names_} or C{_Units_} attribute is
911 not a C{tuple} of at least 2 items.
913 @raise ValueError: Item name is not a C{str} or valid C{identifier}
914 or starts with C{underscore}.
915 '''
916 n, args = len2(((arg,) + args) if args else arg)
917 self = tuple.__new__(cls, args)
918 if not self._validated:
919 self._validate()
921 N = len(self._Names_)
922 if n != N:
923 raise LenError(self.__class__, args=n, _Names_=N)
925 if iteration_name:
926 self._kwdself(**iteration_name)
927 return self
929 def __delattr__(self, name):
930 '''Delete an attribute by B{C{name}}.
932 @note: Items can not be deleted.
933 '''
934 if name in self._Names_:
935 raise _TypeError(_del_, _DOT_(self.classname, name), txt=_immutable_)
936 elif name in (_name_, _name):
937 _Named.__setattr__(self, name, NN) # XXX _Named.name.fset(self, NN)
938 else:
939 tuple.__delattr__(self, name)
941 def __getattr__(self, name):
942 '''Get the value of an attribute or item by B{C{name}}.
943 '''
944 try:
945 return tuple.__getitem__(self, self._Names_.index(name))
946 except IndexError:
947 raise _IndexError(_DOT_(self.classname, Fmt.ANGLE(_name_)), name)
948 except ValueError: # e.g. _iteration
949 return tuple.__getattribute__(self, name)
951# def __getitem__(self, index): # index, slice, etc.
952# '''Get the item(s) at an B{C{index}} or slice.
953# '''
954# return tuple.__getitem__(self, index)
956 def __hash__(self):
957 return tuple.__hash__(self)
959 def __repr__(self):
960 '''Default C{repr(self)}.
961 '''
962 return self.toRepr()
964 def __setattr__(self, name, value):
965 '''Set attribute or item B{C{name}} to B{C{value}}.
966 '''
967 if name in self._Names_:
968 raise _TypeError(_DOT_(self.classname, name), value, txt=_immutable_)
969 elif name in (_name_, _name):
970 _Named.__setattr__(self, name, value) # XXX _Named.name.fset(self, value)
971 else: # e.g. _iteration
972 tuple.__setattr__(self, name, value)
974 def __str__(self):
975 '''Default C{repr(self)}.
976 '''
977 return self.toStr()
979 def dup(self, name=NN, **items):
980 '''Duplicate this tuple replacing one or more items.
982 @kwarg name: Optional new name (C{str}).
983 @kwarg items: Items to be replaced (C{name=value} pairs), if any.
985 @return: A copy of this tuple with B{C{items}}.
987 @raise NameError: Some B{C{items}} invalid.
988 '''
989 tl = list(self)
990 if items:
991 _ix = self._Names_.index
992 try:
993 for n, v in items.items():
994 tl[_ix(n)] = v
995 except ValueError: # bad item name
996 raise _NameError(_DOT_(self.classname, n), v, this=self)
997 return self.classof(*tl, name=name or self.name)
999 def items(self):
1000 '''Yield the items, each as a C{(name, value)} pair (C{2-tuple}).
1002 @see: Method C{.units}.
1003 '''
1004 for n, v in _zip(self._Names_, self): # strict=True
1005 yield n, v
1007 iteritems = items
1009 def _kwdself(self, iteration=None, **name):
1010 '''(INTERNAL) Set C{__new__} keyword arguments.
1011 '''
1012 if iteration is not None:
1013 self._iteration = iteration
1014 if name:
1015 self.name = name
1017 def toRepr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1018 '''Return this C{Named-Tuple} items as C{name=value} string(s).
1020 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1021 Trailing zero decimals are stripped for B{C{prec}} values
1022 of 1 and above, but kept for negative B{C{prec}} values.
1023 @kwarg sep: Separator to join (C{str}).
1024 @kwarg fmt: Optional C{float} format (C{letter}).
1026 @return: Tuple items (C{str}).
1027 '''
1028 t = pairs(self.items(), prec=prec, fmt=fmt)
1029# if self.name:
1030# t = (Fmt.EQUAL(name=repr(self.name)),) + t
1031 return Fmt.PAREN(self.named, sep.join(t)) # XXX (self.classname, sep.join(t))
1033 def toStr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1034 '''Return this C{Named-Tuple} items as string(s).
1036 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1037 Trailing zero decimals are stripped for B{C{prec}} values
1038 of 1 and above, but kept for negative B{C{prec}} values.
1039 @kwarg sep: Separator to join (C{str}).
1040 @kwarg fmt: Optional C{float} format (C{letter}).
1042 @return: Tuple items (C{str}).
1043 '''
1044 return Fmt.PAREN(sep.join(reprs(self, prec=prec, fmt=fmt)))
1046 def toUnits(self, Error=UnitError): # overloaded in .frechet, .hausdorff
1047 '''Return a copy of this C{Named-Tuple} with each item value wrapped
1048 as an instance of its L{units} class.
1050 @kwarg Error: Error to raise for L{units} issues (C{UnitError}).
1052 @return: A duplicate of this C{Named-Tuple} (C{C{Named-Tuple}}).
1054 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1055 '''
1056 t = (v for _, v in self.units(Error=Error))
1057 return self.classof(*tuple(t))
1059 def units(self, Error=UnitError):
1060 '''Yield the items, each as a C{(name, value}) pair (C{2-tuple}) with
1061 the value wrapped as an instance of its L{units} class.
1063 @kwarg Error: Error to raise for L{units} issues (C{UnitError}).
1065 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1067 @see: Method C{.items}.
1068 '''
1069 for n, v, U in _zip(self._Names_, self, self._Units_): # strict=True
1070 if not (v is None or U is None
1071 or (isclass(U) and
1072 isinstance(v, U) and
1073 hasattr(v, _name_) and
1074 v.name == n)): # PYCHOK indent
1075 v = U(v, name=n, Error=Error)
1076 yield n, v
1078 iterunits = units
1080 def _validate(self, underOK=False): # see .EcefMatrix
1081 '''(INTERNAL) One-time check of C{_Names_} and C{_Units_}
1082 for each C{_NamedUnit} I{sub-class separately}.
1083 '''
1084 ns = self._Names_
1085 if not (isinstance(ns, tuple) and len(ns) > 1): # XXX > 0
1086 raise _TypeError(_DOT_(self.classname, _Names_), ns)
1087 for i, n in enumerate(ns):
1088 if not _xvalid(n, underOK=underOK):
1089 t = Fmt.SQUARE(_Names_=i) # PYCHOK no cover
1090 raise _ValueError(_DOT_(self.classname, t), n)
1092 us = self._Units_
1093 if not isinstance(us, tuple):
1094 raise _TypeError(_DOT_(self.classname, _Units_), us)
1095 if len(us) != len(ns):
1096 raise LenError(self.__class__, _Units_=len(us), _Names_=len(ns))
1097 for i, u in enumerate(us):
1098 if not (u is None or callable(u)):
1099 t = Fmt.SQUARE(_Units_=i) # PYCHOK no cover
1100 raise _TypeError(_DOT_(self.classname, t), u)
1102 self.__class__._validated = True
1104 def _xtend(self, xTuple, *items, **name):
1105 '''(INTERNAL) Extend this C{Named-Tuple} with C{items} to an other B{C{xTuple}}.
1106 '''
1107 if (issubclassof(xTuple, _NamedTuple) and
1108 (len(self._Names_) + len(items)) == len(xTuple._Names_) and
1109 self._Names_ == xTuple._Names_[:len(self)]):
1110 n = _name__(**name) or self.name
1111 return xTuple(self + items, name=n) # *(self + items)
1112 c = NN(self.classname, repr(self._Names_)) # PYCHOK no cover
1113 x = NN(xTuple.__name__, repr(xTuple._Names_)) # PYCHOK no cover
1114 raise TypeError(_SPACE_(c, _vs_, x))
1117def callername(up=1, dflt=NN, source=False, underOK=False):
1118 '''Get the name of the invoking callable.
1120 @kwarg up: Number of call stack frames up (C{int}).
1121 @kwarg dflt: Default return value (C{any}).
1122 @kwarg source: Include source file name and line number (C{bool}).
1123 @kwarg underOK: If C{True}, private, internal callables are OK,
1124 otherwise private callables are skipped (C{bool}).
1126 @return: The callable name (C{str}) or B{C{dflt}} if none found.
1127 '''
1128 try: # see .lazily._caller3
1129 for u in range(up, up + 32):
1130 n, f, s = _caller3(u)
1131 if n and (underOK or n.startswith(_DUNDER_) or
1132 not n.startswith(_UNDER_)):
1133 if source:
1134 n = NN(n, _AT_, f, _COLON_, str(s))
1135 return n
1136 except (AttributeError, ValueError):
1137 pass
1138 return dflt
1141def _callername2(args, callername=NN, source=False, underOK=False, up=_UP, **kwds):
1142 '''(INTERNAL) Extract C{callername}, C{source}, C{underOK} and C{up} from C{kwds}.
1143 '''
1144 n = callername or _MODS.named.callername(up=up + 1, source=source,
1145 underOK=underOK or bool(args or kwds))
1146 return n, kwds
1149def _callname(name, class_name, self_name, up=1):
1150 '''(INTERNAL) Assemble the name for an invokation.
1151 '''
1152 n, c = class_name, callername(up=up + 1)
1153 if c:
1154 n = _DOT_(n, Fmt.PAREN(c, name))
1155 if self_name:
1156 n = _SPACE_(n, repr(self_name))
1157 return n
1160def classname(inst, prefixed=None):
1161 '''Return the instance' class name optionally prefixed with the
1162 module name.
1164 @arg inst: The object (any C{type}).
1165 @kwarg prefixed: Include the module name (C{bool}), see
1166 function C{classnaming}.
1168 @return: The B{C{inst}}'s C{[module.]class} name (C{str}).
1169 '''
1170 if prefixed is None:
1171 prefixed = getattr(inst, classnaming.__name__, prefixed)
1172 return modulename(inst.__class__, prefixed=prefixed)
1175def classnaming(prefixed=None):
1176 '''Get/set the default class naming for C{[module.]class} names.
1178 @kwarg prefixed: Include the module name (C{bool}).
1180 @return: Previous class naming setting (C{bool}).
1181 '''
1182 t = _Named._classnaming
1183 if prefixed in (True, False):
1184 _Named._classnaming = prefixed
1185 return t
1188def modulename(clas, prefixed=None): # in .basics._xversion
1189 '''Return the class name optionally prefixed with the
1190 module name.
1192 @arg clas: The class (any C{class}).
1193 @kwarg prefixed: Include the module name (C{bool}), see
1194 function C{classnaming}.
1196 @return: The B{C{class}}'s C{[module.]class} name (C{str}).
1197 '''
1198 try:
1199 n = clas.__name__
1200 except AttributeError:
1201 n = _dunder_name_
1202 if prefixed or (classnaming() if prefixed is None else False):
1203 try:
1204 m = clas.__module__.rsplit(_DOT_, 1)
1205 n = _DOT_.join(m[1:] + [n])
1206 except AttributeError:
1207 pass
1208 return n
1211# def _name__(name=NN, name__=None, _or_nameof=None, **kwds):
1212# '''(INTERNAL) Get single keyword argument C{B{name}=NN|None}.
1213# '''
1214# if kwds: # "unexpected keyword arguments ..."
1215# m = _MODS.errors
1216# raise m._UnexpectedError(**kwds)
1217# if name: # is given
1218# n = _name__(**name) if isinstance(name, dict) else str(name)
1219# elif name__ is not None:
1220# n = getattr(name__, _dunder_name_, NN) # _xattr(name__, __name__=NN)
1221# else:
1222# n = name # NN or None or {} or any False type
1223# if _or_nameof is not None and not n:
1224# n = getattr(_or_nameof, _name_, NN) # _xattr(_or_nameof, name=NN)
1225# return n # str or None or {}
1228def _name__(name=NN, **kwds):
1229 '''(INTERNAL) Get single keyword argument C{B{name}=NN|None}.
1230 '''
1231 if name or kwds:
1232 name, kwds = _name2__(name, **kwds)
1233 if kwds: # "unexpected keyword arguments ..."
1234 m = _MODS.errors
1235 raise m._UnexpectedError(**kwds)
1236 return name if name or name is None else NN
1239def _name1__(kwds_name, **name__or_nameof):
1240 '''(INTERNAL) Resolve and set the C{B{name}=NN}.
1241 '''
1242 if kwds_name or name__or_nameof:
1243 n, kwds_name = _name2__(kwds_name, **name__or_nameof)
1244 kwds_name.update(name=n)
1245 return kwds_name
1248def _name2__(name=NN, name__=None, _or_nameof=None, **kwds):
1249 '''(INTERNAL) Get the C{B{name}=NN|None} and other C{kwds}.
1250 '''
1251 if name: # is given
1252 if isinstance(name, dict):
1253 kwds.update(name) # kwds = _xkwds(kwds, **name)?
1254 n, kwds = _name2__(**kwds)
1255 else:
1256 n = str(name)
1257 elif name__ is not None:
1258 n = getattr(name__, _dunder_name_, NN) # _xattr(name__, __name__=NN)
1259 else:
1260 n = name if name is None else NN
1261 if _or_nameof is not None and not n:
1262 n = getattr(_or_nameof, _name_, NN) # _xattr(_or_nameof, name=NN)
1263 return n, kwds # (str or None or {}), dict
1266def nameof(inst):
1267 '''Get the name of an instance.
1269 @arg inst: The object (any C{type}).
1271 @return: The instance' name (C{str}) or C{""}.
1272 '''
1273 n = _xattr(inst, name=NN)
1274 if not n: # and isinstance(inst, property):
1275 try:
1276 n = inst.fget.__name__
1277 except AttributeError:
1278 n = NN
1279 return n
1282def _notDecap(where):
1283 '''De-Capitalize C{where.__name__}.
1284 '''
1285 n = where.__name__
1286 c = n[3].lower() # len(_not_)
1287 return NN(n[:3], _SPACE_, c, n[4:])
1290def _notError(inst, name, args, kwds): # PYCHOK no cover
1291 '''(INTERNAL) Format an error message.
1292 '''
1293 n = _DOT_(classname(inst, prefixed=True), _dunder_nameof(name, name))
1294 m = _COMMASPACE_.join(modulename(c, prefixed=True) for c in inst.__class__.__mro__[1:-1])
1295 return _COMMASPACE_(unstr(n, *args, **kwds), Fmt.PAREN(_MRO_, m))
1298def _NotImplemented(inst, *other, **kwds):
1299 '''(INTERNAL) Raise a C{__special__} error or return C{NotImplemented},
1300 but only if env variable C{PYGEODESY_NOTIMPLEMENTED=std}.
1301 '''
1302 if _std_NotImplemented:
1303 return NotImplemented
1304 n, kwds = _callername2(other, **kwds) # source=True
1305 t = unstr(_DOT_(classname(inst), n), *other, **kwds)
1306 raise _NotImplementedError(t, txt=repr(inst))
1309def notImplemented(inst, *args, **kwds): # PYCHOK no cover
1310 '''Raise a C{NotImplementedError} for a missing instance method or
1311 property or for a missing caller feature.
1313 @arg inst: Caller instance (C{any}) or C{None} for function.
1314 @arg args: Method or property positional arguments (any C{type}s).
1315 @arg kwds: Method or property keyword arguments (any C{type}s),
1316 except C{B{callername}=NN}, C{B{underOK}=False} and
1317 C{B{up}=2}.
1318 '''
1319 n, kwds = _callername2(args, **kwds)
1320 t = _notError(inst, n, args, kwds) if inst else unstr(n, *args, **kwds)
1321 raise _NotImplementedError(t, txt=_notDecap(notImplemented))
1324def notOverloaded(inst, *args, **kwds): # PYCHOK no cover
1325 '''Raise an C{AssertionError} for a method or property not overloaded.
1327 @arg inst: Instance (C{any}).
1328 @arg args: Method or property positional arguments (any C{type}s).
1329 @arg kwds: Method or property keyword arguments (any C{type}s),
1330 except C{B{callername}=NN}, C{B{underOK}=False} and
1331 C{B{up}=2}.
1332 '''
1333 n, kwds = _callername2(args, **kwds)
1334 t = _notError(inst, n, args, kwds)
1335 raise _AssertionError(t, txt=_notDecap(notOverloaded))
1338def _Pass(arg, **unused): # PYCHOK no cover
1339 '''(INTERNAL) I{Pass-thru} class for C{_NamedTuple._Units_}.
1340 '''
1341 return arg
1344def _xjoined_(prefix, name=NN, enquote=True, **name__or_nameof):
1345 '''(INTERNAL) Join C{prefix} and non-empty C{name}.
1346 '''
1347 if name__or_nameof:
1348 name = _name__(name, **name__or_nameof)
1349 if name and prefix:
1350 if enquote:
1351 name = repr(name)
1352 t = _SPACE_(prefix, name)
1353 else:
1354 t = prefix or name
1355 return t
1358def _xnamed(inst, name=NN, force=False, **name__or_nameof):
1359 '''(INTERNAL) Set the instance' C{.name = B{name}}.
1361 @arg inst: The instance (C{_Named}).
1362 @kwarg name: The name (C{str}).
1363 @kwarg force: If C{True}, force rename (C{bool}).
1365 @return: The B{C{inst}}, renamed if B{C{force}}d
1366 or if not named before.
1367 '''
1368 if name__or_nameof:
1369 name = _name__(name, **name__or_nameof)
1370 if name and isinstance(inst, _Named):
1371 if not inst.name:
1372 inst.name = name
1373 elif force:
1374 inst.rename(name)
1375 return inst
1378def _xother3(inst, other, name=_other_, up=1, **name_other):
1379 '''(INTERNAL) Get C{name} and C{up} for a named C{other}.
1380 '''
1381 if name_other: # and other is None
1382 name, other = _xkwds_item2(name_other)
1383 elif other and len(other) == 1:
1384 name, other = _name__(name), other[0]
1385 else:
1386 raise _AssertionError(name, other, txt=classname(inst, prefixed=True))
1387 return other, name, up
1390def _xotherError(inst, other, name=_other_, up=1):
1391 '''(INTERNAL) Return a C{_TypeError} for an incompatible, named C{other}.
1392 '''
1393 n = _callname(name, classname(inst, prefixed=True), inst.name, up=up + 1)
1394 return _TypeError(name, other, txt=_incompatible(n))
1397def _xvalid(name, underOK=False):
1398 '''(INTERNAL) Check valid attribute name C{name}.
1399 '''
1400 return bool(name and isstr(name)
1401 and name != _name_
1402 and (underOK or not name.startswith(_UNDER_))
1403 and (not iskeyword(name))
1404 and isidentifier(name))
1407__all__ += _ALL_DOCS(_Named,
1408 _NamedBase, # _NamedDict,
1409 _NamedEnum, _NamedEnumItem,
1410 _NamedTuple)
1412# **) MIT License
1413#
1414# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
1415#
1416# Permission is hereby granted, free of charge, to any person obtaining a
1417# copy of this software and associated documentation files (the "Software"),
1418# to deal in the Software without restriction, including without limitation
1419# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1420# and/or sell copies of the Software, and to permit persons to whom the
1421# Software is furnished to do so, subject to the following conditions:
1422#
1423# The above copyright notice and this permission notice shall be included
1424# in all copies or substantial portions of the Software.
1425#
1426# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1427# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1428# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1429# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1430# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1431# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1432# OTHER DEALINGS IN THE SOFTWARE.