Coverage for pygeodesy/named.py: 97%
456 statements
« prev ^ index » next coverage.py v7.2.2, created at 2024-04-04 14:33 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2024-04-04 14:33 -0400
2# -*- coding: utf-8 -*-
4u'''(INTERNAL) Nameable class instances.
6Classes C{_Named}, C{_NamedDict}, C{_NamedEnum}, C{_NamedEnumItem} and
7C{_NamedTuple} and several subclasses thereof, all with nameable instances.
9The items in a C{_NamedDict} are accessable as attributes and the items
10in a C{_NamedTuple} are named to be accessable as attributes, similar to
11standard Python C{namedtuple}s.
13@see: Module L{pygeodesy.namedTuples} for (most of) the C{Named-Tuples}.
14'''
16from pygeodesy.basics import isclass, isidentifier, iskeyword, isstr, issubclassof, \
17 itemsorted, len2, _sizeof, _xcopy, _xdup, _zip
18from pygeodesy.errors import _AssertionError, _AttributeError, _incompatible, \
19 _IndexError, _IsnotError, _KeyError, LenError, \
20 _NameError, _NotImplementedError, _TypeError, \
21 _TypesError, UnitError, _ValueError, _xattr, _xkwds, \
22 _xkwds_get, _xkwds_item2, _xkwds_pop2
23from pygeodesy.interns import MISSING, NN, _at_, _AT_, _COLON_, _COLONSPACE_, _COMMA_, \
24 _COMMASPACE_, _doesn_t_exist_, _DOT_, _DUNDER_, _EQUAL_, \
25 _exists_, _immutable_, _name_, _NL_, _NN_, _no_, _other_, \
26 _s_, _SPACE_, _std_, _UNDER_, _valid_, _vs_, \
27 _dunder_nameof, _isPyPy, _under
28from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _caller3, _getenv
29from pygeodesy.props import _allPropertiesOf_n, deprecated_method, _hasProperty, \
30 _update_all, property_doc_, Property_RO, property_RO, \
31 _update_attrs
32from pygeodesy.streprs import attrs, Fmt, lrstrip, pairs, reprs, unstr
34__all__ = _ALL_LAZY.named
35__version__ = '24.03.22'
37_COMMANL_ = _COMMA_ + _NL_
38_COMMASPACEDOT_ = _COMMASPACE_ + _DOT_
39_del_ = 'del'
40_item_ = 'item'
41_MRO_ = 'MRO'
42# __DUNDER gets mangled in class
43_name = _under(_name_)
44_Names_ = '_Names_'
45_registered_ = 'registered' # PYCHOK used!
46_std_NotImplemented = _getenv('PYGEODESY_NOTIMPLEMENTED', NN).lower() == _std_
47_Units_ = '_Units_'
50def _xjoined_(prefix, name):
51 '''(INTERNAL) Join C{pref} and non-empty C{name}.
52 '''
53 return _SPACE_(prefix, repr(name)) if name and prefix else (prefix or name)
56def _xnamed(inst, name, force=False):
57 '''(INTERNAL) Set the instance' C{.name = B{name}}.
59 @arg inst: The instance (C{_Named}).
60 @arg name: The name (C{str}).
61 @kwarg force: Force name change (C{bool}).
63 @return: The B{C{inst}}, named if B{C{force}}d or
64 not named before.
65 '''
66 if name and isinstance(inst, _Named):
67 if not inst.name:
68 inst.name = name
69 elif force:
70 inst.rename(name)
71 return inst
74def _xother3(inst, other, name=_other_, up=1, **name_other):
75 '''(INTERNAL) Get C{name} and C{up} for a named C{other}.
76 '''
77 if name_other: # and other is None
78 name, other = _xkwds_item2(name_other)
79 elif other and len(other) == 1:
80 other = other[0]
81 else:
82 raise _AssertionError(name, other, txt=classname(inst, prefixed=True))
83 return other, name, up
86def _xotherError(inst, other, name=_other_, up=1):
87 '''(INTERNAL) Return a C{_TypeError} for an incompatible, named C{other}.
88 '''
89 n = _callname(name, classname(inst, prefixed=True), inst.name, up=up + 1)
90 return _TypeError(name, other, txt=_incompatible(n))
93def _xvalid(name, underOK=False):
94 '''(INTERNAL) Check valid attribute name C{name}.
95 '''
96 return bool(name and isstr(name)
97 and name != _name_
98 and (underOK or not name.startswith(_UNDER_))
99 and (not iskeyword(name))
100 and isidentifier(name))
103class ADict(dict):
104 '''A C{dict} with both key I{and} attribute access to
105 the C{dict} items.
106 '''
107 _iteration = None # Iteration number (C{int}) or C{None}
109 def __getattr__(self, name):
110 '''Get the value of an item by B{C{name}}.
111 '''
112 try:
113 return self[name]
114 except KeyError:
115 if name == _name_:
116 return NN
117 raise self._AttributeError(name)
119 def __repr__(self):
120 '''Default C{repr(self)}.
121 '''
122 return self.toRepr()
124 def __str__(self):
125 '''Default C{str(self)}.
126 '''
127 return self.toStr()
129 def _AttributeError(self, name):
130 '''(INTERNAL) Create an C{AttributeError}.
131 '''
132 if _DOT_ not in name: # NOT classname(self)!
133 name = _DOT_(self.__class__.__name__, name)
134 return _AttributeError(item=name, txt=_doesn_t_exist_)
136 @property_RO
137 def iteration(self): # see .named._NamedBase
138 '''Get the iteration number (C{int}) or
139 C{None} if not available/applicable.
140 '''
141 return self._iteration
143 def set_(self, iteration=None, **items): # PYCHOK signature
144 '''Add one or several new items or replace existing ones.
146 @kwarg iteration: Optional C{iteration} (C{int}).
147 @kwarg items: One or more C{name=value} pairs.
148 '''
149 if iteration is not None:
150 self._iteration = iteration
151 if items:
152 dict.update(self, items)
153 return self # in RhumbLineBase.Intersecant2, _PseudoRhumbLine.Position
155 def toRepr(self, **prec_fmt):
156 '''Like C{repr(dict)} but with C{name} prefix and with
157 C{floats} formatted by function L{pygeodesy.fstr}.
158 '''
159 n = _xattr(self, name=NN) or self.__class__.__name__
160 return Fmt.PAREN(n, self._toT(_EQUAL_, **prec_fmt))
162 def toStr(self, **prec_fmt):
163 '''Like C{str(dict)} but with C{floats} formatted by
164 function L{pygeodesy.fstr}.
165 '''
166 return Fmt.CURLY(self._toT(_COLONSPACE_, **prec_fmt))
168 def _toT(self, sep, **kwds):
169 '''(INTERNAL) Helper for C{.toRepr} and C{.toStr}.
170 '''
171 kwds = _xkwds(kwds, prec=6, fmt=Fmt.F, sep=sep)
172 return _COMMASPACE_.join(pairs(itemsorted(self), **kwds))
175class _Named(object):
176 '''(INTERNAL) Root class for named objects.
177 '''
178 _iteration = None # iteration number (C{int}) or C{None}
179 _name = NN # name (C{str})
180 _classnaming = False # prefixed (C{bool})
181# _updates = 0 # OBSOLETE Property/property updates
183 def __imatmul__(self, other): # PYCHOK no cover
184 '''Not implemented.'''
185 return _NotImplemented(self, other) # PYCHOK Python 3.5+
187 def __matmul__(self, other): # PYCHOK no cover
188 '''Not implemented.'''
189 return _NotImplemented(self, other) # PYCHOK Python 3.5+
191 def __repr__(self):
192 '''Default C{repr(self)}.
193 '''
194 return Fmt.ANGLE(_SPACE_(self, _at_, hex(id(self))))
196 def __rmatmul__(self, other): # PYCHOK no cover
197 '''Not implemented.'''
198 return _NotImplemented(self, other) # PYCHOK Python 3.5+
200 def __str__(self):
201 '''Default C{str(self)}.
202 '''
203 return self.named2
205 def attrs(self, *names, **sep_Nones_pairs_kwds):
206 '''Join named attributes as I{name=value} strings, with C{float}s formatted by
207 function L{pygeodesy.fstr}.
209 @arg names: The attribute names, all positional (C{str}).
210 @kwarg sep_Nones_pairs_kwds: Keyword arguments for function L{pygeodesy.pairs},
211 except C{B{sep}=", "} and C{B{Nones}=True} to in-/exclude missing
212 or C{None}-valued attributes.
214 @return: All C{name=value} pairs, joined by B{C{sep}} (C{str}).
216 @see: Functions L{pygeodesy.attrs}, L{pygeodesy.pairs} and L{pygeodesy.fstr}
217 '''
218 sep, kwds = _xkwds_pop2(sep_Nones_pairs_kwds, sep=_COMMASPACE_)
219 return sep.join(attrs(self, *names, **kwds))
221 @Property_RO
222 def classname(self):
223 '''Get this object's C{[module.]class} name (C{str}), see
224 property C{.classnaming} and function C{classnaming}.
225 '''
226 return classname(self, prefixed=self._classnaming)
228 @property_doc_(''' the class naming (C{bool}).''')
229 def classnaming(self):
230 '''Get the class naming (C{bool}), see function C{classnaming}.
231 '''
232 return self._classnaming
234 @classnaming.setter # PYCHOK setter!
235 def classnaming(self, prefixed):
236 '''Set the class naming for C{[module.].class} names (C{bool})
237 to C{True} to include the module name.
238 '''
239 b = bool(prefixed)
240 if self._classnaming != b:
241 self._classnaming = b
242 _update_attrs(self, *_Named_Property_ROs)
244 def classof(self, *args, **kwds):
245 '''Create another instance of this very class.
247 @arg args: Optional, positional arguments.
248 @kwarg kwds: Optional, keyword arguments.
250 @return: New instance (B{self.__class__}).
251 '''
252 return _xnamed(self.__class__(*args, **kwds), self.name)
254 def copy(self, deep=False, name=NN):
255 '''Make a shallow or deep copy of this instance.
257 @kwarg deep: If C{True} make a deep, otherwise
258 a shallow copy (C{bool}).
259 @kwarg name: Optional, non-empty name (C{str}).
261 @return: The copy (C{This class}).
262 '''
263 c = _xcopy(self, deep=deep)
264 if name:
265 c.rename(name)
266 return c
268 def _DOT_(self, *names):
269 '''(INTERNAL) Period-join C{self.name} and C{names}.
270 '''
271 return _DOT_(self.name, *names)
273 def dup(self, deep=False, **items):
274 '''Duplicate this instance, replacing some attributes.
276 @kwarg deep: If C{True} duplicate deep, otherwise shallow.
277 @kwarg items: Attributes to be changed (C{any}).
279 @return: The duplicate (C{This class}).
281 @raise AttributeError: Some B{C{items}} invalid.
282 '''
283 n = self.name
284 m, items = _xkwds_pop2(items, name=n)
285 d = _xdup(self, deep=deep, **items)
286 if m != n:
287 d.rename(m)
288# if items:
289# _update_all(d)
290 return d
292 def _instr(self, name, prec, *attrs, **fmt_props_kwds):
293 '''(INTERNAL) Format, used by C{Conic}, C{Ellipsoid}, C{Transform}, C{Triaxial}.
294 '''
295 def _fmt_props_kwds(fmt=Fmt.F, props=(), **kwds):
296 return fmt, props, kwds
298 fmt, props, kwds = _fmt_props_kwds(**fmt_props_kwds)
300 t = () if name is None else (Fmt.EQUAL(name=repr(name or self.name)),)
301 if attrs:
302 t += pairs(((a, getattr(self, a)) for a in attrs),
303 prec=prec, ints=True, fmt=fmt)
304 if props:
305 t += pairs(((p.name, getattr(self, p.name)) for p in props),
306 prec=prec, ints=True)
307 if kwds:
308 t += pairs(kwds, prec=prec)
309 return _COMMASPACE_.join(t)
311 @property_RO
312 def iteration(self): # see .karney.GDict
313 '''Get the most recent iteration number (C{int}) or C{None}
314 if not available or not applicable.
316 @note: The interation number may be an aggregate number over
317 several, nested functions.
318 '''
319 return self._iteration
321 def methodname(self, which):
322 '''Get a method C{[module.]class.method} name of this object (C{str}).
324 @arg which: The method (C{callable}).
325 '''
326 return _DOT_(self.classname, which.__name__ if callable(which) else _NN_)
328 @property_doc_(''' the name (C{str}).''')
329 def name(self):
330 '''Get the name (C{str}).
331 '''
332 return self._name
334 @name.setter # PYCHOK setter!
335 def name(self, name):
336 '''Set the name (C{str}).
338 @raise NameError: Can't rename, use method L{rename}.
339 '''
340 m, n = self._name, str(name)
341 if not m:
342 self._name = n
343 elif n != m:
344 n = repr(n)
345 c = self.classname
346 t = _DOT_(c, Fmt.PAREN(self.rename.__name__, n))
347 n = _DOT_(c, Fmt.EQUALSPACED(name=n))
348 m = Fmt.PAREN(_SPACE_('was', repr(m)))
349 n = _SPACE_(n, m)
350 raise _NameError(n, txt=_SPACE_('use', t))
351 # to set the name from a sub-class, use
352 # self.name = name or
353 # _Named.name.fset(self, name), but NOT
354 # _Named(self).name = name
356 @Property_RO
357 def named(self):
358 '''Get the name I{or} class name or C{""} (C{str}).
359 '''
360 return self.name or self.classname
362 @Property_RO
363 def named2(self):
364 '''Get the C{class} name I{and/or} the name or C{""} (C{str}).
365 '''
366 return _xjoined_(self.classname, self.name)
368 @Property_RO
369 def named3(self):
370 '''Get the I{prefixed} C{class} name I{and/or} the name or C{""} (C{str}).
371 '''
372 return _xjoined_(classname(self, prefixed=True), self.name)
374 @Property_RO
375 def named4(self):
376 '''Get the C{package.module.class} name I{and/or} the name or C{""} (C{str}).
377 '''
378 return _xjoined_(_DOT_(self.__module__, self.__class__.__name__), self.name)
380 def rename(self, name):
381 '''Change the name.
383 @arg name: The new name (C{str}).
385 @return: The previous name (C{str}).
386 '''
387 m, n = self._name, str(name)
388 if n != m:
389 self._name = n
390 _update_attrs(self, *_Named_Property_ROs)
391 return m
393 @property_RO
394 def sizeof(self):
395 '''Get the current size in C{bytes} of this instance (C{int}).
396 '''
397 return _sizeof(self)
399 def toRepr(self, **unused): # PYCHOK no cover
400 '''Default C{repr(self)}.
401 '''
402 return repr(self)
404 def toStr(self, **unused): # PYCHOK no cover
405 '''Default C{str(self)}.
406 '''
407 return str(self)
409 @deprecated_method
410 def toStr2(self, **kwds): # PYCHOK no cover
411 '''DEPRECATED on 23.10.07, use method C{toRepr}.'''
412 return self.toRepr(**kwds)
414# def _unstr(self, which, *args, **kwds):
415# '''(INTERNAL) Return the string representation of a method
416# invokation of this instance: C{str(self).method(...)}
417#
418# @see: Function L{pygeodesy.unstr}.
419# '''
420# return _DOT_(self, unstr(which, *args, **kwds))
422 def _xnamed(self, inst, name=NN, force=False):
423 '''(INTERNAL) Set the instance' C{.name = self.name}.
425 @arg inst: The instance (C{_Named}).
426 @kwarg name: Optional name, overriding C{self.name} (C{str}).
427 @kwarg force: Force name change (C{bool}).
429 @return: The B{C{inst}}, named if not named before.
430 '''
431 return _xnamed(inst, name or self.name, force=force)
433 def _xrenamed(self, inst):
434 '''(INTERNAL) Rename the instance' C{.name = self.name}.
436 @arg inst: The instance (C{_Named}).
438 @return: The B{C{inst}}, named if not named before.
440 @raise TypeError: Not C{isinstance(B{inst}, _Named)}.
441 '''
442 if not isinstance(inst, _Named):
443 raise _IsnotError(_valid_, inst=inst)
445 inst.rename(self.name)
446 return inst
448_Named_Property_ROs = _allPropertiesOf_n(5, _Named, Property_RO) # PYCHOK once
451class _NamedBase(_Named):
452 '''(INTERNAL) Base class with name.
453 '''
454 def __repr__(self):
455 '''Default C{repr(self)}.
456 '''
457 return self.toRepr()
459 def __str__(self):
460 '''Default C{str(self)}.
461 '''
462 return self.toStr()
464 def others(self, *other, **name_other_up):
465 '''Refined class comparison, invoked as C{.others(other)},
466 C{.others(name=other)} or C{.others(other, name='other')}.
468 @arg other: The other instance (any C{type}).
469 @kwarg name_other_up: Overriding C{name=other} and C{up=1}
470 keyword arguments.
472 @return: The B{C{other}} iff compatible with this instance's
473 C{class} or C{type}.
475 @raise TypeError: Mismatch of the B{C{other}} and this
476 instance's C{class} or C{type}.
477 '''
478 if other: # most common, just one arg B{C{other}}
479 other0 = other[0]
480 if isinstance(other0, self.__class__) or \
481 isinstance(self, other0.__class__):
482 return other0
484 other, name, up = _xother3(self, other, **name_other_up)
485 if isinstance(self, other.__class__) or \
486 isinstance(other, self.__class__):
487 return _xnamed(other, name)
489 raise _xotherError(self, other, name=name, up=up + 1)
491 def toRepr(self, **kwds): # PYCHOK expected
492 '''(INTERNAL) I{Could be overloaded}.
494 @kwarg kwds: Optional, C{toStr} keyword arguments.
496 @return: C{toStr}() with keyword arguments (as C{str}).
497 '''
498 t = lrstrip(self.toStr(**kwds))
499# if self.name:
500# t = NN(Fmt.EQUAL(name=repr(self.name)), sep, t)
501 return Fmt.PAREN(self.classname, t) # XXX (self.named, t)
503# def toRepr(self, **kwds)
504# if kwds:
505# s = NN.join(reprs((self,), **kwds))
506# else: # super().__repr__ only for Python 3+
507# s = super(self.__class__, self).__repr__()
508# return Fmt.PAREN(self.named, s) # clips(s)
510 def toStr(self, **kwds): # PYCHOK no cover
511 '''I{Must be overloaded}.'''
512 notOverloaded(self, **kwds)
514# def toStr(self, **kwds):
515# if kwds:
516# s = NN.join(strs((self,), **kwds))
517# else: # super().__str__ only for Python 3+
518# s = super(self.__class__, self).__str__()
519# return s
521 def _update(self, updated, *attrs, **setters):
522 '''(INTERNAL) Zap cached instance attributes and overwrite C{__dict__} or L{Property_RO} values.
523 '''
524 u = _update_all(self, *attrs) if updated else 0
525 if setters:
526 d = self.__dict__
527 # double-check that setters are Property_RO's
528 for n, v in setters.items():
529 if n in d or _hasProperty(self, n, Property_RO):
530 d[n] = v
531 else:
532 raise _AssertionError(n, v, txt=repr(self))
533 u += len(setters)
534 return u
537class _NamedDict(ADict, _Named):
538 '''(INTERNAL) Named C{dict} with key I{and} attribute
539 access to the items.
540 '''
541 def __init__(self, *args, **kwds):
542 if args: # args override kwds
543 if len(args) != 1: # or not isinstance(args[0], dict)
544 t = unstr(self.classname, *args, **kwds) # PYCHOK no cover
545 raise _ValueError(args=len(args), txt=t)
546 kwds = _xkwds(dict(args[0]), **kwds)
547 n, kwds = _xkwds_pop2(kwds, name=NN)
548 if n:
549 _Named.name.fset(self, n) # see _Named.name
550 ADict.__init__(self, kwds)
552 def __delattr__(self, name):
553 '''Delete an attribute or item by B{C{name}}.
554 '''
555 if name in self: # in ADict.keys(self):
556 ADict.pop(self, name)
557 elif name in (_name_, _name):
558 # ADict.__setattr__(self, name, NN)
559 _Named.rename(self, NN)
560 else:
561 ADict.__delattr__(self, name)
563 def __getattr__(self, name):
564 '''Get the value of an item by B{C{name}}.
565 '''
566 try:
567 return self[name]
568 except KeyError:
569 if name == _name_:
570 return _Named.name.fget(self)
571 raise ADict._AttributeError(self, self._DOT_(name))
573 def __getitem__(self, key):
574 '''Get the value of an item by B{C{key}}.
575 '''
576 if key == _name_:
577 raise self._KeyError(key)
578 return ADict.__getitem__(self, key)
580 def _KeyError(self, key, *value): # PYCHOK no cover
581 '''(INTERNAL) Create a C{KeyError}.
582 '''
583 n = self.name or self.__class__.__name__
584 t = Fmt.SQUARE(n, key)
585 if value:
586 t = Fmt.EQUALSPACED(t, *value)
587 return _KeyError(t)
589 def __setattr__(self, name, value):
590 '''Set attribute or item B{C{name}} to B{C{value}}.
591 '''
592 if name in self: # in ADict.keys(self)
593 ADict.__setitem__(self, name, value) # self[name] = value
594 else:
595 ADict.__setattr__(self, name, value)
597 def __setitem__(self, key, value):
598 '''Set item B{C{key}} to B{C{value}}.
599 '''
600 if key == _name_:
601 raise self._KeyError(key, repr(value))
602 ADict.__setitem__(self, key, value)
605class _NamedEnum(_NamedDict):
606 '''(INTERNAL) Enum-like C{_NamedDict} with attribute access
607 restricted to valid keys.
608 '''
609 _item_Classes = ()
611 def __init__(self, Class, *Classes, **name):
612 '''New C{_NamedEnum}.
614 @arg Class: Initial class or type acceptable as items
615 values (C{type}).
616 @arg Classes: Additional, acceptable classes or C{type}s.
617 '''
618 self._item_Classes = (Class,) + Classes
619 n = _xkwds_get(name, name=NN) or NN(Class.__name__, _s_)
620 if n and _xvalid(n, underOK=True):
621 _Named.name.fset(self, n) # see _Named.name
623 def __getattr__(self, name):
624 '''Get the value of an attribute or item by B{C{name}}.
625 '''
626 return _NamedDict.__getattr__(self, name)
628 def __repr__(self):
629 '''Default C{repr(self)}.
630 '''
631 return self.toRepr()
633 def __str__(self):
634 '''Default C{str(self)}.
635 '''
636 return self.toStr()
638 def _assert(self, **kwds):
639 '''(INTERNAL) Check attribute name against given, registered name.
640 '''
641 pypy = _isPyPy()
642 for n, v in kwds.items():
643 if isinstance(v, _LazyNamedEnumItem): # property
644 assert (n == v.name) if pypy else (n is v.name)
645 # assert not hasattr(self.__class__, n)
646 setattr(self.__class__, n, v)
647 elif isinstance(v, self._item_Classes): # PYCHOK no cover
648 assert self[n] is v and getattr(self, n) \
649 and self.find(v) == n
650 else:
651 raise _TypeError(v, name=n)
653 def find(self, item, dflt=None, all=False):
654 '''Find a registered item.
656 @arg item: The item to look for (any C{type}).
657 @kwarg dflt: Value to return if not found (any C{type}).
658 @kwarg all: Use C{True} to search I{all} items or C{False} only
659 the currently I{registered} ones (C{bool}).
661 @return: The B{C{item}}'s name if found (C{str}), or C{{dflt}}
662 if there is no such B{C{item}}.
663 '''
664 for k, v in self.items(all=all): # or ADict.items(self)
665 if v is item:
666 return k
667 return dflt
669 def get(self, name, dflt=None):
670 '''Get the value of a I{registered} item.
672 @arg name: The name of the item (C{str}).
673 @kwarg dflt: Value to return (any C{type}).
675 @return: The item with B{C{name}} if found, or B{C{dflt}} if
676 there is no I{registered} item with that B{C{name}}.
677 '''
678 # getattr needed to instantiate L{_LazyNamedEnumItem}
679 return getattr(self, name, dflt)
681 def items(self, all=False, asorted=False):
682 '''Yield all or only the I{registered} items.
684 @kwarg all: Use C{True} to yield I{all} items or C{False} for
685 only the currently I{registered} ones (C{bool}).
686 @kwarg asorted: If C{True}, yield the items in I{alphabetical,
687 case-insensitive} order (C{bool}).
688 '''
689 if all: # instantiate any remaining L{_LazyNamedEnumItem}
690 for n, p in tuple(self.__class__.__dict__.items()):
691 if isinstance(p, _LazyNamedEnumItem):
692 _ = getattr(self, n)
693 return itemsorted(self) if asorted else ADict.items(self)
695 def keys(self, **all_asorted):
696 '''Yield the name (C{str}) of I{all} or only the currently I{registered}
697 items, optionally sorted I{alphabetically, case-insensitively}.
699 @kwarg all_asorted: See method C{items}.
700 '''
701 for k, _ in self.items(**all_asorted):
702 yield k
704 def popitem(self):
705 '''Remove I{an, any} currently I{registed} item.
707 @return: The removed item.
708 '''
709 return self._zapitem(*ADict.popitem(self))
711 def register(self, item):
712 '''Registed one new item or I{all} or I{any} unregistered ones.
714 @arg item: The item (any C{type}) or B{I{all}} or B{C{any}}.
716 @return: The item name (C{str}) or C("all") or C{"any"}.
718 @raise NameError: An B{C{item}} with that name is already
719 registered the B{C{item}} has no or an
720 invalid name.
722 @raise TypeError: The B{C{item}} type invalid.
723 '''
724 if item is all or item is any:
725 _ = self.items(all=True)
726 n = item.__name__
727 else:
728 try:
729 n = item.name
730 if not (n and isstr(n) and isidentifier(n)):
731 raise ValueError()
732 except (AttributeError, ValueError, TypeError) as x:
733 raise _NameError(_DOT_(_item_, _name_), item, cause=x)
734 if n in self:
735 t = _SPACE_(_item_, self._DOT_(n), _exists_)
736 raise _NameError(t, txt=repr(item))
737 if not isinstance(item, self._item_Classes):
738 raise _TypesError(self._DOT_(n), item, *self._item_Classes)
739 self[n] = item
740 return n
742 def unregister(self, name_or_item):
743 '''Remove a I{registered} item.
745 @arg name_or_item: Name (C{str}) or the item (any C{type}).
747 @return: The unregistered item.
749 @raise AttributeError: No such B{C{item}}.
751 @raise NameError: No item with that B{C{name}}.
752 '''
753 if isstr(name_or_item):
754 name = name_or_item
755 else:
756 name = self.find(name_or_item, dflt=MISSING) # all=True?
757 if name is MISSING:
758 t = _SPACE_(_no_, 'such', self.classname, _item_)
759 raise _AttributeError(t, txt=repr(name_or_item))
760 try:
761 item = ADict.pop(self, name)
762 except KeyError:
763 raise _NameError(item=self._DOT_(name), txt=_doesn_t_exist_)
764 return self._zapitem(name, item)
766 pop = unregister
768 def toRepr(self, prec=6, fmt=Fmt.F, sep=_COMMANL_, **all_asorted): # PYCHOK _NamedDict, ADict
769 '''Like C{repr(dict)} but C{name}s optionally sorted and
770 C{floats} formatted by function L{pygeodesy.fstr}.
771 '''
772 t = ((self._DOT_(n), v) for n, v in self.items(**all_asorted))
773 return sep.join(pairs(t, prec=prec, fmt=fmt, sep=_COLONSPACE_))
775 def toStr(self, *unused, **all_asorted): # PYCHOK _NamedDict, ADict
776 '''Return a string with all C{name}s, optionally sorted.
777 '''
778 return self._DOT_(_COMMASPACEDOT_.join(self.keys(**all_asorted)))
780 def values(self, **all_asorted):
781 '''Yield the value (C{type}) of all or only the I{registered} items,
782 optionally sorted I{alphabetically} and I{case-insensitively}.
784 @kwarg all_asorted: See method C{items}.
785 '''
786 for _, v in self.items(**all_asorted):
787 yield v
789 def _zapitem(self, name, item):
790 # remove _LazyNamedEnumItem property value if still present
791 if self.__dict__.get(name, None) is item:
792 self.__dict__.pop(name) # [name] = None
793 item._enum = None
794 return item
797class _LazyNamedEnumItem(property_RO): # XXX or descriptor?
798 '''(INTERNAL) Lazily instantiated L{_NamedEnumItem}.
799 '''
800 pass
803def _lazyNamedEnumItem(name, *args, **kwds):
804 '''(INTERNAL) L{_LazyNamedEnumItem} property-like factory.
806 @see: Luciano Ramalho, "Fluent Python", O'Reilly, Example
807 19-24, 2016 p. 636 or Example 22-28, 2022 p. 869+
808 '''
809 def _fget(inst):
810 # assert isinstance(inst, _NamedEnum)
811 try: # get the item from the instance' __dict__
812 # item = inst.__dict__[name] # ... or ADict
813 item = inst[name]
814 except KeyError:
815 # instantiate an _NamedEnumItem, it self-registers
816 item = inst._Lazy(*args, **_xkwds(kwds, name=name))
817 # assert inst[name] is item # MUST be registered
818 # store the item in the instance' __dict__ ...
819 # inst.__dict__[name] = item # ... or update the
820 inst.update({name: item}) # ... ADict for Triaxials
821 # remove the property from the registry class, such that
822 # (a) the property no longer overrides the instance' item
823 # in inst.__dict__ and (b) _NamedEnum.items(all=True) only
824 # sees any un-instantiated ones yet to be instantiated
825 p = getattr(inst.__class__, name, None)
826 if isinstance(p, _LazyNamedEnumItem):
827 delattr(inst.__class__, name)
828 # assert isinstance(item, _NamedEnumItem)
829 return item
831 p = _LazyNamedEnumItem(_fget)
832 p.name = name
833 return p
836class _NamedEnumItem(_NamedBase):
837 '''(INTERNAL) Base class for items in a C{_NamedEnum} registery.
838 '''
839 _enum = None
841# def __ne__(self, other): # XXX fails for Lcc.conic = conic!
842# '''Compare this and an other item.
843#
844# @return: C{True} if different, C{False} otherwise.
845# '''
846# return not self.__eq__(other)
848 @property_doc_(''' the I{registered} name (C{str}).''')
849 def name(self):
850 '''Get the I{registered} name (C{str}).
851 '''
852 return self._name
854 @name.setter # PYCHOK setter!
855 def name(self, name):
856 '''Set the name, unless already registered (C{str}).
857 '''
858 if self._enum:
859 raise _NameError(str(name), self, txt=_registered_) # XXX _TypeError
860 self._name = str(name)
862 def _register(self, enum, name):
863 '''(INTERNAL) Add this item as B{C{enum.name}}.
865 @note: Don't register if name is empty or doesn't
866 start with a letter.
867 '''
868 if name and _xvalid(name, underOK=True):
869 self.name = name
870 if name[:1].isalpha(): # '_...' not registered
871 enum.register(self)
872 self._enum = enum
874 def unregister(self):
875 '''Remove this instance from its C{_NamedEnum} registry.
877 @raise AssertionError: Mismatch of this and registered item.
879 @raise NameError: This item is unregistered.
880 '''
881 enum = self._enum
882 if enum and self.name and self.name in enum:
883 item = enum.unregister(self.name)
884 if item is not self:
885 t = _SPACE_(repr(item), _vs_, repr(self)) # PYCHOK no cover
886 raise _AssertionError(t)
889class _NamedTuple(tuple, _Named):
890 '''(INTERNAL) Base for named C{tuple}s with both index I{and}
891 attribute name access to the items.
893 @note: This class is similar to Python's C{namedtuple},
894 but statically defined, lighter and limited.
895 '''
896 _Names_ = () # item names, non-identifier, no leading underscore
897 '''Tuple specifying the C{name} of each C{Named-Tuple} item.
899 @note: Specify at least 2 item names.
900 '''
901 _Units_ = () # .units classes
902 '''Tuple defining the C{units} of the value of each C{Named-Tuple} item.
904 @note: The C{len(_Units_)} must match C{len(_Names_)}.
905 '''
906 _validated = False # set to True I{per sub-class!}
908 def __new__(cls, arg, *args, **iteration_name):
909 '''New L{_NamedTuple} initialized with B{C{positional}} arguments.
911 @arg arg: Tuple items (C{tuple}, C{list}, ...) or first tuple
912 item of several more in other positional arguments.
913 @arg args: Tuple items (C{any}), all positional arguments.
914 @kwarg iteration_name: Only keyword arguments C{B{iteration}=None}
915 and C{B{name}=NN} are used, any other are
916 I{silently} ignored.
918 @raise LenError: Unequal number of positional arguments and
919 number of item C{_Names_} or C{_Units_}.
921 @raise TypeError: The C{_Names_} or C{_Units_} attribute is
922 not a C{tuple} of at least 2 items.
924 @raise ValueError: Item name is not a C{str} or valid C{identifier}
925 or starts with C{underscore}.
926 '''
927 n, args = len2(((arg,) + args) if args else arg)
928 self = tuple.__new__(cls, args)
929 if not self._validated:
930 self._validate()
932 N = len(self._Names_)
933 if n != N:
934 raise LenError(self.__class__, args=n, _Names_=N)
936 if iteration_name:
937 self._kwdself(**iteration_name)
938 return self
940 def __delattr__(self, name):
941 '''Delete an attribute by B{C{name}}.
943 @note: Items can not be deleted.
944 '''
945 if name in self._Names_:
946 raise _TypeError(_del_, _DOT_(self.classname, name), txt=_immutable_)
947 elif name in (_name_, _name):
948 _Named.__setattr__(self, name, NN) # XXX _Named.name.fset(self, NN)
949 else:
950 tuple.__delattr__(self, name)
952 def __getattr__(self, name):
953 '''Get the value of an attribute or item by B{C{name}}.
954 '''
955 try:
956 return tuple.__getitem__(self, self._Names_.index(name))
957 except IndexError:
958 raise _IndexError(_DOT_(self.classname, Fmt.ANGLE(_name_)), name)
959 except ValueError: # e.g. _iteration
960 return tuple.__getattribute__(self, name)
962# def __getitem__(self, index): # index, slice, etc.
963# '''Get the item(s) at an B{C{index}} or slice.
964# '''
965# return tuple.__getitem__(self, index)
967 def __hash__(self):
968 return tuple.__hash__(self)
970 def __repr__(self):
971 '''Default C{repr(self)}.
972 '''
973 return self.toRepr()
975 def __setattr__(self, name, value):
976 '''Set attribute or item B{C{name}} to B{C{value}}.
977 '''
978 if name in self._Names_:
979 raise _TypeError(_DOT_(self.classname, name), value, txt=_immutable_)
980 elif name in (_name_, _name):
981 _Named.__setattr__(self, name, value) # XXX _Named.name.fset(self, value)
982 else: # e.g. _iteration
983 tuple.__setattr__(self, name, value)
985 def __str__(self):
986 '''Default C{repr(self)}.
987 '''
988 return self.toStr()
990 def dup(self, name=NN, **items):
991 '''Duplicate this tuple replacing one or more items.
993 @kwarg name: Optional new name (C{str}).
994 @kwarg items: Items to be replaced (C{name=value} pairs), if any.
996 @return: A copy of this tuple with B{C{items}}.
998 @raise NameError: Some B{C{items}} invalid.
999 '''
1000 tl = list(self)
1001 if items:
1002 _ix = self._Names_.index
1003 try:
1004 for n, v in items.items():
1005 tl[_ix(n)] = v
1006 except ValueError: # bad item name
1007 raise _NameError(_DOT_(self.classname, n), v, this=self)
1008 return self.classof(*tl, name=name or self.name)
1010 def items(self):
1011 '''Yield the items, each as a C{(name, value)} pair (C{2-tuple}).
1013 @see: Method C{.units}.
1014 '''
1015 for n, v in _zip(self._Names_, self): # strict=True
1016 yield n, v
1018 iteritems = items
1020 def _kwdself(self, iteration=None, name=NN, **unused):
1021 '''(INTERNAL) Set C{__new__} keyword arguments.
1022 '''
1023 if iteration is not None:
1024 self._iteration = iteration
1025 if name:
1026 self.name = name
1028 def toRepr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1029 '''Return this C{Named-Tuple} items as C{name=value} string(s).
1031 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1032 Trailing zero decimals are stripped for B{C{prec}} values
1033 of 1 and above, but kept for negative B{C{prec}} values.
1034 @kwarg sep: Separator to join (C{str}).
1035 @kwarg fmt: Optional C{float} format (C{letter}).
1037 @return: Tuple items (C{str}).
1038 '''
1039 t = pairs(self.items(), prec=prec, fmt=fmt)
1040# if self.name:
1041# t = (Fmt.EQUAL(name=repr(self.name)),) + t
1042 return Fmt.PAREN(self.named, sep.join(t)) # XXX (self.classname, sep.join(t))
1044 def toStr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1045 '''Return this C{Named-Tuple} items as string(s).
1047 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1048 Trailing zero decimals are stripped for B{C{prec}} values
1049 of 1 and above, but kept for negative B{C{prec}} values.
1050 @kwarg sep: Separator to join (C{str}).
1051 @kwarg fmt: Optional C{float} format (C{letter}).
1053 @return: Tuple items (C{str}).
1054 '''
1055 return Fmt.PAREN(sep.join(reprs(self, prec=prec, fmt=fmt)))
1057 def toUnits(self, Error=UnitError): # overloaded in .frechet, .hausdorff
1058 '''Return a copy of this C{Named-Tuple} with each item value wrapped
1059 as an instance of its L{units} class.
1061 @kwarg Error: Error to raise for L{units} issues (C{UnitError}).
1063 @return: A duplicate of this C{Named-Tuple} (C{C{Named-Tuple}}).
1065 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1066 '''
1067 t = (v for _, v in self.units(Error=Error))
1068 return self.classof(*tuple(t))
1070 def units(self, Error=UnitError):
1071 '''Yield the items, each as a C{(name, value}) pair (C{2-tuple}) with
1072 the value wrapped as an instance of its L{units} class.
1074 @kwarg Error: Error to raise for L{units} issues (C{UnitError}).
1076 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1078 @see: Method C{.items}.
1079 '''
1080 for n, v, U in _zip(self._Names_, self, self._Units_): # strict=True
1081 if not (v is None or U is None
1082 or (isclass(U) and
1083 isinstance(v, U) and
1084 hasattr(v, _name_) and
1085 v.name == n)): # PYCHOK indent
1086 v = U(v, name=n, Error=Error)
1087 yield n, v
1089 iterunits = units
1091 def _validate(self, underOK=False): # see .EcefMatrix
1092 '''(INTERNAL) One-time check of C{_Names_} and C{_Units_}
1093 for each C{_NamedUnit} I{sub-class separately}.
1094 '''
1095 ns = self._Names_
1096 if not (isinstance(ns, tuple) and len(ns) > 1): # XXX > 0
1097 raise _TypeError(_DOT_(self.classname, _Names_), ns)
1098 for i, n in enumerate(ns):
1099 if not _xvalid(n, underOK=underOK):
1100 t = Fmt.SQUARE(_Names_=i) # PYCHOK no cover
1101 raise _ValueError(_DOT_(self.classname, t), n)
1103 us = self._Units_
1104 if not isinstance(us, tuple):
1105 raise _TypeError(_DOT_(self.classname, _Units_), us)
1106 if len(us) != len(ns):
1107 raise LenError(self.__class__, _Units_=len(us), _Names_=len(ns))
1108 for i, u in enumerate(us):
1109 if not (u is None or callable(u)):
1110 t = Fmt.SQUARE(_Units_=i) # PYCHOK no cover
1111 raise _TypeError(_DOT_(self.classname, t), u)
1113 self.__class__._validated = True
1115 def _xtend(self, xTuple, *items, **name):
1116 '''(INTERNAL) Extend this C{Named-Tuple} with C{items} to an other B{C{xTuple}}.
1117 '''
1118 if (issubclassof(xTuple, _NamedTuple) and
1119 (len(self._Names_) + len(items)) == len(xTuple._Names_) and
1120 self._Names_ == xTuple._Names_[:len(self)]):
1121 return xTuple(self + items, **_xkwds(name, name=self.name)) # *(self + items)
1122 c = NN(self.classname, repr(self._Names_)) # PYCHOK no cover
1123 x = NN(xTuple.__name__, repr(xTuple._Names_)) # PYCHOK no cover
1124 raise TypeError(_SPACE_(c, _vs_, x))
1127def callername(up=1, dflt=NN, source=False, underOK=False):
1128 '''Get the name of the invoking callable.
1130 @kwarg up: Number of call stack frames up (C{int}).
1131 @kwarg dflt: Default return value (C{any}).
1132 @kwarg source: Include source file name and line number (C{bool}).
1133 @kwarg underOK: If C{True}, private, internal callables are OK,
1134 otherwise private callables are skipped (C{bool}).
1136 @return: The callable name (C{str}) or B{C{dflt}} if none found.
1137 '''
1138 try: # see .lazily._caller3
1139 for u in range(up, up + 32):
1140 n, f, s = _caller3(u)
1141 if n and (underOK or n.startswith(_DUNDER_) or
1142 not n.startswith(_UNDER_)):
1143 if source:
1144 n = NN(n, _AT_, f, _COLON_, str(s))
1145 return n
1146 except (AttributeError, ValueError):
1147 pass
1148 return dflt
1151def _callername2(args, callername=NN, source=False, underOK=False, up=2, **kwds):
1152 '''(INTERNAL) Extract C{callername}, C{source}, C{underOK} and C{up} from C{kwds}.
1153 '''
1154 n = callername or _MODS.named.callername(up=up + 1, source=source,
1155 underOK=underOK or bool(args or kwds))
1156 return n, kwds
1159def _callname(name, class_name, self_name, up=1):
1160 '''(INTERNAL) Assemble the name for an invokation.
1161 '''
1162 n, c = class_name, callername(up=up + 1)
1163 if c:
1164 n = _DOT_(n, Fmt.PAREN(c, name))
1165 if self_name:
1166 n = _SPACE_(n, repr(self_name))
1167 return n
1170def classname(inst, prefixed=None):
1171 '''Return the instance' class name optionally prefixed with the
1172 module name.
1174 @arg inst: The object (any C{type}).
1175 @kwarg prefixed: Include the module name (C{bool}), see
1176 function C{classnaming}.
1178 @return: The B{C{inst}}'s C{[module.]class} name (C{str}).
1179 '''
1180 if prefixed is None:
1181 prefixed = getattr(inst, classnaming.__name__, prefixed)
1182 return modulename(inst.__class__, prefixed=prefixed)
1185def classnaming(prefixed=None):
1186 '''Get/set the default class naming for C{[module.]class} names.
1188 @kwarg prefixed: Include the module name (C{bool}).
1190 @return: Previous class naming setting (C{bool}).
1191 '''
1192 t = _Named._classnaming
1193 if prefixed in (True, False):
1194 _Named._classnaming = prefixed
1195 return t
1198def modulename(clas, prefixed=None): # in .basics._xversion
1199 '''Return the class name optionally prefixed with the
1200 module name.
1202 @arg clas: The class (any C{class}).
1203 @kwarg prefixed: Include the module name (C{bool}), see
1204 function C{classnaming}.
1206 @return: The B{C{class}}'s C{[module.]class} name (C{str}).
1207 '''
1208 try:
1209 n = clas.__name__
1210 except AttributeError:
1211 n = '__name__' # _DUNDER_(NN, _name_, NN)
1212 if prefixed or (classnaming() if prefixed is None else False):
1213 try:
1214 m = clas.__module__.rsplit(_DOT_, 1)
1215 n = _DOT_.join(m[1:] + [n])
1216 except AttributeError:
1217 pass
1218 return n
1221def nameof(inst):
1222 '''Get the name of an instance.
1224 @arg inst: The object (any C{type}).
1226 @return: The instance' name (C{str}) or C{""}.
1227 '''
1228 n = _xattr(inst, name=NN)
1229 if not n: # and isinstance(inst, property):
1230 try:
1231 n = inst.fget.__name__
1232 except AttributeError:
1233 n = NN
1234 return n
1237def _notDecap(where):
1238 '''De-Capitalize C{where.__name__}.
1239 '''
1240 n = where.__name__
1241 c = n[3].lower() # len(_not_)
1242 return NN(n[:3], _SPACE_, c, n[4:])
1245def _notError(inst, name, args, kwds): # PYCHOK no cover
1246 '''(INTERNAL) Format an error message.
1247 '''
1248 n = _DOT_(classname(inst, prefixed=True), _dunder_nameof(name, name))
1249 m = _COMMASPACE_.join(modulename(c, prefixed=True) for c in inst.__class__.__mro__[1:-1])
1250 return _COMMASPACE_(unstr(n, *args, **kwds), Fmt.PAREN(_MRO_, m))
1253def _NotImplemented(inst, *other, **kwds):
1254 '''(INTERNAL) Raise a C{__special__} error or return C{NotImplemented},
1255 but only if env variable C{PYGEODESY_NOTIMPLEMENTED=std}.
1256 '''
1257 if _std_NotImplemented:
1258 return NotImplemented
1259 n, kwds = _callername2(other, **kwds) # source=True
1260 t = unstr(_DOT_(classname(inst), n), *other, **kwds)
1261 raise _NotImplementedError(t, txt=repr(inst))
1264def notImplemented(inst, *args, **kwds): # PYCHOK no cover
1265 '''Raise a C{NotImplementedError} for a missing instance method or
1266 property or for a missing caller feature.
1268 @arg inst: Instance (C{any}) or C{None} for caller.
1269 @arg args: Method or property positional arguments (any C{type}s).
1270 @arg kwds: Method or property keyword arguments (any C{type}s),
1271 except C{B{callername}=NN}, C{B{underOK}=False} and
1272 C{B{up}=2}.
1273 '''
1274 n, kwds = _callername2(args, **kwds)
1275 t = _notError(inst, n, args, kwds) if inst else unstr(n, *args, **kwds)
1276 raise _NotImplementedError(t, txt=_notDecap(notImplemented))
1279def notOverloaded(inst, *args, **kwds): # PYCHOK no cover
1280 '''Raise an C{AssertionError} for a method or property not overloaded.
1282 @arg inst: Instance (C{any}).
1283 @arg args: Method or property positional arguments (any C{type}s).
1284 @arg kwds: Method or property keyword arguments (any C{type}s),
1285 except C{B{callername}=NN}, C{B{underOK}=False} and
1286 C{B{up}=2}.
1287 '''
1288 n, kwds = _callername2(args, **kwds)
1289 t = _notError(inst, n, args, kwds)
1290 raise _AssertionError(t, txt=_notDecap(notOverloaded))
1293def _Pass(arg, **unused): # PYCHOK no cover
1294 '''(INTERNAL) I{Pass-thru} class for C{_NamedTuple._Units_}.
1295 '''
1296 return arg
1299__all__ += _ALL_DOCS(_Named,
1300 _NamedBase, # _NamedDict,
1301 _NamedEnum, _NamedEnumItem,
1302 _NamedTuple)
1304# **) MIT License
1305#
1306# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
1307#
1308# Permission is hereby granted, free of charge, to any person obtaining a
1309# copy of this software and associated documentation files (the "Software"),
1310# to deal in the Software without restriction, including without limitation
1311# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1312# and/or sell copies of the Software, and to permit persons to whom the
1313# Software is furnished to do so, subject to the following conditions:
1314#
1315# The above copyright notice and this permission notice shall be included
1316# in all copies or substantial portions of the Software.
1317#
1318# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1319# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1320# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1321# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1322# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1323# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1324# OTHER DEALINGS IN THE SOFTWARE.