Coverage for pygeodesy/named.py: 95%

569 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-25 13:15 -0400

1 

2# -*- coding: utf-8 -*- 

3 

4u'''(INTERNAL) Nameable class instances. 

5 

6Classes C{_Named}, C{_NamedDict}, C{_NamedEnum}, C{_NamedEnumItem} and 

7C{_NamedTuple} and several subclasses thereof, all with nameable instances. 

8 

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. 

12 

13@see: Module L{pygeodesy.namedTuples} for (most of) the C{Named-Tuples}. 

14''' 

15 

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, _incompatible, \ 

20 _IndexError, _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_, _immutable_, _name_, _NL_, _NN_, \ 

29 _no_, _other_, _s_, _SPACE_, _std_, _UNDER_, _vs_ 

30from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS 

31# from pygeodesy.ltp import Ltp, _toLocal, _toLtp # _MODS 

32# from pygeodesy.ltpTuples import Aer, Enu, Ned # _MODS 

33from pygeodesy.props import _allPropertiesOf_n, deprecated_method, _hasProperty, \ 

34 _update_all, property_doc_, Property_RO, property_RO, \ 

35 _update_attrs, property_ROver 

36from pygeodesy.streprs import attrs, Fmt, lrstrip, pairs, reprs, unstr 

37# from pygeodesy.units import _toUnit # _MODS 

38 

39__all__ = _ALL_LAZY.named 

40__version__ = '25.04.14' 

41 

42_COMMANL_ = _COMMA_ + _NL_ 

43_COMMASPACEDOT_ = _COMMASPACE_ + _DOT_ 

44_del_ = 'del' 

45_item_ = 'item' 

46_MRO_ = 'MRO' 

47# __DUNDER gets mangled in class 

48_name = _under(_name_) 

49_Names_ = '_Names_' 

50_registered_ = 'registered' # PYCHOK used! 

51_std_NotImplemented = _envPYGEODESY('NOTIMPLEMENTED', NN).lower() == _std_ 

52_such_ = 'such' 

53_Units_ = '_Units_' 

54_UP = 2 

55 

56 

57class ADict(dict): 

58 '''A C{dict} with both key I{and} attribute access to 

59 the C{dict} items. 

60 ''' 

61 _iteration = None # Iteration number (C{int}) or C{None} 

62 

63 def __getattr__(self, name): 

64 '''Get the value of an item by B{C{name}}. 

65 ''' 

66 try: 

67 return self[name] 

68 except KeyError: 

69 if name == _name_: 

70 return NN 

71 raise self._AttributeError(name) 

72 

73 def __repr__(self): 

74 '''Default C{repr(self)}. 

75 ''' 

76 return self.toRepr() 

77 

78 def __setattr__(self, name, value): 

79 '''Set the value of a I{known} item by B{C{name}}. 

80 ''' 

81 try: 

82 if self[name] != value: 

83 self[name] = value 

84 except KeyError: 

85 dict.__setattr__(self, name, value) 

86 

87 def __str__(self): 

88 '''Default C{str(self)}. 

89 ''' 

90 return self.toStr() 

91 

92 def _AttributeError(self, name): 

93 '''(INTERNAL) Create an C{AttributeError}. 

94 ''' 

95 if _DOT_ not in name: # NOT classname(self)! 

96 name = _DOT_(self.typename, name) 

97 return _AttributeError(item=name, txt=_doesn_t_exist_) 

98 

99 @property_RO 

100 def iteration(self): # see .named._NamedBase 

101 '''Get the iteration number (C{int}) or 

102 C{None} if not available/applicable. 

103 ''' 

104 return self._iteration 

105 

106 def set_(self, iteration=None, **items): # PYCHOK signature 

107 '''Add one or several new items or replace existing ones. 

108 

109 @kwarg iteration: Optional C{iteration} (C{int}). 

110 @kwarg items: One or more C{name=value} pairs. 

111 ''' 

112 if iteration is not None: 

113 self._iteration = iteration 

114 if items: 

115 dict.update(self, items) 

116 return self # in RhumbLineBase.Intersecant2, _PseudoRhumbLine.Position 

117 

118 def _toL(self): 

119 '''(INTERNAL) Get items as list. 

120 ''' 

121 return list(_EQUAL_(*t) for t in self.items()) # _kwdstr 

122 

123 def toRepr(self, **prec_fmt): 

124 '''Like C{repr(dict)} but with C{name} prefix and with 

125 C{floats} formatted by function L{pygeodesy.fstr}. 

126 ''' 

127 n = _xattr(self, name=NN) or self.typename 

128 return Fmt.PAREN(n, self._toT(_EQUAL_, **prec_fmt)) 

129 

130 def toStr(self, **prec_fmt): 

131 '''Like C{str(dict)} but with C{floats} formatted by 

132 function L{pygeodesy.fstr}. 

133 ''' 

134 return Fmt.CURLY(self._toT(_COLONSPACE_, **prec_fmt)) 

135 

136 def _toT(self, sep, **kwds): 

137 '''(INTERNAL) Helper for C{.toRepr} and C{.toStr}. 

138 ''' 

139 kwds = _xkwds(kwds, prec=6, fmt=Fmt.F, sep=sep) 

140 return _COMMASPACE_.join(pairs(itemsorted(self), **kwds)) 

141 

142 @property_RO 

143 def typename(self): 

144 '''Get this C{ADict}'s type name (C{str}). 

145 ''' 

146 return type(self).__name__ # typename(type(self)) 

147 

148 

149class _Named(object): 

150 '''(INTERNAL) Root class for named objects. 

151 ''' 

152 _iteration = None # iteration number (C{int}) or C{None} 

153 _name = NN # name (C{str}) 

154 _classnaming = False # prefixed (C{bool}) 

155# _updates = 0 # OBSOLETE Property/property updates 

156 

157 def __format__(self, fmt): # PYCHOK no cover 

158 '''Not implemented.''' 

159 return _NotImplemented(self, fmt) 

160 

161 def __imatmul__(self, other): # PYCHOK no cover 

162 '''Not implemented.''' 

163 return _NotImplemented(self, other) # PYCHOK Python 3.5+ 

164 

165 def __matmul__(self, other): # PYCHOK no cover 

166 '''Not implemented.''' 

167 return _NotImplemented(self, other) # PYCHOK Python 3.5+ 

168 

169 def __repr__(self): 

170 '''Default C{repr(self)}. 

171 ''' 

172 return Fmt.repr_at(self) 

173 

174 def __rmatmul__(self, other): # PYCHOK no cover 

175 '''Not implemented.''' 

176 return _NotImplemented(self, other) # PYCHOK Python 3.5+ 

177 

178 def __str__(self): 

179 '''Default C{str(self)}. 

180 ''' 

181 return self.named2 

182 

183 def attrs(self, *names, **sep_Nones_pairs_kwds): 

184 '''Join named attributes as I{name=value} strings, with C{float}s formatted by 

185 function L{pygeodesy.fstr}. 

186 

187 @arg names: The attribute names, all positional (C{str}). 

188 @kwarg sep_Nones_pairs_kwds: Keyword arguments for function L{pygeodesy.pairs}, 

189 except C{B{sep}=", "} and C{B{Nones}=True} to in-/exclude missing 

190 or C{None}-valued attributes. 

191 

192 @return: All C{name=value} pairs, joined by B{C{sep}} (C{str}). 

193 

194 @see: Functions L{pygeodesy.attrs}, L{pygeodesy.pairs} and L{pygeodesy.fstr} 

195 ''' 

196 sep, kwds = _xkwds_pop2(sep_Nones_pairs_kwds, sep=_COMMASPACE_) 

197 return sep.join(attrs(self, *names, **kwds)) 

198 

199 @Property_RO 

200 def classname(self): 

201 '''Get this object's C{[module.]class} name (C{str}), see 

202 property C{.classnaming} and function C{classnaming}. 

203 ''' 

204 return classname(self, prefixed=self._classnaming) 

205 

206 @property_doc_(''' the class naming (C{bool}).''') 

207 def classnaming(self): 

208 '''Get the class naming (C{bool}), see function C{classnaming}. 

209 ''' 

210 return self._classnaming 

211 

212 @classnaming.setter # PYCHOK setter! 

213 def classnaming(self, prefixed): 

214 '''Set the class naming for C{[module.].class} names (C{bool}) 

215 to C{True} to include the module name. 

216 ''' 

217 b = bool(prefixed) 

218 if self._classnaming != b: 

219 self._classnaming = b 

220 _update_attrs(self, *_Named_Property_ROs) 

221 

222 def classof(self, *args, **kwds): 

223 '''Create another instance of this very class. 

224 

225 @arg args: Optional, positional arguments. 

226 @kwarg kwds: Optional, keyword arguments. 

227 

228 @return: New instance (B{self.__class__}). 

229 ''' 

230 return _xnamed(self.__class__(*args, **kwds), self.name) 

231 

232 def copy(self, deep=False, **name): 

233 '''Make a shallow or deep copy of this instance. 

234 

235 @kwarg deep: If C{True}, make a deep, otherwise a shallow 

236 copy (C{bool}). 

237 @kwarg name: Optional, non-empty C{B{name}=NN} (C{str}). 

238 

239 @return: The copy (C{This class}). 

240 ''' 

241 c = _xcopy(self, deep=deep) 

242 if name: 

243 _ = c.rename(name) 

244 return c 

245 

246 def _DOT_(self, *names): 

247 '''(INTERNAL) Period-join C{self.name} and C{names}. 

248 ''' 

249 return _DOT_(self.name, *names) 

250 

251 def dup(self, deep=False, **items): 

252 '''Duplicate this instance, replacing some attributes. 

253 

254 @kwarg deep: If C{True}, duplicate deep, otherwise shallow 

255 (C{bool}). 

256 @kwarg items: Attributes to be changed (C{any}), including 

257 optional C{B{name}} (C{str}). 

258 

259 @return: The duplicate (C{This class}). 

260 

261 @raise AttributeError: Some B{C{items}} invalid. 

262 ''' 

263 n = self.name 

264 m, items = _xkwds_pop2(items, name=n) 

265 d = _xdup(self, deep=deep, **items) 

266 if m != n: 

267 d.rename(m) # zap _Named_Property_ROs 

268# if items: 

269# _update_all(d) 

270 return d 

271 

272 def _instr(self, *attrs, **fmt_prec_props_sep_name__kwds): 

273 '''(INTERNAL) Format, used by C{Conic}, C{Ellipsoid}, C{Geodesic...}, C{Transform}, C{Triaxial}. 

274 ''' 

275 def _fmt_prec_props_kwds(fmt=Fmt.F, prec=6, props=(), sep=_COMMASPACE_, **kwds): 

276 return fmt, prec, props, sep, kwds 

277 

278 name, kwds = _name2__(**fmt_prec_props_sep_name__kwds) 

279 fmt, prec, props, sep, kwds = _fmt_prec_props_kwds(**kwds) 

280 

281 t = () if name is None else (Fmt.EQUAL(name=repr(name or self.name)),) 

282 if attrs: 

283 t += pairs(((a, getattr(self, a)) for a in attrs), 

284 prec=prec, ints=True, fmt=fmt) 

285 if props: 

286 t += pairs(((p.name, getattr(self, p.name)) for p in props), 

287 prec=prec, ints=True) 

288 if kwds: 

289 t += pairs(kwds, prec=prec) 

290 return sep.join(t) if sep else t 

291 

292 @property_RO 

293 def iteration(self): # see .karney.GDict 

294 '''Get the most recent iteration number (C{int}) or C{None} 

295 if not available or not applicable. 

296 

297 @note: The interation number may be an aggregate number over 

298 several, nested functions. 

299 ''' 

300 return self._iteration 

301 

302 def methodname(self, which): 

303 '''Get a method C{[module.]class.method} name of this object (C{str}). 

304 

305 @arg which: The method (C{callable}). 

306 ''' 

307 return _DOT_(self.classname, typename(which) if callable(which) else _NN_) 

308 

309 @property_doc_(''' the name (C{str}).''') 

310 def name(self): 

311 '''Get the name (C{str}). 

312 ''' 

313 return self._name 

314 

315 @name.setter # PYCHOK setter! 

316 def name(self, name): 

317 '''Set the C{B{name}=NN} (C{str}). 

318 

319 @raise NameError: Can't rename, use method L{rename}. 

320 ''' 

321 m, n = self._name, _name__(name) 

322 if not m: 

323 self._name = n 

324 elif n != m: 

325 n = repr(n) 

326 c = self.classname 

327 t = _DOT_(c, Fmt.PAREN(typename(self.rename), n)) 

328 n = _DOT_(c, Fmt.EQUALSPACED(name=n)) 

329 m = Fmt.PAREN(_SPACE_('was', repr(m))) 

330 n = _SPACE_(n, m) 

331 raise _NameError(n, txt=_SPACE_('use', t)) 

332 # to set the name from a sub-class, use 

333 # self.name = name or 

334 # _Named.name.fset(self, name), but NOT 

335 # _Named(self).name = name 

336 

337 def _name__(self, name): # usually **name 

338 '''(INTERNAL) Get the C{name} or this C{name}. 

339 ''' 

340 return _name__(name, _or_nameof=self) # nameof(self) 

341 

342 def _name1__(self, kwds): 

343 '''(INTERNAL) Resolve and set the C{B{name}=NN}. 

344 ''' 

345 return _name1__(kwds, _or_nameof=self.name) if self.name else kwds 

346 

347 @Property_RO 

348 def named(self): 

349 '''Get the name I{or} class name or C{""} (C{str}). 

350 ''' 

351 return self.name or self.classname 

352 

353# @Property_RO 

354# def named_(self): 

355# '''Get the C{class} name I{and/or} the str(name) or C{""} (C{str}). 

356# ''' 

357# return _xjoined_(self.classname, self.name, enquote=False) 

358 

359 @Property_RO 

360 def named2(self): 

361 '''Get the C{class} name I{and/or} the repr(name) or C{""} (C{str}). 

362 ''' 

363 return _xjoined_(self.classname, self.name) 

364 

365 @Property_RO 

366 def named3(self): 

367 '''Get the I{prefixed} C{class} name I{and/or} the name or C{""} (C{str}). 

368 ''' 

369 return _xjoined_(classname(self, prefixed=True), self.name) 

370 

371 @Property_RO 

372 def named4(self): 

373 '''Get the C{package.module.class} name I{and/or} the name or C{""} (C{str}). 

374 ''' 

375 return _xjoined_(_DOT_(self.__module__, self.typename), self.name) 

376 

377 def _notImplemented(self, *args, **kwds): 

378 '''(INTERNAL) See function L{notImplemented}. 

379 ''' 

380 notImplemented(self, *args, **_xkwds(kwds, up=_UP + 1)) 

381 

382 def _notOverloaded(self, *args, **kwds): 

383 '''(INTERNAL) See function L{notOverloaded}. 

384 ''' 

385 notOverloaded(self, *args, **_xkwds(kwds, up=_UP + 1)) 

386 

387 def rename(self, name): 

388 '''Change the name. 

389 

390 @arg name: The new name (C{str}). 

391 

392 @return: The previous name (C{str}). 

393 ''' 

394 m, n = self._name, _name__(name) 

395 if n != m: 

396 self._name = n 

397 _update_attrs(self, *_Named_Property_ROs) 

398 return m 

399 

400 def renamed(self, name): 

401 '''Change the name. 

402 

403 @arg name: The new name (C{str}). 

404 

405 @return: This instance (C{str}). 

406 ''' 

407 _ = self.rename(name) 

408 return self 

409 

410 @property_RO 

411 def sizeof(self): 

412 '''Get the current size in C{bytes} of this instance (C{int}). 

413 ''' 

414 return _sizeof(self) 

415 

416 def toRepr(self, **unused): # PYCHOK no cover 

417 '''Default C{repr(self)}. 

418 ''' 

419 return repr(self) 

420 

421 def toStr(self, **unused): # PYCHOK no cover 

422 '''Default C{str(self)}. 

423 ''' 

424 return str(self) 

425 

426 @deprecated_method 

427 def toStr2(self, **kwds): # PYCHOK no cover 

428 '''DEPRECATED on 23.10.07, use method C{toRepr}.''' 

429 return self.toRepr(**kwds) 

430 

431# def _unstr(self, which, *args, **kwds): 

432# '''(INTERNAL) Return the string representation of a method 

433# invokation of this instance: C{str(self).method(...)} 

434# 

435# @see: Function L{pygeodesy.unstr}. 

436# ''' 

437# return _DOT_(self, unstr(which, *args, **kwds)) 

438 

439 @property_RO 

440 def typename(self): 

441 '''Get this object's type name (C{str}). 

442 ''' 

443 return type(self).__name__ # typename(type(self)) 

444 

445 def _xnamed(self, inst, name=NN, **force): 

446 '''(INTERNAL) Set the instance' C{.name = self.name}. 

447 

448 @arg inst: The instance (C{_Named}). 

449 @kwarg name: The name (C{str}). 

450 @kwarg force: If C{True}, force rename (C{bool}). 

451 

452 @return: The B{C{inst}}, renamed if B{C{force}}d 

453 or if not named before. 

454 ''' 

455 return _xnamed(inst, name, **force) 

456 

457 def _xrenamed(self, inst): 

458 '''(INTERNAL) Rename the instance' C{.name = self.name}. 

459 

460 @arg inst: The instance (C{_Named}). 

461 

462 @return: The B{C{inst}}, named if not named before. 

463 

464 @raise TypeError: Not C{isinstance(B{inst}, _Named)}. 

465 ''' 

466 _xinstanceof(_Named, inst=inst) # assert 

467 return inst.renamed(self.name) 

468 

469_Named_Property_ROs = _allPropertiesOf_n(5, _Named, Property_RO) # PYCHOK once 

470 

471 

472class _NamedBase(_Named): 

473 '''(INTERNAL) Base class with name. 

474 ''' 

475 def __repr__(self): 

476 '''Default C{repr(self)}. 

477 ''' 

478 return self.toRepr() 

479 

480 def __str__(self): 

481 '''Default C{str(self)}. 

482 ''' 

483 return self.toStr() 

484 

485 def others(self, *other, **name_other_up): 

486 '''Refined class comparison, invoked as C{.others(other)}, 

487 C{.others(name=other)} or C{.others(other, name='other')}. 

488 

489 @arg other: The other instance (any C{type}). 

490 @kwarg name_other_up: Overriding C{name=other} and C{up=1} 

491 keyword arguments. 

492 

493 @return: The B{C{other}} iff compatible with this instance's 

494 C{class} or C{type}. 

495 

496 @raise TypeError: Mismatch of the B{C{other}} and this 

497 instance's C{class} or C{type}. 

498 ''' 

499 if other: # most common, just one arg B{C{other}} 

500 other0 = other[0] 

501 if isinstance(other0, self.__class__) or \ 

502 isinstance(self, other0.__class__): 

503 return other0 

504 

505 other, name, up = _xother3(self, other, **name_other_up) 

506 if isinstance(self, other.__class__) or \ 

507 isinstance(other, self.__class__): 

508 return _xnamed(other, name) 

509 

510 raise _xotherError(self, other, name=name, up=up + 1) 

511 

512 def toRepr(self, **kwds): # PYCHOK expected 

513 '''(INTERNAL) I{Could be overloaded}. 

514 

515 @kwarg kwds: Optional, C{toStr} keyword arguments. 

516 

517 @return: C{toStr}() with keyword arguments (as C{str}). 

518 ''' 

519 t = lrstrip(self.toStr(**kwds)) 

520# if self.name: 

521# t = NN(Fmt.EQUAL(name=repr(self.name)), sep, t) 

522 return Fmt.PAREN(self.classname, t) # XXX (self.named, t) 

523 

524# def toRepr(self, **kwds) 

525# if kwds: 

526# s = NN.join(reprs((self,), **kwds)) 

527# else: # super().__repr__ only for Python 3+ 

528# s = super(self.__class__, self).__repr__() 

529# return Fmt.PAREN(self.named, s) # clips(s) 

530 

531 def toStr(self, **kwds): # PYCHOK no cover 

532 '''I{Must be overloaded}.''' 

533 notOverloaded(self, **kwds) 

534 

535# def toStr(self, **kwds): 

536# if kwds: 

537# s = NN.join(strs((self,), **kwds)) 

538# else: # super().__str__ only for Python 3+ 

539# s = super(self.__class__, self).__str__() 

540# return s 

541 

542 def _update(self, updated, *attrs, **setters): 

543 '''(INTERNAL) Zap cached instance attributes and overwrite C{__dict__} or L{Property_RO} values. 

544 ''' 

545 u = _update_all(self, *attrs) if updated else 0 

546 if setters: 

547 d = self.__dict__ 

548 # double-check that setters are Property_RO's 

549 for n, v in setters.items(): 

550 if n in d or _hasProperty(self, n, Property_RO): 

551 d[n] = v 

552 else: 

553 raise _AssertionError(n, v, txt=repr(self)) 

554 u += len(setters) 

555 return u 

556 

557 

558class _NamedDict(ADict, _Named): 

559 '''(INTERNAL) Named C{dict} with key I{and} attribute access 

560 to the items. 

561 ''' 

562 def __init__(self, *args, **kwds): 

563 if args: # is args[0] a dict? 

564 if len(args) != 1: # or not isinstance(args[0], dict) 

565 kwds = _name1__(kwds) 

566 t = unstr(self.classname, *args, **kwds) # PYCHOK no cover 

567 raise _ValueError(args=len(args), txt=t) 

568 kwds = _xkwds(dict(args[0]), **kwds) # args[0] overrides kwds 

569 n, kwds = _name2__(**kwds) 

570 if n: 

571 _Named.name.fset(self, n) # see _Named.name 

572 ADict.__init__(self, kwds) 

573 

574 def __delattr__(self, name): 

575 '''Delete an attribute or item by B{C{name}}. 

576 ''' 

577 if name in self: # in ADict.keys(self): 

578 ADict.pop(self, name) 

579 elif name in (_name_, _name): 

580 # ADict.__setattr__(self, name, NN) 

581 _Named.rename(self, NN) 

582 else: 

583 ADict.__delattr__(self, name) 

584 

585 def __getattr__(self, name): 

586 '''Get the value of an item by B{C{name}}. 

587 ''' 

588 try: 

589 return self[name] 

590 except KeyError: 

591 if name == _name_: 

592 return _Named.name.fget(self) 

593 raise ADict._AttributeError(self, self._DOT_(name)) 

594 

595 def __getitem__(self, key): 

596 '''Get the value of an item by B{C{key}}. 

597 ''' 

598 if key == _name_: 

599 raise self._KeyError(key) 

600 return ADict.__getitem__(self, key) 

601 

602 def _KeyError(self, key, *value): # PYCHOK no cover 

603 '''(INTERNAL) Create a C{KeyError}. 

604 ''' 

605 n = self.name or self.typename 

606 t = Fmt.SQUARE(n, key) 

607 if value: 

608 t = Fmt.EQUALSPACED(t, *value) 

609 return _KeyError(t) 

610 

611 def __setattr__(self, name, value): 

612 '''Set attribute or item B{C{name}} to B{C{value}}. 

613 ''' 

614 if name in self: # in ADict.keys(self) 

615 ADict.__setitem__(self, name, value) # self[name] = value 

616 else: 

617 ADict.__setattr__(self, name, value) 

618 

619 def __setitem__(self, key, value): 

620 '''Set item B{C{key}} to B{C{value}}. 

621 ''' 

622 if key == _name_: 

623 raise self._KeyError(key, repr(value)) 

624 ADict.__setitem__(self, key, value) 

625 

626 

627class _NamedEnum(_NamedDict): 

628 '''(INTERNAL) Enum-like C{_NamedDict} with attribute access 

629 restricted to valid keys. 

630 ''' 

631 _item_Classes = () 

632 

633 def __init__(self, Class, *Classes, **name): 

634 '''New C{_NamedEnum}. 

635 

636 @arg Class: Initial class or type acceptable as items 

637 values (C{type}). 

638 @arg Classes: Additional, acceptable classes or C{type}s. 

639 ''' 

640 self._item_Classes = (Class,) + Classes 

641 n = _name__(**name) or NN(typename(Class), _s_) 

642 if n and _xvalid(n, underOK=True): 

643 _Named.name.fset(self, n) # see _Named.name 

644 

645 def __getattr__(self, name): 

646 '''Get the value of an attribute or item by B{C{name}}. 

647 ''' 

648 return _NamedDict.__getattr__(self, name) 

649 

650 def __repr__(self): 

651 '''Default C{repr(self)}. 

652 ''' 

653 return self.toRepr() 

654 

655 def __str__(self): 

656 '''Default C{str(self)}. 

657 ''' 

658 return self.toStr() 

659 

660 def _assert(self, **kwds): 

661 '''(INTERNAL) Check attribute name against given, registered name. 

662 ''' 

663 pypy = _isPyPy() 

664 _isa = isinstance 

665 for n, v in kwds.items(): 

666 if _isa(v, _LazyNamedEnumItem): # property 

667 assert (n == v.name) if pypy else (n is v.name) 

668 # assert not hasattr(self.__class__, n) 

669 setattr(self.__class__, n, v) 

670 elif _isa(v, self._item_Classes): # PYCHOK no cover 

671 assert self[n] is v and getattr(self, n) \ 

672 and self.find(v) == n 

673 else: 

674 raise _TypeError(v, name=n) 

675 

676 def find(self, item, dflt=None, all=False): 

677 '''Find a registered item. 

678 

679 @arg item: The item to look for (any C{type}). 

680 @kwarg dflt: Value to return if not found (any C{type}). 

681 @kwarg all: Use C{True} to search I{all} items or C{False} only 

682 the currently I{registered} ones (C{bool}). 

683 

684 @return: The B{C{item}}'s name if found (C{str}), or C{{dflt}} 

685 if there is no such B{C{item}}. 

686 ''' 

687 for k, v in self.items(all=all): # or ADict.items(self) 

688 if v is item: 

689 return k 

690 return dflt 

691 

692 def get(self, name, dflt=None): 

693 '''Get the value of a I{registered} item. 

694 

695 @arg name: The name of the item (C{str}). 

696 @kwarg dflt: Value to return (any C{type}). 

697 

698 @return: The item with B{C{name}} if found, or B{C{dflt}} if 

699 there is no I{registered} item with that B{C{name}}. 

700 ''' 

701 # getattr needed to instantiate L{_LazyNamedEnumItem} 

702 return getattr(self, name, dflt) 

703 

704 def items(self, all=False, asorted=False): 

705 '''Yield all or only the I{registered} items. 

706 

707 @kwarg all: Use C{True} to yield I{all} items or C{False} for 

708 only the currently I{registered} ones (C{bool}). 

709 @kwarg asorted: If C{True}, yield the items in I{alphabetical, 

710 case-insensitive} order (C{bool}). 

711 ''' 

712 if all: # instantiate any remaining L{_LazyNamedEnumItem} 

713 _isa = isinstance 

714 for n, p in tuple(type(self).__dict__.items()): 

715 if _isa(p, _LazyNamedEnumItem): 

716 _ = getattr(self, n) 

717 return itemsorted(self) if asorted else ADict.items(self) 

718 

719 def keys(self, **all_asorted): 

720 '''Yield the name (C{str}) of I{all} or only the currently I{registered} 

721 items, optionally sorted I{alphabetically, case-insensitively}. 

722 

723 @kwarg all_asorted: See method C{items}. 

724 ''' 

725 for k, _ in self.items(**all_asorted): 

726 yield k 

727 

728 def popitem(self): 

729 '''Remove I{an, any} currently I{registed} item. 

730 

731 @return: The removed item. 

732 ''' 

733 return self._zapitem(*ADict.popitem(self)) 

734 

735 def register(self, item): 

736 '''Registed one new item or I{all} or I{any} unregistered ones. 

737 

738 @arg item: The item (any C{type}) or B{I{all}} or B{C{any}}. 

739 

740 @return: The item name (C{str}) or C("all") or C{"any"}. 

741 

742 @raise NameError: An B{C{item}} with that name is already 

743 registered the B{C{item}} has no or an 

744 invalid name. 

745 

746 @raise TypeError: The B{C{item}} type invalid. 

747 ''' 

748 if item is all or item is any: 

749 _ = self.items(all=True) 

750 n = typename(item) 

751 else: 

752 try: 

753 n = item.name 

754 if not (n and isstr(n) and isidentifier(n)): 

755 raise ValueError() 

756 except (AttributeError, ValueError, TypeError) as x: 

757 n = _DOT_(_item_, _name_) 

758 raise _NameError(n, item, cause=x) 

759 if n in self: 

760 t = _SPACE_(_item_, self._DOT_(n), _exists_) 

761 raise _NameError(t, txt=repr(item)) 

762 if not isinstance(item, self._item_Classes): # _xinstanceof 

763 n = self._DOT_(n) 

764 raise _TypesError(n, item, *self._item_Classes) 

765 self[n] = item 

766 return n 

767 

768 def unregister(self, name_or_item): 

769 '''Remove a I{registered} item. 

770 

771 @arg name_or_item: Name (C{str}) or the item (any C{type}). 

772 

773 @return: The unregistered item. 

774 

775 @raise AttributeError: No such B{C{item}}. 

776 

777 @raise NameError: No item with that B{C{name}}. 

778 ''' 

779 if isstr(name_or_item): 

780 name = name_or_item 

781 else: 

782 name = self.find(name_or_item, dflt=MISSING) # all=True? 

783 if name is MISSING: 

784 t = _SPACE_(_no_, _such_, self.classname, _item_) 

785 raise _AttributeError(t, txt=repr(name_or_item)) 

786 try: 

787 item = ADict.pop(self, name) 

788 except KeyError: 

789 raise _NameError(item=self._DOT_(name), txt=_doesn_t_exist_) 

790 return self._zapitem(name, item) 

791 

792 pop = unregister 

793 

794 def toRepr(self, prec=6, fmt=Fmt.F, sep=_COMMANL_, **all_asorted): # PYCHOK _NamedDict, ADict 

795 '''Like C{repr(dict)} but C{name}s optionally sorted and 

796 C{floats} formatted by function L{pygeodesy.fstr}. 

797 ''' 

798 t = ((self._DOT_(n), v) for n, v in self.items(**all_asorted)) 

799 return sep.join(pairs(t, prec=prec, fmt=fmt, sep=_COLONSPACE_)) 

800 

801 def toStr(self, *unused, **all_asorted): # PYCHOK _NamedDict, ADict 

802 '''Return a string with all C{name}s, optionally sorted. 

803 ''' 

804 return self._DOT_(_COMMASPACEDOT_.join(self.keys(**all_asorted))) 

805 

806 def values(self, **all_asorted): 

807 '''Yield the value (C{type}) of all or only the I{registered} items, 

808 optionally sorted I{alphabetically} and I{case-insensitively}. 

809 

810 @kwarg all_asorted: See method C{items}. 

811 ''' 

812 for _, v in self.items(**all_asorted): 

813 yield v 

814 

815 def _zapitem(self, name, item): 

816 # remove _LazyNamedEnumItem property value if still present 

817 if self.__dict__.get(name, None) is item: 

818 self.__dict__.pop(name) # [name] = None 

819 item._enum = None 

820 return item 

821 

822 

823class _LazyNamedEnumItem(property_RO): # XXX or descriptor? 

824 '''(INTERNAL) Lazily instantiated L{_NamedEnumItem}. 

825 ''' 

826 pass 

827 

828 

829def _lazyNamedEnumItem(name, *args, **kwds): 

830 '''(INTERNAL) L{_LazyNamedEnumItem} property-like factory. 

831 

832 @see: Luciano Ramalho, "Fluent Python", O'Reilly, Example 

833 19-24, 2016 p. 636 or Example 22-28, 2022 p. 869+ 

834 ''' 

835 def _fget(inst): 

836 # assert isinstance(inst, _NamedEnum) 

837 try: # get the item from the instance' __dict__ 

838 # item = inst.__dict__[name] # ... or ADict 

839 item = inst[name] 

840 except KeyError: 

841 # instantiate an _NamedEnumItem, it self-registers 

842 item = inst._Lazy(*args, **_xkwds(kwds, name=name)) 

843 # assert inst[name] is item # MUST be registered 

844 # store the item in the instance' __dict__ ... 

845 # inst.__dict__[name] = item # ... or update the 

846 inst.update({name: item}) # ... ADict for Triaxials 

847 # remove the property from the registry class, such that 

848 # (a) the property no longer overrides the instance' item 

849 # in inst.__dict__ and (b) _NamedEnum.items(all=True) only 

850 # sees any un-instantiated ones yet to be instantiated 

851 p = getattr(inst.__class__, name, None) 

852 if isinstance(p, _LazyNamedEnumItem): 

853 delattr(inst.__class__, name) 

854 # assert isinstance(item, _NamedEnumItem) 

855 return item 

856 

857 p = _LazyNamedEnumItem(_fget) 

858 p.name = name 

859 return p 

860 

861 

862class _NamedEnumItem(_NamedBase): 

863 '''(INTERNAL) Base class for items in a C{_NamedEnum} registery. 

864 ''' 

865 _enum = None 

866 

867# def __ne__(self, other): # XXX fails for Lcc.conic = conic! 

868# '''Compare this and an other item. 

869# 

870# @return: C{True} if different, C{False} otherwise. 

871# ''' 

872# return not self.__eq__(other) 

873 

874 @property_doc_(''' the I{registered} name (C{str}).''') 

875 def name(self): 

876 '''Get the I{registered} name (C{str}). 

877 ''' 

878 return self._name 

879 

880 @name.setter # PYCHOK setter! 

881 def name(self, name): 

882 '''Set the name, unless already registered (C{str}). 

883 ''' 

884 name = _name__(name) or _NN_ 

885 if self._enum: 

886 raise _NameError(name, self, txt=_registered_) # _TypeError 

887 if name: 

888 self._name = name 

889 

890 def _register(self, enum, name): 

891 '''(INTERNAL) Add this item as B{C{enum.name}}. 

892 

893 @note: Don't register if name is empty or doesn't 

894 start with a letter. 

895 ''' 

896 name = _name__(name) 

897 if name and _xvalid(name, underOK=True): 

898 self.name = name 

899 if name[:1].isalpha(): # '_...' not registered 

900 enum.register(self) 

901 self._enum = enum 

902 

903 def unregister(self): 

904 '''Remove this instance from its C{_NamedEnum} registry. 

905 

906 @raise AssertionError: Mismatch of this and registered item. 

907 

908 @raise NameError: This item is unregistered. 

909 ''' 

910 enum = self._enum 

911 if enum and self.name and self.name in enum: 

912 item = enum.unregister(self.name) 

913 if item is not self: # PYCHOK no cover 

914 t = _SPACE_(repr(item), _vs_, repr(self)) 

915 raise _AssertionError(t) 

916 

917 

918class _NamedLocal(object): 

919 '''(INTERNAL) Base class for C{CartesianBase}, C{Ecef9Tuple} and C{LatLonBase}. 

920 ''' 

921 

922 @property_ROver 

923 def Ecef(self): 

924 '''Get the ECEF I{class} (L{EcefKarney}), I{once}. 

925 ''' 

926 return _MODS.ecef.EcefKarney 

927 

928 @property_RO 

929 def _ecef9(self): 

930 '''I{Must be overloaded}.''' 

931 notOverloaded(self) 

932 

933 @Property_RO 

934 def _Ltp(self): 

935 '''(INTERNAL) Cache this instance' LTP (L{Ltp}). 

936 ''' 

937 return self._ltp.Ltp(self._ecef9, ecef=self.Ecef(self.datum), name=self.name) 

938 

939 @property_ROver 

940 def _ltp(self): 

941 '''(INTERNAL) Get module L{pygeodesy.ltp}, I{once}. 

942 ''' 

943 return _MODS.ltp 

944 

945 def _ltp_toLocal(self, ltp, Xyz_kwds, **Xyz): # overloaded in C{Ecef9Tuple} 

946 '''(INTERNAL) Invoke C{ltp._toLocal}. 

947 ''' 

948 Xyz_ = self._ltp_toLocal2(Xyz_kwds, **Xyz) 

949 return self._ltp._toLocal(self, ltp, *Xyz_) # self._ecef9 

950 

951 def _ltp_toLocal2(self, Xyz_kwds, _None=None, **Xyz): 

952 '''(INTERNAL) Return 2-tuple C{(Xyz_Class, Xyz_kwds)}. 

953 ''' 

954 C, _ = Xyz_ = _xkwds_pop2(Xyz_kwds, **Xyz) 

955 if C is not _None: # validate C 

956 n, X = _xkwds_item2(Xyz) 

957 if X is not C: 

958 _xsubclassof(X, **{n: C}) 

959 return Xyz_ 

960 

961 @property_ROver 

962 def _ltpTuples(self): 

963 '''(INTERNAL) Get module L{pygeodesy.ltpTuples}, I{once}. 

964 ''' 

965 return _MODS.ltpTuples 

966 

967 def toAer(self, ltp=None, **Aer_and_kwds): 

968 '''Convert this instance to I{local} I{Azimuth, Elevation, slant Range} (AER) components. 

969 

970 @kwarg ltp: The I{local tangent plane} (LTP) to use (L{Ltp}), overriding this 

971 instance' L{LTP<pygeodesy.named._NamedLocal.toLtp>}. 

972 @kwarg Aer_and_kwds: Optional AER class C{B{Aer}=}L{Aer<pygeodesy.ltpTuples.Aer>} 

973 to use and optionally, additional B{C{Aer}} keyword arguments. 

974 

975 @return: An B{C{Aer}} instance. 

976 

977 @raise TypeError: Invalid B{C{ltp}}. 

978 

979 @see: Method L{toLocal<pygeodesy.named._NamedLocal.toLocal>}. 

980 ''' 

981 return self._ltp_toLocal(ltp, Aer_and_kwds, Aer=self._ltpTuples.Aer) 

982 

983 def toEnu(self, ltp=None, **Enu_and_kwds): 

984 '''Convert this instance to I{local} I{East, North, Up} (ENU) components. 

985 

986 @kwarg ltp: The I{local tangent plane} (LTP) to use (L{Ltp}), overriding this 

987 instance' L{LTP<pygeodesy.named._NamedLocal.toLtp>}. 

988 @kwarg Enu_and_kwds: Optional ENU class C{B{Enu}=}L{Enu<pygeodesy.ltpTuples.Enu>} 

989 to use and optionally, additional B{C{Enu}} keyword arguments. 

990 

991 @return: An B{C{Enu}} instance. 

992 

993 @raise TypeError: Invalid B{C{ltp}}. 

994 

995 @see: Method L{toLocal<pygeodesy.named._NamedLocal.toLocal>}. 

996 ''' 

997 return self._ltp_toLocal(ltp, Enu_and_kwds, Enu=self._ltpTuples.Enu) 

998 

999 def toLocal(self, Xyz=None, ltp=None, **Xyz_kwds): 

1000 '''Convert this instance to I{local} components in a I{local tangent plane} (LTP) 

1001 

1002 @kwarg Xyz: Optional I{local} components class (L{XyzLocal}, L{Aer}, 

1003 L{Enu}, L{Ned}) or C{None}. 

1004 @kwarg ltp: The I{local tangent plane} (LTP) to use (L{Ltp}), overriding this 

1005 cartesian's L{LTP<pygeodesy.named._NamedLocal.toLtp>}. 

1006 @kwarg Xyz_kwds: Optionally, additional B{C{Xyz}} keyword arguments, ignored 

1007 if C{B{Xyz} is None}. 

1008 

1009 @return: An B{C{Xyz}} instance or a L{Local9Tuple}C{(x, y, z, lat, lon, 

1010 height, ltp, ecef, M)} if C{B{Xyz} is None} (with C{M=None}). 

1011 

1012 @raise TypeError: Invalid B{C{ltp}}. 

1013 ''' 

1014 return self._ltp_toLocal(ltp, Xyz_kwds, Xyz=Xyz, _None=Xyz) 

1015 

1016 def toLtp(self, Ecef=None, **name): 

1017 '''Return the I{local tangent plane} (LTP) for this instance. 

1018 

1019 @kwarg Ecef: Optional ECEF I{class} (L{EcefKarney}, ... L{EcefYou}), overriding 

1020 this instance' L{Ecef<pygeodesy.named._NamedLocal.Ecef>}. 

1021 @kwarg name: Optional C{B{name}=NN} (C{str}). 

1022 

1023 @return: An B{C{Ltp}} instance. 

1024 ''' 

1025 return self._ltp._toLtp(self, Ecef, self._ecef9, name) # needs self.Ecef and self._Ltp 

1026 

1027 def toNed(self, ltp=None, **Ned_and_kwds): 

1028 '''Convert this instance to I{local} I{North, East, Down} (NED) components. 

1029 

1030 @kwarg ltp: The I{local tangent plane} (LTP) to use (L{Ltp}), overriding this 

1031 instance' L{LTP<pygeodesy.named._NamedLocal.toLtp>}. 

1032 @kwarg Ned_and_kwds: Optional NED class C{B{Ned}=}L{Ned<pygeodesy.ltpTuples.Ned>} 

1033 to use and optionally, additional B{C{Ned}} keyword arguments. 

1034 

1035 @return: An B{C{Ned}} instance. 

1036 

1037 @raise TypeError: Invalid B{C{ltp}}. 

1038 

1039 @see: Method L{toLocal<pygeodesy.named._NamedLocal.toLocal>}. 

1040 ''' 

1041 return self._ltp_toLocal(ltp, Ned_and_kwds, Ned=self._ltpTuples.Ned) 

1042 

1043 def toXyz(self, ltp=None, **Xyz_and_kwds): 

1044 '''Convert this instance to I{local} I{X, Y, Z} (XYZ) components. 

1045 

1046 @kwarg ltp: The I{local tangent plane} (LTP) to use (L{Ltp}), overriding this 

1047 instance' L{LTP<pygeodesy.named._NamedLocal.toLtp>}. 

1048 @kwarg Xyz_and_kwds: Optional XYZ class C{B{Xyz}=}L{Xyz<pygeodesy.ltpTuples.XyzLocal>} 

1049 to use and optionally, additional B{C{Xyz}} keyword arguments. 

1050 

1051 @return: An B{C{Xyz}} instance. 

1052 

1053 @raise TypeError: Invalid B{C{ltp}}. 

1054 

1055 @see: Method L{toLocal<pygeodesy.named._NamedLocal.toLocal>}. 

1056 ''' 

1057 return self._ltp_toLocal(ltp, Xyz_and_kwds, Xyz=self._ltpTuples.XyzLocal) 

1058 

1059 

1060# from pygeodesy.props import _NamedProperty 

1061 

1062 

1063class _NamedTuple(tuple, _Named): 

1064 '''(INTERNAL) Base for named C{tuple}s with both index I{and} 

1065 attribute name access to the items. 

1066 

1067 @note: This class is similar to Python's C{namedtuple}, 

1068 but statically defined, lighter and limited. 

1069 ''' 

1070 _Names_ = () # item names, non-identifier, no leading underscore 

1071 '''Tuple specifying the C{name} of each C{Named-Tuple} item. 

1072 

1073 @note: Specify at least 2 item names. 

1074 ''' 

1075 _Units_ = () # .units classes 

1076 '''Tuple defining the C{units} of the value of each C{Named-Tuple} item. 

1077 

1078 @note: The C{len(_Units_)} must match C{len(_Names_)}. 

1079 ''' 

1080 _validated = False # set to True I{per sub-class!} 

1081 

1082 def __new__(cls, arg, *args, **iteration_name): 

1083 '''New L{_NamedTuple} initialized with B{C{positional}} arguments. 

1084 

1085 @arg arg: Tuple items (C{tuple}, C{list}, ...) or first tuple 

1086 item of several more in other positional arguments. 

1087 @arg args: Tuple items (C{any}), all positional arguments. 

1088 @kwarg iteration_name: Only keyword arguments C{B{iteration}=None} 

1089 and C{B{name}=NN} are used, any other are 

1090 I{silently} ignored. 

1091 

1092 @raise LenError: Unequal number of positional arguments and 

1093 number of item C{_Names_} or C{_Units_}. 

1094 

1095 @raise TypeError: The C{_Names_} or C{_Units_} attribute is 

1096 not a C{tuple} of at least 2 items. 

1097 

1098 @raise ValueError: Item name is not a C{str} or valid C{identifier} 

1099 or starts with C{underscore}. 

1100 ''' 

1101 n, args = len2(((arg,) + args) if args else arg) 

1102 self = tuple.__new__(cls, args) 

1103 if not self._validated: 

1104 self._validate() 

1105 

1106 N = len(self._Names_) 

1107 if n != N: 

1108 raise LenError(self.__class__, args=n, _Names_=N) 

1109 

1110 if iteration_name: 

1111 i, name = _xkwds_pop2(iteration_name, iteration=None) 

1112 if i is not None: 

1113 self._iteration = i 

1114 if name: 

1115 self.name = name 

1116 return self 

1117 

1118 def __delattr__(self, name): 

1119 '''Delete an attribute by B{C{name}}. 

1120 

1121 @note: Items can not be deleted. 

1122 ''' 

1123 if name in self._Names_: 

1124 t = _SPACE_(_del_, self._DOT_(name)) 

1125 raise _TypeError(t, txt=_immutable_) 

1126 elif name in (_name_, _name): 

1127 _Named.__setattr__(self, name, NN) # XXX _Named.name.fset(self, NN) 

1128 else: 

1129 tuple.__delattr__(self, name) 

1130 

1131 def __getattr__(self, name): 

1132 '''Get the value of an attribute or item by B{C{name}}. 

1133 ''' 

1134 try: 

1135 return tuple.__getitem__(self, self._Names_.index(name)) 

1136 except IndexError as x: 

1137 raise _IndexError(self._DOT_(name), cause=x) 

1138 except ValueError: # e.g. _iteration 

1139 return tuple.__getattr__(self, name) # __getattribute__ 

1140 

1141# def __getitem__(self, index): # index, slice, etc. 

1142# '''Get the item(s) at an B{C{index}} or slice. 

1143# ''' 

1144# return tuple.__getitem__(self, index) 

1145 

1146 def __hash__(self): 

1147 return tuple.__hash__(self) 

1148 

1149 def __repr__(self): 

1150 '''Default C{repr(self)}. 

1151 ''' 

1152 return self.toRepr() 

1153 

1154 def __setattr__(self, name, value): 

1155 '''Set attribute or item B{C{name}} to B{C{value}}. 

1156 ''' 

1157 if name in self._Names_: 

1158 t = Fmt.EQUALSPACED(self._DOT_(name), repr(value)) 

1159 raise _TypeError(t, txt=_immutable_) 

1160 elif name in (_name_, _name): 

1161 _Named.__setattr__(self, name, value) # XXX _Named.name.fset(self, value) 

1162 else: # e.g. _iteration 

1163 tuple.__setattr__(self, name, value) 

1164 

1165 def __str__(self): 

1166 '''Default C{repr(self)}. 

1167 ''' 

1168 return self.toStr() 

1169 

1170 def _DOT_(self, *names): 

1171 '''(INTERNAL) Period-join C{self.classname} and C{names}. 

1172 ''' 

1173 return _DOT_(self.classname, *names) 

1174 

1175 def dup(self, name=NN, **items): 

1176 '''Duplicate this tuple replacing one or more items. 

1177 

1178 @kwarg name: Optional new name (C{str}). 

1179 @kwarg items: Items to be replaced (C{name=value} pairs), if any. 

1180 

1181 @return: A copy of this tuple with B{C{items}}. 

1182 

1183 @raise NameError: Some B{C{items}} invalid. 

1184 ''' 

1185 t = list(self) 

1186 U = self._Units_ 

1187 if items: 

1188 _ix = self._Names_.index 

1189 _2U = _MODS.units._toUnit 

1190 try: 

1191 for n, v in items.items(): 

1192 i = _ix(n) 

1193 t[i] = _2U(U[i], v, name=n) 

1194 except ValueError: # bad item name 

1195 raise _NameError(self._DOT_(n), v, this=self) 

1196 return self.classof(*t).reUnit(*U, name=name) 

1197 

1198 def items(self): 

1199 '''Yield the items, each as a C{(name, value)} pair (C{2-tuple}). 

1200 

1201 @see: Method C{.units}. 

1202 ''' 

1203 for n, v in _zip(self._Names_, self): # strict=True 

1204 yield n, v 

1205 

1206 iteritems = items 

1207 

1208 def reUnit(self, *Units, **name): 

1209 '''Replace some of this C{Named-Tuple}'s C{Units}. 

1210 

1211 @arg Units: One or more C{Unit} classes, all positional. 

1212 @kwarg name: Optional C{B{name}=NN} (C{str}). 

1213 

1214 @return: This instance with updated C{Units}. 

1215 

1216 @note: This C{Named-Tuple}'s values are I{not updated}. 

1217 ''' 

1218 U = self._Units_ 

1219 n = min(len(U), len(Units)) 

1220 if n: 

1221 R = Units + U[n:] 

1222 if R != U: 

1223 self._Units_ = R 

1224 return self.renamed(name) if name else self 

1225 

1226 def toRepr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature 

1227 '''Return this C{Named-Tuple} items as C{name=value} string(s). 

1228 

1229 @kwarg prec: The C{float} precision, number of decimal digits (0..9). 

1230 Trailing zero decimals are stripped for B{C{prec}} values 

1231 of 1 and above, but kept for negative B{C{prec}} values. 

1232 @kwarg sep: Separator to join (C{str}). 

1233 @kwarg fmt: Optional C{float} format (C{letter}). 

1234 

1235 @return: Tuple items (C{str}). 

1236 ''' 

1237 t = pairs(self.items(), prec=prec, fmt=fmt) 

1238# if self.name: 

1239# t = (Fmt.EQUAL(name=repr(self.name)),) + t 

1240 return Fmt.PAREN(self.named, sep.join(t)) # XXX (self.classname, sep.join(t)) 

1241 

1242 def toStr(self, prec=6, sep=_COMMASPACE_, fmt=Fmt.F, **unused): # PYCHOK signature 

1243 '''Return this C{Named-Tuple} items as string(s). 

1244 

1245 @kwarg prec: The C{float} precision, number of decimal digits (0..9). 

1246 Trailing zero decimals are stripped for B{C{prec}} values 

1247 of 1 and above, but kept for negative B{C{prec}} values. 

1248 @kwarg sep: Separator to join (C{str}). 

1249 @kwarg fmt: Optional C{float} format (C{letter}). 

1250 

1251 @return: Tuple items (C{str}). 

1252 ''' 

1253 return Fmt.PAREN(sep.join(reprs(self, prec=prec, fmt=fmt))) 

1254 

1255 def toUnits(self, Error=UnitError, **name): # overloaded in .frechet, .hausdorff 

1256 '''Return a copy of this C{Named-Tuple} with each item value wrapped 

1257 as an instance of its L{units} class. 

1258 

1259 @kwarg Error: Error to raise for L{units} issues (C{UnitError}). 

1260 @kwarg name: Optional C{B{name}=NN} (C{str}). 

1261 

1262 @return: A duplicate of this C{Named-Tuple} (C{C{Named-Tuple}}). 

1263 

1264 @raise Error: Invalid C{Named-Tuple} item or L{units} class. 

1265 ''' 

1266 t = tuple(v for _, v in self.units(Error=Error)) 

1267 return self.classof(*t).reUnit(*self._Units_, **name) 

1268 

1269 def units(self, **Error): 

1270 '''Yield the items, each as a C{2-tuple (name, value}) with the 

1271 value wrapped as an instance of its L{units} class. 

1272 

1273 @kwarg Error: Optional C{B{Error}=UnitError} to raise. 

1274 

1275 @raise Error: Invalid C{Named-Tuple} item or L{units} class. 

1276 

1277 @see: Method C{.items}. 

1278 ''' 

1279 _2U = _MODS.units._toUnit 

1280 for n, v, U in _zip(self._Names_, self, self._Units_): # strict=True 

1281 yield n, _2U(U, v, name=n, **Error) 

1282 

1283 iterunits = units 

1284 

1285 def _validate(self, underOK=False): # see .EcefMatrix 

1286 '''(INTERNAL) One-time check of C{_Names_} and C{_Units_} 

1287 for each C{_NamedUnit} I{sub-class separately}. 

1288 ''' 

1289 ns = self._Names_ 

1290 if not (isinstance(ns, tuple) and len(ns) > 1): # XXX > 0 

1291 raise _TypeError(self._DOT_(_Names_), ns) 

1292 for i, n in enumerate(ns): 

1293 if not _xvalid(n, underOK=underOK): 

1294 t = Fmt.SQUARE(_Names_=i) # PYCHOK no cover 

1295 raise _ValueError(self._DOT_(t), n) 

1296 

1297 us = self._Units_ 

1298 if not isinstance(us, tuple): 

1299 raise _TypeError(self._DOT_(_Units_), us) 

1300 if len(us) != len(ns): 

1301 raise LenError(self.__class__, _Units_=len(us), _Names_=len(ns)) 

1302 for i, u in enumerate(us): 

1303 if not (u is None or callable(u)): 

1304 t = Fmt.SQUARE(_Units_=i) # PYCHOK no cover 

1305 raise _TypeError(self._DOT_(t), u) 

1306 

1307 type(self)._validated = True 

1308 

1309 def _xtend(self, xTuple, *items, **name): 

1310 '''(INTERNAL) Extend this C{Named-Tuple} with C{items} to an other B{C{xTuple}}. 

1311 ''' 

1312 _xsubclassof(_NamedTuple, xTuple=xTuple) 

1313 if len(xTuple._Names_) != (len(self._Names_) + len(items)) or \ 

1314 xTuple._Names_[:len(self)] != self._Names_: # PYCHOK no cover 

1315 c = NN(self.classname, repr(self._Names_)) 

1316 x = NN(typename(xTuple), repr(xTuple._Names_)) 

1317 raise TypeError(_SPACE_(c, _vs_, x)) 

1318 t = self + items 

1319 return xTuple(t, name=self._name__(name)) # .reUnit(*self._Units_) 

1320 

1321 

1322def callername(up=1, dflt=NN, source=False, underOK=False): 

1323 '''Get the name of the invoking callable. 

1324 

1325 @kwarg up: Number of call stack frames up (C{int}). 

1326 @kwarg dflt: Default return value (C{any}). 

1327 @kwarg source: Include source file name and line number (C{bool}). 

1328 @kwarg underOK: If C{True}, private, internal callables are OK, 

1329 otherwise private callables are skipped (C{bool}). 

1330 

1331 @return: The callable name (C{str}) or B{C{dflt}} if none found. 

1332 ''' 

1333 try: # see .lazily._caller3 

1334 for u in range(up, up + 32): 

1335 n, f, s = _caller3(u) 

1336 if n and (underOK or n.startswith(_DUNDER_) or 

1337 not n.startswith(_UNDER_)): 

1338 if source: 

1339 n = NN(n, _AT_, f, _COLON_, str(s)) 

1340 return n 

1341 except (AttributeError, ValueError): 

1342 pass 

1343 return dflt 

1344 

1345 

1346def _callername2(args, callername=NN, source=False, underOK=False, up=_UP, **kwds): 

1347 '''(INTERNAL) Extract C{callername}, C{source}, C{underOK} and C{up} from C{kwds}. 

1348 ''' 

1349 n = callername or _MODS.named.callername(up=up + 1, source=source, 

1350 underOK=underOK or bool(args or kwds)) 

1351 return n, kwds 

1352 

1353 

1354def _callname(name, class_name, self_name, up=1): 

1355 '''(INTERNAL) Assemble the name for an invokation. 

1356 ''' 

1357 n, c = class_name, callername(up=up + 1) 

1358 if c: 

1359 n = _DOT_(n, Fmt.PAREN(c, name)) 

1360 if self_name: 

1361 n = _SPACE_(n, repr(self_name)) 

1362 return n 

1363 

1364 

1365def classname(inst, prefixed=None): 

1366 '''Return the instance' class name optionally prefixed with the 

1367 module name. 

1368 

1369 @arg inst: The object (any C{type}). 

1370 @kwarg prefixed: Include the module name (C{bool}), see 

1371 function C{classnaming}. 

1372 

1373 @return: The B{C{inst}}'s C{[module.]class} name (C{str}). 

1374 ''' 

1375 if prefixed is None: 

1376 prefixed = getattr(inst, typename(classnaming), prefixed) 

1377 return modulename(inst.__class__, prefixed=prefixed) 

1378 

1379 

1380def classnaming(prefixed=None): 

1381 '''Get/set the default class naming for C{[module.]class} names. 

1382 

1383 @kwarg prefixed: Include the module name (C{bool}). 

1384 

1385 @return: Previous class naming setting (C{bool}). 

1386 ''' 

1387 t = _Named._classnaming 

1388 if isbool(prefixed): 

1389 _Named._classnaming = prefixed 

1390 return t 

1391 

1392 

1393def modulename(clas, prefixed=None): # in .basics._xversion 

1394 '''Return the class name optionally prefixed with the 

1395 module name. 

1396 

1397 @arg clas: The class (any C{class}). 

1398 @kwarg prefixed: Include the module name (C{bool}), see 

1399 function C{classnaming}. 

1400 

1401 @return: The B{C{class}}'s C{[module.]class} name (C{str}). 

1402 ''' 

1403 try: 

1404 n = typename(clas) 

1405 except AttributeError: 

1406 n = clas if isstr(clas) else _DNAME_ 

1407 if prefixed or (classnaming() if prefixed is None else False): 

1408 try: 

1409 m = clas.__module__.rsplit(_DOT_, 1) 

1410 n = _DOT_.join(m[1:] + [n]) 

1411 except AttributeError: 

1412 pass 

1413 return n 

1414 

1415 

1416# def _name__(name=NN, name__=None, _or_nameof=None, **kwds): 

1417# '''(INTERNAL) Get single keyword argument C{B{name}=NN|None}. 

1418# ''' 

1419# if kwds: # "unexpected keyword arguments ..." 

1420# m = _MODS.errors 

1421# raise m._UnexpectedError(**kwds) 

1422# if name: # is given 

1423# n = _name__(**name) if isinstance(name, dict) else str(name) 

1424# elif name__ is not None: 

1425# n = getattr(name__, _DNAME_, NN) # _xattr(name__, __name__=NN) 

1426# else: 

1427# n = name # NN or None or {} or any False type 

1428# if _or_nameof is not None and not n: 

1429# n = getattr(_or_nameof, _name_, NN) # _xattr(_or_nameof, name=NN) 

1430# return n # str or None or {} 

1431 

1432 

1433def _name__(name=NN, **kwds): 

1434 '''(INTERNAL) Get single keyword argument C{B{name}=NN|None}. 

1435 ''' 

1436 if name or kwds: 

1437 name, kwds = _name2__(name, **kwds) 

1438 if kwds: # "unexpected keyword arguments ..." 

1439 raise _UnexpectedError(**kwds) 

1440 return name if name or name is None else NN 

1441 

1442 

1443def _name1__(kwds_name, **name__or_nameof): 

1444 '''(INTERNAL) Resolve and set the C{B{name}=NN}. 

1445 ''' 

1446 if kwds_name or name__or_nameof: 

1447 n, kwds_name = _name2__(kwds_name, **name__or_nameof) 

1448 kwds_name.update(name=n) 

1449 return kwds_name 

1450 

1451 

1452def _name2__(name=NN, name__=None, _or_nameof=None, **kwds): 

1453 '''(INTERNAL) Get the C{B{name}=NN|None} and other C{kwds}. 

1454 ''' 

1455 if name: # is given 

1456 if isinstance(name, dict): 

1457 kwds.update(name) # kwds = _xkwds(kwds, **name)? 

1458 n, kwds = _name2__(**kwds) 

1459 else: 

1460 n = str(name) 

1461 elif name__ is not None: 

1462 n = typename(name__, NN) 

1463 else: 

1464 n = name if name is None else NN 

1465 if _or_nameof is not None and not n: 

1466 n = _xattr(_or_nameof, name=NN) # nameof 

1467 return n, kwds # (str or None or {}), dict 

1468 

1469 

1470def nameof(inst): 

1471 '''Get the name of an instance. 

1472 

1473 @arg inst: The object (any C{type}). 

1474 

1475 @return: The instance' name (C{str}) or C{""}. 

1476 ''' 

1477 n = _xattr(inst, name=NN) 

1478 if not n: # and isinstance(inst, property): 

1479 try: 

1480 n = typename(inst.fget) 

1481 except AttributeError: 

1482 n = NN 

1483 return n 

1484 

1485 

1486def _notDecap(where): 

1487 '''De-Capitalize C{where.__name__}. 

1488 ''' 

1489 n = typename(where) 

1490 c = n[3].lower() # len(_not_) 

1491 return NN(n[:3], _SPACE_, c, n[4:]) 

1492 

1493 

1494def _notError(inst, name, args, kwds): # PYCHOK no cover 

1495 '''(INTERNAL) Format an error message. 

1496 ''' 

1497 n = _DOT_(classname(inst, prefixed=True), typename(name, name)) 

1498 m = _COMMASPACE_.join(modulename(c, prefixed=True) for c in type(inst).__mro__[1:-1]) 

1499 return _COMMASPACE_(unstr(n, *args, **kwds), Fmt.PAREN(_MRO_, m)) 

1500 

1501 

1502def _NotImplemented(inst, *other, **kwds): 

1503 '''(INTERNAL) Raise a C{__special__} error or return C{NotImplemented}, 

1504 but only if env variable C{PYGEODESY_NOTIMPLEMENTED=std}. 

1505 ''' 

1506 if _std_NotImplemented: 

1507 return NotImplemented 

1508 n, kwds = _callername2(other, **kwds) # source=True 

1509 t = unstr(_DOT_(classname(inst), n), *other, **kwds) 

1510 raise _NotImplementedError(t, txt=repr(inst)) 

1511 

1512 

1513def notImplemented(inst, *args, **kwds): # PYCHOK no cover 

1514 '''Raise a C{NotImplementedError} for a missing instance method or 

1515 property or for a missing caller feature. 

1516 

1517 @arg inst: Caller instance (C{any}) or C{None} for function. 

1518 @arg args: Method or property positional arguments (any C{type}s). 

1519 @arg kwds: Method or property keyword arguments (any C{type}s), 

1520 except C{B{callername}=NN}, C{B{underOK}=False} and 

1521 C{B{up}=2}. 

1522 ''' 

1523 n, kwds = _callername2(args, **kwds) 

1524 t = _notError(inst, n, args, kwds) if inst else unstr(n, *args, **kwds) 

1525 raise _NotImplementedError(t, txt=_notDecap(notImplemented)) 

1526 

1527 

1528def notOverloaded(inst, *args, **kwds): # PYCHOK no cover 

1529 '''Raise an C{AssertionError} for a method or property not overloaded. 

1530 

1531 @arg inst: Instance (C{any}). 

1532 @arg args: Method or property positional arguments (any C{type}s). 

1533 @arg kwds: Method or property keyword arguments (any C{type}s), 

1534 except C{B{callername}=NN}, C{B{underOK}=False} and 

1535 C{B{up}=2}. 

1536 ''' 

1537 n, kwds = _callername2(args, **kwds) 

1538 t = _notError(inst, n, args, kwds) 

1539 raise _AssertionError(t, txt=_notDecap(notOverloaded)) 

1540 

1541 

1542def _Pass(arg, **unused): # PYCHOK no cover 

1543 '''(INTERNAL) I{Pass-thru} class for C{_NamedTuple._Units_}. 

1544 ''' 

1545 return arg 

1546 

1547 

1548def _xjoined_(prefix, name=NN, enquote=True, **name__or_nameof): 

1549 '''(INTERNAL) Join C{prefix} and non-empty C{name}. 

1550 ''' 

1551 if name__or_nameof: 

1552 name = _name__(name, **name__or_nameof) 

1553 if name and prefix: 

1554 if enquote: 

1555 name = repr(name) 

1556 t = _SPACE_(prefix, name) 

1557 else: 

1558 t = prefix or name 

1559 return t 

1560 

1561 

1562def _xnamed(inst, name=NN, force=False, **name__or_nameof): 

1563 '''(INTERNAL) Set the instance' C{.name = B{name}}. 

1564 

1565 @arg inst: The instance (C{_Named}). 

1566 @kwarg name: The name (C{str}). 

1567 @kwarg force: If C{True}, force rename (C{bool}). 

1568 

1569 @return: The B{C{inst}}, renamed if B{C{force}}d 

1570 or if not named before. 

1571 ''' 

1572 if name__or_nameof: 

1573 name = _name__(name, **name__or_nameof) 

1574 if name and isinstance(inst, _Named): 

1575 if not inst.name: 

1576 inst.name = name 

1577 elif force: 

1578 inst.rename(name) 

1579 return inst 

1580 

1581 

1582def _xother3(inst, other, name=_other_, up=1, **name_other): 

1583 '''(INTERNAL) Get C{name} and C{up} for a named C{other}. 

1584 ''' 

1585 if name_other: # and other is None 

1586 name, other = _xkwds_item2(name_other) 

1587 elif other and len(other) == 1: 

1588 name, other = _name__(name), other[0] 

1589 else: 

1590 raise _AssertionError(name, other, txt=classname(inst, prefixed=True)) 

1591 return other, name, up 

1592 

1593 

1594def _xotherError(inst, other, name=_other_, up=1): 

1595 '''(INTERNAL) Return a C{_TypeError} for an incompatible, named C{other}. 

1596 ''' 

1597 n = _callname(name, classname(inst, prefixed=True), inst.name, up=up + 1) 

1598 return _TypeError(name, other, txt=_incompatible(n)) 

1599 

1600 

1601def _xvalid(name, underOK=False): 

1602 '''(INTERNAL) Check valid attribute name C{name}. 

1603 ''' 

1604 return bool(name and isstr(name) 

1605 and name != _name_ 

1606 and (underOK or not name.startswith(_UNDER_)) 

1607 and (not iskeyword(name)) 

1608 and isidentifier(name)) 

1609 

1610 

1611__all__ += _ALL_DOCS(_Named, 

1612 _NamedBase, # _NamedDict, 

1613 _NamedEnum, _NamedEnumItem, 

1614 _NamedLocal, 

1615 _NamedTuple) 

1616 

1617# **) MIT License 

1618# 

1619# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved. 

1620# 

1621# Permission is hereby granted, free of charge, to any person obtaining a 

1622# copy of this software and associated documentation files (the "Software"), 

1623# to deal in the Software without restriction, including without limitation 

1624# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

1625# and/or sell copies of the Software, and to permit persons to whom the 

1626# Software is furnished to do so, subject to the following conditions: 

1627# 

1628# The above copyright notice and this permission notice shall be included 

1629# in all copies or substantial portions of the Software. 

1630# 

1631# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

1632# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

1633# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

1634# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

1635# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

1636# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

1637# OTHER DEALINGS IN THE SOFTWARE.