Coverage for pygeodesy/named.py: 97%

466 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-05-06 16:50 -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 isclass, isidentifier, iskeyword, isstr, issubclassof, \ 

17 itemsorted, len2, _sizeof, _xcopy, _xdup, _zip 

18from pygeodesy.errors import _AssertionError, _AttributeError, _incompatible, \ 

19 _IndexError, _IsnotError, _KeyError, LenError, \ 

20 _NameError, _NotImplementedError, _TypeError, \ 

21 _TypesError, UnitError, _ValueError, _xattr, _xkwds, \ 

22 _xkwds_get, _xkwds_item2, _xkwds_pop2 

23from pygeodesy.interns import MISSING, NN, _AT_, _COLON_, _COLONSPACE_, _COMMA_, \ 

24 _COMMASPACE_, _doesn_t_exist_, _DOT_, _DUNDER_, _EQUAL_, \ 

25 _exists_, _immutable_, _name_, _NL_, _NN_, _no_, _other_, \ 

26 _s_, _SPACE_, _std_, _UNDER_, _valid_, _vs_, \ 

27 _dunder_nameof, _isPyPy, _under 

28from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _caller3, _getenv 

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

30 _update_all, property_doc_, Property_RO, property_RO, \ 

31 _update_attrs 

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

33 

34__all__ = _ALL_LAZY.named 

35__version__ = '24.05.04' 

36 

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_' 

48_UP = 2 

49 

50 

51def _xjoined_(prefix, name, enquote=True): 

52 '''(INTERNAL) Join C{pref} and non-empty C{name}. 

53 ''' 

54 if name and prefix: 

55 if enquote: 

56 name = repr(name) 

57 n = _SPACE_(prefix, name) 

58 else: 

59 n = prefix or name 

60 return n 

61 

62 

63def _xnamed(inst, name, force=False): 

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

65 

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

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

68 @kwarg force: Force name change (C{bool}). 

69 

70 @return: The B{C{inst}}, named if B{C{force}}d or 

71 not named before. 

72 ''' 

73 if name and isinstance(inst, _Named): 

74 if not inst.name: 

75 inst.name = name 

76 elif force: 

77 inst.rename(name) 

78 return inst 

79 

80 

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

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

83 ''' 

84 if name_other: # and other is None 

85 name, other = _xkwds_item2(name_other) 

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

87 other = other[0] 

88 else: 

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

90 return other, name, up 

91 

92 

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

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

95 ''' 

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

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

98 

99 

100def _xvalid(name, underOK=False): 

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

102 ''' 

103 return bool(name and isstr(name) 

104 and name != _name_ 

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

106 and (not iskeyword(name)) 

107 and isidentifier(name)) 

108 

109 

110class ADict(dict): 

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

112 the C{dict} items. 

113 ''' 

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

115 

116 def __getattr__(self, name): 

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

118 ''' 

119 try: 

120 return self[name] 

121 except KeyError: 

122 if name == _name_: 

123 return NN 

124 raise self._AttributeError(name) 

125 

126 def __repr__(self): 

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

128 ''' 

129 return self.toRepr() 

130 

131 def __str__(self): 

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

133 ''' 

134 return self.toStr() 

135 

136 def _AttributeError(self, name): 

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

138 ''' 

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

140 name = _DOT_(self.__class__.__name__, name) 

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

142 

143 @property_RO 

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

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

146 C{None} if not available/applicable. 

147 ''' 

148 return self._iteration 

149 

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

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

152 

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

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

155 ''' 

156 if iteration is not None: 

157 self._iteration = iteration 

158 if items: 

159 dict.update(self, items) 

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

161 

162 def toRepr(self, **prec_fmt): 

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

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

165 ''' 

166 n = _xattr(self, name=NN) or self.__class__.__name__ 

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

168 

169 def toStr(self, **prec_fmt): 

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

171 function L{pygeodesy.fstr}. 

172 ''' 

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

174 

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

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

177 ''' 

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

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

180 

181 

182class _Named(object): 

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

184 ''' 

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

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

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

188# _updates = 0 # OBSOLETE Property/property updates 

189 

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

191 '''Not implemented.''' 

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

193 

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

195 '''Not implemented.''' 

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

197 

198 def __repr__(self): 

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

200 ''' 

201 return Fmt.repr_at(self) 

202 

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

204 '''Not implemented.''' 

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

206 

207 def __str__(self): 

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

209 ''' 

210 return self.named2 

211 

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

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

214 function L{pygeodesy.fstr}. 

215 

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

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

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

219 or C{None}-valued attributes. 

220 

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

222 

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

224 ''' 

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

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

227 

228 @Property_RO 

229 def classname(self): 

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

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

232 ''' 

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

234 

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

236 def classnaming(self): 

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

238 ''' 

239 return self._classnaming 

240 

241 @classnaming.setter # PYCHOK setter! 

242 def classnaming(self, prefixed): 

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

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

245 ''' 

246 b = bool(prefixed) 

247 if self._classnaming != b: 

248 self._classnaming = b 

249 _update_attrs(self, *_Named_Property_ROs) 

250 

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

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

253 

254 @arg args: Optional, positional arguments. 

255 @kwarg kwds: Optional, keyword arguments. 

256 

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

258 ''' 

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

260 

261 def copy(self, deep=False, name=NN): 

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

263 

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

265 a shallow copy (C{bool}). 

266 @kwarg name: Optional, non-empty name (C{str}). 

267 

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

269 ''' 

270 c = _xcopy(self, deep=deep) 

271 if name: 

272 c.rename(name) 

273 return c 

274 

275 def _DOT_(self, *names): 

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

277 ''' 

278 return _DOT_(self.name, *names) 

279 

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

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

282 

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

284 @kwarg items: Attributes to be changed (C{any}). 

285 

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

287 

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

289 ''' 

290 n = self.name 

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

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

293 if m != n: 

294 d.rename(m) 

295# if items: 

296# _update_all(d) 

297 return d 

298 

299 def _instr(self, name, prec, *attrs, **fmt_props_kwds): 

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

301 ''' 

302 def _fmt_props_kwds(fmt=Fmt.F, props=(), **kwds): 

303 return fmt, props, kwds 

304 

305 fmt, props, kwds = _fmt_props_kwds(**fmt_props_kwds) 

306 

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

308 if attrs: 

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

310 prec=prec, ints=True, fmt=fmt) 

311 if props: 

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

313 prec=prec, ints=True) 

314 if kwds: 

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

316 return _COMMASPACE_.join(t) 

317 

318 @property_RO 

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

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

321 if not available or not applicable. 

322 

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

324 several, nested functions. 

325 ''' 

326 return self._iteration 

327 

328 def methodname(self, which): 

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

330 

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

332 ''' 

333 return _DOT_(self.classname, which.__name__ if callable(which) else _NN_) 

334 

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

336 def name(self): 

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

338 ''' 

339 return self._name 

340 

341 @name.setter # PYCHOK setter! 

342 def name(self, name): 

343 '''Set the name (C{str}). 

344 

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

346 ''' 

347 m, n = self._name, str(name) 

348 if not m: 

349 self._name = n 

350 elif n != m: 

351 n = repr(n) 

352 c = self.classname 

353 t = _DOT_(c, Fmt.PAREN(self.rename.__name__, n)) 

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

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

356 n = _SPACE_(n, m) 

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

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

359 # self.name = name or 

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

361 # _Named(self).name = name 

362 

363 @Property_RO 

364 def named(self): 

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

366 ''' 

367 return self.name or self.classname 

368 

369# @Property_RO 

370# def named_(self): 

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

372# ''' 

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

374 

375 @Property_RO 

376 def named2(self): 

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

378 ''' 

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

380 

381 @Property_RO 

382 def named3(self): 

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

384 ''' 

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

386 

387 @Property_RO 

388 def named4(self): 

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

390 ''' 

391 return _xjoined_(_DOT_(self.__module__, self.__class__.__name__), self.name) 

392 

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

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

395 ''' 

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

397 

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

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

400 ''' 

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

402 

403 def rename(self, name): 

404 '''Change the name. 

405 

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

407 

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

409 ''' 

410 m, n = self._name, str(name) 

411 if n != m: 

412 self._name = n 

413 _update_attrs(self, *_Named_Property_ROs) 

414 return m 

415 

416 @property_RO 

417 def sizeof(self): 

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

419 ''' 

420 return _sizeof(self) 

421 

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

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

424 ''' 

425 return repr(self) 

426 

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

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

429 ''' 

430 return str(self) 

431 

432 @deprecated_method 

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

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

435 return self.toRepr(**kwds) 

436 

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

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

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

440# 

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

442# ''' 

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

444 

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

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

447 

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

449 @kwarg name: Optional name, overriding C{self.name} (C{str}). 

450 @kwarg force: Force name change (C{bool}). 

451 

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

453 ''' 

454 return _xnamed(inst, name or self.name, force=force) 

455 

456 def _xrenamed(self, inst): 

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

458 

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

460 

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

462 

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

464 ''' 

465 if not isinstance(inst, _Named): 

466 raise _IsnotError(_valid_, inst=inst) 

467 

468 inst.rename(self.name) 

469 return inst 

470 

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

472 

473 

474class _NamedBase(_Named): 

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

476 ''' 

477 def __repr__(self): 

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

479 ''' 

480 return self.toRepr() 

481 

482 def __str__(self): 

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

484 ''' 

485 return self.toStr() 

486 

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

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

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

490 

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

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

493 keyword arguments. 

494 

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

496 C{class} or C{type}. 

497 

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

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

500 ''' 

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

502 other0 = other[0] 

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

504 isinstance(self, other0.__class__): 

505 return other0 

506 

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

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

509 isinstance(other, self.__class__): 

510 return _xnamed(other, name) 

511 

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

513 

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

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

516 

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

518 

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

520 ''' 

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

522# if self.name: 

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

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

525 

526# def toRepr(self, **kwds) 

527# if kwds: 

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

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

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

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

532 

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

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

535 notOverloaded(self, **kwds) 

536 

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

538# if kwds: 

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

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

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

542# return s 

543 

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

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

546 ''' 

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

548 if setters: 

549 d = self.__dict__ 

550 # double-check that setters are Property_RO's 

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

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

553 d[n] = v 

554 else: 

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

556 u += len(setters) 

557 return u 

558 

559 

560class _NamedDict(ADict, _Named): 

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

562 access to the items. 

563 ''' 

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

565 if args: # args override kwds 

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

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

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

569 kwds = _xkwds(dict(args[0]), **kwds) 

570 n, kwds = _xkwds_pop2(kwds, name=NN) 

571 if n: 

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

573 ADict.__init__(self, kwds) 

574 

575 def __delattr__(self, name): 

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

577 ''' 

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

579 ADict.pop(self, name) 

580 elif name in (_name_, _name): 

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

582 _Named.rename(self, NN) 

583 else: 

584 ADict.__delattr__(self, name) 

585 

586 def __getattr__(self, name): 

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

588 ''' 

589 try: 

590 return self[name] 

591 except KeyError: 

592 if name == _name_: 

593 return _Named.name.fget(self) 

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

595 

596 def __getitem__(self, key): 

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

598 ''' 

599 if key == _name_: 

600 raise self._KeyError(key) 

601 return ADict.__getitem__(self, key) 

602 

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

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

605 ''' 

606 n = self.name or self.__class__.__name__ 

607 t = Fmt.SQUARE(n, key) 

608 if value: 

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

610 return _KeyError(t) 

611 

612 def __setattr__(self, name, value): 

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

614 ''' 

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

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

617 else: 

618 ADict.__setattr__(self, name, value) 

619 

620 def __setitem__(self, key, value): 

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

622 ''' 

623 if key == _name_: 

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

625 ADict.__setitem__(self, key, value) 

626 

627 

628class _NamedEnum(_NamedDict): 

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

630 restricted to valid keys. 

631 ''' 

632 _item_Classes = () 

633 

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

635 '''New C{_NamedEnum}. 

636 

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

638 values (C{type}). 

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

640 ''' 

641 self._item_Classes = (Class,) + Classes 

642 n = _xkwds_get(name, name=NN) or NN(Class.__name__, _s_) 

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

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

645 

646 def __getattr__(self, name): 

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

648 ''' 

649 return _NamedDict.__getattr__(self, name) 

650 

651 def __repr__(self): 

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

653 ''' 

654 return self.toRepr() 

655 

656 def __str__(self): 

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

658 ''' 

659 return self.toStr() 

660 

661 def _assert(self, **kwds): 

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

663 ''' 

664 pypy = _isPyPy() 

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

666 if isinstance(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 isinstance(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 for n, p in tuple(self.__class__.__dict__.items()): 

714 if isinstance(p, _LazyNamedEnumItem): 

715 _ = getattr(self, n) 

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

717 

718 def keys(self, **all_asorted): 

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

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

721 

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

723 ''' 

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

725 yield k 

726 

727 def popitem(self): 

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

729 

730 @return: The removed item. 

731 ''' 

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

733 

734 def register(self, item): 

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

736 

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

738 

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

740 

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

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

743 invalid name. 

744 

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

746 ''' 

747 if item is all or item is any: 

748 _ = self.items(all=True) 

749 n = item.__name__ 

750 else: 

751 try: 

752 n = item.name 

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

754 raise ValueError() 

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

756 raise _NameError(_DOT_(_item_, _name_), item, cause=x) 

757 if n in self: 

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

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

760 if not isinstance(item, self._item_Classes): 

761 raise _TypesError(self._DOT_(n), item, *self._item_Classes) 

762 self[n] = item 

763 return n 

764 

765 def unregister(self, name_or_item): 

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

767 

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

769 

770 @return: The unregistered item. 

771 

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

773 

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

775 ''' 

776 if isstr(name_or_item): 

777 name = name_or_item 

778 else: 

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

780 if name is MISSING: 

781 t = _SPACE_(_no_, 'such', self.classname, _item_) 

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

783 try: 

784 item = ADict.pop(self, name) 

785 except KeyError: 

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

787 return self._zapitem(name, item) 

788 

789 pop = unregister 

790 

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

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

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

794 ''' 

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

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

797 

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

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

800 ''' 

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

802 

803 def values(self, **all_asorted): 

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

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

806 

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

808 ''' 

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

810 yield v 

811 

812 def _zapitem(self, name, item): 

813 # remove _LazyNamedEnumItem property value if still present 

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

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

816 item._enum = None 

817 return item 

818 

819 

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

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

822 ''' 

823 pass 

824 

825 

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

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

828 

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

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

831 ''' 

832 def _fget(inst): 

833 # assert isinstance(inst, _NamedEnum) 

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

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

836 item = inst[name] 

837 except KeyError: 

838 # instantiate an _NamedEnumItem, it self-registers 

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

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

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

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

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

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

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

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

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

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

849 if isinstance(p, _LazyNamedEnumItem): 

850 delattr(inst.__class__, name) 

851 # assert isinstance(item, _NamedEnumItem) 

852 return item 

853 

854 p = _LazyNamedEnumItem(_fget) 

855 p.name = name 

856 return p 

857 

858 

859class _NamedEnumItem(_NamedBase): 

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

861 ''' 

862 _enum = None 

863 

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

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

866# 

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

868# ''' 

869# return not self.__eq__(other) 

870 

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

872 def name(self): 

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

874 ''' 

875 return self._name 

876 

877 @name.setter # PYCHOK setter! 

878 def name(self, name): 

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

880 ''' 

881 if self._enum: 

882 raise _NameError(str(name), self, txt=_registered_) # XXX _TypeError 

883 self._name = str(name) 

884 

885 def _register(self, enum, name): 

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

887 

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

889 start with a letter. 

890 ''' 

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

892 self.name = name 

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

894 enum.register(self) 

895 self._enum = enum 

896 

897 def unregister(self): 

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

899 

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

901 

902 @raise NameError: This item is unregistered. 

903 ''' 

904 enum = self._enum 

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

906 item = enum.unregister(self.name) 

907 if item is not self: 

908 t = _SPACE_(repr(item), _vs_, repr(self)) # PYCHOK no cover 

909 raise _AssertionError(t) 

910 

911 

912class _NamedTuple(tuple, _Named): 

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

914 attribute name access to the items. 

915 

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

917 but statically defined, lighter and limited. 

918 ''' 

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

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

921 

922 @note: Specify at least 2 item names. 

923 ''' 

924 _Units_ = () # .units classes 

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

926 

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

928 ''' 

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

930 

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

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

933 

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

935 item of several more in other positional arguments. 

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

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

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

939 I{silently} ignored. 

940 

941 @raise LenError: Unequal number of positional arguments and 

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

943 

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

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

946 

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

948 or starts with C{underscore}. 

949 ''' 

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

951 self = tuple.__new__(cls, args) 

952 if not self._validated: 

953 self._validate() 

954 

955 N = len(self._Names_) 

956 if n != N: 

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

958 

959 if iteration_name: 

960 self._kwdself(**iteration_name) 

961 return self 

962 

963 def __delattr__(self, name): 

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

965 

966 @note: Items can not be deleted. 

967 ''' 

968 if name in self._Names_: 

969 raise _TypeError(_del_, _DOT_(self.classname, name), txt=_immutable_) 

970 elif name in (_name_, _name): 

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

972 else: 

973 tuple.__delattr__(self, name) 

974 

975 def __getattr__(self, name): 

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

977 ''' 

978 try: 

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

980 except IndexError: 

981 raise _IndexError(_DOT_(self.classname, Fmt.ANGLE(_name_)), name) 

982 except ValueError: # e.g. _iteration 

983 return tuple.__getattribute__(self, name) 

984 

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

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

987# ''' 

988# return tuple.__getitem__(self, index) 

989 

990 def __hash__(self): 

991 return tuple.__hash__(self) 

992 

993 def __repr__(self): 

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

995 ''' 

996 return self.toRepr() 

997 

998 def __setattr__(self, name, value): 

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

1000 ''' 

1001 if name in self._Names_: 

1002 raise _TypeError(_DOT_(self.classname, name), value, txt=_immutable_) 

1003 elif name in (_name_, _name): 

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

1005 else: # e.g. _iteration 

1006 tuple.__setattr__(self, name, value) 

1007 

1008 def __str__(self): 

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

1010 ''' 

1011 return self.toStr() 

1012 

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

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

1015 

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

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

1018 

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

1020 

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

1022 ''' 

1023 tl = list(self) 

1024 if items: 

1025 _ix = self._Names_.index 

1026 try: 

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

1028 tl[_ix(n)] = v 

1029 except ValueError: # bad item name 

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

1031 return self.classof(*tl, name=name or self.name) 

1032 

1033 def items(self): 

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

1035 

1036 @see: Method C{.units}. 

1037 ''' 

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

1039 yield n, v 

1040 

1041 iteritems = items 

1042 

1043 def _kwdself(self, iteration=None, name=NN, **unused): 

1044 '''(INTERNAL) Set C{__new__} keyword arguments. 

1045 ''' 

1046 if iteration is not None: 

1047 self._iteration = iteration 

1048 if name: 

1049 self.name = name 

1050 

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

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

1053 

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

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

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

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

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

1059 

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

1061 ''' 

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

1063# if self.name: 

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

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

1066 

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

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

1069 

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

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

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

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

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

1075 

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

1077 ''' 

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

1079 

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

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

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

1083 

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

1085 

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

1087 

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

1089 ''' 

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

1091 return self.classof(*tuple(t)) 

1092 

1093 def units(self, Error=UnitError): 

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

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

1096 

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

1098 

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

1100 

1101 @see: Method C{.items}. 

1102 ''' 

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

1104 if not (v is None or U is None 

1105 or (isclass(U) and 

1106 isinstance(v, U) and 

1107 hasattr(v, _name_) and 

1108 v.name == n)): # PYCHOK indent 

1109 v = U(v, name=n, Error=Error) 

1110 yield n, v 

1111 

1112 iterunits = units 

1113 

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

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

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

1117 ''' 

1118 ns = self._Names_ 

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

1120 raise _TypeError(_DOT_(self.classname, _Names_), ns) 

1121 for i, n in enumerate(ns): 

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

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

1124 raise _ValueError(_DOT_(self.classname, t), n) 

1125 

1126 us = self._Units_ 

1127 if not isinstance(us, tuple): 

1128 raise _TypeError(_DOT_(self.classname, _Units_), us) 

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

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

1131 for i, u in enumerate(us): 

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

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

1134 raise _TypeError(_DOT_(self.classname, t), u) 

1135 

1136 self.__class__._validated = True 

1137 

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

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

1140 ''' 

1141 if (issubclassof(xTuple, _NamedTuple) and 

1142 (len(self._Names_) + len(items)) == len(xTuple._Names_) and 

1143 self._Names_ == xTuple._Names_[:len(self)]): 

1144 return xTuple(self + items, **_xkwds(name, name=self.name)) # *(self + items) 

1145 c = NN(self.classname, repr(self._Names_)) # PYCHOK no cover 

1146 x = NN(xTuple.__name__, repr(xTuple._Names_)) # PYCHOK no cover 

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

1148 

1149 

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

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

1152 

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

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

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

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

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

1158 

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

1160 ''' 

1161 try: # see .lazily._caller3 

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

1163 n, f, s = _caller3(u) 

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

1165 not n.startswith(_UNDER_)): 

1166 if source: 

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

1168 return n 

1169 except (AttributeError, ValueError): 

1170 pass 

1171 return dflt 

1172 

1173 

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

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

1176 ''' 

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

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

1179 return n, kwds 

1180 

1181 

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

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

1184 ''' 

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

1186 if c: 

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

1188 if self_name: 

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

1190 return n 

1191 

1192 

1193def classname(inst, prefixed=None): 

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

1195 module name. 

1196 

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

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

1199 function C{classnaming}. 

1200 

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

1202 ''' 

1203 if prefixed is None: 

1204 prefixed = getattr(inst, classnaming.__name__, prefixed) 

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

1206 

1207 

1208def classnaming(prefixed=None): 

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

1210 

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

1212 

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

1214 ''' 

1215 t = _Named._classnaming 

1216 if prefixed in (True, False): 

1217 _Named._classnaming = prefixed 

1218 return t 

1219 

1220 

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

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

1223 module name. 

1224 

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

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

1227 function C{classnaming}. 

1228 

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

1230 ''' 

1231 try: 

1232 n = clas.__name__ 

1233 except AttributeError: 

1234 n = '__name__' # _DUNDER_(NN, _name_, NN) 

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

1236 try: 

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

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

1239 except AttributeError: 

1240 pass 

1241 return n 

1242 

1243 

1244def nameof(inst): 

1245 '''Get the name of an instance. 

1246 

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

1248 

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

1250 ''' 

1251 n = _xattr(inst, name=NN) 

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

1253 try: 

1254 n = inst.fget.__name__ 

1255 except AttributeError: 

1256 n = NN 

1257 return n 

1258 

1259 

1260def _notDecap(where): 

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

1262 ''' 

1263 n = where.__name__ 

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

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

1266 

1267 

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

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

1270 ''' 

1271 n = _DOT_(classname(inst, prefixed=True), _dunder_nameof(name, name)) 

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

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

1274 

1275 

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

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

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

1279 ''' 

1280 if _std_NotImplemented: 

1281 return NotImplemented 

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

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

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

1285 

1286 

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

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

1289 property or for a missing caller feature. 

1290 

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

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

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

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

1295 C{B{up}=2}. 

1296 ''' 

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

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

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

1300 

1301 

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

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

1304 

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

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

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

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

1309 C{B{up}=2}. 

1310 ''' 

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

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

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

1314 

1315 

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

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

1318 ''' 

1319 return arg 

1320 

1321 

1322__all__ += _ALL_DOCS(_Named, 

1323 _NamedBase, # _NamedDict, 

1324 _NamedEnum, _NamedEnumItem, 

1325 _NamedTuple) 

1326 

1327# **) MIT License 

1328# 

1329# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved. 

1330# 

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

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

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

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

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

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

1337# 

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

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

1340# 

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

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

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

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

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

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

1347# OTHER DEALINGS IN THE SOFTWARE.