Coverage for pygeodesy/named.py: 96%
434 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-12 12:31 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-12 12:31 -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, \
17 issubclassof, len2, _sizeof, _xcopy, _xdup, _zip
18from pygeodesy.errors import _AssertionError, _AttributeError, _incompatible, \
19 _IndexError, _IsnotError, itemsorted, LenError, \
20 _NameError, _NotImplementedError, _TypeError, \
21 _TypesError, _ValueError, UnitError, _xattr, \
22 _xkwds, _xkwds_get, _xkwds_pop, _xkwds_popitem
23from pygeodesy.interns import NN, _at_, _AT_, _COLON_, _COLONSPACE_, _COMMA_, \
24 _COMMASPACE_, _doesn_t_exist_, _DOT_, _DUNDER_, \
25 _EQUAL_, _EQUALSPACED_, _exists_, _immutable_, _name_, \
26 _NL_, _NN_, _not_, _other_, _s_, _SPACE_, _std_, \
27 _UNDER_, _valid_, _vs_, _dunder_nameof, _under
28from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _caller3, _getenv
29from pygeodesy.props import _allPropertiesOf_n, deprecated_method, Property_RO, \
30 _hasProperty, property_doc_, property_RO, \
31 _update_all, _update_attrs
32from pygeodesy.streprs import attrs, Fmt, lrstrip, pairs, reprs, unstr
34__all__ = _ALL_LAZY.named
35__version__ = '23.08.05'
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 not other and len(name_other) == 1
78 name, other = _xkwds_popitem(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, _OK=False):
94 '''(INTERNAL) Check valid attribute name C{name}.
95 '''
96 return True if (name and isstr(name)
97 and name != _name_
98 and (_OK or not name.startswith(_UNDER_))
99 and (not iskeyword(name))
100 and isidentifier(name)) else False
103class _Dict(dict):
104 '''(INTERNAL) An C{dict} with both key I{and}
105 attribute access to the C{dict} items.
106 '''
107 def __getattr__(self, name):
108 '''Get an attribute or item by B{C{name}}.
109 '''
110 try:
111 return self[name]
112 except KeyError:
113 pass
114 name = _DOT_(self.__class__.__name__, name)
115 raise _AttributeError(item=name, txt=_doesn_t_exist_)
117 def __repr__(self):
118 '''Default C{repr(self)}.
119 '''
120 return self.toRepr()
122 def __str__(self):
123 '''Default C{str(self)}.
124 '''
125 return self.toStr()
127 def set_(self, **items): # PYCHOK signature
128 '''Add one or several new items or replace existing ones.
130 @kwarg items: One or more C{name=value} pairs.
131 '''
132 dict.update(self, items)
134 def toRepr(self, **prec_fmt): # PYCHOK signature
135 '''Like C{repr(dict)} but with C{name} prefix and with
136 C{floats} formatted by function L{pygeodesy.fstr}.
137 '''
138 n = _xkwds_get(self, name=classname(self))
139 return Fmt.PAREN(n, self._toT(_EQUAL_, **prec_fmt))
141 def toStr(self, **prec_fmt): # PYCHOK signature
142 '''Like C{str(dict)} but with C{floats} formatted by
143 function L{pygeodesy.fstr}.
144 '''
145 return Fmt.CURLY(self._toT(_COLONSPACE_, **prec_fmt))
147 def _toT(self, sep, **kwds):
148 '''(INTERNAL) Helper for C{.toRepr} and C{.toStr}, also
149 in C{_NamedDict} below.
150 '''
151 kwds = _xkwds(kwds, prec=6, fmt=Fmt.F, sep=sep)
152 return _COMMASPACE_.join(pairs(itemsorted(self), **kwds))
155class _Named(object):
156 '''(INTERNAL) Root class for named objects.
157 '''
158 _iteration = None # iteration number (C{int}) or C{None}
159 _name = NN # name (C{str})
160 _classnaming = False # prefixed (C{bool})
161# _updates = 0 # OBSOLETE Property/property updates
163 def __imatmul__(self, other): # PYCHOK no cover
164 '''Not implemented.'''
165 return _NotImplemented(self, other) # PYCHOK Python 3.5+
167 def __matmul__(self, other): # PYCHOK no cover
168 '''Not implemented.'''
169 return _NotImplemented(self, other) # PYCHOK Python 3.5+
171 def __repr__(self):
172 '''Default C{repr(self)}.
173 '''
174 return Fmt.ANGLE(_SPACE_(self, _at_, hex(id(self))))
176 def __rmatmul__(self, other): # PYCHOK no cover
177 '''Not implemented.'''
178 return _NotImplemented(self, other) # PYCHOK Python 3.5+
180 def __str__(self):
181 '''Default C{str(self)}.
182 '''
183 return self.named2
185 def attrs(self, *names, **sep_COMMASPACE__Nones_True__pairs_kwds):
186 '''Join named attributes as I{name=value} strings, with C{float}s formatted by
187 function L{pygeodesy.fstr}.
189 @arg names: The attribute names, all positional (C{str}).
190 @kwarg sep_COMMASPACE__Nones_True__pairs_kwds: Keyword argument for function
191 L{pygeodesy.pairs}, except C{B{sep}=", "} and C{B{Nones}=True} to
192 in- or exclude missing or C{None}-valued attributes.
194 @return: All C{name=value} pairs, joined by B{C{sep}} (C{str}).
196 @see: Functions L{pygeodesy.attrs}, L{pygeodesy.fstr} and L{pygeodesy.pairs}.
197 '''
198 def _sep_kwds(sep=_COMMASPACE_, **kwds):
199 return sep, kwds
201 sep, kwds = _sep_kwds(**sep_COMMASPACE__Nones_True__pairs_kwds)
202 return sep.join(attrs(self, *names, **kwds))
204 @Property_RO
205 def classname(self):
206 '''Get this object's C{[module.]class} name (C{str}), see
207 property C{.classnaming} and function C{classnaming}.
208 '''
209 return classname(self, prefixed=self._classnaming)
211 @property_doc_(''' the class naming (C{bool}).''')
212 def classnaming(self):
213 '''Get the class naming (C{bool}), see function C{classnaming}.
214 '''
215 return self._classnaming
217 @classnaming.setter # PYCHOK setter!
218 def classnaming(self, prefixed):
219 '''Set the class naming for C{[module.].class} names (C{bool})
220 to C{True} to include the module name.
221 '''
222 b = bool(prefixed)
223 if self._classnaming != b:
224 self._classnaming = b
225 _update_attrs(self, *_Named_Property_ROs)
227 def classof(self, *args, **kwds):
228 '''Create another instance of this very class.
230 @arg args: Optional, positional arguments.
231 @kwarg kwds: Optional, keyword arguments.
233 @return: New instance (B{self.__class__}).
234 '''
235 return _xnamed(self.__class__(*args, **kwds), self.name)
237 def copy(self, deep=False, name=NN):
238 '''Make a shallow or deep copy of this instance.
240 @kwarg deep: If C{True} make a deep, otherwise
241 a shallow copy (C{bool}).
242 @kwarg name: Optional, non-empty name (C{str}).
244 @return: The copy (C{This class} or sub-class thereof).
245 '''
246 c = _xcopy(self, deep=deep)
247 if name:
248 c.rename(name)
249 return c
251 def _DOT_(self, *names):
252 '''(INTERNAL) Period-join C{self.name} and C{names}.
253 '''
254 return _DOT_(self.name, *names)
256 def dup(self, name=NN, **items):
257 '''Duplicate this instance, replacing some items.
259 @kwarg name: Optional, non-empty name (C{str}).
260 @kwarg items: Attributes to be changed (C{any}).
262 @return: The duplicate (C{This class} or sub-class thereof).
264 @raise AttributeError: Some B{C{items}} invalid.
265 '''
266 d = _xdup(self, **items)
267 if name:
268 d.rename(name=name)
269 return d
271 def _instr(self, name, prec, *attrs, **props_kwds):
272 '''(INTERNAL) Format, used by C{Conic}, C{Ellipsoid}, C{Transform}, C{Triaxial}.
273 '''
274 def _props_kwds(props=(), **kwds):
275 return props, kwds
277 t = Fmt.EQUAL(_name_, repr(name or self.name)),
278 if attrs:
279 t += pairs(((a, getattr(self, a)) for a in attrs),
280 prec=prec, ints=True)
281 props, kwds =_props_kwds(**props_kwds)
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 _COMMASPACE_.join(t[1:] if name is None 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, which.__name__ 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 name (C{str}).
316 @raise NameError: Can't rename, use method L{rename}.
317 '''
318 m, n = self._name, str(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(self.rename.__name__, n))
325 m = Fmt.PAREN(_SPACE_('was', repr(m)))
326 n = _DOT_(c, _EQUALSPACED_(_name_, n))
327 n = _SPACE_(n, m)
328 raise _NameError(_SPACE_('use', t), txt=_not_(n))
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 @Property_RO
335 def named(self):
336 '''Get the name I{or} class name or C{""} (C{str}).
337 '''
338 return self.name or self.classname
340 @Property_RO
341 def named2(self):
342 '''Get the C{class} name I{and/or} the name or C{""} (C{str}).
343 '''
344 return _xjoined_(self.classname, self.name)
346 @Property_RO
347 def named3(self):
348 '''Get the I{prefixed} C{class} name I{and/or} the name or C{""} (C{str}).
349 '''
350 return _xjoined_(classname(self, prefixed=True), self.name)
352 @Property_RO
353 def named4(self):
354 '''Get the C{package.module.class} name I{and/or} the name or C{""} (C{str}).
355 '''
356 return _xjoined_(_DOT_(self.__module__, self.__class__.__name__), self.name)
358 def rename(self, name):
359 '''Change the name.
361 @arg name: The new name (C{str}).
363 @return: The old name (C{str}).
364 '''
365 m, n = self._name, str(name)
366 if n != m:
367 self._name = n
368 _update_attrs(self, *_Named_Property_ROs)
369 return m
371 @property_RO
372 def sizeof(self):
373 '''Get the current size in C{bytes} of this instance (C{int}).
374 '''
375 return _sizeof(self)
377 def toRepr(self, **unused): # PYCHOK no cover
378 '''Default C{repr(self)}.
379 '''
380 return repr(self)
382 def toStr(self, **unused): # PYCHOK no cover
383 '''Default C{str(self)}.
384 '''
385 return str(self)
387 @deprecated_method
388 def toStr2(self, **kwds): # PYCHOK no cover
389 '''DEPRECATED, use method C{toRepr}.'''
390 return self.toRepr(**kwds)
392 def _unstr(self, which, *args, **kwds):
393 '''(INTERNAL) Return the string representation of a method
394 invokation of this instance: C{str(self).method(...)}
396 @see: Function L{pygeodesy.unstr}.
397 '''
398 n = _DOT_(self, which.__name__ if callable(which) else _NN_)
399 return unstr(n, *args, **kwds)
401 def _xnamed(self, inst, name=NN, force=False):
402 '''(INTERNAL) Set the instance' C{.name = self.name}.
404 @arg inst: The instance (C{_Named}).
405 @kwarg name: Optional name, overriding C{self.name} (C{str}).
406 @kwarg force: Force name change (C{bool}).
408 @return: The B{C{inst}}, named if not named before.
409 '''
410 return _xnamed(inst, name or self.name, force=force)
412 def _xrenamed(self, inst):
413 '''(INTERNAL) Rename the instance' C{.name = self.name}.
415 @arg inst: The instance (C{_Named}).
417 @return: The B{C{inst}}, named if not named before.
419 @raise TypeError: Not C{isinstance(B{inst}, _Named)}.
420 '''
421 if not isinstance(inst, _Named):
422 raise _IsnotError(_valid_, inst=inst)
424 inst.rename(self.name)
425 return inst
427_Named_Property_ROs = _allPropertiesOf_n(5, _Named, Property_RO) # PYCHOK once
430class _NamedBase(_Named):
431 '''(INTERNAL) Base class with name.
432 '''
433 def __repr__(self):
434 '''Default C{repr(self)}.
435 '''
436 return self.toRepr()
438 def __str__(self):
439 '''Default C{str(self)}.
440 '''
441 return self.toStr()
443# def notImplemented(self, attr):
444# '''Raise error for a missing method, function or attribute.
445#
446# @arg attr: Attribute name (C{str}).
447#
448# @raise NotImplementedError: No such attribute.
449# '''
450# c = self.__class__.__name__
451# return NotImplementedError(_DOT_(c, attr))
453 def others(self, *other, **name_other_up): # see .points.LatLon_.others
454 '''Refined class comparison, invoked as C{.others(other=other)},
455 C{.others(name=other)} or C{.others(other, name='other')}.
457 @arg other: The other instance (any C{type}).
458 @kwarg name_other_up: Overriding C{name=other} and C{up=1}
459 keyword arguments.
461 @return: The B{C{other}} iff compatible with this instance's
462 C{class} or C{type}.
464 @raise TypeError: Mismatch of the B{C{other}} and this
465 instance's C{class} or C{type}.
466 '''
467 if other: # most common, just one arg B{C{other}}
468 other0 = other[0]
469 if isinstance(other0, self.__class__) or \
470 isinstance(self, other0.__class__):
471 return other0
473 other, name, up = _xother3(self, other, **name_other_up)
474 if isinstance(self, other.__class__) or \
475 isinstance(other, self.__class__):
476 return _xnamed(other, name)
478 raise _xotherError(self, other, name=name, up=up + 1)
480 def toRepr(self, **kwds): # PYCHOK expected
481 '''(INTERNAL) I{Could be overloaded}.
483 @kwarg kwds: Optional, C{toStr} keyword arguments.
485 @return: C{toStr}() with keyword arguments (as C{str}).
486 '''
487 t = lrstrip(self.toStr(**kwds))
488# if self.name:
489# t = NN(Fmt.EQUAL(name=repr(self.name)), sep, t)
490 return Fmt.PAREN(self.classname, t) # XXX (self.named, t)
492# def toRepr(self, **kwds)
493# if kwds:
494# s = NN.join(reprs((self,), **kwds))
495# else: # super().__repr__ only for Python 3+
496# s = super(self.__class__, self).__repr__()
497# return Fmt.PAREN(self.named, s) # clips(s)
499 def toStr(self, **kwds): # PYCHOK no cover
500 '''(INTERNAL) I{Must be overloaded}, see function C{notOverloaded}.
501 '''
502 notOverloaded(self, **kwds)
504# def toStr(self, **kwds):
505# if kwds:
506# s = NN.join(strs((self,), **kwds))
507# else: # super().__str__ only for Python 3+
508# s = super(self.__class__, self).__str__()
509# return s
511 def _update(self, updated, *attrs, **setters):
512 '''(INTERNAL) Zap cached instance attributes and overwrite C{__dict__} or L{Property_RO} values.
513 '''
514 u = _update_all(self, *attrs) if updated else 0
515 if setters:
516 d = self.__dict__
517 # double-check that setters are Property_RO's
518 for n, v in setters.items():
519 if n in d or _hasProperty(self, n, Property_RO):
520 d[n] = v
521 else:
522 raise _AssertionError(n, v, txt=repr(self))
523 u += len(setters)
524 return u
527class _NamedDict(_Dict, _Named):
528 '''(INTERNAL) Named C{dict} with key I{and} attribute
529 access to the items.
530 '''
531 def __init__(self, *args, **kwds):
532 if args: # args override kwds
533 if len(args) != 1:
534 t = unstr(self.classname, *args, **kwds) # PYCHOK no cover
535 raise _ValueError(args=len(args), txt=t)
536 kwds = _xkwds(dict(args[0]), **kwds)
537 if _name_ in kwds:
538 _Named.name.fset(self, kwds.pop(_name_)) # see _Named.name
539 _Dict.__init__(self, kwds)
541 def __delattr__(self, name):
542 '''Delete an attribute or item by B{C{name}}.
543 '''
544 if name in _Dict.keys(self):
545 _Dict.pop(name)
546 elif name in (_name_, _name):
547 # _Dict.__setattr__(self, name, NN)
548 _Named.rename(self, NN)
549 else:
550 _Dict.__delattr__(self, name)
552 def __getattr__(self, name):
553 '''Get an attribute or item by B{C{name}}.
554 '''
555 try:
556 return self[name]
557 except KeyError:
558 if name == _name_:
559 return _Named.name.fget(self)
560 raise _AttributeError(item=self._DOT_(name), txt=_doesn_t_exist_)
562 def __getitem__(self, key):
563 '''Get the value of an item by B{C{key}}.
564 '''
565 if key == _name_:
566 raise KeyError(Fmt.SQUARE(self.classname, key))
567 return _Dict.__getitem__(self, key)
569 def __setattr__(self, name, value):
570 '''Set attribute or item B{C{name}} to B{C{value}}.
571 '''
572 if name in _Dict.keys(self):
573 _Dict.__setitem__(self, name, value) # self[name] = value
574 else:
575 _Dict.__setattr__(self, name, value)
577 def __setitem__(self, key, value):
578 '''Set item B{C{key}} to B{C{value}}.
579 '''
580 if key == _name_:
581 raise KeyError(_EQUAL_(Fmt.SQUARE(self.classname, key), repr(value)))
582 _Dict.__setitem__(self, key, value)
584 def toRepr(self, **prec_fmt): # PYCHOK signature
585 '''Like C{repr(dict)} but with C{name} prefix and with
586 C{floats} formatted by function L{pygeodesy.fstr}.
587 '''
588 return Fmt.PAREN(self.name, self._toT(_EQUAL_, **prec_fmt))
590 def toStr(self, **prec_fmt): # PYCHOK signature
591 '''Like C{str(dict)} but with C{floats} formatted by
592 function L{pygeodesy.fstr}.
593 '''
594 return Fmt.CURLY(self._toT(_COLONSPACE_, **prec_fmt))
597class _NamedEnum(_NamedDict):
598 '''(INTERNAL) Enum-like C{_NamedDict} with attribute access
599 restricted to valid keys.
600 '''
601 _item_Classes = ()
603 def __init__(self, Class, *Classes, **name):
604 '''New C{_NamedEnum}.
606 @arg Class: Initial class or type acceptable as items
607 values (C{type}).
608 @arg Classes: Additional, acceptable classes or C{type}s.
609 '''
610 self._item_Classes = (Class,) + Classes
611 n = _xkwds_get(name, name=NN) or NN(Class.__name__, _s_)
612 if n and _xvalid(n, _OK=True):
613 _Named.name.fset(self, n) # see _Named.name
615 def __getattr__(self, name):
616 '''Get the value of an attribute or item by B{C{name}}.
617 '''
618 try:
619 return self[name]
620 except KeyError:
621 if name == _name_:
622 return _NamedDict.name.fget(self)
623 raise _AttributeError(item=self._DOT_(name), txt=_doesn_t_exist_)
625 def __repr__(self):
626 '''Default C{repr(self)}.
627 '''
628 return self.toRepr()
630 def __str__(self):
631 '''Default C{str(self)}.
632 '''
633 return self.toStr()
635 def _assert(self, **kwds):
636 '''(INTERNAL) Check attribute name against given, registered name.
637 '''
638 for n, v in kwds.items():
639 if isinstance(v, _LazyNamedEnumItem): # property
640 assert n is v.name
641 # assert not hasattr(self.__class__, n)
642 setattr(self.__class__, n, v)
643 elif isinstance(v, self._item_Classes): # PYCHOK no cover
644 assert self[n] is v and getattr(self, n) \
645 and self.find(v) == n
646 else:
647 raise _TypeError(v, name=n)
649 def find(self, item, dflt=None):
650 '''Find a registered item.
652 @arg item: The item to look for (any C{type}).
653 @kwarg dflt: Value to return if not found (any C{type}).
655 @return: The B{C{item}}'s name if found (C{str}), or C{{dflt}} if
656 there is no such I{registered} B{C{item}}.
657 '''
658 for k, v in self.items(): # or _Dict.items(self)
659 if v is item:
660 return k
661 return dflt
663 def get(self, name, dflt=None):
664 '''Get the value of a I{registered} item.
666 @arg name: The name of the item (C{str}).
667 @kwarg dflt: Value to return (any C{type}).
669 @return: The item with B{C{name}} if found, or B{C{dflt}} if
670 there is no item I{registered} with that B{C{name}}.
671 '''
672 # getattr needed to instantiate L{_LazyNamedEnumItem}
673 return getattr(self, name, dflt)
675 def items(self, all=False, asorted=False):
676 '''Yield all or only the I{registered} items.
678 @kwarg all: Use C{True} to yield {all} items or C{False}
679 for only the currently I{registered} ones.
680 @kwarg asorted: If C{True}, yield the items sorted in
681 I{alphabetical, case-insensitive} order.
682 '''
683 if all: # instantiate any remaining L{_LazyNamedEnumItem} ...
684 # ... and remove the L{_LazyNamedEnumItem} from the class
685 for n in tuple(n for n, p in self.__class__.__dict__.items()
686 if isinstance(p, _LazyNamedEnumItem)):
687 _ = getattr(self, n)
688 return itemsorted(self) if asorted else _Dict.items(self)
690 def keys(self, **all_asorted):
691 '''Yield the keys (C{str}) of all or only the I{registered} items,
692 optionally sorted I{alphabetically} and I{case-insensitively}.
694 @kwarg all_asorted: See method C{items}..
695 '''
696 for k, _ in self.items(**all_asorted):
697 yield k
699 def popitem(self):
700 '''Remove I{an, any} curretly I{registed} item.
702 @return: The removed item.
703 '''
704 return self._zapitem(*_Dict.pop(self))
706 def register(self, item):
707 '''Registed a new item.
709 @arg item: The item (any C{type}).
711 @return: The item name (C{str}).
713 @raise NameError: An B{C{item}} already registered with
714 that name or the B{C{item}} has no, an
715 empty or an invalid name.
717 @raise TypeError: The B{C{item}} type invalid.
718 '''
719 try:
720 n = item.name
721 if not (n and isstr(n) and isidentifier(n)):
722 raise ValueError
723 except (AttributeError, ValueError, TypeError) as x:
724 raise _NameError(_DOT_(_item_, _name_), item, cause=x)
725 if n in self:
726 raise _NameError(self._DOT_(n), item, txt=_exists_)
727 if not (self._item_Classes and isinstance(item, self._item_Classes)):
728 raise _TypesError(self._DOT_(n), item, *self._item_Classes)
729 self[n] = item
731 def unregister(self, name_or_item):
732 '''Remove a I{registered} item.
734 @arg name_or_item: Name (C{str}) or the item (any C{type}).
736 @return: The unregistered item.
738 @raise NameError: No item with that B{C{name}}.
740 @raise ValueError: No such item.
741 '''
742 if isstr(name_or_item):
743 name = name_or_item
744 else:
745 name = self.find(name_or_item)
746 try:
747 item = _Dict.pop(self, name)
748 except KeyError:
749 raise _NameError(item=self._DOT_(name), txt=_doesn_t_exist_)
750 return self._zapitem(name, item)
752 pop = unregister
754 def toRepr(self, prec=6, fmt=Fmt.F, sep=_COMMANL_, **all_asorted): # PYCHOK _NamedDict
755 '''Like C{repr(dict)} but C{name}s optionally sorted and
756 C{floats} formatted by function L{pygeodesy.fstr}.
757 '''
758 t = ((self._DOT_(n), v) for n, v in self.items(**all_asorted))
759 return sep.join(pairs(t, prec=prec, fmt=fmt, sep=_COLONSPACE_))
761 def toStr(self, *unused, **all_asorted): # PYCHOK _NamedDict
762 '''Return a string with all C{name}s, optionally sorted.
763 '''
764 return self._DOT_(_COMMASPACEDOT_.join(self.keys(**all_asorted)))
766 def values(self, **all_asorted):
767 '''Yield the value (C{type}) of all or only the I{registered} items,
768 optionally sorted I{alphabetically} and I{case-insensitively}.
770 @kwarg all_asorted: See method C{items}.
771 '''
772 for _, v in self.items(**all_asorted):
773 yield v
775 def _zapitem(self, name, item):
776 # remove _LazyNamedEnumItem property value if still present
777 if self.__dict__.get(name, None) is item:
778 self.__dict__.pop(name) # [name] = None
779 item._enum = None
780 return item
783class _LazyNamedEnumItem(property_RO): # XXX or descriptor?
784 '''(INTERNAL) Lazily instantiated L{_NamedEnumItem}.
785 '''
786 pass
789def _lazyNamedEnumItem(name, *args, **kwds):
790 '''(INTERNAL) L{_LazyNamedEnumItem} property-like factory.
792 @see: Luciano Ramalho, "Fluent Python", page 636, O'Reilly, 2016,
793 "Coding a Property Factory", especially Example 19-24.
794 '''
795 def _fget(inst):
796 # assert isinstance(inst, _NamedEnum)
797 try: # get the item from the instance' __dict__
798 # item = inst.__dict__[name] # ... or _Dict
799 item = inst[name]
800 except KeyError:
801 # instantiate an _NamedEnumItem, it self-registers
802 item = inst._Lazy(*args, **_xkwds(kwds, name=name))
803 # assert inst[name] is item # MUST be registered
804 # store the item in the instance' __dict__ ...
805 # inst.__dict__[name] = item # ... or update the
806 inst.update({name: item}) # ... _Dict for Triaxials
807 # remove the property from the registry class, such that
808 # (a) the property no longer overrides the instance' item
809 # in inst.__dict__ and (b) _NamedEnum.items(all=True) only
810 # sees any un-instantiated ones yet to be instantiated
811 p = getattr(inst.__class__, name, None)
812 if isinstance(p, _LazyNamedEnumItem):
813 delattr(inst.__class__, name)
814 # assert isinstance(item, _NamedEnumItem)
815 return item
817 p = _LazyNamedEnumItem(_fget)
818 p.name = name
819 return p
822class _NamedEnumItem(_NamedBase):
823 '''(INTERNAL) Base class for items in a C{_NamedEnum} registery.
824 '''
825 _enum = None
827# def __ne__(self, other): # XXX fails for Lcc.conic = conic!
828# '''Compare this and an other item.
829#
830# @return: C{True} if different, C{False} otherwise.
831# '''
832# return not self.__eq__(other)
834 @property_doc_(''' the I{registered} name (C{str}).''')
835 def name(self):
836 '''Get the I{registered} name (C{str}).
837 '''
838 return self._name
840 @name.setter # PYCHOK setter!
841 def name(self, name):
842 '''Set the name, unless already registered (C{str}).
843 '''
844 if self._enum:
845 raise _NameError(str(name), self, txt=_registered_) # XXX _TypeError
846 self._name = str(name)
848 def _register(self, enum, name):
849 '''(INTERNAL) Add this item as B{C{enum.name}}.
851 @note: Don't register if name is empty or doesn't
852 start with a letter.
853 '''
854 if name and _xvalid(name, _OK=True):
855 self.name = name
856 if name[:1].isalpha(): # '_...' not registered
857 enum.register(self)
858 self._enum = enum
860 def unregister(self):
861 '''Remove this instance from its C{_NamedEnum} registry.
863 @raise AssertionError: Mismatch of this and registered item.
865 @raise NameError: This item is unregistered.
866 '''
867 enum = self._enum
868 if enum and self.name and self.name in enum:
869 item = enum.unregister(self.name)
870 if item is not self:
871 t = _SPACE_(repr(item), _vs_, repr(self)) # PYCHOK no cover
872 raise _AssertionError(t)
875class _NamedTuple(tuple, _Named):
876 '''(INTERNAL) Base for named C{tuple}s with both index I{and}
877 attribute name access to the items.
879 @note: This class is similar to Python's C{namedtuple},
880 but statically defined, lighter and limited.
881 '''
882 _Names_ = () # item names, non-identifier, no leading underscore
883 '''Tuple specifying the C{name} of each C{Named-Tuple} item.
885 @note: Specify at least 2 item names.
886 '''
887 _Units_ = () # .units classes
888 '''Tuple defining the C{units} of the value of each C{Named-Tuple} item.
890 @note: The C{len(_Units_)} must match C{len(_Names_)}.
891 '''
892 _validated = False # set to True I{per sub-class!}
894 def __new__(cls, arg, *args, **iteration_name):
895 '''New L{_NamedTuple} initialized with B{C{positional}} arguments.
897 @arg arg: Tuple items (C{tuple}, C{list}, ...) or first tuple
898 item of several more in other positional arguments.
899 @arg args: Tuple items (C{any}), all positional arguments.
900 @kwarg iteration_name: Only keyword arguments C{B{iteration}=None}
901 and C{B{name}=NN} are used, any other are
902 I{silently} ignored.
904 @raise LenError: Unequal number of positional arguments and
905 number of item C{_Names_} or C{_Units_}.
907 @raise TypeError: The C{_Names_} or C{_Units_} attribute is
908 not a C{tuple} of at least 2 items.
910 @raise ValueError: Item name is not a C{str} or valid C{identifier}
911 or starts with C{underscore}.
912 '''
913 n, args = len2(((arg,) + args) if args else arg)
914 self = tuple.__new__(cls, args)
915 if not self._validated:
916 self._validate()
918 N = len(self._Names_)
919 if n != N:
920 raise LenError(self.__class__, args=n, _Names_=N)
922 if iteration_name:
923 self._kwdself(**iteration_name)
924 return self
926 def __delattr__(self, name):
927 '''Delete an attribute by B{C{name}}.
929 @note: Items can not be deleted.
930 '''
931 if name in self._Names_:
932 raise _TypeError(_del_, _DOT_(self.classname, name), txt=_immutable_)
933 elif name in (_name_, _name):
934 _Named.__setattr__(self, name, NN) # XXX _Named.name.fset(self, NN)
935 else:
936 tuple.__delattr__(self, name)
938 def __getattr__(self, name):
939 '''Get the value of an attribute or item by B{C{name}}.
940 '''
941 try:
942 return tuple.__getitem__(self, self._Names_.index(name))
943 except IndexError:
944 raise _IndexError(_DOT_(self.classname, Fmt.ANGLE(_name_)), name)
945 except ValueError: # e.g. _iteration
946 return tuple.__getattribute__(self, name)
948# def __getitem__(self, index): # index, slice, etc.
949# '''Get the item(s) at an B{C{index}} or slice.
950# '''
951# return tuple.__getitem__(self, index)
953 def __repr__(self):
954 '''Default C{repr(self)}.
955 '''
956 return self.toRepr()
958 def __setattr__(self, name, value):
959 '''Set attribute or item B{C{name}} to B{C{value}}.
960 '''
961 if name in self._Names_:
962 raise _TypeError(_DOT_(self.classname, name), value, txt=_immutable_)
963 elif name in (_name_, _name):
964 _Named.__setattr__(self, name, value) # XXX _Named.name.fset(self, value)
965 else: # e.g. _iteration
966 tuple.__setattr__(self, name, value)
968 def __str__(self):
969 '''Default C{repr(self)}.
970 '''
971 return self.toStr()
973 def dup(self, name=NN, **items):
974 '''Duplicate this tuple replacing one or more items.
976 @kwarg name: Optional new name (C{str}).
977 @kwarg items: Items to be replaced (C{name=value} pairs), if any.
979 @return: A copy of this tuple with B{C{items}}.
981 @raise NameError: Some B{C{items}} invalid.
982 '''
983 tl = list(self)
984 if items:
985 _ix = self._Names_.index
986 try:
987 for n, v in items.items():
988 tl[_ix(n)] = v
989 except ValueError: # bad item name
990 raise _NameError(_DOT_(self.classname, n), v, this=self)
991 return self.classof(*tl, name=name or self.name)
993 def items(self):
994 '''Yield the items, each as a C{(name, value)} pair (C{2-tuple}).
996 @see: Method C{.units}.
997 '''
998 for n, v in _zip(self._Names_, self): # strict=True
999 yield n, v
1001 iteritems = items
1003 def _kwdself(self, iteration=None, name=NN, **unused):
1004 '''(INTERNAL) Set C{__new__} keyword arguments.
1005 '''
1006 if iteration is not None:
1007 self._iteration = iteration
1008 if name:
1009 self.name = name
1011 def toRepr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1012 '''Return this C{Named-Tuple} items as C{name=value} string(s).
1014 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1015 Trailing zero decimals are stripped for B{C{prec}} values
1016 of 1 and above, but kept for negative B{C{prec}} values.
1017 @kwarg sep: Separator to join (C{str}).
1018 @kwarg fmt: Optional, C{float} format (C{str}).
1020 @return: Tuple items (C{str}).
1021 '''
1022 t = pairs(self.items(), prec=prec, fmt=fmt)
1023# if self.name:
1024# t = (Fmt.EQUAL(name=repr(self.name)),) + t
1025 return Fmt.PAREN(self.named, sep.join(t)) # XXX (self.classname, sep.join(t))
1027 def toStr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature
1028 '''Return this C{Named-Tuple} items as string(s).
1030 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
1031 Trailing zero decimals are stripped for B{C{prec}} values
1032 of 1 and above, but kept for negative B{C{prec}} values.
1033 @kwarg sep: Separator to join (C{str}).
1034 @kwarg fmt: Optional C{float} format (C{str}).
1036 @return: Tuple items (C{str}).
1037 '''
1038 return Fmt.PAREN(sep.join(reprs(self, prec=prec, fmt=fmt)))
1040 def toUnits(self, Error=UnitError): # overloaded in .frechet, .hausdorff
1041 '''Return a copy of this C{Named-Tuple} with each item value wrapped
1042 as an instance of its L{units} class.
1044 @kwarg Error: Error to raise for L{units} issues (C{UnitError}).
1046 @return: A duplicate of this C{Named-Tuple} (C{C{Named-Tuple}}).
1048 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1049 '''
1050 t = (v for _, v in self.units(Error=Error))
1051 return self.classof(*tuple(t))
1053 def units(self, Error=UnitError):
1054 '''Yield the items, each as a C{(name, value}) pair (C{2-tuple}) with
1055 the value wrapped as an instance of its L{units} class.
1057 @kwarg Error: Error to raise for L{units} issues (C{UnitError}).
1059 @raise Error: Invalid C{Named-Tuple} item or L{units} class.
1061 @see: Method C{.items}.
1062 '''
1063 for n, v, U in _zip(self._Names_, self, self._Units_): # strict=True
1064 if not (v is None or U is None
1065 or (isclass(U) and
1066 isinstance(v, U) and
1067 hasattr(v, _name_) and
1068 v.name == n)): # PYCHOK indent
1069 v = U(v, name=n, Error=Error)
1070 yield n, v
1072 iterunits = units
1074 def _validate(self, _OK=False): # see .EcefMatrix
1075 '''(INTERNAL) One-time check of C{_Names_} and C{_Units_}
1076 for each C{_NamedUnit} I{sub-class separately}.
1077 '''
1078 ns = self._Names_
1079 if not (isinstance(ns, tuple) and len(ns) > 1): # XXX > 0
1080 raise _TypeError(_DOT_(self.classname, _Names_), ns)
1081 for i, n in enumerate(ns):
1082 if not _xvalid(n, _OK=_OK):
1083 t = Fmt.SQUARE(_Names_=i) # PYCHOK no cover
1084 raise _ValueError(_DOT_(self.classname, t), n)
1086 us = self._Units_
1087 if not isinstance(us, tuple):
1088 raise _TypeError(_DOT_(self.classname, _Units_), us)
1089 if len(us) != len(ns):
1090 raise LenError(self.__class__, _Units_=len(us), _Names_=len(ns))
1091 for i, u in enumerate(us):
1092 if not (u is None or callable(u)):
1093 t = Fmt.SQUARE(_Units_=i) # PYCHOK no cover
1094 raise _TypeError(_DOT_(self.classname, t), u)
1096 self.__class__._validated = True
1098 def _xtend(self, xTuple, *items):
1099 '''(INTERNAL) Extend this C{Named-Tuple} with C{items} to an other B{C{xTuple}}.
1100 '''
1101 if (issubclassof(xTuple, _NamedTuple) and
1102 (len(self._Names_) + len(items)) == len(xTuple._Names_) and
1103 self._Names_ == xTuple._Names_[:len(self)]):
1104 return self._xnamed(xTuple(self + items)) # *(self + items)
1105 c = NN(self.classname, repr(self._Names_)) # PYCHOK no cover
1106 x = NN(xTuple.__name__, repr(xTuple._Names_)) # PYCHOK no cover
1107 raise TypeError(_SPACE_(c, _vs_, x))
1110def callername(up=1, dflt=NN, source=False, underOK=False):
1111 '''Get the name of the invoking callable.
1113 @kwarg up: Number of call stack frames up (C{int}).
1114 @kwarg dflt: Default return value (C{any}).
1115 @kwarg source: Include source file name and line
1116 number (C{bool}).
1117 @kwarg underOK: Private, internal callables are OK (C{bool}).
1119 @return: The callable name (C{str}) or B{C{dflt}} if none found.
1120 '''
1121 try: # see .lazily._caller3
1122 for u in range(up, up + 32):
1123 n, f, s = _caller3(u)
1124 if n and (underOK or n.startswith(_DUNDER_) or
1125 not n.startswith(_UNDER_)):
1126 if source:
1127 n = NN(n, _AT_, f, _COLON_, str(s))
1128 return n
1129 except (AttributeError, ValueError):
1130 pass
1131 return dflt
1134def _callname(name, class_name, self_name, up=1):
1135 '''(INTERNAL) Assemble the name for an invokation.
1136 '''
1137 n, c = class_name, callername(up=up + 1)
1138 if c:
1139 n = _DOT_(n, Fmt.PAREN(c, name))
1140 if self_name:
1141 n = _SPACE_(n, repr(self_name))
1142 return n
1145def classname(inst, prefixed=None):
1146 '''Return the instance' class name optionally prefixed with the
1147 module name.
1149 @arg inst: The object (any C{type}).
1150 @kwarg prefixed: Include the module name (C{bool}), see
1151 function C{classnaming}.
1153 @return: The B{C{inst}}'s C{[module.]class} name (C{str}).
1154 '''
1155 if prefixed is None:
1156 prefixed = getattr(inst, classnaming.__name__, prefixed)
1157 return modulename(inst.__class__, prefixed=prefixed)
1160def classnaming(prefixed=None):
1161 '''Get/set the default class naming for C{[module.]class} names.
1163 @kwarg prefixed: Include the module name (C{bool}).
1165 @return: Previous class naming setting (C{bool}).
1166 '''
1167 t = _Named._classnaming
1168 if prefixed in (True, False):
1169 _Named._classnaming = prefixed
1170 return t
1173def modulename(clas, prefixed=None): # in .basics._xversion
1174 '''Return the class name optionally prefixed with the
1175 module name.
1177 @arg clas: The class (any C{class}).
1178 @kwarg prefixed: Include the module name (C{bool}), see
1179 function C{classnaming}.
1181 @return: The B{C{class}}'s C{[module.]class} name (C{str}).
1182 '''
1183 try:
1184 n = clas.__name__
1185 except AttributeError:
1186 n = '__name__' # _DUNDER_(NN, _name_, NN)
1187 if prefixed or (classnaming() if prefixed is None else False):
1188 try:
1189 m = clas.__module__.rsplit(_DOT_, 1)
1190 n = _DOT_.join(m[1:] + [n])
1191 except AttributeError:
1192 pass
1193 return n
1196def nameof(inst):
1197 '''Get the name of an instance.
1199 @arg inst: The object (any C{type}).
1201 @return: The instance' name (C{str}) or C{""}.
1202 '''
1203 n = _xattr(inst, name=NN)
1204 if not n: # and isinstance(inst, property):
1205 try:
1206 n = inst.fget.__name__
1207 except AttributeError:
1208 n = NN
1209 return n
1212def _notError(inst, name, args, kwds): # PYCHOK no cover
1213 '''(INTERNAL) Format an error message.
1214 '''
1215 n = _DOT_(classname(inst, prefixed=True), _dunder_nameof(name, name))
1216 m = _COMMASPACE_.join(modulename(c, prefixed=True) for c in inst.__class__.__mro__[1:-1])
1217 return _COMMASPACE_(unstr(n, *args, **kwds), Fmt.PAREN(_MRO_, m))
1220def _NotImplemented(inst, *other, **kwds):
1221 '''(INTERNAL) Raise a C{__special__} error or return C{NotImplemented},
1222 but only if env variable C{PYGEODESY_NOTIMPLEMENTED=std}.
1223 '''
1224 if _std_NotImplemented:
1225 return NotImplemented
1226 u = _xkwds_pop(kwds, up=2)
1227 n = _DOT_(classname(inst), callername(up=u, underOK=True)) # source=True
1228 raise _NotImplementedError(unstr(n, *other, **kwds), txt=repr(inst))
1231def notImplemented(inst, *args, **kwds): # PYCHOK no cover
1232 '''Raise a C{NotImplementedError} for a missing instance method or
1233 property or for a missing caller feature.
1235 @arg inst: Instance (C{any}) or C{None} for caller.
1236 @arg args: Method or property positional arguments (any C{type}s).
1237 @arg kwds: Method or property keyword arguments (any C{type}s).
1238 '''
1239 u = _xkwds_pop(kwds, up=2)
1240 n = _xkwds_pop(kwds, callername=NN) or callername(up=u)
1241 t = _notError(inst, n, args, kwds) if inst else unstr(n, *args, **kwds)
1242 raise _NotImplementedError(t, txt=notImplemented.__name__.replace('I', ' i'))
1245def notOverloaded(inst, *args, **kwds): # PYCHOK no cover
1246 '''Raise an C{AssertionError} for a method or property not overloaded.
1248 @arg inst: Instance (C{any}).
1249 @arg args: Method or property positional arguments (any C{type}s).
1250 @arg kwds: Method or property keyword arguments (any C{type}s).
1251 '''
1252 u = _xkwds_pop(kwds, up=2)
1253 n = _xkwds_pop(kwds, callername=NN) or callername(up=u)
1254 t = _notError(inst, n, args, kwds)
1255 raise _AssertionError(t, txt=notOverloaded.__name__.replace('O', ' o'))
1258def _Pass(arg, **unused): # PYCHOK no cover
1259 '''(INTERNAL) I{Pass-thru} class for C{_NamedTuple._Units_}.
1260 '''
1261 return arg
1264__all__ += _ALL_DOCS(_Named,
1265 _NamedBase, # _NamedDict,
1266 _NamedEnum, _NamedEnumItem,
1267 _NamedTuple)
1269# **) MIT License
1270#
1271# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.
1272#
1273# Permission is hereby granted, free of charge, to any person obtaining a
1274# copy of this software and associated documentation files (the "Software"),
1275# to deal in the Software without restriction, including without limitation
1276# the rights to use, copy, modify, merge, publish, distribute, sublicense,
1277# and/or sell copies of the Software, and to permit persons to whom the
1278# Software is furnished to do so, subject to the following conditions:
1279#
1280# The above copyright notice and this permission notice shall be included
1281# in all copies or substantial portions of the Software.
1282#
1283# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1284# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1285# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1286# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
1287# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1288# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1289# OTHER DEALINGS IN THE SOFTWARE.
1291# % env PYGEODESY_FOR_DOCS=1 python -m pygeodesy.named
1292# all 71 locals OK