Coverage for pygeodesy/named.py: 96%

509 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-05-25 12:04 -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_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_, \ 

26 _dunder_name_, _EQUAL_, _exists_, _immutable_, _name_, \ 

27 _NL_, _NN_, _no_, _other_, _s_, _SPACE_, _std_, \ 

28 _UNDER_, _valid_, _vs_ 

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

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

31 _update_all, property_doc_, Property_RO, property_RO, \ 

32 _update_attrs 

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

34 

35__all__ = _ALL_LAZY.named 

36__version__ = '24.05.21' 

37 

38_COMMANL_ = _COMMA_ + _NL_ 

39_COMMASPACEDOT_ = _COMMASPACE_ + _DOT_ 

40_del_ = 'del' 

41_item_ = 'item' 

42_MRO_ = 'MRO' 

43# __DUNDER gets mangled in class 

44_name = _under(_name_) 

45_Names_ = '_Names_' 

46_registered_ = 'registered' # PYCHOK used! 

47_std_NotImplemented = _getenv('PYGEODESY_NOTIMPLEMENTED', NN).lower() == _std_ 

48_such_ = 'such' 

49_Units_ = '_Units_' 

50_UP = 2 

51 

52 

53def _xjoined_(prefix, name=NN, enquote=True, **name__of_name): 

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

55 ''' 

56 if name__of_name: 

57 name = _name__(name, **name__of_name) 

58 if name and prefix: 

59 if enquote: 

60 name = repr(name) 

61 t = _SPACE_(prefix, name) 

62 else: 

63 t = prefix or name 

64 return t 

65 

66 

67def _xnamed(inst, name=NN, force=False, **name__of_name): 

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

69 

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

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

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

73 

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

75 or if not named before. 

76 ''' 

77 if name__of_name: 

78 name = _name__(name, **name__of_name) 

79 if name and isinstance(inst, _Named): 

80 if not inst.name: 

81 inst.name = name 

82 elif force: 

83 inst.rename(name) 

84 return inst 

85 

86 

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

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

89 ''' 

90 if name_other: # and other is None 

91 name, other = _xkwds_item2(name_other) 

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

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

94 else: 

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

96 return other, name, up 

97 

98 

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

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

101 ''' 

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

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

104 

105 

106def _xvalid(name, underOK=False): 

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

108 ''' 

109 return bool(name and isstr(name) 

110 and name != _name_ 

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

112 and (not iskeyword(name)) 

113 and isidentifier(name)) 

114 

115 

116class ADict(dict): 

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

118 the C{dict} items. 

119 ''' 

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

121 

122 def __getattr__(self, name): 

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

124 ''' 

125 try: 

126 return self[name] 

127 except KeyError: 

128 if name == _name_: 

129 return NN 

130 raise self._AttributeError(name) 

131 

132 def __repr__(self): 

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

134 ''' 

135 return self.toRepr() 

136 

137 def __str__(self): 

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

139 ''' 

140 return self.toStr() 

141 

142 def _AttributeError(self, name): 

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

144 ''' 

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

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

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

148 

149 @property_RO 

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

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

152 C{None} if not available/applicable. 

153 ''' 

154 return self._iteration 

155 

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

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

158 

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

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

161 ''' 

162 if iteration is not None: 

163 self._iteration = iteration 

164 if items: 

165 dict.update(self, items) 

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

167 

168 def toRepr(self, **prec_fmt): 

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

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

171 ''' 

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

173 print(1, n) 

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

175 

176 def toStr(self, **prec_fmt): 

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

178 function L{pygeodesy.fstr}. 

179 ''' 

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

181 

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

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

184 ''' 

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

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

187 

188 

189class _Named(object): 

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

191 ''' 

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

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

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

195# _updates = 0 # OBSOLETE Property/property updates 

196 

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

198 '''Not implemented.''' 

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

200 

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

202 '''Not implemented.''' 

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

204 

205 def __repr__(self): 

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

207 ''' 

208 return Fmt.repr_at(self) 

209 

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

211 '''Not implemented.''' 

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

213 

214 def __str__(self): 

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

216 ''' 

217 return self.named2 

218 

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

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

221 function L{pygeodesy.fstr}. 

222 

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

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

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

226 or C{None}-valued attributes. 

227 

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

229 

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

231 ''' 

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

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

234 

235 @Property_RO 

236 def classname(self): 

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

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

239 ''' 

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

241 

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

243 def classnaming(self): 

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

245 ''' 

246 return self._classnaming 

247 

248 @classnaming.setter # PYCHOK setter! 

249 def classnaming(self, prefixed): 

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

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

252 ''' 

253 b = bool(prefixed) 

254 if self._classnaming != b: 

255 self._classnaming = b 

256 _update_attrs(self, *_Named_Property_ROs) 

257 

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

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

260 

261 @arg args: Optional, positional arguments. 

262 @kwarg kwds: Optional, keyword arguments. 

263 

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

265 ''' 

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

267 

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

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

270 

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

272 a shallow copy (C{bool}). 

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

274 

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

276 ''' 

277 c = _xcopy(self, deep=deep) 

278 if name: 

279 _ = c.rename(name) 

280 return c 

281 

282 def _DOT_(self, *names): 

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

284 ''' 

285 return _DOT_(self.name, *names) 

286 

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

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

289 

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

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

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

293 

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

295 

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

297 ''' 

298 n = self.name 

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

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

301 if m != n: 

302 d.rename(m) # zap _Named_Property_ROs 

303# if items: 

304# _update_all(d) 

305 return d 

306 

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

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

309 ''' 

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

311 return fmt, props, kwds 

312 

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

314 

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

316 if attrs: 

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

318 prec=prec, ints=True, fmt=fmt) 

319 if props: 

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

321 prec=prec, ints=True) 

322 if kwds: 

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

324 return _COMMASPACE_.join(t) 

325 

326 @property_RO 

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

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

329 if not available or not applicable. 

330 

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

332 several, nested functions. 

333 ''' 

334 return self._iteration 

335 

336 def methodname(self, which): 

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

338 

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

340 ''' 

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

342 

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

344 def name(self): 

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

346 ''' 

347 return self._name 

348 

349 @name.setter # PYCHOK setter! 

350 def name(self, name): 

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

352 

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

354 ''' 

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

356 if not m: 

357 self._name = n 

358 elif n != m: 

359 n = repr(n) 

360 c = self.classname 

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

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

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

364 n = _SPACE_(n, m) 

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

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

367 # self.name = name or 

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

369 # _Named(self).name = name 

370 

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

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

373 ''' 

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

375 

376 @Property_RO 

377 def named(self): 

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

379 ''' 

380 return self.name or self.classname 

381 

382# @Property_RO 

383# def named_(self): 

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

385# ''' 

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

387 

388 @Property_RO 

389 def named2(self): 

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

391 ''' 

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

393 

394 @Property_RO 

395 def named3(self): 

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

397 ''' 

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

399 

400 @Property_RO 

401 def named4(self): 

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

403 ''' 

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

405 

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

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

408 ''' 

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

410 

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

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

413 ''' 

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

415 

416 def rename(self, name): 

417 '''Change the name. 

418 

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

420 

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

422 ''' 

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

424 if n != m: 

425 self._name = n 

426 _update_attrs(self, *_Named_Property_ROs) 

427 return m 

428 

429 def renamed(self, name): 

430 '''Change the name. 

431 

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

433 

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

435 ''' 

436 _ = self.rename(name) 

437 return self 

438 

439 @property_RO 

440 def sizeof(self): 

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

442 ''' 

443 return _sizeof(self) 

444 

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

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

447 ''' 

448 return repr(self) 

449 

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

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

452 ''' 

453 return str(self) 

454 

455 @deprecated_method 

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

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

458 return self.toRepr(**kwds) 

459 

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

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

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

463# 

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

465# ''' 

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

467 

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

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

470 

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

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

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

474 

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

476 or if not named before. 

477 ''' 

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

479 

480 def _xrenamed(self, inst): 

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

482 

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

484 

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

486 

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

488 ''' 

489 if not isinstance(inst, _Named): 

490 raise _IsnotError(_valid_, inst=inst) 

491 

492 _ = inst.rename(self.name) 

493 return inst 

494 

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

496 

497 

498class _NamedBase(_Named): 

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

500 ''' 

501 def __repr__(self): 

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

503 ''' 

504 return self.toRepr() 

505 

506 def __str__(self): 

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

508 ''' 

509 return self.toStr() 

510 

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

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

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

514 

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

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

517 keyword arguments. 

518 

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

520 C{class} or C{type}. 

521 

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

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

524 ''' 

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

526 other0 = other[0] 

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

528 isinstance(self, other0.__class__): 

529 return other0 

530 

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

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

533 isinstance(other, self.__class__): 

534 return _xnamed(other, name) 

535 

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

537 

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

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

540 

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

542 

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

544 ''' 

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

546# if self.name: 

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

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

549 

550# def toRepr(self, **kwds) 

551# if kwds: 

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

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

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

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

556 

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

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

559 notOverloaded(self, **kwds) 

560 

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

562# if kwds: 

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

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

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

566# return s 

567 

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

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

570 ''' 

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

572 if setters: 

573 d = self.__dict__ 

574 # double-check that setters are Property_RO's 

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

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

577 d[n] = v 

578 else: 

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

580 u += len(setters) 

581 return u 

582 

583 

584class _NamedDict(ADict, _Named): 

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

586 to the items. 

587 ''' 

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

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

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

591 kwds = _name1__(kwds) 

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

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

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

595 n, kwds = _name2__(**kwds) 

596 if n: 

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

598 ADict.__init__(self, kwds) 

599 

600 def __delattr__(self, name): 

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

602 ''' 

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

604 ADict.pop(self, name) 

605 elif name in (_name_, _name): 

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

607 _Named.rename(self, NN) 

608 else: 

609 ADict.__delattr__(self, name) 

610 

611 def __getattr__(self, name): 

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

613 ''' 

614 try: 

615 return self[name] 

616 except KeyError: 

617 if name == _name_: 

618 return _Named.name.fget(self) 

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

620 

621 def __getitem__(self, key): 

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

623 ''' 

624 if key == _name_: 

625 raise self._KeyError(key) 

626 return ADict.__getitem__(self, key) 

627 

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

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

630 ''' 

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

632 t = Fmt.SQUARE(n, key) 

633 if value: 

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

635 return _KeyError(t) 

636 

637 def __setattr__(self, name, value): 

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

639 ''' 

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

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

642 else: 

643 ADict.__setattr__(self, name, value) 

644 

645 def __setitem__(self, key, value): 

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

647 ''' 

648 if key == _name_: 

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

650 ADict.__setitem__(self, key, value) 

651 

652 

653class _NamedEnum(_NamedDict): 

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

655 restricted to valid keys. 

656 ''' 

657 _item_Classes = () 

658 

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

660 '''New C{_NamedEnum}. 

661 

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

663 values (C{type}). 

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

665 ''' 

666 self._item_Classes = (Class,) + Classes 

667 n = _name__(**name) or NN(Class.__name__, _s_) # _dunder_nameof 

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

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

670 

671 def __getattr__(self, name): 

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

673 ''' 

674 return _NamedDict.__getattr__(self, name) 

675 

676 def __repr__(self): 

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

678 ''' 

679 return self.toRepr() 

680 

681 def __str__(self): 

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

683 ''' 

684 return self.toStr() 

685 

686 def _assert(self, **kwds): 

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

688 ''' 

689 pypy = _isPyPy() 

690 _isa = isinstance 

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

692 if _isa(v, _LazyNamedEnumItem): # property 

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

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

695 setattr(self.__class__, n, v) 

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

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

698 and self.find(v) == n 

699 else: 

700 raise _TypeError(v, name=n) 

701 

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

703 '''Find a registered item. 

704 

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

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

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

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

709 

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

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

712 ''' 

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

714 if v is item: 

715 return k 

716 return dflt 

717 

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

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

720 

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

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

723 

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

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

726 ''' 

727 # getattr needed to instantiate L{_LazyNamedEnumItem} 

728 return getattr(self, name, dflt) 

729 

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

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

732 

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

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

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

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

737 ''' 

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

739 _isa = isinstance 

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

741 if _isa(p, _LazyNamedEnumItem): 

742 _ = getattr(self, n) 

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

744 

745 def keys(self, **all_asorted): 

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

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

748 

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

750 ''' 

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

752 yield k 

753 

754 def popitem(self): 

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

756 

757 @return: The removed item. 

758 ''' 

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

760 

761 def register(self, item): 

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

763 

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

765 

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

767 

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

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

770 invalid name. 

771 

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

773 ''' 

774 if item is all or item is any: 

775 _ = self.items(all=True) 

776 n = item.__name__ 

777 else: 

778 try: 

779 n = item.name 

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

781 raise ValueError() 

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

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

784 if n in self: 

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

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

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

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

789 self[n] = item 

790 return n 

791 

792 def unregister(self, name_or_item): 

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

794 

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

796 

797 @return: The unregistered item. 

798 

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

800 

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

802 ''' 

803 if isstr(name_or_item): 

804 name = name_or_item 

805 else: 

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

807 if name is MISSING: 

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

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

810 try: 

811 item = ADict.pop(self, name) 

812 except KeyError: 

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

814 return self._zapitem(name, item) 

815 

816 pop = unregister 

817 

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

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

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

821 ''' 

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

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

824 

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

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

827 ''' 

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

829 

830 def values(self, **all_asorted): 

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

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

833 

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

835 ''' 

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

837 yield v 

838 

839 def _zapitem(self, name, item): 

840 # remove _LazyNamedEnumItem property value if still present 

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

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

843 item._enum = None 

844 return item 

845 

846 

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

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

849 ''' 

850 pass 

851 

852 

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

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

855 

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

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

858 ''' 

859 def _fget(inst): 

860 # assert isinstance(inst, _NamedEnum) 

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

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

863 item = inst[name] 

864 except KeyError: 

865 # instantiate an _NamedEnumItem, it self-registers 

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

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

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

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

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

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

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

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

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

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

876 if isinstance(p, _LazyNamedEnumItem): 

877 delattr(inst.__class__, name) 

878 # assert isinstance(item, _NamedEnumItem) 

879 return item 

880 

881 p = _LazyNamedEnumItem(_fget) 

882 p.name = name 

883 return p 

884 

885 

886class _NamedEnumItem(_NamedBase): 

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

888 ''' 

889 _enum = None 

890 

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

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

893# 

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

895# ''' 

896# return not self.__eq__(other) 

897 

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

899 def name(self): 

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

901 ''' 

902 return self._name 

903 

904 @name.setter # PYCHOK setter! 

905 def name(self, name): 

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

907 ''' 

908 name = _name__(name) or _NN_ 

909 if self._enum: 

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

911 if name: 

912 self._name = name 

913 

914 def _register(self, enum, name): 

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

916 

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

918 start with a letter. 

919 ''' 

920 name = _name__(name) 

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

922 self.name = name 

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

924 enum.register(self) 

925 self._enum = enum 

926 

927 def unregister(self): 

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

929 

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

931 

932 @raise NameError: This item is unregistered. 

933 ''' 

934 enum = self._enum 

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

936 item = enum.unregister(self.name) 

937 if item is not self: 

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

939 raise _AssertionError(t) 

940 

941 

942class _NamedTuple(tuple, _Named): 

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

944 attribute name access to the items. 

945 

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

947 but statically defined, lighter and limited. 

948 ''' 

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

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

951 

952 @note: Specify at least 2 item names. 

953 ''' 

954 _Units_ = () # .units classes 

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

956 

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

958 ''' 

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

960 

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

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

963 

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

965 item of several more in other positional arguments. 

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

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

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

969 I{silently} ignored. 

970 

971 @raise LenError: Unequal number of positional arguments and 

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

973 

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

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

976 

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

978 or starts with C{underscore}. 

979 ''' 

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

981 self = tuple.__new__(cls, args) 

982 if not self._validated: 

983 self._validate() 

984 

985 N = len(self._Names_) 

986 if n != N: 

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

988 

989 if iteration_name: 

990 self._kwdself(**iteration_name) 

991 return self 

992 

993 def __delattr__(self, name): 

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

995 

996 @note: Items can not be deleted. 

997 ''' 

998 if name in self._Names_: 

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

1000 elif name in (_name_, _name): 

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

1002 else: 

1003 tuple.__delattr__(self, name) 

1004 

1005 def __getattr__(self, name): 

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

1007 ''' 

1008 try: 

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

1010 except IndexError: 

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

1012 except ValueError: # e.g. _iteration 

1013 return tuple.__getattribute__(self, name) 

1014 

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

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

1017# ''' 

1018# return tuple.__getitem__(self, index) 

1019 

1020 def __hash__(self): 

1021 return tuple.__hash__(self) 

1022 

1023 def __repr__(self): 

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

1025 ''' 

1026 return self.toRepr() 

1027 

1028 def __setattr__(self, name, value): 

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

1030 ''' 

1031 if name in self._Names_: 

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

1033 elif name in (_name_, _name): 

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

1035 else: # e.g. _iteration 

1036 tuple.__setattr__(self, name, value) 

1037 

1038 def __str__(self): 

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

1040 ''' 

1041 return self.toStr() 

1042 

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

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

1045 

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

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

1048 

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

1050 

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

1052 ''' 

1053 tl = list(self) 

1054 if items: 

1055 _ix = self._Names_.index 

1056 try: 

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

1058 tl[_ix(n)] = v 

1059 except ValueError: # bad item name 

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

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

1062 

1063 def items(self): 

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

1065 

1066 @see: Method C{.units}. 

1067 ''' 

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

1069 yield n, v 

1070 

1071 iteritems = items 

1072 

1073 def _kwdself(self, iteration=None, **name): 

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

1075 ''' 

1076 if iteration is not None: 

1077 self._iteration = iteration 

1078 if name: 

1079 self.name = name 

1080 

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

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

1083 

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

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

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

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

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

1089 

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

1091 ''' 

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

1093# if self.name: 

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

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

1096 

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

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

1099 

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

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

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

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

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

1105 

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

1107 ''' 

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

1109 

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

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

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

1113 

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

1115 

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

1117 

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

1119 ''' 

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

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

1122 

1123 def units(self, Error=UnitError): 

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

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

1126 

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

1128 

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

1130 

1131 @see: Method C{.items}. 

1132 ''' 

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

1134 if not (v is None or U is None 

1135 or (isclass(U) and 

1136 isinstance(v, U) and 

1137 hasattr(v, _name_) and 

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

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

1140 yield n, v 

1141 

1142 iterunits = units 

1143 

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

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

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

1147 ''' 

1148 ns = self._Names_ 

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

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

1151 for i, n in enumerate(ns): 

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

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

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

1155 

1156 us = self._Units_ 

1157 if not isinstance(us, tuple): 

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

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

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

1161 for i, u in enumerate(us): 

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

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

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

1165 

1166 self.__class__._validated = True 

1167 

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

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

1170 ''' 

1171 if (issubclassof(xTuple, _NamedTuple) and 

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

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

1174 n = _name__(**name) or self.name 

1175 return xTuple(self + items, name=n) # *(self + items) 

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

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

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

1179 

1180 

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

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

1183 

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

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

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

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

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

1189 

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

1191 ''' 

1192 try: # see .lazily._caller3 

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

1194 n, f, s = _caller3(u) 

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

1196 not n.startswith(_UNDER_)): 

1197 if source: 

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

1199 return n 

1200 except (AttributeError, ValueError): 

1201 pass 

1202 return dflt 

1203 

1204 

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

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

1207 ''' 

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

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

1210 return n, kwds 

1211 

1212 

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

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

1215 ''' 

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

1217 if c: 

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

1219 if self_name: 

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

1221 return n 

1222 

1223 

1224def classname(inst, prefixed=None): 

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

1226 module name. 

1227 

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

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

1230 function C{classnaming}. 

1231 

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

1233 ''' 

1234 if prefixed is None: 

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

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

1237 

1238 

1239def classnaming(prefixed=None): 

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

1241 

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

1243 

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

1245 ''' 

1246 t = _Named._classnaming 

1247 if prefixed in (True, False): 

1248 _Named._classnaming = prefixed 

1249 return t 

1250 

1251 

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

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

1254 module name. 

1255 

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

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

1258 function C{classnaming}. 

1259 

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

1261 ''' 

1262 try: 

1263 n = clas.__name__ 

1264 except AttributeError: 

1265 n = _dunder_name_ 

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

1267 try: 

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

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

1270 except AttributeError: 

1271 pass 

1272 return n 

1273 

1274 

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

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

1277# ''' 

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

1279# m = _MODS.errors 

1280# raise m._UnexpectedError(**kwds) 

1281# if name: # is given 

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

1283# elif name__ is not None: 

1284# n = getattr(name__, _dunder_name_, NN) # _xattr(name__, __name__=NN) 

1285# else: 

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

1287# if _or_nameof is not None and not n: 

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

1289# return n # str or None or {} 

1290 

1291 

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

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

1294 ''' 

1295 if name or kwds: 

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

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

1298 m = _MODS.errors 

1299 raise m._UnexpectedError(**kwds) 

1300 return name if name or name is None else NN 

1301 

1302 

1303def _name1__(kwds_name, _or_nameof=None): 

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

1305 ''' 

1306 if kwds_name: 

1307 n, kwds_name = _name2__(kwds_name, _or_nameof=_or_nameof) 

1308 if n: 

1309 kwds_name.update(name=n) 

1310 return kwds_name 

1311 

1312 

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

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

1315 ''' 

1316 if name: # is given 

1317 if isinstance(name, dict): 

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

1319 n, kwds = _name2__(**kwds) 

1320 else: 

1321 n = str(name) 

1322 elif name__ is not None: 

1323 n = getattr(name__, _dunder_name_, NN) # _xattr(name__, __name__=NN) 

1324 else: 

1325 n = name if name is None else NN 

1326 if _or_nameof is not None and not n: 

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

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

1329 

1330 

1331def nameof(inst): 

1332 '''Get the name of an instance. 

1333 

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

1335 

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

1337 ''' 

1338 n = _xattr(inst, name=NN) 

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

1340 try: 

1341 n = inst.fget.__name__ 

1342 except AttributeError: 

1343 n = NN 

1344 return n 

1345 

1346 

1347def _notDecap(where): 

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

1349 ''' 

1350 n = where.__name__ 

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

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

1353 

1354 

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

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

1357 ''' 

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

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

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

1361 

1362 

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

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

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

1366 ''' 

1367 if _std_NotImplemented: 

1368 return NotImplemented 

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

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

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

1372 

1373 

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

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

1376 property or for a missing caller feature. 

1377 

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

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

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

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

1382 C{B{up}=2}. 

1383 ''' 

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

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

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

1387 

1388 

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

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

1391 

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

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

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

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

1396 C{B{up}=2}. 

1397 ''' 

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

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

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

1401 

1402 

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

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

1405 ''' 

1406 return arg 

1407 

1408 

1409__all__ += _ALL_DOCS(_Named, 

1410 _NamedBase, # _NamedDict, 

1411 _NamedEnum, _NamedEnumItem, 

1412 _NamedTuple) 

1413 

1414# **) MIT License 

1415# 

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

1417# 

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

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

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

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

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

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

1424# 

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

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

1427# 

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

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

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

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

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

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

1434# OTHER DEALINGS IN THE SOFTWARE.