Coverage for pygeodesy/named.py: 97%

470 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-05-15 16:36 -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, _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.internals import _caller3, _dunder_nameof, _isPyPy, _sizeof, _under 

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

25 _COMMASPACE_, _doesn_t_exist_, _DOT_, _DUNDER_, _EQUAL_, \ 

26 _exists_, _immutable_, _name_, _NL_, _NN_, _no_, \ 

27 _other_, _s_, _SPACE_, _std_, _UNDER_, _valid_, _vs_ 

28from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _ALL_MODS as _MODS, _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.13' 

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_such_ = 'such' 

48_Units_ = '_Units_' 

49_UP = 2 

50 

51 

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

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

54 ''' 

55 if name and prefix: 

56 if enquote: 

57 name = repr(name) 

58 n = _SPACE_(prefix, name) 

59 else: 

60 n = prefix or name 

61 return n 

62 

63 

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

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

66 

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

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

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

70 

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

72 not named before. 

73 ''' 

74 if name and isinstance(inst, _Named): 

75 if not inst.name: 

76 inst.name = name 

77 elif force: 

78 inst.rename(name) 

79 return inst 

80 

81 

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

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

84 ''' 

85 if name_other: # and other is None 

86 name, other = _xkwds_item2(name_other) 

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

88 other = other[0] 

89 else: 

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

91 return other, name, up 

92 

93 

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

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

96 ''' 

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

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

99 

100 

101def _xvalid(name, underOK=False): 

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

103 ''' 

104 return bool(name and isstr(name) 

105 and name != _name_ 

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

107 and (not iskeyword(name)) 

108 and isidentifier(name)) 

109 

110 

111class ADict(dict): 

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

113 the C{dict} items. 

114 ''' 

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

116 

117 def __getattr__(self, name): 

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

119 ''' 

120 try: 

121 return self[name] 

122 except KeyError: 

123 if name == _name_: 

124 return NN 

125 raise self._AttributeError(name) 

126 

127 def __repr__(self): 

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

129 ''' 

130 return self.toRepr() 

131 

132 def __str__(self): 

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

134 ''' 

135 return self.toStr() 

136 

137 def _AttributeError(self, name): 

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

139 ''' 

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

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

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

143 

144 @property_RO 

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

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

147 C{None} if not available/applicable. 

148 ''' 

149 return self._iteration 

150 

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

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

153 

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

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

156 ''' 

157 if iteration is not None: 

158 self._iteration = iteration 

159 if items: 

160 dict.update(self, items) 

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

162 

163 def toRepr(self, **prec_fmt): 

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

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

166 ''' 

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

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

169 

170 def toStr(self, **prec_fmt): 

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

172 function L{pygeodesy.fstr}. 

173 ''' 

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

175 

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

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

178 ''' 

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

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

181 

182 

183class _Named(object): 

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

185 ''' 

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

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

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

189# _updates = 0 # OBSOLETE Property/property updates 

190 

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

192 '''Not implemented.''' 

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

194 

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

196 '''Not implemented.''' 

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

198 

199 def __repr__(self): 

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

201 ''' 

202 return Fmt.repr_at(self) 

203 

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

205 '''Not implemented.''' 

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

207 

208 def __str__(self): 

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

210 ''' 

211 return self.named2 

212 

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

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

215 function L{pygeodesy.fstr}. 

216 

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

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

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

220 or C{None}-valued attributes. 

221 

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

223 

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

225 ''' 

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

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

228 

229 @Property_RO 

230 def classname(self): 

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

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

233 ''' 

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

235 

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

237 def classnaming(self): 

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

239 ''' 

240 return self._classnaming 

241 

242 @classnaming.setter # PYCHOK setter! 

243 def classnaming(self, prefixed): 

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

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

246 ''' 

247 b = bool(prefixed) 

248 if self._classnaming != b: 

249 self._classnaming = b 

250 _update_attrs(self, *_Named_Property_ROs) 

251 

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

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

254 

255 @arg args: Optional, positional arguments. 

256 @kwarg kwds: Optional, keyword arguments. 

257 

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

259 ''' 

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

261 

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

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

264 

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

266 a shallow copy (C{bool}). 

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

268 

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

270 ''' 

271 c = _xcopy(self, deep=deep) 

272 if name: 

273 c.rename(name) 

274 return c 

275 

276 def _DOT_(self, *names): 

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

278 ''' 

279 return _DOT_(self.name, *names) 

280 

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

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

283 

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

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

286 

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

288 

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

290 ''' 

291 n = self.name 

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

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

294 if m != n: 

295 d.rename(m) 

296# if items: 

297# _update_all(d) 

298 return d 

299 

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

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

302 ''' 

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

304 return fmt, props, kwds 

305 

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

307 

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

309 if attrs: 

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

311 prec=prec, ints=True, fmt=fmt) 

312 if props: 

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

314 prec=prec, ints=True) 

315 if kwds: 

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

317 return _COMMASPACE_.join(t) 

318 

319 @property_RO 

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

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

322 if not available or not applicable. 

323 

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

325 several, nested functions. 

326 ''' 

327 return self._iteration 

328 

329 def methodname(self, which): 

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

331 

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

333 ''' 

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

335 

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

337 def name(self): 

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

339 ''' 

340 return self._name 

341 

342 @name.setter # PYCHOK setter! 

343 def name(self, name): 

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

345 

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

347 ''' 

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

349 if not m: 

350 self._name = n 

351 elif n != m: 

352 n = repr(n) 

353 c = self.classname 

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

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

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

357 n = _SPACE_(n, m) 

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

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

360 # self.name = name or 

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

362 # _Named(self).name = name 

363 

364 @Property_RO 

365 def named(self): 

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

367 ''' 

368 return self.name or self.classname 

369 

370# @Property_RO 

371# def named_(self): 

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

373# ''' 

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

375 

376 @Property_RO 

377 def named2(self): 

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

379 ''' 

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

381 

382 @Property_RO 

383 def named3(self): 

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

385 ''' 

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

387 

388 @Property_RO 

389 def named4(self): 

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

391 ''' 

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

393 

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

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

396 ''' 

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

398 

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

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

401 ''' 

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

403 

404 def rename(self, name): 

405 '''Change the name. 

406 

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

408 

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

410 ''' 

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

412 if n != m: 

413 self._name = n 

414 _update_attrs(self, *_Named_Property_ROs) 

415 return m 

416 

417 @property_RO 

418 def sizeof(self): 

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

420 ''' 

421 return _sizeof(self) 

422 

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

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

425 ''' 

426 return repr(self) 

427 

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

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

430 ''' 

431 return str(self) 

432 

433 @deprecated_method 

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

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

436 return self.toRepr(**kwds) 

437 

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

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

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

441# 

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

443# ''' 

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

445 

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

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

448 

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

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

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

452 

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

454 ''' 

455 return _xnamed(inst, name or self.name, force=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 if not isinstance(inst, _Named): 

467 raise _IsnotError(_valid_, inst=inst) 

468 

469 inst.rename(self.name) 

470 return inst 

471 

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

473 

474 

475class _NamedBase(_Named): 

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

477 ''' 

478 def __repr__(self): 

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

480 ''' 

481 return self.toRepr() 

482 

483 def __str__(self): 

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

485 ''' 

486 return self.toStr() 

487 

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

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

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

491 

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

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

494 keyword arguments. 

495 

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

497 C{class} or C{type}. 

498 

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

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

501 ''' 

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

503 other0 = other[0] 

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

505 isinstance(self, other0.__class__): 

506 return other0 

507 

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

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

510 isinstance(other, self.__class__): 

511 return _xnamed(other, name) 

512 

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

514 

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

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

517 

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

519 

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

521 ''' 

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

523# if self.name: 

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

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

526 

527# def toRepr(self, **kwds) 

528# if kwds: 

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

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

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

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

533 

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

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

536 notOverloaded(self, **kwds) 

537 

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

539# if kwds: 

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

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

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

543# return s 

544 

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

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

547 ''' 

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

549 if setters: 

550 d = self.__dict__ 

551 # double-check that setters are Property_RO's 

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

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

554 d[n] = v 

555 else: 

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

557 u += len(setters) 

558 return u 

559 

560 

561class _NamedDict(ADict, _Named): 

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

563 access to the items. 

564 ''' 

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

566 if args: # args override kwds 

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

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

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

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

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

572 if n: 

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

574 ADict.__init__(self, kwds) 

575 

576 def __delattr__(self, name): 

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

578 ''' 

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

580 ADict.pop(self, name) 

581 elif name in (_name_, _name): 

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

583 _Named.rename(self, NN) 

584 else: 

585 ADict.__delattr__(self, name) 

586 

587 def __getattr__(self, name): 

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

589 ''' 

590 try: 

591 return self[name] 

592 except KeyError: 

593 if name == _name_: 

594 return _Named.name.fget(self) 

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

596 

597 def __getitem__(self, key): 

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

599 ''' 

600 if key == _name_: 

601 raise self._KeyError(key) 

602 return ADict.__getitem__(self, key) 

603 

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

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

606 ''' 

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

608 t = Fmt.SQUARE(n, key) 

609 if value: 

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

611 return _KeyError(t) 

612 

613 def __setattr__(self, name, value): 

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

615 ''' 

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

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

618 else: 

619 ADict.__setattr__(self, name, value) 

620 

621 def __setitem__(self, key, value): 

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

623 ''' 

624 if key == _name_: 

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

626 ADict.__setitem__(self, key, value) 

627 

628 

629class _NamedEnum(_NamedDict): 

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

631 restricted to valid keys. 

632 ''' 

633 _item_Classes = () 

634 

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

636 '''New C{_NamedEnum}. 

637 

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

639 values (C{type}). 

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

641 ''' 

642 self._item_Classes = (Class,) + Classes 

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

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

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

646 

647 def __getattr__(self, name): 

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

649 ''' 

650 return _NamedDict.__getattr__(self, name) 

651 

652 def __repr__(self): 

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

654 ''' 

655 return self.toRepr() 

656 

657 def __str__(self): 

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

659 ''' 

660 return self.toStr() 

661 

662 def _assert(self, **kwds): 

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

664 ''' 

665 pypy = _isPyPy() 

666 _isa = isinstance 

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

668 if _isa(v, _LazyNamedEnumItem): # property 

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

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

671 setattr(self.__class__, n, v) 

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

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

674 and self.find(v) == n 

675 else: 

676 raise _TypeError(v, name=n) 

677 

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

679 '''Find a registered item. 

680 

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

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

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

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

685 

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

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

688 ''' 

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

690 if v is item: 

691 return k 

692 return dflt 

693 

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

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

696 

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

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

699 

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

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

702 ''' 

703 # getattr needed to instantiate L{_LazyNamedEnumItem} 

704 return getattr(self, name, dflt) 

705 

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

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

708 

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

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

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

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

713 ''' 

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

715 _isa = isinstance 

716 for n, p in tuple(self.__class__.__dict__.items()): 

717 if _isa(p, _LazyNamedEnumItem): 

718 _ = getattr(self, n) 

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

720 

721 def keys(self, **all_asorted): 

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

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

724 

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

726 ''' 

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

728 yield k 

729 

730 def popitem(self): 

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

732 

733 @return: The removed item. 

734 ''' 

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

736 

737 def register(self, item): 

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

739 

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

741 

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

743 

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

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

746 invalid name. 

747 

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

749 ''' 

750 if item is all or item is any: 

751 _ = self.items(all=True) 

752 n = item.__name__ 

753 else: 

754 try: 

755 n = item.name 

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

757 raise ValueError() 

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

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

760 if n in self: 

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

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

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

764 raise _TypesError(self._DOT_(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 if self._enum: 

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

886 self._name = str(name) 

887 

888 def _register(self, enum, name): 

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

890 

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

892 start with a letter. 

893 ''' 

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

895 self.name = name 

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

897 enum.register(self) 

898 self._enum = enum 

899 

900 def unregister(self): 

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

902 

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

904 

905 @raise NameError: This item is unregistered. 

906 ''' 

907 enum = self._enum 

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

909 item = enum.unregister(self.name) 

910 if item is not self: 

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

912 raise _AssertionError(t) 

913 

914 

915class _NamedTuple(tuple, _Named): 

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

917 attribute name access to the items. 

918 

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

920 but statically defined, lighter and limited. 

921 ''' 

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

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

924 

925 @note: Specify at least 2 item names. 

926 ''' 

927 _Units_ = () # .units classes 

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

929 

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

931 ''' 

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

933 

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

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

936 

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

938 item of several more in other positional arguments. 

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

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

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

942 I{silently} ignored. 

943 

944 @raise LenError: Unequal number of positional arguments and 

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

946 

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

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

949 

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

951 or starts with C{underscore}. 

952 ''' 

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

954 self = tuple.__new__(cls, args) 

955 if not self._validated: 

956 self._validate() 

957 

958 N = len(self._Names_) 

959 if n != N: 

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

961 

962 if iteration_name: 

963 self._kwdself(**iteration_name) 

964 return self 

965 

966 def __delattr__(self, name): 

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

968 

969 @note: Items can not be deleted. 

970 ''' 

971 if name in self._Names_: 

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

973 elif name in (_name_, _name): 

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

975 else: 

976 tuple.__delattr__(self, name) 

977 

978 def __getattr__(self, name): 

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

980 ''' 

981 try: 

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

983 except IndexError: 

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

985 except ValueError: # e.g. _iteration 

986 return tuple.__getattribute__(self, name) 

987 

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

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

990# ''' 

991# return tuple.__getitem__(self, index) 

992 

993 def __hash__(self): 

994 return tuple.__hash__(self) 

995 

996 def __repr__(self): 

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

998 ''' 

999 return self.toRepr() 

1000 

1001 def __setattr__(self, name, value): 

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

1003 ''' 

1004 if name in self._Names_: 

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

1006 elif name in (_name_, _name): 

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

1008 else: # e.g. _iteration 

1009 tuple.__setattr__(self, name, value) 

1010 

1011 def __str__(self): 

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

1013 ''' 

1014 return self.toStr() 

1015 

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

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

1018 

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

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

1021 

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

1023 

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

1025 ''' 

1026 tl = list(self) 

1027 if items: 

1028 _ix = self._Names_.index 

1029 try: 

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

1031 tl[_ix(n)] = v 

1032 except ValueError: # bad item name 

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

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

1035 

1036 def items(self): 

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

1038 

1039 @see: Method C{.units}. 

1040 ''' 

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

1042 yield n, v 

1043 

1044 iteritems = items 

1045 

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

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

1048 ''' 

1049 if iteration is not None: 

1050 self._iteration = iteration 

1051 if name: 

1052 self.name = name 

1053 

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

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

1056 

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

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

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

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

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

1062 

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

1064 ''' 

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

1066# if self.name: 

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

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

1069 

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

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

1072 

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

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

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

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

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

1078 

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

1080 ''' 

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

1082 

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

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

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

1086 

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

1088 

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

1090 

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

1092 ''' 

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

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

1095 

1096 def units(self, Error=UnitError): 

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

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

1099 

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

1101 

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

1103 

1104 @see: Method C{.items}. 

1105 ''' 

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

1107 if not (v is None or U is None 

1108 or (isclass(U) and 

1109 isinstance(v, U) and 

1110 hasattr(v, _name_) and 

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

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

1113 yield n, v 

1114 

1115 iterunits = units 

1116 

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

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

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

1120 ''' 

1121 ns = self._Names_ 

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

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

1124 for i, n in enumerate(ns): 

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

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

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

1128 

1129 us = self._Units_ 

1130 if not isinstance(us, tuple): 

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

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

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

1134 for i, u in enumerate(us): 

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

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

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

1138 

1139 self.__class__._validated = True 

1140 

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

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

1143 ''' 

1144 if (issubclassof(xTuple, _NamedTuple) and 

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

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

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

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

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

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

1151 

1152 

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

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

1155 

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

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

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

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

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

1161 

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

1163 ''' 

1164 try: # see .lazily._caller3 

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

1166 n, f, s = _caller3(u) 

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

1168 not n.startswith(_UNDER_)): 

1169 if source: 

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

1171 return n 

1172 except (AttributeError, ValueError): 

1173 pass 

1174 return dflt 

1175 

1176 

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

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

1179 ''' 

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

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

1182 return n, kwds 

1183 

1184 

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

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

1187 ''' 

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

1189 if c: 

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

1191 if self_name: 

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

1193 return n 

1194 

1195 

1196def classname(inst, prefixed=None): 

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

1198 module name. 

1199 

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

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

1202 function C{classnaming}. 

1203 

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

1205 ''' 

1206 if prefixed is None: 

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

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

1209 

1210 

1211def classnaming(prefixed=None): 

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

1213 

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

1215 

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

1217 ''' 

1218 t = _Named._classnaming 

1219 if prefixed in (True, False): 

1220 _Named._classnaming = prefixed 

1221 return t 

1222 

1223 

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

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

1226 module name. 

1227 

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

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

1230 function C{classnaming}. 

1231 

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

1233 ''' 

1234 try: 

1235 n = clas.__name__ 

1236 except AttributeError: 

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

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

1239 try: 

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

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

1242 except AttributeError: 

1243 pass 

1244 return n 

1245 

1246 

1247def nameof(inst): 

1248 '''Get the name of an instance. 

1249 

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

1251 

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

1253 ''' 

1254 n = _xattr(inst, name=NN) 

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

1256 try: 

1257 n = inst.fget.__name__ 

1258 except AttributeError: 

1259 n = NN 

1260 return n 

1261 

1262 

1263def _notDecap(where): 

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

1265 ''' 

1266 n = where.__name__ 

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

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

1269 

1270 

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

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

1273 ''' 

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

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

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

1277 

1278 

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

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

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

1282 ''' 

1283 if _std_NotImplemented: 

1284 return NotImplemented 

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

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

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

1288 

1289 

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

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

1292 property or for a missing caller feature. 

1293 

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

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

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

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

1298 C{B{up}=2}. 

1299 ''' 

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

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

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

1303 

1304 

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

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

1307 

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

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

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

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

1312 C{B{up}=2}. 

1313 ''' 

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

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

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

1317 

1318 

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

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

1321 ''' 

1322 return arg 

1323 

1324 

1325__all__ += _ALL_DOCS(_Named, 

1326 _NamedBase, # _NamedDict, 

1327 _NamedEnum, _NamedEnumItem, 

1328 _NamedTuple) 

1329 

1330# **) MIT License 

1331# 

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

1333# 

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

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

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

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

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

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

1340# 

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

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

1343# 

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

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

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

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

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

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

1350# OTHER DEALINGS IN THE SOFTWARE.