Coverage for pygeodesy/named.py: 96%
530 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-03-25 15:01 -0400
« prev ^ index » next coverage.py v7.10.7, created at 2026-03-25 15:01 -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, _KeyError, LenError, _NameError, \
21 _NotImplementedError, _TypeError, _TypesError, \
22 _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__ = '26.01.14'
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 for n, v in kwds.items():
662 if isinstance(v, _LazyNamedEnumItem): # property
663 assert (n == v.name) if pypy else (n is v.name)
664 # assert not hasattr(self.__class__, n)
665 setattr(self.__class__, n, v)
666 elif isinstance(v, self._item_Classes): # PYCHOK no cover
667 assert self[n] is v and getattr(self, n) \
668 and self.find(v) == n
669 else:
670 raise _TypeError(v, name=n)
672 def _asserts(self): # in .triaxials.triaxial3
673 '''(INTERNAL) Yield all asserted items.
674 '''
675 for n, p in tuple(type(self).__dict__.items()):
676 if isinstance(p, _LazyNamedEnumItem):
677 yield n, p
679 def find(self, item, dflt=None, all=False):
680 '''Find a registered item.
682 @arg item: The item to look for (any C{type}).
683 @kwarg dflt: Value to return if not found (any C{type}).
684 @kwarg all: Use C{True} to search I{all} items or C{False} only
685 the currently I{registered} ones (C{bool}).
687 @return: The B{C{item}}'s name if found (C{str}), or C{{dflt}}
688 if there is no such B{C{item}}.
689 '''
690 for k, v in self.items(all=all): # or ADict.items(self)
691 if v is item:
692 return k
693 return dflt
695 def get(self, name, dflt=None):
696 '''Get the value of a I{registered} item.
698 @arg name: The name of the item (C{str}).
699 @kwarg dflt: Value to return (any C{type}).
701 @return: The item with B{C{name}} if found, or B{C{dflt}} if
702 there is no I{registered} item with that B{C{name}}.
703 '''
704 # getattr needed to instantiate L{_LazyNamedEnumItem}
705 return getattr(self, name, dflt)
707 def items(self, all=False, asorted=False):
708 '''Yield all or only the I{registered} items.
710 @kwarg all: Use C{True} to yield I{all} items or C{False} for
711 only the currently I{registered} ones (C{bool}).
712 @kwarg asorted: If C{True}, yield the items in I{alphabetical,
713 case-insensitive} order (C{bool}).
714 '''
715 if all: # instantiate any remaining L{_LazyNamedEnumItem}
716 for n, _ in self._asserts():
717 _ = getattr(self, n)
718 return itemsorted(self) if asorted else ADict.items(self)
720 def keys(self, **all_asorted):
721 '''Yield the name (C{str}) of I{all} or only the currently I{registered}
722 items, optionally sorted I{alphabetically, case-insensitively}.
724 @kwarg all_asorted: See method C{items}.
725 '''
726 for k, _ in self.items(**all_asorted):
727 yield k
729 def popitem(self):
730 '''Remove I{an, any} currently I{registed} item.
732 @return: The removed item.
733 '''
734 return self._zapitem(*ADict.popitem(self))
736 def register(self, item):
737 '''Registed one new item or I{all} or I{any} unregistered ones.
739 @arg item: The item (any C{type}) or B{I{all}} or B{C{any}}.
741 @return: The item name (C{str}) or C("all") or C{"any"}.
743 @raise NameError: An B{C{item}} with that name is already
744 registered the B{C{item}} has no or an
745 invalid name.
747 @raise TypeError: The B{C{item}} type invalid.
748 '''
749 if item is all or item is any:
750 _ = self.items(all=True)
751 n = typename(item)
752 else:
753 try:
754 n = item.name
755 if not (n and isstr(n) and isidentifier(n)):
756 raise ValueError()
757 except (AttributeError, ValueError, TypeError) as x:
758 n = _DOT_(_item_, _name_)
759 raise _NameError(n, item, cause=x)
760 if n in self:
761 t = _SPACE_(_item_, self._DOT_(n), _exists_)
762 raise _NameError(t, txt=repr(item))
763 if not isinstance(item, self._item_Classes): # _xinstanceof
764 n = self._DOT_(n)
765 raise _TypesError(n, item, *self._item_Classes)
766 self[n] = item
767 return n
769 def unregister(self, name_or_item):
770 '''Remove a I{registered} item.
772 @arg name_or_item: Name (C{str}) or the item (any C{type}).
774 @return: The unregistered item.
776 @raise AttributeError: No such B{C{item}}.
778 @raise NameError: No item with that B{C{name}}.
779 '''
780 if isstr(name_or_item):
781 name = name_or_item
782 else:
783 name = self.find(name_or_item, dflt=MISSING) # all=True?
784 if name is MISSING:
785 t = _SPACE_(_no_, _such_, self.classname, _item_)
786 raise _AttributeError(t, txt=repr(name_or_item))
787 try:
788 item = ADict.pop(self, name)
789 except KeyError:
790 raise _NameError(item=self._DOT_(name), txt=_doesn_t_exist_)
791 return self._zapitem(name, item)
793 pop = unregister
795 def toRepr(self, prec=6, fmt=Fmt.F, sep=_COMMANL_, **all_asorted): # PYCHOK _NamedDict, ADict
796 '''Like C{repr(dict)} but C{name}s optionally sorted and
797 C{floats} formatted by function L{pygeodesy.fstr}.
798 '''
799 t = ((self._DOT_(n), v) for n, v in self.items(**all_asorted))
800 return sep.join(pairs(t, prec=prec, fmt=fmt, sep=_COLONSPACE_))
802 def toStr(self, *unused, **all_asorted): # PYCHOK _NamedDict, ADict
803 '''Return a string with all C{name}s, optionally sorted.
804 '''
805 return self._DOT_(_COMMASPACEDOT_.join(self.keys(**all_asorted)))
807 def values(self, **all_asorted):
808 '''Yield the value (C{type}) of all or only the I{registered} items,
809 optionally sorted I{alphabetically} and I{case-insensitively}.
811 @kwarg all_asorted: See method C{items}.
812 '''
813 for _, v in self.items(**all_asorted):
814 yield v
816 def _zapitem(self, name, item):
817 # remove _LazyNamedEnumItem property value if still present
818 if self.__dict__.get(name, None) is item:
819 self.__dict__.pop(name) # [name] = None
820 item._enum = None
821 return item
824class _LazyNamedEnumItem(property_RO): # XXX or descriptor?
825 '''(INTERNAL) Lazily instantiated L{_NamedEnumItem}.
826 '''
827 pass
830def _lazyNamedEnumItem(name, *args, **kwds):
831 '''(INTERNAL) L{_LazyNamedEnumItem} property-like factory.
833 @see: Luciano Ramalho, "Fluent Python", O'Reilly, Example
834 19-24, 2016 p. 636 or Example 22-28, 2022 p. 869+
835 '''
836 def _fget(inst):
837 # assert isinstance(inst, _NamedEnum)
838 try: # get the item from the instance' __dict__
839 # item = inst.__dict__[name] # ... or ADict
840 item = inst[name]
841 except KeyError:
842 # instantiate an _NamedEnumItem, it self-registers
843 item = inst._Lazy(*args, **_xkwds(kwds, name=name))
844 # assert inst[name] is item # MUST be registered
845 # store the item in the instance' __dict__ ...
846 # inst.__dict__[name] = item # ... or update the
847 inst.update({name: item}) # ... ADict for Triaxials
848 # remove the property from the registry class, such that
849 # (a) the property no longer overrides the instance' item
850 # in inst.__dict__ and (b) _NamedEnum.items(all=True) only
851 # sees any un-instantiated ones yet to be instantiated
852 p = getattr(inst.__class__, name, None)
853 if isinstance(p, _LazyNamedEnumItem):
854 delattr(inst.__class__, name)
855 # assert isinstance(item, _NamedEnumItem)
856 return item
858 p = _LazyNamedEnumItem(_fget)
859 p.name = name
860 return p
863class _NamedEnumItem(_NamedBase):
864 '''(INTERNAL) Base class for items in a C{_NamedEnum} registry.
865 '''
866 _enum = None
868# def __ne__(self, other): # XXX fails for Lcc.conic = conic!
869# '''Compare this and an other item.
870#
871# @return: C{True} if different, C{False} otherwise.
872# '''
873# return not self.__eq__(other)
875 @property_doc_(''' the I{registered} name (C{str}).''')
876 def name(self):
877 '''Get the I{registered} name (C{str}).
878 '''
879 return self._name
881 @name.setter # PYCHOK setter!
882 def name(self, name):
883 '''Set the name, unless already registered (C{str}).
884 '''
885 name = _name__(name) or _NN_
886 if self._enum:
887 raise _NameError(name, self, txt=_registered_) # _TypeError
888 if name:
889 self._name = name
891 def _register(self, enum, name):
892 '''(INTERNAL) Add this item as B{C{enum.name}}.
894 @note: Don't register if name is empty or doesn't
895 start with a letter.
896 '''
897 name = _name__(name)
898 if name and _xvalid(name, underOK=True):
899 self.name = name
900 if name[:1].isalpha(): # '_...' not registered
901 enum.register(self)
902 self._enum = enum
904 def unregister(self):
905 '''Remove this instance from its C{_NamedEnum} registry.
907 @raise AssertionError: Mismatch of this and registered item.
909 @raise NameError: This item is unregistered.
910 '''
911 enum = self._enum
912 if enum and self.name and self.name in enum:
913 item = enum.unregister(self.name)
914 if item is not self: # PYCHOK no cover
915 t = _SPACE_(repr(item), _vs_, repr(self))
916 raise _AssertionError(t)
919# from pygeodesy.props import _NamedProperty
922class _NamedTuple(tuple, _Named):
923 '''(INTERNAL) Base for named C{tuple}s with both index I{and}
924 attribute name access to the items.
926 @note: This class is similar to Python's C{namedtuple},
927 but statically defined, lighter and limited.
928 '''
929 _Names_ = () # item names, non-identifier, no leading underscore
930 '''Tuple specifying the C{name} of each C{Named-Tuple} item.
932 @note: Specify at least 2 item names.
933 '''
934 _Units_ = () # .units classes
935 '''Tuple defining the C{units} of the value of each C{Named-Tuple} item.
937 @note: The C{len(_Units_)} must match C{len(_Names_)}.
938 '''
939 _validated = False # set to True I{per sub-class!}
941 def __new__(cls, arg, *args, **iteration_name):
942 '''New L{_NamedTuple} initialized with B{C{positional}} arguments.
944 @arg arg: Tuple items (C{tuple}, C{list}, ...) or first tuple
945 item of several more in other positional arguments.
946 @arg args: Tuple items (C{any}), all positional arguments.
947 @kwarg iteration_name: Only keyword arguments C{B{iteration}=None}
948 and C{B{name}=NN} are used, any other are
949 I{silently} ignored.
951 @raise LenError: Unequal number of positional arguments and
952 number of item C{_Names_} or C{_Units_}.
954 @raise TypeError: The C{_Names_} or C{_Units_} attribute is
955 not a C{tuple} of at least 2 items.
957 @raise ValueError: Item name is not a C{str} or valid C{identifier}
958 or starts with C{underscore}.
959 '''
960 n, args = len2(((arg,) + args) if args else arg)
961 self = tuple.__new__(cls, args)
962 if not self._validated:
963 self._validate()
965 N = len(self._Names_)
966 if n != N:
967 raise LenError(self.__class__, args=n, _Names_=N)
969 if iteration_name:
970 i, name = _xkwds_pop2(iteration_name, iteration=None)
971 if i is not None:
972 self._iteration = i
973 if name:
974 self.name = name
975 return self
977 def __delattr__(self, name):
978 '''Delete an attribute by B{C{name}}.
980 @note: Items can not be deleted.
981 '''
982 if name in self._Names_:
983 raise _ImmutableError(self, name) # _del_
984 elif name in (_name_, _name):
985 _Named.__setattr__(self, name, NN) # XXX _Named.name.fset(self, NN)
986 else:
987 tuple.__delattr__(self, name)
989 def __getattr__(self, name):
990 '''Get the value of an attribute or item by B{C{name}}.
991 '''
992 try:
993 return tuple.__getitem__(self, self._Names_.index(name))
994 except IndexError: # as x:
995 pass # raise _IndexError(self._DOT_(name), cause=x)
996 except ValueError: # e.g. _iteration
997 try: # tuple has no __getattr__?
998 return tuple.__getattr__(self, name) # __getattribute__?
999 except AttributeError:
1000 pass
1001 raise _AttributeError(self._DOT_(name), txt=repr(self))
1003# def __getitem__(self, index): # index, slice, etc.
1004# '''Get the item(s) at an B{C{index}} or slice.
1005# '''
1006# return tuple.__getitem__(self, index)
1008 def __hash__(self):
1009 return tuple.__hash__(self)
1011 def __repr__(self):
1012 '''Default C{repr(self)}.
1013 '''
1014 return self.toRepr()
1016 def __setattr__(self, name, value):
1017 '''Set attribute or item B{C{name}} to B{C{value}}.
1018 '''
1019 if name in self._Names_:
1020 raise _ImmutableError(self, name, value)
1021 elif name in (_name_, _name):
1022 _Named.__setattr__(self, name, value) # XXX _Named.name.fset(self, value)
1023 else: # e.g. _iteration
1024 tuple.__setattr__(self, name, value)
1026 def __str__(self):
1027 '''Default C{repr(self)}.
1028 '''
1029 return self.toStr()
1031 def _DOT_(self, *names):
1032 '''(INTERNAL) Period-join C{self.classname} and C{names}.
1033 '''
1034 return _DOT_(self.classname, *names)
1036 def dup(self, name=NN, **items):
1037 '''Duplicate this tuple replacing one or more items.
1039 @kwarg name: Optional new name (C{str}).
1040 @kwarg items: Items to be replaced (C{name=value} pairs), if any.
1042 @return: A copy of this tuple with B{C{items}}.
1044 @raise NameError: Some B{C{items}} invalid.
1045 '''
1046 t = list(self)
1047 U = self._Units_
1048 if items:
1049 _ix = self._Names_.index
1050 _2U = _MODS.units._toUnit
1051 try:
1052 for n, v in items.items():
1053 i = _ix(n)
1054 t[i] = _2U(U[i], v, name=n)
1055 except ValueError: # bad item name
1056 raise _NameError(self._DOT_(n), v, this=self)
1057 return self.classof(*t).reUnit(*U, name=name)
1059 def items(self):
1060 '''Yield the items, each as a C{(name, value)} pair (C{2-tuple}).
1062 @see: Method C{.units}.
1063 '''
1064 for n, v in _zip(self._Names_, self): # strict=True
1065 yield n, v
1067 iteritems = items
1069 def reUnit(self, *Units, **name):
1070 '''Replace some of this C{Named-Tuple}'s C{Units}.
1072 @arg Units: One or more C{Unit} classes, all positional.
1073 @kwarg name: Optional C{B{name}=NN} (C{str}).
1075 @return: This instance with updated C{Units}.
1077 @note: This C{Named-Tuple}'s values are I{not updated}.
1078 '''
1079 U = self._Units_
1080 n = min(len(U), len(Units))
1081 if n:
1082 R = Units + U[n:]
1083 if R != U:
1084 self._Units_ = R
1085 return self.renamed(name) if name else self
1087 def toRepr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1088 '''Return this C{Named-Tuple} items as C{name=value} string(s).
1090 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1091 Trailing zero decimals are stripped for B{C{prec}} values
1092 of 1 and above, but kept for negative B{C{prec}} values.
1093 @kwarg sep: Separator to join (C{str}).
1094 @kwarg fmt: Optional C{float} format (C{letter}).
1096 @return: Tuple items (C{str}).
1097 '''
1098 t = pairs(self.items(), prec=prec, fmt=fmt)
1099# if self.name:
1100# t = (Fmt.EQUAL(name=repr(self.name)),) + t
1101 return Fmt.PAREN(self.named, sep.join(t)) # XXX (self.classname, sep.join(t))
1103 def toStr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1104 '''Return this C{Named-Tuple} items as string(s).
1106 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1107 Trailing zero decimals are stripped for B{C{prec}} values
1108 of 1 and above, but kept for negative B{C{prec}} values.
1109 @kwarg sep: Separator to join (C{str}).
1110 @kwarg fmt: Optional C{float} format (C{letter}).
1112 @return: Tuple items (C{str}).
1113 '''
1114 t = reprs(self, prec=prec, fmt=fmt)
1115 return Fmt.PAREN(sep.join(t)) if sep else t
1117 def toUnits(self, Error=UnitError, **name): # overloaded in .frechet, .hausdorff
1118 '''Return a copy of this C{Named-Tuple} with each item value wrapped
1119 as an instance of its L{units} class.
1121 @kwarg Error: Error to raise for L{units} issues (C{UnitError}).
1122 @kwarg name: Optional C{B{name}=NN} (C{str}).
1124 @return: A duplicate of this C{Named-Tuple} (C{C{Named-Tuple}}).
1126 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1127 '''
1128 t = tuple(v for _, v in self.units(Error=Error))
1129 return self.classof(*t).reUnit(*self._Units_, **name)
1131 def units(self, **Error):
1132 '''Yield the items, each as a C{2-tuple (name, value}) with the
1133 value wrapped as an instance of its L{units} class.
1135 @kwarg Error: Optional C{B{Error}=UnitError} to raise.
1137 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1139 @see: Method C{.items}.
1140 '''
1141 _2U = _MODS.units._toUnit
1142 for n, v, U in _zip(self._Names_, self, self._Units_): # strict=True
1143 yield n, _2U(U, v, name=n, **Error)
1145 iterunits = units
1147 def _validate(self, underOK=False): # see .EcefMatrix
1148 '''(INTERNAL) One-time check of C{_Names_} and C{_Units_}
1149 for each C{_NamedUnit} I{sub-class separately}.
1150 '''
1151 ns = self._Names_
1152 if not (isinstance(ns, tuple) and len(ns) > 1): # XXX > 0
1153 raise _TypeError(self._DOT_(_Names_), ns)
1154 for i, n in enumerate(ns):
1155 if not _xvalid(n, underOK=underOK):
1156 t = Fmt.SQUARE(_Names_=i) # PYCHOK no cover
1157 raise _ValueError(self._DOT_(t), n)
1159 us = self._Units_
1160 if not isinstance(us, tuple):
1161 raise _TypeError(self._DOT_(_Units_), us)
1162 if len(us) != len(ns):
1163 raise LenError(self.__class__, _Units_=len(us), _Names_=len(ns))
1164 for i, u in enumerate(us):
1165 if not (u is None or callable(u)):
1166 t = Fmt.SQUARE(_Units_=i) # PYCHOK no cover
1167 raise _TypeError(self._DOT_(t), u)
1169 type(self)._validated = True
1171 def _xtend(self, xTuple, *items, **name):
1172 '''(INTERNAL) Extend this C{Named-Tuple} with C{items} to an other B{C{xTuple}}.
1173 '''
1174 _xsubclassof(_NamedTuple, xTuple=xTuple)
1175 if len(xTuple._Names_) != (len(self._Names_) + len(items)) or \
1176 xTuple._Names_[:len(self)] != self._Names_: # PYCHOK no cover
1177 c = NN(self.classname, repr(self._Names_))
1178 x = NN(typename(xTuple), repr(xTuple._Names_))
1179 raise TypeError(_SPACE_(c, _vs_, x))
1180 t = self + items
1181 return xTuple(t, name=self._name__(name)) # .reUnit(*self._Units_)
1184def callername(up=1, dflt=NN, source=False, underOK=False):
1185 '''Get the name of the invoking callable.
1187 @kwarg up: Number of call stack frames up (C{int}).
1188 @kwarg dflt: Default return value (C{any}).
1189 @kwarg source: Include source file name and line number (C{bool}).
1190 @kwarg underOK: If C{True}, private, internal callables are OK,
1191 otherwise private callables are skipped (C{bool}).
1193 @return: The callable name (C{str}) or B{C{dflt}} if none found.
1194 '''
1195 try: # see .internals._caller3
1196 for u in range(up, up + 32):
1197 n, f, s = _caller3(u)
1198 if n and (underOK or n.startswith(_DUNDER_) or
1199 not n.startswith(_UNDER_)):
1200 if source:
1201 n = NN(n, _AT_, f, _COLON_, str(s))
1202 return n
1203 except (AttributeError, ValueError):
1204 pass
1205 return dflt
1208def _callername2(args, callername=NN, source=False, underOK=False, up=_UP, **kwds):
1209 '''(INTERNAL) Extract C{callername}, C{source}, C{underOK} and C{up} from C{kwds}.
1210 '''
1211 n = callername or _MODS.named.callername(up=up + 1, source=source,
1212 underOK=underOK or bool(args or kwds))
1213 return n, kwds
1216def _callname(name, class_name, self_name, up=1):
1217 '''(INTERNAL) Assemble the name for an invokation.
1218 '''
1219 n, c = class_name, callername(up=up + 1)
1220 if c:
1221 n = _DOT_(n, Fmt.PAREN(c, name))
1222 if self_name:
1223 n = _SPACE_(n, repr(self_name))
1224 return n
1227def classname(inst, prefixed=None):
1228 '''Return the instance' class name optionally prefixed with the
1229 module name.
1231 @arg inst: The object (any C{type}).
1232 @kwarg prefixed: Include the module name (C{bool}), see
1233 function C{classnaming}.
1235 @return: The B{C{inst}}'s C{[module.]class} name (C{str}).
1236 '''
1237 if prefixed is None:
1238 prefixed = getattr(inst, typename(classnaming), prefixed)
1239 return modulename(inst.__class__, prefixed=prefixed)
1242def classnaming(prefixed=None):
1243 '''Get/set the default class naming for C{[module.]class} names.
1245 @kwarg prefixed: Include the module name (C{bool}).
1247 @return: Previous class naming setting (C{bool}).
1248 '''
1249 t = _Named._classnaming
1250 if isbool(prefixed):
1251 _Named._classnaming = prefixed
1252 return t
1255def modulename(clas, prefixed=None): # in .basics._xversion
1256 '''Return the class name optionally prefixed with the
1257 module name.
1259 @arg clas: The class (any C{class} or C{str}).
1260 @kwarg prefixed: Include the module name (C{bool}), see
1261 function C{classnaming}.
1263 @return: The B{C{class}}'s C{[module.]class} name (C{str}).
1264 '''
1265 try:
1266 n = typename(clas)
1267 except AttributeError:
1268 n = clas if isstr(clas) else _DNAME_
1269 if prefixed or (classnaming() if prefixed is None else False):
1270 try:
1271 m = clas.__module__.rsplit(_DOT_, 1)
1272 n = _DOT_.join(m[1:] + [n])
1273 except AttributeError:
1274 pass
1275 return n
1278# def _name__(name=NN, name__=None, _or_nameof=None, **kwds):
1279# '''(INTERNAL) Get single keyword argument C{B{name}=NN|None}.
1280# '''
1281# if kwds: # "unexpected keyword arguments ..."
1282# m = _MODS.errors
1283# raise m._UnexpectedError(**kwds)
1284# if name: # is given
1285# n = _name__(**name) if isinstance(name, dict) else str(name)
1286# elif name__ is not None:
1287# n = getattr(name__, _DNAME_, NN) # _xattr(name__, __name__=NN)
1288# else:
1289# n = name # NN or None or {} or any False type
1290# if _or_nameof is not None and not n:
1291# n = getattr(_or_nameof, _name_, NN) # _xattr(_or_nameof, name=NN)
1292# return n # str or None or {}
1295def _name__(name=NN, **kwds):
1296 '''(INTERNAL) Get single keyword argument C{B{name}=NN|None}.
1297 '''
1298 if name or kwds:
1299 name, kwds = _name2__(name, **kwds)
1300 if kwds: # "unexpected keyword arguments ..."
1301 raise _UnexpectedError(**kwds)
1302 return name if name or name is None else NN
1305def _name1__(kwds_name, **name__or_nameof):
1306 '''(INTERNAL) Resolve and set the C{B{name}=NN}.
1307 '''
1308 if kwds_name or name__or_nameof:
1309 n, kwds_name = _name2__(kwds_name, **name__or_nameof)
1310 kwds_name.update(name=n)
1311 return kwds_name
1314def _name2__(name=NN, name__=None, _or_nameof=None, **kwds):
1315 '''(INTERNAL) Get the C{B{name}=NN|None} and other C{kwds}.
1316 '''
1317 if name: # is given
1318 if isinstance(name, dict):
1319 kwds.update(name) # kwds = _xkwds(kwds, **name)?
1320 n, kwds = _name2__(**kwds)
1321 else:
1322 n = str(name)
1323 elif name__ is not None:
1324 n = typename(name__, NN)
1325 else:
1326 n = name if name is None else NN
1327 if _or_nameof is not None and not n:
1328 n = _xattr(_or_nameof, name=NN) # nameof
1329 return n, kwds # (str or None or {}), dict
1332def nameof(inst):
1333 '''Get the name of an instance.
1335 @arg inst: The object (any C{type}).
1337 @return: The instance' name (C{str}) or C{""}.
1338 '''
1339 n = _xattr(inst, name=NN)
1340 if not n: # and isinstance(inst, property):
1341 try:
1342 n = typename(inst.fget)
1343 except AttributeError:
1344 n = NN
1345 return n
1348def _notDecap(where):
1349 '''De-Capitalize C{where.__name__}.
1350 '''
1351 n = typename(where)
1352 c = n[3].lower() # len(_not_)
1353 return NN(n[:3], _SPACE_, c, n[4:])
1356def _notError(inst, name, args, kwds): # PYCHOK no cover
1357 '''(INTERNAL) Format an error message.
1358 '''
1359 n = _DOT_(classname(inst, prefixed=True), typename(name, name))
1360 m = _COMMASPACE_.join(modulename(c, prefixed=True) for c in type(inst).__mro__[1:-1])
1361 return _COMMASPACE_(unstr(n, *args, **kwds), Fmt.PAREN(_MRO_, m))
1364def _NotImplemented(inst, *other, **kwds):
1365 '''(INTERNAL) Raise a C{__special__} error or return C{NotImplemented},
1366 but only if env variable C{PYGEODESY_NOTIMPLEMENTED=std}.
1367 '''
1368 if _std_NotImplemented:
1369 return NotImplemented
1370 n, kwds = _callername2(other, **kwds) # source=True
1371 t = unstr(_DOT_(classname(inst), n), *other, **kwds)
1372 raise _NotImplementedError(t, txt=repr(inst))
1375def notImplemented(inst, *args, **kwds): # PYCHOK no cover
1376 '''Raise a C{NotImplementedError} for a missing instance method or
1377 property or for a missing caller feature.
1379 @arg inst: Caller instance (C{any}) or C{None} for function.
1380 @arg args: Method or property positional arguments (any C{type}s).
1381 @arg kwds: Method or property keyword arguments (any C{type}s),
1382 except C{B{callername}=NN}, C{B{underOK}=False} and
1383 C{B{up}=2}.
1384 '''
1385 n, kwds = _callername2(args, **kwds)
1386 t = _notError(inst, n, args, kwds) if inst else unstr(n, *args, **kwds)
1387 raise _NotImplementedError(t, txt=_notDecap(notImplemented))
1390def notOverloaded(inst, *args, **kwds): # PYCHOK no cover
1391 '''Raise an C{AssertionError} for a method or property not overloaded.
1393 @arg inst: Instance (C{any}).
1394 @arg args: Method or property positional arguments (any C{type}s).
1395 @arg kwds: Method or property keyword arguments (any C{type}s),
1396 except C{B{callername}=NN}, C{B{underOK}=False} and
1397 C{B{up}=2}.
1398 '''
1399 n, kwds = _callername2(args, **kwds)
1400 t = _notError(inst, n, args, kwds)
1401 raise _AssertionError(t, txt=_notDecap(notOverloaded))
1404def _Pass(arg, **unused): # PYCHOK no cover
1405 '''(INTERNAL) I{Pass-thru} class for C{_NamedTuple._Units_}.
1406 '''
1407 return arg
1410def _xjoined_(prefix, name=NN, enquote=True, **name__or_nameof):
1411 '''(INTERNAL) Join C{prefix} and non-empty C{name}.
1412 '''
1413 if name__or_nameof:
1414 name = _name__(name, **name__or_nameof)
1415 if name and prefix:
1416 if enquote:
1417 name = repr(name)
1418 t = _SPACE_(prefix, name)
1419 else:
1420 t = prefix or name
1421 return t
1424def _xnamed(inst, name=NN, force=False, **name__or_nameof):
1425 '''(INTERNAL) Set the instance' C{.name = B{name}}.
1427 @arg inst: The instance (C{_Named}).
1428 @kwarg name: The name (C{str}).
1429 @kwarg force: If C{True}, force rename (C{bool}).
1431 @return: The B{C{inst}}, renamed if B{C{force}}d
1432 or if not named before.
1433 '''
1434 if name__or_nameof:
1435 name = _name__(name, **name__or_nameof)
1436 if name and isinstance(inst, _Named):
1437 if not inst.name:
1438 inst.name = name
1439 elif force:
1440 inst.rename(name)
1441 return inst
1444def _xother3(inst, other, name=_other_, up=1, **name_other):
1445 '''(INTERNAL) Get C{name} and C{up} for a named C{other}.
1446 '''
1447 if name_other: # and other is None
1448 name, other = _xkwds_item2(name_other)
1449 elif other and len(other) == 1:
1450 name, other = _name__(name), other[0]
1451 else:
1452 raise _AssertionError(name, other, txt=classname(inst, prefixed=True))
1453 return other, name, up
1456def _xotherError(inst, other, name=_other_, up=1):
1457 '''(INTERNAL) Return a C{_TypeError} for an incompatible, named C{other}.
1458 '''
1459 n = _callname(name, classname(inst, prefixed=True), inst.name, up=up + 1)
1460 return _TypeError(name, other, txt=_incompatible(n))
1463def _xvalid(name, underOK=False):
1464 '''(INTERNAL) Check valid attribute name C{name}.
1465 '''
1466 return bool(name and isstr(name)
1467 and name != _name_
1468 and (underOK or not name.startswith(_UNDER_))
1469 and (not iskeyword(name))
1470 and isidentifier(name))
1473__all__ += _ALL_DOCS(_Named,
1474 _NamedBase, # _NamedDict,
1475 _NamedEnum, _NamedEnumItem,
1476 _NamedTuple)
1478# **) MIT License
1479#
1480# Copyright (C) 2016-2026 -- mrJean1 at Gmail -- All Rights Reserved.
1481#
1482# Permission is hereby granted, free of charge, to any person obtaining a
1483# copy of this software and associated documentation files (the "Software"),
1484# to deal in the Software without restriction, including without limitation
1485# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1486# and/or sell copies of the Software, and to permit persons to whom the
1487# Software is furnished to do so, subject to the following conditions:
1488#
1489# The above copyright notice and this permission notice shall be included
1490# in all copies or substantial portions of the Software.
1491#
1492# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1493# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1494# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1495# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1496# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1497# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1498# OTHER DEALINGS IN THE SOFTWARE.