Coverage for pygeodesy/named.py: 96%
528 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-09-09 13:03 -0400
« prev ^ index » next coverage.py v7.6.1, created at 2025-09-09 13:03 -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 isbool, isidentifier, iskeyword, isstr, itemsorted, \
17 len2, _xcopy, _xdup, _xinstanceof, _xsubclassof, _zip
18# from pygeodesy.ecef import EcefKarney # _MODS
19from pygeodesy.errors import _AssertionError, _AttributeError, _ImmutableError, \
20 _incompatible, _IndexError, _KeyError, LenError, \
21 _NameError, _NotImplementedError, _TypeError, \
22 _TypesError, _UnexpectedError, UnitError, _ValueError, \
23 _xattr, _xkwds, _xkwds_item2, _xkwds_pop2
24from pygeodesy.internals import _caller3, _envPYGEODESY, _isPyPy, _sizeof, \
25 typename, _under
26from pygeodesy.interns import MISSING, NN, _AT_, _COLON_, _COLONSPACE_, _COMMA_, \
27 _COMMASPACE_, _DNAME_, _doesn_t_exist_, _DOT_, _DUNDER_, \
28 _EQUAL_, _exists_, _name_, _NL_, _NN_, _no_, _other_, \
29 _s_, _SPACE_, _std_, _UNDER_, _vs_
30from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS
31from pygeodesy.props import _allPropertiesOf_n, deprecated_method, _hasProperty, \
32 _update_all, property_doc_, Property_RO, property_RO, \
33 _update_attrs
34from pygeodesy.streprs import attrs, Fmt, lrstrip, pairs, reprs, unstr
35# from pygeodesy.units import _toUnit # _MODS
37__all__ = _ALL_LAZY.named
38__version__ = '25.09.04'
40_COMMANL_ = _COMMA_ + _NL_
41_COMMASPACEDOT_ = _COMMASPACE_ + _DOT_
42_item_ = 'item'
43_MRO_ = 'MRO'
44# __DUNDER gets mangled in class
45_name = _under(_name_)
46_Names_ = '_Names_'
47_registered_ = 'registered' # PYCHOK used!
48_std_NotImplemented = _envPYGEODESY('NOTIMPLEMENTED', NN).lower() == _std_
49_such_ = 'such'
50_Units_ = '_Units_'
51_UP = 2
54class ADict(dict):
55 '''A C{dict} with both key I{and} attribute access to
56 the C{dict} items.
57 '''
58 _iteration = None # Iteration number (C{int}) or C{None}
60 def __getattr__(self, name):
61 '''Get the value of an item by B{C{name}}.
62 '''
63 try:
64 return self[name]
65 except KeyError:
66 if name == _name_:
67 return NN
68 raise self._AttributeError(name)
70 def __repr__(self):
71 '''Default C{repr(self)}.
72 '''
73 return self.toRepr()
75 def __setattr__(self, name, value):
76 '''Set the value of a I{known} item by B{C{name}}.
77 '''
78 try:
79 if self[name] != value:
80 self[name] = value
81 except KeyError:
82 dict.__setattr__(self, name, value)
84 def __str__(self):
85 '''Default C{str(self)}.
86 '''
87 return self.toStr()
89 def _AttributeError(self, name):
90 '''(INTERNAL) Create an C{AttributeError}.
91 '''
92 if _DOT_ not in name: # NOT classname(self)!
93 name = _DOT_(self.typename, name)
94 return _AttributeError(item=name, txt=_doesn_t_exist_)
96 @property_RO
97 def iteration(self): # see .named._NamedBase
98 '''Get the iteration number (C{int}) or
99 C{None} if not available/applicable.
100 '''
101 return self._iteration
103 def set_(self, iteration=None, **items): # PYCHOK signature
104 '''Add one or several new items or replace existing ones.
106 @kwarg iteration: Optional C{iteration} (C{int}).
107 @kwarg items: One or more C{name=value} pairs.
108 '''
109 if iteration is not None:
110 self._iteration = iteration
111 if items:
112 dict.update(self, items)
113 return self # in RhumbLineBase.Intersecant2, _PseudoRhumbLine.Position, ...
115 def _toL(self):
116 '''(INTERNAL) Get items as list.
117 '''
118 return list(_EQUAL_(*t) for t in self.items()) # _kwdstr
120 def toRepr(self, **prec_fmt):
121 '''Like C{repr(dict)} but with C{name} prefix and with
122 C{floats} formatted by function L{pygeodesy.fstr}.
123 '''
124 n = _xattr(self, name=NN) or self.typename
125 return Fmt.PAREN(n, self._toT(_EQUAL_, **prec_fmt))
127 def toStr(self, **prec_fmt):
128 '''Like C{str(dict)} but with C{floats} formatted by
129 function L{pygeodesy.fstr}.
130 '''
131 return Fmt.CURLY(self._toT(_COLONSPACE_, **prec_fmt))
133 def _toT(self, sep, **kwds):
134 '''(INTERNAL) Helper for C{.toRepr} and C{.toStr}.
135 '''
136 kwds = _xkwds(kwds, prec=6, fmt=Fmt.F, sep=sep)
137 return _COMMASPACE_.join(pairs(itemsorted(self), **kwds))
139 @property_RO
140 def typename(self):
141 '''Get this C{ADict}'s type name (C{str}).
142 '''
143 return type(self).__name__ # typename(type(self))
146class _Named(object):
147 '''(INTERNAL) Root class for named objects.
148 '''
149 _iteration = None # iteration number (C{int}) or C{None}
150 _name = NN # name (C{str})
151 _classnaming = False # prefixed (C{bool})
152# _updates = 0 # OBSOLETE Property/property updates
154 def __format__(self, fmt): # PYCHOK no cover
155 '''Not implemented.'''
156 return _NotImplemented(self, fmt)
158 def __imatmul__(self, other): # PYCHOK no cover
159 '''Not implemented.'''
160 return _NotImplemented(self, other) # PYCHOK Python 3.5+
162 def __matmul__(self, other): # PYCHOK no cover
163 '''Not implemented.'''
164 return _NotImplemented(self, other) # PYCHOK Python 3.5+
166 def __repr__(self):
167 '''Default C{repr(self)}.
168 '''
169 return Fmt.repr_at(self)
171 def __rmatmul__(self, other): # PYCHOK no cover
172 '''Not implemented.'''
173 return _NotImplemented(self, other) # PYCHOK Python 3.5+
175 def __str__(self):
176 '''Default C{str(self)}.
177 '''
178 return self.named2
180 def attrs(self, *names, **sep_Nones_pairs_kwds):
181 '''Join named attributes as I{name=value} strings, with C{float}s formatted by
182 function L{pygeodesy.fstr}.
184 @arg names: The attribute names, all positional (C{str}).
185 @kwarg sep_Nones_pairs_kwds: Keyword arguments for function L{pygeodesy.pairs},
186 except C{B{sep}=", "} and C{B{Nones}=True} to in-/exclude missing
187 or C{None}-valued attributes.
189 @return: All C{name=value} pairs, joined by B{C{sep}} (C{str}).
191 @see: Functions L{pygeodesy.attrs}, L{pygeodesy.pairs} and L{pygeodesy.fstr}
192 '''
193 sep, kwds = _xkwds_pop2(sep_Nones_pairs_kwds, sep=_COMMASPACE_)
194 return sep.join(attrs(self, *names, **kwds))
196 @Property_RO
197 def classname(self):
198 '''Get this object's C{[module.]class} name (C{str}), see
199 property C{.classnaming} and function C{classnaming}.
200 '''
201 return classname(self, prefixed=self._classnaming)
203 @property_doc_(''' the class naming (C{bool}).''')
204 def classnaming(self):
205 '''Get the class naming (C{bool}), see function C{classnaming}.
206 '''
207 return self._classnaming
209 @classnaming.setter # PYCHOK setter!
210 def classnaming(self, prefixed):
211 '''Set the class naming for C{[module.].class} names (C{bool})
212 to C{True} to include the module name.
213 '''
214 b = bool(prefixed)
215 if self._classnaming != b:
216 self._classnaming = b
217 _update_attrs(self, *_Named_Property_ROs)
219 def classof(self, *args, **kwds):
220 '''Create another instance of this very class.
222 @arg args: Optional, positional arguments.
223 @kwarg kwds: Optional, keyword arguments.
225 @return: New instance (B{self.__class__}).
226 '''
227 return _xnamed(self.__class__(*args, **kwds), self.name)
229 def copy(self, deep=False, **name):
230 '''Make a shallow or deep copy of this instance.
232 @kwarg deep: If C{True}, make a deep, otherwise a shallow
233 copy (C{bool}).
234 @kwarg name: Optional, non-empty C{B{name}=NN} (C{str}).
236 @return: The copy (C{This class}).
237 '''
238 c = _xcopy(self, deep=deep)
239 if name:
240 _ = c.rename(name)
241 return c
243 def _DOT_(self, *names):
244 '''(INTERNAL) Period-join C{self.name} and C{names}.
245 '''
246 return _DOT_(self.name, *names)
248 def dup(self, deep=False, **items):
249 '''Duplicate this instance, replacing some attributes.
251 @kwarg deep: If C{True}, duplicate deep, otherwise shallow
252 (C{bool}).
253 @kwarg items: Attributes to be changed (C{any}), including
254 optional C{B{name}} (C{str}).
256 @return: The duplicate (C{This class}).
258 @raise AttributeError: Some B{C{items}} invalid.
259 '''
260 n = self.name
261 m, items = _xkwds_pop2(items, name=n)
262 d = _xdup(self, deep=deep, **items)
263 if m != n:
264 d.rename(m) # zap _Named_Property_ROs
265# if items:
266# _update_all(d)
267 return d
269 def _instr(self, *attrs, **fmt_prec_props_sep_name__kwds):
270 '''(INTERNAL) Format, used by C{Conic}, C{Ellipsoid}, C{Geodesic...}, C{Transform}, C{Triaxial}.
271 '''
272 def _fmt_prec_props_kwds(fmt=Fmt.F, prec=6, props=(), sep=_COMMASPACE_, **kwds):
273 return fmt, prec, props, sep, kwds
275 name, kwds = _name2__(**fmt_prec_props_sep_name__kwds)
276 fmt, prec, props, sep, kwds = _fmt_prec_props_kwds(**kwds)
278 t = () if name is None else (Fmt.EQUAL(name=repr(name or self.name)),)
279 if attrs:
280 t += pairs(((a, getattr(self, a)) for a in attrs),
281 prec=prec, ints=True, fmt=fmt)
282 if props:
283 t += pairs(((p.name, getattr(self, p.name)) for p in props),
284 prec=prec, ints=True)
285 if kwds:
286 t += pairs(kwds, prec=prec)
287 return sep.join(t) if sep else t
289 @property_RO
290 def iteration(self): # see .karney.GDict
291 '''Get the most recent iteration number (C{int}) or C{None}
292 if not available or not applicable.
294 @note: The interation number may be an aggregate number over
295 several, nested functions.
296 '''
297 return self._iteration
299 def methodname(self, which):
300 '''Get a method C{[module.]class.method} name of this object (C{str}).
302 @arg which: The method (C{callable}).
303 '''
304 return _DOT_(self.classname, typename(which) if callable(which) else _NN_)
306 @property_doc_(''' the name (C{str}).''')
307 def name(self):
308 '''Get the name (C{str}).
309 '''
310 return self._name
312 @name.setter # PYCHOK setter!
313 def name(self, name):
314 '''Set the C{B{name}=NN} (C{str}).
316 @raise NameError: Can't rename, use method L{rename}.
317 '''
318 m, n = self._name, _name__(name)
319 if not m:
320 self._name = n
321 elif n != m:
322 n = repr(n)
323 c = self.classname
324 t = _DOT_(c, Fmt.PAREN(typename(self.rename), n))
325 n = _DOT_(c, Fmt.EQUALSPACED(name=n))
326 m = Fmt.PAREN(_SPACE_('was', repr(m)))
327 n = _SPACE_(n, m)
328 raise _NameError(n, txt=_SPACE_('use', t))
329 # to set the name from a sub-class, use
330 # self.name = name or
331 # _Named.name.fset(self, name), but NOT
332 # _Named(self).name = name
334 def _name__(self, name): # usually **name
335 '''(INTERNAL) Get the C{name} or this C{name}.
336 '''
337 return _name__(name, _or_nameof=self) # nameof(self)
339 def _name1__(self, kwds):
340 '''(INTERNAL) Resolve and set the C{B{name}=NN}.
341 '''
342 return _name1__(kwds, _or_nameof=self.name) if self.name else kwds
344 @Property_RO
345 def named(self):
346 '''Get the name I{or} class name or C{""} (C{str}).
347 '''
348 return self.name or self.classname
350# @Property_RO
351# def named_(self):
352# '''Get the C{class} name I{and/or} the str(name) or C{""} (C{str}).
353# '''
354# return _xjoined_(self.classname, self.name, enquote=False)
356 @Property_RO
357 def named2(self):
358 '''Get the C{class} name I{and/or} the repr(name) or C{""} (C{str}).
359 '''
360 return _xjoined_(self.classname, self.name)
362 @Property_RO
363 def named3(self):
364 '''Get the I{prefixed} C{class} name I{and/or} the name or C{""} (C{str}).
365 '''
366 return _xjoined_(classname(self, prefixed=True), self.name)
368 @Property_RO
369 def named4(self):
370 '''Get the C{package.module.class} name I{and/or} the name or C{""} (C{str}).
371 '''
372 return _xjoined_(_DOT_(self.__module__, self.typename), self.name)
374 def _notImplemented(self, *args, **kwds):
375 '''(INTERNAL) See function L{notImplemented}.
376 '''
377 notImplemented(self, *args, **_xkwds(kwds, up=_UP + 1))
379 def _notOverloaded(self, *args, **kwds):
380 '''(INTERNAL) See function L{notOverloaded}.
381 '''
382 notOverloaded(self, *args, **_xkwds(kwds, up=_UP + 1))
384 def rename(self, name):
385 '''Change the name.
387 @arg name: The new name (C{str}).
389 @return: The previous name (C{str}).
390 '''
391 m, n = self._name, _name__(name)
392 if n != m:
393 self._name = n
394 _update_attrs(self, *_Named_Property_ROs)
395 return m
397 def renamed(self, name):
398 '''Change the name.
400 @arg name: The new name (C{str}).
402 @return: This instance (C{str}).
403 '''
404 _ = self.rename(name)
405 return self
407 @property_RO
408 def sizeof(self):
409 '''Get the current size in C{bytes} of this instance (C{int}).
410 '''
411 return _sizeof(self)
413 def toRepr(self, **unused): # PYCHOK no cover
414 '''Default C{repr(self)}.
415 '''
416 return repr(self)
418 def toStr(self, **unused): # PYCHOK no cover
419 '''Default C{str(self)}.
420 '''
421 return str(self)
423 @deprecated_method
424 def toStr2(self, **kwds): # PYCHOK no cover
425 '''DEPRECATED on 23.10.07, use method C{toRepr}.'''
426 return self.toRepr(**kwds)
428# def _unstr(self, which, *args, **kwds):
429# '''(INTERNAL) Return the string representation of a method
430# invokation of this instance: C{str(self).method(...)}
431#
432# @see: Function L{pygeodesy.unstr}.
433# '''
434# return _DOT_(self, unstr(which, *args, **kwds))
436 @property_RO
437 def typename(self):
438 '''Get this object's type name (C{str}).
439 '''
440 return type(self).__name__ # typename(type(self))
442 def _xnamed(self, inst, name=NN, **force):
443 '''(INTERNAL) Set the instance' C{.name = self.name}.
445 @arg inst: The instance (C{_Named}).
446 @kwarg name: The name (C{str}).
447 @kwarg force: If C{True}, force rename (C{bool}).
449 @return: The B{C{inst}}, renamed if B{C{force}}d
450 or if not named before.
451 '''
452 return _xnamed(inst, name, **force)
454 def _xrenamed(self, inst):
455 '''(INTERNAL) Rename the instance' C{.name = self.name}.
457 @arg inst: The instance (C{_Named}).
459 @return: The B{C{inst}}, named if not named before.
461 @raise TypeError: Not C{isinstance(B{inst}, _Named)}.
462 '''
463 _xinstanceof(_Named, inst=inst) # assert
464 return inst.renamed(self.name)
466_Named_Property_ROs = _allPropertiesOf_n(5, _Named, Property_RO) # PYCHOK once
469class _NamedBase(_Named):
470 '''(INTERNAL) Base class with name.
471 '''
472 def __repr__(self):
473 '''Default C{repr(self)}.
474 '''
475 return self.toRepr()
477 def __str__(self):
478 '''Default C{str(self)}.
479 '''
480 return self.toStr()
482 def others(self, *other, **name_other_up):
483 '''Refined class comparison, invoked as C{.others(other)},
484 C{.others(name=other)} or C{.others(other, name='other')}.
486 @arg other: The other instance (any C{type}).
487 @kwarg name_other_up: Overriding C{name=other} and C{up=1}
488 keyword arguments.
490 @return: The B{C{other}} iff compatible with this instance's
491 C{class} or C{type}.
493 @raise TypeError: Mismatch of the B{C{other}} and this
494 instance's C{class} or C{type}.
495 '''
496 if other: # most common, just one arg B{C{other}}
497 other0 = other[0]
498 if isinstance(other0, self.__class__) or \
499 isinstance(self, other0.__class__):
500 return other0
502 other, name, up = _xother3(self, other, **name_other_up)
503 if isinstance(self, other.__class__) or \
504 isinstance(other, self.__class__):
505 return _xnamed(other, name)
507 raise _xotherError(self, other, name=name, up=up + 1)
509 def toRepr(self, **kwds): # PYCHOK expected
510 '''(INTERNAL) I{Could be overloaded}.
512 @kwarg kwds: Optional, C{toStr} keyword arguments.
514 @return: C{toStr}() with keyword arguments (as C{str}).
515 '''
516 t = lrstrip(self.toStr(**kwds))
517# if self.name:
518# t = NN(Fmt.EQUAL(name=repr(self.name)), sep, t)
519 return Fmt.PAREN(self.classname, t) # XXX (self.named, t)
521# def toRepr(self, **kwds)
522# if kwds:
523# s = NN.join(reprs((self,), **kwds))
524# else: # super().__repr__ only for Python 3+
525# s = super(self.__class__, self).__repr__()
526# return Fmt.PAREN(self.named, s) # clips(s)
528 def toStr(self, **kwds): # PYCHOK no cover
529 '''I{Must be overloaded}.'''
530 notOverloaded(self, **kwds)
532# def toStr(self, **kwds):
533# if kwds:
534# s = NN.join(strs((self,), **kwds))
535# else: # super().__str__ only for Python 3+
536# s = super(self.__class__, self).__str__()
537# return s
539 def _update(self, updated, *attrs, **setters):
540 '''(INTERNAL) Zap cached instance attributes and overwrite C{__dict__} or L{Property_RO} values.
541 '''
542 u = _update_all(self, *attrs) if updated else 0
543 if setters:
544 d = self.__dict__
545 # double-check that setters are Property_RO's
546 for n, v in setters.items():
547 if n in d or _hasProperty(self, n, Property_RO):
548 d[n] = v
549 else:
550 raise _AssertionError(n, v, txt=repr(self))
551 u += len(setters)
552 return u
555class _NamedDict(ADict, _Named):
556 '''(INTERNAL) Named C{dict} with key I{and} attribute access
557 to the items.
558 '''
559 def __init__(self, *args, **kwds):
560 if args: # is args[0] a dict?
561 if len(args) != 1: # or not isinstance(args[0], dict)
562 kwds = _name1__(kwds)
563 t = unstr(self.classname, *args, **kwds) # PYCHOK no cover
564 raise _ValueError(args=len(args), txt=t)
565 kwds = _xkwds(dict(args[0]), **kwds) # args[0] overrides kwds
566 n, kwds = _name2__(**kwds)
567 if n:
568 _Named.name.fset(self, n) # see _Named.name
569 ADict.__init__(self, kwds)
571 def __delattr__(self, name):
572 '''Delete an attribute or item by B{C{name}}.
573 '''
574 if name in self: # in ADict.keys(self):
575 ADict.pop(self, name)
576 elif name in (_name_, _name):
577 # ADict.__setattr__(self, name, NN)
578 _Named.rename(self, NN)
579 else:
580 ADict.__delattr__(self, name)
582 def __getattr__(self, name):
583 '''Get the value of an item by B{C{name}}.
584 '''
585 try:
586 return self[name]
587 except KeyError:
588 if name == _name_:
589 return _Named.name.fget(self)
590 raise ADict._AttributeError(self, self._DOT_(name))
592 def __getitem__(self, key):
593 '''Get the value of an item by B{C{key}}.
594 '''
595 if key == _name_:
596 raise self._KeyError(key)
597 return ADict.__getitem__(self, key)
599 def _KeyError(self, key, *value): # PYCHOK no cover
600 '''(INTERNAL) Create a C{KeyError}.
601 '''
602 n = self.name or self.typename
603 t = Fmt.SQUARE(n, key)
604 if value:
605 t = Fmt.EQUALSPACED(t, *value)
606 return _KeyError(t)
608 def __setattr__(self, name, value):
609 '''Set attribute or item B{C{name}} to B{C{value}}.
610 '''
611 if name in self: # in ADict.keys(self)
612 ADict.__setitem__(self, name, value) # self[name] = value
613 else:
614 ADict.__setattr__(self, name, value)
616 def __setitem__(self, key, value):
617 '''Set item B{C{key}} to B{C{value}}.
618 '''
619 if key == _name_:
620 raise self._KeyError(key, repr(value))
621 ADict.__setitem__(self, key, value)
624class _NamedEnum(_NamedDict):
625 '''(INTERNAL) Enum-like C{_NamedDict} with attribute access
626 restricted to valid keys.
627 '''
628 _item_Classes = ()
630 def __init__(self, Class, *Classes, **name):
631 '''New C{_NamedEnum}.
633 @arg Class: Initial class or type acceptable as items
634 values (C{type}).
635 @arg Classes: Additional, acceptable classes or C{type}s.
636 '''
637 self._item_Classes = (Class,) + Classes
638 n = _name__(**name) or NN(typename(Class), _s_)
639 if n and _xvalid(n, underOK=True):
640 _Named.name.fset(self, n) # see _Named.name
642 def __getattr__(self, name):
643 '''Get the value of an attribute or item by B{C{name}}.
644 '''
645 return _NamedDict.__getattr__(self, name)
647 def __repr__(self):
648 '''Default C{repr(self)}.
649 '''
650 return self.toRepr()
652 def __str__(self):
653 '''Default C{str(self)}.
654 '''
655 return self.toStr()
657 def _assert(self, **kwds):
658 '''(INTERNAL) Check attribute name against given, registered name.
659 '''
660 pypy = _isPyPy()
661 _isa = isinstance
662 for n, v in kwds.items():
663 if _isa(v, _LazyNamedEnumItem): # property
664 assert (n == v.name) if pypy else (n is v.name)
665 # assert not hasattr(self.__class__, n)
666 setattr(self.__class__, n, v)
667 elif _isa(v, self._item_Classes): # PYCHOK no cover
668 assert self[n] is v and getattr(self, n) \
669 and self.find(v) == n
670 else:
671 raise _TypeError(v, name=n)
673 def find(self, item, dflt=None, all=False):
674 '''Find a registered item.
676 @arg item: The item to look for (any C{type}).
677 @kwarg dflt: Value to return if not found (any C{type}).
678 @kwarg all: Use C{True} to search I{all} items or C{False} only
679 the currently I{registered} ones (C{bool}).
681 @return: The B{C{item}}'s name if found (C{str}), or C{{dflt}}
682 if there is no such B{C{item}}.
683 '''
684 for k, v in self.items(all=all): # or ADict.items(self)
685 if v is item:
686 return k
687 return dflt
689 def get(self, name, dflt=None):
690 '''Get the value of a I{registered} item.
692 @arg name: The name of the item (C{str}).
693 @kwarg dflt: Value to return (any C{type}).
695 @return: The item with B{C{name}} if found, or B{C{dflt}} if
696 there is no I{registered} item with that B{C{name}}.
697 '''
698 # getattr needed to instantiate L{_LazyNamedEnumItem}
699 return getattr(self, name, dflt)
701 def items(self, all=False, asorted=False):
702 '''Yield all or only the I{registered} items.
704 @kwarg all: Use C{True} to yield I{all} items or C{False} for
705 only the currently I{registered} ones (C{bool}).
706 @kwarg asorted: If C{True}, yield the items in I{alphabetical,
707 case-insensitive} order (C{bool}).
708 '''
709 if all: # instantiate any remaining L{_LazyNamedEnumItem}
710 _isa = isinstance
711 for n, p in tuple(type(self).__dict__.items()):
712 if _isa(p, _LazyNamedEnumItem):
713 _ = getattr(self, n)
714 return itemsorted(self) if asorted else ADict.items(self)
716 def keys(self, **all_asorted):
717 '''Yield the name (C{str}) of I{all} or only the currently I{registered}
718 items, optionally sorted I{alphabetically, case-insensitively}.
720 @kwarg all_asorted: See method C{items}.
721 '''
722 for k, _ in self.items(**all_asorted):
723 yield k
725 def popitem(self):
726 '''Remove I{an, any} currently I{registed} item.
728 @return: The removed item.
729 '''
730 return self._zapitem(*ADict.popitem(self))
732 def register(self, item):
733 '''Registed one new item or I{all} or I{any} unregistered ones.
735 @arg item: The item (any C{type}) or B{I{all}} or B{C{any}}.
737 @return: The item name (C{str}) or C("all") or C{"any"}.
739 @raise NameError: An B{C{item}} with that name is already
740 registered the B{C{item}} has no or an
741 invalid name.
743 @raise TypeError: The B{C{item}} type invalid.
744 '''
745 if item is all or item is any:
746 _ = self.items(all=True)
747 n = typename(item)
748 else:
749 try:
750 n = item.name
751 if not (n and isstr(n) and isidentifier(n)):
752 raise ValueError()
753 except (AttributeError, ValueError, TypeError) as x:
754 n = _DOT_(_item_, _name_)
755 raise _NameError(n, item, cause=x)
756 if n in self:
757 t = _SPACE_(_item_, self._DOT_(n), _exists_)
758 raise _NameError(t, txt=repr(item))
759 if not isinstance(item, self._item_Classes): # _xinstanceof
760 n = self._DOT_(n)
761 raise _TypesError(n, item, *self._item_Classes)
762 self[n] = item
763 return n
765 def unregister(self, name_or_item):
766 '''Remove a I{registered} item.
768 @arg name_or_item: Name (C{str}) or the item (any C{type}).
770 @return: The unregistered item.
772 @raise AttributeError: No such B{C{item}}.
774 @raise NameError: No item with that B{C{name}}.
775 '''
776 if isstr(name_or_item):
777 name = name_or_item
778 else:
779 name = self.find(name_or_item, dflt=MISSING) # all=True?
780 if name is MISSING:
781 t = _SPACE_(_no_, _such_, self.classname, _item_)
782 raise _AttributeError(t, txt=repr(name_or_item))
783 try:
784 item = ADict.pop(self, name)
785 except KeyError:
786 raise _NameError(item=self._DOT_(name), txt=_doesn_t_exist_)
787 return self._zapitem(name, item)
789 pop = unregister
791 def toRepr(self, prec=6, fmt=Fmt.F, sep=_COMMANL_, **all_asorted): # PYCHOK _NamedDict, ADict
792 '''Like C{repr(dict)} but C{name}s optionally sorted and
793 C{floats} formatted by function L{pygeodesy.fstr}.
794 '''
795 t = ((self._DOT_(n), v) for n, v in self.items(**all_asorted))
796 return sep.join(pairs(t, prec=prec, fmt=fmt, sep=_COLONSPACE_))
798 def toStr(self, *unused, **all_asorted): # PYCHOK _NamedDict, ADict
799 '''Return a string with all C{name}s, optionally sorted.
800 '''
801 return self._DOT_(_COMMASPACEDOT_.join(self.keys(**all_asorted)))
803 def values(self, **all_asorted):
804 '''Yield the value (C{type}) of all or only the I{registered} items,
805 optionally sorted I{alphabetically} and I{case-insensitively}.
807 @kwarg all_asorted: See method C{items}.
808 '''
809 for _, v in self.items(**all_asorted):
810 yield v
812 def _zapitem(self, name, item):
813 # remove _LazyNamedEnumItem property value if still present
814 if self.__dict__.get(name, None) is item:
815 self.__dict__.pop(name) # [name] = None
816 item._enum = None
817 return item
820class _LazyNamedEnumItem(property_RO): # XXX or descriptor?
821 '''(INTERNAL) Lazily instantiated L{_NamedEnumItem}.
822 '''
823 pass
826def _lazyNamedEnumItem(name, *args, **kwds):
827 '''(INTERNAL) L{_LazyNamedEnumItem} property-like factory.
829 @see: Luciano Ramalho, "Fluent Python", O'Reilly, Example
830 19-24, 2016 p. 636 or Example 22-28, 2022 p. 869+
831 '''
832 def _fget(inst):
833 # assert isinstance(inst, _NamedEnum)
834 try: # get the item from the instance' __dict__
835 # item = inst.__dict__[name] # ... or ADict
836 item = inst[name]
837 except KeyError:
838 # instantiate an _NamedEnumItem, it self-registers
839 item = inst._Lazy(*args, **_xkwds(kwds, name=name))
840 # assert inst[name] is item # MUST be registered
841 # store the item in the instance' __dict__ ...
842 # inst.__dict__[name] = item # ... or update the
843 inst.update({name: item}) # ... ADict for Triaxials
844 # remove the property from the registry class, such that
845 # (a) the property no longer overrides the instance' item
846 # in inst.__dict__ and (b) _NamedEnum.items(all=True) only
847 # sees any un-instantiated ones yet to be instantiated
848 p = getattr(inst.__class__, name, None)
849 if isinstance(p, _LazyNamedEnumItem):
850 delattr(inst.__class__, name)
851 # assert isinstance(item, _NamedEnumItem)
852 return item
854 p = _LazyNamedEnumItem(_fget)
855 p.name = name
856 return p
859class _NamedEnumItem(_NamedBase):
860 '''(INTERNAL) Base class for items in a C{_NamedEnum} registery.
861 '''
862 _enum = None
864# def __ne__(self, other): # XXX fails for Lcc.conic = conic!
865# '''Compare this and an other item.
866#
867# @return: C{True} if different, C{False} otherwise.
868# '''
869# return not self.__eq__(other)
871 @property_doc_(''' the I{registered} name (C{str}).''')
872 def name(self):
873 '''Get the I{registered} name (C{str}).
874 '''
875 return self._name
877 @name.setter # PYCHOK setter!
878 def name(self, name):
879 '''Set the name, unless already registered (C{str}).
880 '''
881 name = _name__(name) or _NN_
882 if self._enum:
883 raise _NameError(name, self, txt=_registered_) # _TypeError
884 if name:
885 self._name = name
887 def _register(self, enum, name):
888 '''(INTERNAL) Add this item as B{C{enum.name}}.
890 @note: Don't register if name is empty or doesn't
891 start with a letter.
892 '''
893 name = _name__(name)
894 if name and _xvalid(name, underOK=True):
895 self.name = name
896 if name[:1].isalpha(): # '_...' not registered
897 enum.register(self)
898 self._enum = enum
900 def unregister(self):
901 '''Remove this instance from its C{_NamedEnum} registry.
903 @raise AssertionError: Mismatch of this and registered item.
905 @raise NameError: This item is unregistered.
906 '''
907 enum = self._enum
908 if enum and self.name and self.name in enum:
909 item = enum.unregister(self.name)
910 if item is not self: # PYCHOK no cover
911 t = _SPACE_(repr(item), _vs_, repr(self))
912 raise _AssertionError(t)
915# from pygeodesy.props import _NamedProperty
918class _NamedTuple(tuple, _Named):
919 '''(INTERNAL) Base for named C{tuple}s with both index I{and}
920 attribute name access to the items.
922 @note: This class is similar to Python's C{namedtuple},
923 but statically defined, lighter and limited.
924 '''
925 _Names_ = () # item names, non-identifier, no leading underscore
926 '''Tuple specifying the C{name} of each C{Named-Tuple} item.
928 @note: Specify at least 2 item names.
929 '''
930 _Units_ = () # .units classes
931 '''Tuple defining the C{units} of the value of each C{Named-Tuple} item.
933 @note: The C{len(_Units_)} must match C{len(_Names_)}.
934 '''
935 _validated = False # set to True I{per sub-class!}
937 def __new__(cls, arg, *args, **iteration_name):
938 '''New L{_NamedTuple} initialized with B{C{positional}} arguments.
940 @arg arg: Tuple items (C{tuple}, C{list}, ...) or first tuple
941 item of several more in other positional arguments.
942 @arg args: Tuple items (C{any}), all positional arguments.
943 @kwarg iteration_name: Only keyword arguments C{B{iteration}=None}
944 and C{B{name}=NN} are used, any other are
945 I{silently} ignored.
947 @raise LenError: Unequal number of positional arguments and
948 number of item C{_Names_} or C{_Units_}.
950 @raise TypeError: The C{_Names_} or C{_Units_} attribute is
951 not a C{tuple} of at least 2 items.
953 @raise ValueError: Item name is not a C{str} or valid C{identifier}
954 or starts with C{underscore}.
955 '''
956 n, args = len2(((arg,) + args) if args else arg)
957 self = tuple.__new__(cls, args)
958 if not self._validated:
959 self._validate()
961 N = len(self._Names_)
962 if n != N:
963 raise LenError(self.__class__, args=n, _Names_=N)
965 if iteration_name:
966 i, name = _xkwds_pop2(iteration_name, iteration=None)
967 if i is not None:
968 self._iteration = i
969 if name:
970 self.name = name
971 return self
973 def __delattr__(self, name):
974 '''Delete an attribute by B{C{name}}.
976 @note: Items can not be deleted.
977 '''
978 if name in self._Names_:
979 raise _ImmutableError(self, name) # _del_
980 elif name in (_name_, _name):
981 _Named.__setattr__(self, name, NN) # XXX _Named.name.fset(self, NN)
982 else:
983 tuple.__delattr__(self, name)
985 def __getattr__(self, name):
986 '''Get the value of an attribute or item by B{C{name}}.
987 '''
988 try:
989 return tuple.__getitem__(self, self._Names_.index(name))
990 except IndexError as x:
991 raise _IndexError(self._DOT_(name), cause=x)
992 except ValueError: # e.g. _iteration
993 return tuple.__getattr__(self, name) # __getattribute__
995# def __getitem__(self, index): # index, slice, etc.
996# '''Get the item(s) at an B{C{index}} or slice.
997# '''
998# return tuple.__getitem__(self, index)
1000 def __hash__(self):
1001 return tuple.__hash__(self)
1003 def __repr__(self):
1004 '''Default C{repr(self)}.
1005 '''
1006 return self.toRepr()
1008 def __setattr__(self, name, value):
1009 '''Set attribute or item B{C{name}} to B{C{value}}.
1010 '''
1011 if name in self._Names_:
1012 raise _ImmutableError(self, name, value)
1013 elif name in (_name_, _name):
1014 _Named.__setattr__(self, name, value) # XXX _Named.name.fset(self, value)
1015 else: # e.g. _iteration
1016 tuple.__setattr__(self, name, value)
1018 def __str__(self):
1019 '''Default C{repr(self)}.
1020 '''
1021 return self.toStr()
1023 def _DOT_(self, *names):
1024 '''(INTERNAL) Period-join C{self.classname} and C{names}.
1025 '''
1026 return _DOT_(self.classname, *names)
1028 def dup(self, name=NN, **items):
1029 '''Duplicate this tuple replacing one or more items.
1031 @kwarg name: Optional new name (C{str}).
1032 @kwarg items: Items to be replaced (C{name=value} pairs), if any.
1034 @return: A copy of this tuple with B{C{items}}.
1036 @raise NameError: Some B{C{items}} invalid.
1037 '''
1038 t = list(self)
1039 U = self._Units_
1040 if items:
1041 _ix = self._Names_.index
1042 _2U = _MODS.units._toUnit
1043 try:
1044 for n, v in items.items():
1045 i = _ix(n)
1046 t[i] = _2U(U[i], v, name=n)
1047 except ValueError: # bad item name
1048 raise _NameError(self._DOT_(n), v, this=self)
1049 return self.classof(*t).reUnit(*U, name=name)
1051 def items(self):
1052 '''Yield the items, each as a C{(name, value)} pair (C{2-tuple}).
1054 @see: Method C{.units}.
1055 '''
1056 for n, v in _zip(self._Names_, self): # strict=True
1057 yield n, v
1059 iteritems = items
1061 def reUnit(self, *Units, **name):
1062 '''Replace some of this C{Named-Tuple}'s C{Units}.
1064 @arg Units: One or more C{Unit} classes, all positional.
1065 @kwarg name: Optional C{B{name}=NN} (C{str}).
1067 @return: This instance with updated C{Units}.
1069 @note: This C{Named-Tuple}'s values are I{not updated}.
1070 '''
1071 U = self._Units_
1072 n = min(len(U), len(Units))
1073 if n:
1074 R = Units + U[n:]
1075 if R != U:
1076 self._Units_ = R
1077 return self.renamed(name) if name else self
1079 def toRepr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1080 '''Return this C{Named-Tuple} items as C{name=value} string(s).
1082 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1083 Trailing zero decimals are stripped for B{C{prec}} values
1084 of 1 and above, but kept for negative B{C{prec}} values.
1085 @kwarg sep: Separator to join (C{str}).
1086 @kwarg fmt: Optional C{float} format (C{letter}).
1088 @return: Tuple items (C{str}).
1089 '''
1090 t = pairs(self.items(), prec=prec, fmt=fmt)
1091# if self.name:
1092# t = (Fmt.EQUAL(name=repr(self.name)),) + t
1093 return Fmt.PAREN(self.named, sep.join(t)) # XXX (self.classname, sep.join(t))
1095 def toStr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1096 '''Return this C{Named-Tuple} items as string(s).
1098 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1099 Trailing zero decimals are stripped for B{C{prec}} values
1100 of 1 and above, but kept for negative B{C{prec}} values.
1101 @kwarg sep: Separator to join (C{str}).
1102 @kwarg fmt: Optional C{float} format (C{letter}).
1104 @return: Tuple items (C{str}).
1105 '''
1106 return Fmt.PAREN(sep.join(reprs(self, prec=prec, fmt=fmt)))
1108 def toUnits(self, Error=UnitError, **name): # overloaded in .frechet, .hausdorff
1109 '''Return a copy of this C{Named-Tuple} with each item value wrapped
1110 as an instance of its L{units} class.
1112 @kwarg Error: Error to raise for L{units} issues (C{UnitError}).
1113 @kwarg name: Optional C{B{name}=NN} (C{str}).
1115 @return: A duplicate of this C{Named-Tuple} (C{C{Named-Tuple}}).
1117 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1118 '''
1119 t = tuple(v for _, v in self.units(Error=Error))
1120 return self.classof(*t).reUnit(*self._Units_, **name)
1122 def units(self, **Error):
1123 '''Yield the items, each as a C{2-tuple (name, value}) with the
1124 value wrapped as an instance of its L{units} class.
1126 @kwarg Error: Optional C{B{Error}=UnitError} to raise.
1128 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1130 @see: Method C{.items}.
1131 '''
1132 _2U = _MODS.units._toUnit
1133 for n, v, U in _zip(self._Names_, self, self._Units_): # strict=True
1134 yield n, _2U(U, v, name=n, **Error)
1136 iterunits = units
1138 def _validate(self, underOK=False): # see .EcefMatrix
1139 '''(INTERNAL) One-time check of C{_Names_} and C{_Units_}
1140 for each C{_NamedUnit} I{sub-class separately}.
1141 '''
1142 ns = self._Names_
1143 if not (isinstance(ns, tuple) and len(ns) > 1): # XXX > 0
1144 raise _TypeError(self._DOT_(_Names_), ns)
1145 for i, n in enumerate(ns):
1146 if not _xvalid(n, underOK=underOK):
1147 t = Fmt.SQUARE(_Names_=i) # PYCHOK no cover
1148 raise _ValueError(self._DOT_(t), n)
1150 us = self._Units_
1151 if not isinstance(us, tuple):
1152 raise _TypeError(self._DOT_(_Units_), us)
1153 if len(us) != len(ns):
1154 raise LenError(self.__class__, _Units_=len(us), _Names_=len(ns))
1155 for i, u in enumerate(us):
1156 if not (u is None or callable(u)):
1157 t = Fmt.SQUARE(_Units_=i) # PYCHOK no cover
1158 raise _TypeError(self._DOT_(t), u)
1160 type(self)._validated = True
1162 def _xtend(self, xTuple, *items, **name):
1163 '''(INTERNAL) Extend this C{Named-Tuple} with C{items} to an other B{C{xTuple}}.
1164 '''
1165 _xsubclassof(_NamedTuple, xTuple=xTuple)
1166 if len(xTuple._Names_) != (len(self._Names_) + len(items)) or \
1167 xTuple._Names_[:len(self)] != self._Names_: # PYCHOK no cover
1168 c = NN(self.classname, repr(self._Names_))
1169 x = NN(typename(xTuple), repr(xTuple._Names_))
1170 raise TypeError(_SPACE_(c, _vs_, x))
1171 t = self + items
1172 return xTuple(t, name=self._name__(name)) # .reUnit(*self._Units_)
1175def callername(up=1, dflt=NN, source=False, underOK=False):
1176 '''Get the name of the invoking callable.
1178 @kwarg up: Number of call stack frames up (C{int}).
1179 @kwarg dflt: Default return value (C{any}).
1180 @kwarg source: Include source file name and line number (C{bool}).
1181 @kwarg underOK: If C{True}, private, internal callables are OK,
1182 otherwise private callables are skipped (C{bool}).
1184 @return: The callable name (C{str}) or B{C{dflt}} if none found.
1185 '''
1186 try: # see .lazily._caller3
1187 for u in range(up, up + 32):
1188 n, f, s = _caller3(u)
1189 if n and (underOK or n.startswith(_DUNDER_) or
1190 not n.startswith(_UNDER_)):
1191 if source:
1192 n = NN(n, _AT_, f, _COLON_, str(s))
1193 return n
1194 except (AttributeError, ValueError):
1195 pass
1196 return dflt
1199def _callername2(args, callername=NN, source=False, underOK=False, up=_UP, **kwds):
1200 '''(INTERNAL) Extract C{callername}, C{source}, C{underOK} and C{up} from C{kwds}.
1201 '''
1202 n = callername or _MODS.named.callername(up=up + 1, source=source,
1203 underOK=underOK or bool(args or kwds))
1204 return n, kwds
1207def _callname(name, class_name, self_name, up=1):
1208 '''(INTERNAL) Assemble the name for an invokation.
1209 '''
1210 n, c = class_name, callername(up=up + 1)
1211 if c:
1212 n = _DOT_(n, Fmt.PAREN(c, name))
1213 if self_name:
1214 n = _SPACE_(n, repr(self_name))
1215 return n
1218def classname(inst, prefixed=None):
1219 '''Return the instance' class name optionally prefixed with the
1220 module name.
1222 @arg inst: The object (any C{type}).
1223 @kwarg prefixed: Include the module name (C{bool}), see
1224 function C{classnaming}.
1226 @return: The B{C{inst}}'s C{[module.]class} name (C{str}).
1227 '''
1228 if prefixed is None:
1229 prefixed = getattr(inst, typename(classnaming), prefixed)
1230 return modulename(inst.__class__, prefixed=prefixed)
1233def classnaming(prefixed=None):
1234 '''Get/set the default class naming for C{[module.]class} names.
1236 @kwarg prefixed: Include the module name (C{bool}).
1238 @return: Previous class naming setting (C{bool}).
1239 '''
1240 t = _Named._classnaming
1241 if isbool(prefixed):
1242 _Named._classnaming = prefixed
1243 return t
1246def modulename(clas, prefixed=None): # in .basics._xversion
1247 '''Return the class name optionally prefixed with the
1248 module name.
1250 @arg clas: The class (any C{class}).
1251 @kwarg prefixed: Include the module name (C{bool}), see
1252 function C{classnaming}.
1254 @return: The B{C{class}}'s C{[module.]class} name (C{str}).
1255 '''
1256 try:
1257 n = typename(clas)
1258 except AttributeError:
1259 n = clas if isstr(clas) else _DNAME_
1260 if prefixed or (classnaming() if prefixed is None else False):
1261 try:
1262 m = clas.__module__.rsplit(_DOT_, 1)
1263 n = _DOT_.join(m[1:] + [n])
1264 except AttributeError:
1265 pass
1266 return n
1269# def _name__(name=NN, name__=None, _or_nameof=None, **kwds):
1270# '''(INTERNAL) Get single keyword argument C{B{name}=NN|None}.
1271# '''
1272# if kwds: # "unexpected keyword arguments ..."
1273# m = _MODS.errors
1274# raise m._UnexpectedError(**kwds)
1275# if name: # is given
1276# n = _name__(**name) if isinstance(name, dict) else str(name)
1277# elif name__ is not None:
1278# n = getattr(name__, _DNAME_, NN) # _xattr(name__, __name__=NN)
1279# else:
1280# n = name # NN or None or {} or any False type
1281# if _or_nameof is not None and not n:
1282# n = getattr(_or_nameof, _name_, NN) # _xattr(_or_nameof, name=NN)
1283# return n # str or None or {}
1286def _name__(name=NN, **kwds):
1287 '''(INTERNAL) Get single keyword argument C{B{name}=NN|None}.
1288 '''
1289 if name or kwds:
1290 name, kwds = _name2__(name, **kwds)
1291 if kwds: # "unexpected keyword arguments ..."
1292 raise _UnexpectedError(**kwds)
1293 return name if name or name is None else NN
1296def _name1__(kwds_name, **name__or_nameof):
1297 '''(INTERNAL) Resolve and set the C{B{name}=NN}.
1298 '''
1299 if kwds_name or name__or_nameof:
1300 n, kwds_name = _name2__(kwds_name, **name__or_nameof)
1301 kwds_name.update(name=n)
1302 return kwds_name
1305def _name2__(name=NN, name__=None, _or_nameof=None, **kwds):
1306 '''(INTERNAL) Get the C{B{name}=NN|None} and other C{kwds}.
1307 '''
1308 if name: # is given
1309 if isinstance(name, dict):
1310 kwds.update(name) # kwds = _xkwds(kwds, **name)?
1311 n, kwds = _name2__(**kwds)
1312 else:
1313 n = str(name)
1314 elif name__ is not None:
1315 n = typename(name__, NN)
1316 else:
1317 n = name if name is None else NN
1318 if _or_nameof is not None and not n:
1319 n = _xattr(_or_nameof, name=NN) # nameof
1320 return n, kwds # (str or None or {}), dict
1323def nameof(inst):
1324 '''Get the name of an instance.
1326 @arg inst: The object (any C{type}).
1328 @return: The instance' name (C{str}) or C{""}.
1329 '''
1330 n = _xattr(inst, name=NN)
1331 if not n: # and isinstance(inst, property):
1332 try:
1333 n = typename(inst.fget)
1334 except AttributeError:
1335 n = NN
1336 return n
1339def _notDecap(where):
1340 '''De-Capitalize C{where.__name__}.
1341 '''
1342 n = typename(where)
1343 c = n[3].lower() # len(_not_)
1344 return NN(n[:3], _SPACE_, c, n[4:])
1347def _notError(inst, name, args, kwds): # PYCHOK no cover
1348 '''(INTERNAL) Format an error message.
1349 '''
1350 n = _DOT_(classname(inst, prefixed=True), typename(name, name))
1351 m = _COMMASPACE_.join(modulename(c, prefixed=True) for c in type(inst).__mro__[1:-1])
1352 return _COMMASPACE_(unstr(n, *args, **kwds), Fmt.PAREN(_MRO_, m))
1355def _NotImplemented(inst, *other, **kwds):
1356 '''(INTERNAL) Raise a C{__special__} error or return C{NotImplemented},
1357 but only if env variable C{PYGEODESY_NOTIMPLEMENTED=std}.
1358 '''
1359 if _std_NotImplemented:
1360 return NotImplemented
1361 n, kwds = _callername2(other, **kwds) # source=True
1362 t = unstr(_DOT_(classname(inst), n), *other, **kwds)
1363 raise _NotImplementedError(t, txt=repr(inst))
1366def notImplemented(inst, *args, **kwds): # PYCHOK no cover
1367 '''Raise a C{NotImplementedError} for a missing instance method or
1368 property or for a missing caller feature.
1370 @arg inst: Caller instance (C{any}) or C{None} for function.
1371 @arg args: Method or property positional arguments (any C{type}s).
1372 @arg kwds: Method or property keyword arguments (any C{type}s),
1373 except C{B{callername}=NN}, C{B{underOK}=False} and
1374 C{B{up}=2}.
1375 '''
1376 n, kwds = _callername2(args, **kwds)
1377 t = _notError(inst, n, args, kwds) if inst else unstr(n, *args, **kwds)
1378 raise _NotImplementedError(t, txt=_notDecap(notImplemented))
1381def notOverloaded(inst, *args, **kwds): # PYCHOK no cover
1382 '''Raise an C{AssertionError} for a method or property not overloaded.
1384 @arg inst: Instance (C{any}).
1385 @arg args: Method or property positional arguments (any C{type}s).
1386 @arg kwds: Method or property keyword arguments (any C{type}s),
1387 except C{B{callername}=NN}, C{B{underOK}=False} and
1388 C{B{up}=2}.
1389 '''
1390 n, kwds = _callername2(args, **kwds)
1391 t = _notError(inst, n, args, kwds)
1392 raise _AssertionError(t, txt=_notDecap(notOverloaded))
1395def _Pass(arg, **unused): # PYCHOK no cover
1396 '''(INTERNAL) I{Pass-thru} class for C{_NamedTuple._Units_}.
1397 '''
1398 return arg
1401def _xjoined_(prefix, name=NN, enquote=True, **name__or_nameof):
1402 '''(INTERNAL) Join C{prefix} and non-empty C{name}.
1403 '''
1404 if name__or_nameof:
1405 name = _name__(name, **name__or_nameof)
1406 if name and prefix:
1407 if enquote:
1408 name = repr(name)
1409 t = _SPACE_(prefix, name)
1410 else:
1411 t = prefix or name
1412 return t
1415def _xnamed(inst, name=NN, force=False, **name__or_nameof):
1416 '''(INTERNAL) Set the instance' C{.name = B{name}}.
1418 @arg inst: The instance (C{_Named}).
1419 @kwarg name: The name (C{str}).
1420 @kwarg force: If C{True}, force rename (C{bool}).
1422 @return: The B{C{inst}}, renamed if B{C{force}}d
1423 or if not named before.
1424 '''
1425 if name__or_nameof:
1426 name = _name__(name, **name__or_nameof)
1427 if name and isinstance(inst, _Named):
1428 if not inst.name:
1429 inst.name = name
1430 elif force:
1431 inst.rename(name)
1432 return inst
1435def _xother3(inst, other, name=_other_, up=1, **name_other):
1436 '''(INTERNAL) Get C{name} and C{up} for a named C{other}.
1437 '''
1438 if name_other: # and other is None
1439 name, other = _xkwds_item2(name_other)
1440 elif other and len(other) == 1:
1441 name, other = _name__(name), other[0]
1442 else:
1443 raise _AssertionError(name, other, txt=classname(inst, prefixed=True))
1444 return other, name, up
1447def _xotherError(inst, other, name=_other_, up=1):
1448 '''(INTERNAL) Return a C{_TypeError} for an incompatible, named C{other}.
1449 '''
1450 n = _callname(name, classname(inst, prefixed=True), inst.name, up=up + 1)
1451 return _TypeError(name, other, txt=_incompatible(n))
1454def _xvalid(name, underOK=False):
1455 '''(INTERNAL) Check valid attribute name C{name}.
1456 '''
1457 return bool(name and isstr(name)
1458 and name != _name_
1459 and (underOK or not name.startswith(_UNDER_))
1460 and (not iskeyword(name))
1461 and isidentifier(name))
1464__all__ += _ALL_DOCS(_Named,
1465 _NamedBase, # _NamedDict,
1466 _NamedEnum, _NamedEnumItem,
1467 _NamedTuple)
1469# **) MIT License
1470#
1471# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.
1472#
1473# Permission is hereby granted, free of charge, to any person obtaining a
1474# copy of this software and associated documentation files (the "Software"),
1475# to deal in the Software without restriction, including without limitation
1476# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1477# and/or sell copies of the Software, and to permit persons to whom the
1478# Software is furnished to do so, subject to the following conditions:
1479#
1480# The above copyright notice and this permission notice shall be included
1481# in all copies or substantial portions of the Software.
1482#
1483# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1484# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1485# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1486# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1487# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1488# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1489# OTHER DEALINGS IN THE SOFTWARE.