1

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

3

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

5

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

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

8

9 The items in a C{_NamedDict} are accessable as attributes and the items

10 in a C{_NamedTuple} are named to be accessable as attributes, similar to

11 standard Python C{namedtuple}s.

12

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

14'''

15

16 from pygeodesy.basics import isidentifier, iskeyword, isstr, itemsorted, len2, \

17 _xcopy, _xdup, _xinstanceof, _xsubclassof, _zip

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

19 _IndexError, _KeyError, LenError, _NameError, \

20 _NotImplementedError, _TypeError, _TypesError, \

21 _UnexpectedError, UnitError, _ValueError, \

22 _xattr, _xkwds, _xkwds_item2, _xkwds_pop2

23 from pygeodesy.internals import _caller3, _dunder_nameof, _isPyPy, _sizeof, _under

24 from 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_, _vs_

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

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

31 _update_all, property_doc_, Property_RO, property_RO, \

32 _update_attrs

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

34# from pygeodesy.units import _toUnit # _MODS

35

36__all__ = _ALL_LAZY.named

37__version__ = '24.06.10'

38

39_COMMANL_ = _COMMA_ + _NL_

40_COMMASPACEDOT_ = _COMMASPACE_ + _DOT_

41_del_ = 'del'

42_item_ = 'item'

43_MRO_ = 'MRO'

44# __DUNDER gets mangled in class

45_name = _under(_name_)

46_Names_ = '_Names_'

47_registered_ = 'registered' # PYCHOK used!

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

49_such_ = 'such'

50_Units_ = '_Units_'

51_UP = 2

52

53

54 class ADict(dict):

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

56 the C{dict} items.

57 '''

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

59

60 def __getattr__(self, name):

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

62 '''

63 try:

64 return self[name]

65 except KeyError:

66 if name == _name_:

67 return NN

68 raise self._AttributeError(name)

69

70 def __repr__(self):

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

72 '''

73 return self.toRepr()

74

75 def __str__(self):

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

77 '''

78 return self.toStr()

79

80 def _AttributeError(self, name):

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

82 '''

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

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

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

86

87 @property_RO

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

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

90 C{None} if not available/applicable.

91 '''

92 return self._iteration

93

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

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

96

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

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

99 '''

100 if iteration is not None:

101 self._iteration = iteration

102 if items:

103 dict.update(self, items)

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

105

106 def toRepr(self, **prec_fmt):

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

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

109 '''

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

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

112

113 def toStr(self, **prec_fmt):

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

115 function L{pygeodesy.fstr}.

116 '''

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

118

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

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

121 '''

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

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

124

125

126 class _Named(object):

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

128 '''

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

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

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

132# _updates = 0 # OBSOLETE Property/property updates

133

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

135 '''Not implemented.'''

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

137

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

139 '''Not implemented.'''

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

141

142 def __repr__(self):

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

144 '''

145 return Fmt.repr_at(self)

146

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

148 '''Not implemented.'''

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

150

151 def __str__(self):

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

153 '''

154 return self.named2

155

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

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

158 function L{pygeodesy.fstr}.

159

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

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

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

163 or C{None}-valued attributes.

164

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

166

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

168 '''

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

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

171

172 @Property_RO

173 def classname(self):

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

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

176 '''

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

178

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

180 def classnaming(self):

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

182 '''

183 return self._classnaming

184

185 @classnaming.setter # PYCHOK setter!

186 def classnaming(self, prefixed):

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

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

189 '''

190 b = bool(prefixed)

191 if self._classnaming != b:

192 self._classnaming = b

193 _update_attrs(self, *_Named_Property_ROs)

194

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

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

197

198 @arg args: Optional, positional arguments.

199 @kwarg kwds: Optional, keyword arguments.

200

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

202 '''

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

204

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

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

207

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

209 a shallow copy (C{bool}).

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

211

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

213 '''

214 c = _xcopy(self, deep=deep)

215 if name:

216 _ = c.rename(name)

217 return c

218

219 def _DOT_(self, *names):

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

221 '''

222 return _DOT_(self.name, *names)

223

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

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

226

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

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

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

230

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

232

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

234 '''

235 n = self.name

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

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

238 if m != n:

239 d.rename(m) # zap _Named_Property_ROs

240# if items:

241# _update_all(d)

242 return d

243

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

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

246 '''

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

248 return fmt, props, kwds

249

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

251

252 t = () if name is None else (Fmt.EQUAL(name=repr(self._name__(name))),)

253 if attrs:

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

255 prec=prec, ints=True, fmt=fmt)

256 if props:

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

258 prec=prec, ints=True)

259 if kwds:

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

261 return _COMMASPACE_.join(t)

262

263 @property_RO

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

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

266 if not available or not applicable.

267

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

269 several, nested functions.

270 '''

271 return self._iteration

272

273 def methodname(self, which):

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

275

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

277 '''

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

279

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

281 def name(self):

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

283 '''

284 return self._name

285

286 @name.setter # PYCHOK setter!

287 def name(self, name):

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

289

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

291 '''

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

293 if not m:

294 self._name = n

295 elif n != m:

296 n = repr(n)

297 c = self.classname

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

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

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

301 n = _SPACE_(n, m)

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

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

304 # self.name = name or

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

306 # _Named(self).name = name

307

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

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

310 '''

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

312

313 def _name1__(self, kwds):

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

315 '''

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

317

318 @Property_RO

319 def named(self):

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

321 '''

322 return self.name or self.classname

323

324# @Property_RO

325# def named_(self):

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

327# '''

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

329

330 @Property_RO

331 def named2(self):

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

333 '''

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

335

336 @Property_RO

337 def named3(self):

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

339 '''

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

341

342 @Property_RO

343 def named4(self):

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

345 '''

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

347

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

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

350 '''

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

352

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

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

355 '''

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

357

358 def rename(self, name):

359 '''Change the name.

360

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

362

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

364 '''

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

366 if n != m:

367 self._name = n

368 _update_attrs(self, *_Named_Property_ROs)

369 return m

370

371 def renamed(self, name):

372 '''Change the name.

373

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

375

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

377 '''

378 _ = self.rename(name)

379 return self

380

381 @property_RO

382 def sizeof(self):

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

384 '''

385 return _sizeof(self)

386

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

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

389 '''

390 return repr(self)

391

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

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

394 '''

395 return str(self)

396

397 @deprecated_method

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

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

400 return self.toRepr(**kwds)

401

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

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

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

405#

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

407# '''

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

409

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

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

412

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

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

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

416

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

418 or if not named before.

419 '''

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

421

422 def _xrenamed(self, inst):

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

424

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

426

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

428

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

430 '''

431 _xinstanceof(_Named, inst=inst) # assert

432 return inst.renamed(self.name)

433

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

435

436

437 class _NamedBase(_Named):

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

439 '''

440 def __repr__(self):

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

442 '''

443 return self.toRepr()

444

445 def __str__(self):

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

447 '''

448 return self.toStr()

449

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

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

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

453

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

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

456 keyword arguments.

457

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

459 C{class} or C{type}.

460

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

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

463 '''

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

465 other0 = other[0]

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

467 isinstance(self, other0.__class__):

468 return other0

469

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

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

472 isinstance(other, self.__class__):

473 return _xnamed(other, name)

474

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

476

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

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

479

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

481

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

483 '''

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

485# if self.name:

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

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

488

489# def toRepr(self, **kwds)

490# if kwds:

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

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

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

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

495

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

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

498 notOverloaded(self, **kwds)

499

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

501# if kwds:

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

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

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

505# return s

506

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

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

509 '''

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

511 if setters:

512 d = self.__dict__

513 # double-check that setters are Property_RO's

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

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

516 d[n] = v

517 else:

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

519 u += len(setters)

520 return u

521

522

523 class _NamedDict(ADict, _Named):

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

525 to the items.

526 '''

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

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

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

530 kwds = _name1__(kwds)

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

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

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

534 n, kwds = _name2__(**kwds)

535 if n:

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

537 ADict.__init__(self, kwds)

538

539 def __delattr__(self, name):

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

541 '''

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

543 ADict.pop(self, name)

544 elif name in (_name_, _name):

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

546 _Named.rename(self, NN)

547 else:

548 ADict.__delattr__(self, name)

549

550 def __getattr__(self, name):

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

552 '''

553 try:

554 return self[name]

555 except KeyError:

556 if name == _name_:

557 return _Named.name.fget(self)

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

559

560 def __getitem__(self, key):

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

562 '''

563 if key == _name_:

564 raise self._KeyError(key)

565 return ADict.__getitem__(self, key)

566

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

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

569 '''

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

571 t = Fmt.SQUARE(n, key)

572 if value:

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

574 return _KeyError(t)

575

576 def __setattr__(self, name, value):

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

578 '''

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

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

581 else:

582 ADict.__setattr__(self, name, value)

583

584 def __setitem__(self, key, value):

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

586 '''

587 if key == _name_:

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

589 ADict.__setitem__(self, key, value)

590

591

592 class _NamedEnum(_NamedDict):

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

594 restricted to valid keys.

595 '''

596 _item_Classes = ()

597

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

599 '''New C{_NamedEnum}.

600

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

602 values (C{type}).

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

604 '''

605 self._item_Classes = (Class,) + Classes

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

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

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

609

610 def __getattr__(self, name):

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

612 '''

613 return _NamedDict.__getattr__(self, name)

614

615 def __repr__(self):

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

617 '''

618 return self.toRepr()

619

620 def __str__(self):

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

622 '''

623 return self.toStr()

624

625 def _assert(self, **kwds):

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

627 '''

628 pypy = _isPyPy()

629 _isa = isinstance

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

631 if _isa(v, _LazyNamedEnumItem): # property

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

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

634 setattr(self.__class__, n, v)

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

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

637 and self.find(v) == n

638 else:

639 raise _TypeError(v, name=n)

640

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

642 '''Find a registered item.

643

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

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

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

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

648

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

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

651 '''

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

653 if v is item:

654 return k

655 return dflt

656

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

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

659

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

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

662

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

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

665 '''

666 # getattr needed to instantiate L{_LazyNamedEnumItem}

667 return getattr(self, name, dflt)

668

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

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

671

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

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

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

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

676 '''

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

678 _isa = isinstance

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

680 if _isa(p, _LazyNamedEnumItem):

681 _ = getattr(self, n)

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

683

684 def keys(self, **all_asorted):

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

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

687

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

689 '''

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

691 yield k

692

693 def popitem(self):

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

695

696 @return: The removed item.

697 '''

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

699

700 def register(self, item):

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

702

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

704

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

706

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

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

709 invalid name.

710

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

712 '''

713 if item is all or item is any:

714 _ = self.items(all=True)

715 n = item.__name__

716 else:

717 try:

718 n = item.name

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

720 raise ValueError()

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

722 n = _DOT_(_item_, _name_)

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

724 if n in self:

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

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

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

728 n = self._DOT_(n)

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

730 self[n] = item

731 return n

732

733 def unregister(self, name_or_item):

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

735

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

737

738 @return: The unregistered item.

739

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

741

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

743 '''

744 if isstr(name_or_item):

745 name = name_or_item

746 else:

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

748 if name is MISSING:

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

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

751 try:

752 item = ADict.pop(self, name)

753 except KeyError:

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

755 return self._zapitem(name, item)

756

757 pop = unregister

758

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

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

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

762 '''

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

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

765

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

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

768 '''

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

770

771 def values(self, **all_asorted):

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

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

774

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

776 '''

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

778 yield v

779

780 def _zapitem(self, name, item):

781 # remove _LazyNamedEnumItem property value if still present

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

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

784 item._enum = None

785 return item

786

787

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

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

790 '''

791 pass

792

793

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

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

796

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

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

799 '''

800 def _fget(inst):

801 # assert isinstance(inst, _NamedEnum)

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

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

804 item = inst[name]

805 except KeyError:

806 # instantiate an _NamedEnumItem, it self-registers

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

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

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

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

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

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

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

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

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

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

817 if isinstance(p, _LazyNamedEnumItem):

818 delattr(inst.__class__, name)

819 # assert isinstance(item, _NamedEnumItem)

820 return item

821

822 p = _LazyNamedEnumItem(_fget)

823 p.name = name

824 return p

825

826

827 class _NamedEnumItem(_NamedBase):

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

829 '''

830 _enum = None

831

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

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

834#

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

836# '''

837# return not self.__eq__(other)

838

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

840 def name(self):

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

842 '''

843 return self._name

844

845 @name.setter # PYCHOK setter!

846 def name(self, name):

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

848 '''

849 name = _name__(name) or _NN_

850 if self._enum:

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

852 if name:

853 self._name = name

854

855 def _register(self, enum, name):

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

857

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

859 start with a letter.

860 '''

861 name = _name__(name)

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

863 self.name = name

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

865 enum.register(self)

866 self._enum = enum

867

868 def unregister(self):

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

870

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

872

873 @raise NameError: This item is unregistered.

874 '''

875 enum = self._enum

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

877 item = enum.unregister(self.name)

878 if item is not self: # PYCHOK no cover

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

880 raise _AssertionError(t)

881

882

883 class _NamedTuple(tuple, _Named):

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

885 attribute name access to the items.

886

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

888 but statically defined, lighter and limited.

889 '''

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

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

892

893 @note: Specify at least 2 item names.

894 '''

895 _Units_ = () # .units classes

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

897

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

899 '''

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

901

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

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

904

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

906 item of several more in other positional arguments.

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

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

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

910 I{silently} ignored.

911

912 @raise LenError: Unequal number of positional arguments and

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

914

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

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

917

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

919 or starts with C{underscore}.

920 '''

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

922 self = tuple.__new__(cls, args)

923 if not self._validated:

924 self._validate()

925

926 N = len(self._Names_)

927 if n != N:

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

929

930 if iteration_name:

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

932 if i is not None:

933 self._iteration = i

934 if name:

935 self.name = name

936 return self

937

938 def __delattr__(self, name):

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

940

941 @note: Items can not be deleted.

942 '''

943 if name in self._Names_:

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

945 raise _TypeError(t, txt=_immutable_)

946 elif name in (_name_, _name):

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

948 else:

949 tuple.__delattr__(self, name)

950

951 def __getattr__(self, name):

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

953 '''

954 try:

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

956 except IndexError as x:

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

958 except ValueError: # e.g. _iteration

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

960

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

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

963# '''

964# return tuple.__getitem__(self, index)

965

966 def __hash__(self):

967 return tuple.__hash__(self)

968

969 def __repr__(self):

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

971 '''

972 return self.toRepr()

973

974 def __setattr__(self, name, value):

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

976 '''

977 if name in self._Names_:

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

979 raise _TypeError(t, txt=_immutable_)

980 elif name in (_name_, _name):

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

982 else: # e.g. _iteration

983 tuple.__setattr__(self, name, value)

984

985 def __str__(self):

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

987 '''

988 return self.toStr()

989

990 def _DOT_(self, *names):

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

992 '''

993 return _DOT_(self.classname, *names)

994

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

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

997

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

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

1000

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

1002

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

1004 '''

1005 t = list(self)

1006 U = self._Units_

1007 if items:

1008 _ix = self._Names_.index

1009 _2U = _MODS.units._toUnit

1010 try:

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

1012 i = _ix(n)

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

1014 except ValueError: # bad item name

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

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

1017

1018 def items(self):

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

1020

1021 @see: Method C{.units}.

1022 '''

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

1024 yield n, v

1025

1026 iteritems = items

1027

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

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

1030

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

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

1033

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

1035

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

1037 '''

1038 U = self._Units_

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

1040 if n:

1041 R = Units + U[n:]

1042 if R != U:

1043 self._Units_ = R

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

1045

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

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

1048

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

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

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

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

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

1054

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

1056 '''

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

1058# if self.name:

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

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

1061

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

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

1064

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

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

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

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

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

1070

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

1072 '''

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

1074

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

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

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

1078

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

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

1081

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

1083

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

1085 '''

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

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

1088

1089 def units(self, **Error):

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

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

1092

1093 @kwarg Error: Optional C{B{Error}=UnitError} to raise for

1094 L{units} issues.

1095

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

1097

1098 @see: Method C{.items}.

1099 '''

1100 _2U = _MODS.units._toUnit

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

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

1103

1104 iterunits = units

1105

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

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

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

1109 '''

1110 ns = self._Names_

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

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

1113 for i, n in enumerate(ns):

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

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

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

1117

1118 us = self._Units_

1119 if not isinstance(us, tuple):

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

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

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

1123 for i, u in enumerate(us):

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

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

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

1127

1128 self.__class__._validated = True

1129

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

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

1132 '''

1133 _xsubclassof(_NamedTuple, xTuple=xTuple)

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

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

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

1137 x = NN(xTuple.__name__, repr(xTuple._Names_))

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

1139 t = self + items

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

1141

1142

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

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

1145

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

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

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

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

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

1151

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

1153 '''

1154 try: # see .lazily._caller3

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

1156 n, f, s = _caller3(u)

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

1158 not n.startswith(_UNDER_)):

1159 if source:

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

1161 return n

1162 except (AttributeError, ValueError):

1163 pass

1164 return dflt

1165

1166

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

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

1169 '''

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

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

1172 return n, kwds

1173

1174

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

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

1177 '''

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

1179 if c:

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

1181 if self_name:

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

1183 return n

1184

1185

1186 def classname(inst, prefixed=None):

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

1188 module name.

1189

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

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

1192 function C{classnaming}.

1193

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

1195 '''

1196 if prefixed is None:

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

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

1199

1200

1201 def classnaming(prefixed=None):

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

1203

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

1205

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

1207 '''

1208 t = _Named._classnaming

1209 if prefixed in (True, False):

1210 _Named._classnaming = prefixed

1211 return t

1212

1213

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

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

1216 module name.

1217

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

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

1220 function C{classnaming}.

1221

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

1223 '''

1224 try:

1225 n = clas.__name__

1226 except AttributeError:

1227 n = _dunder_name_

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

1229 try:

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

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

1232 except AttributeError:

1233 pass

1234 return n

1235

1236

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

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

1239# '''

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

1241# m = _MODS.errors

1242# raise m._UnexpectedError(**kwds)

1243# if name: # is given

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

1245# elif name__ is not None:

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

1247# else:

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

1249# if _or_nameof is not None and not n:

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

1251# return n # str or None or {}

1252

1253

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

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

1256 '''

1257 if name or kwds:

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

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

1260 raise _UnexpectedError(**kwds)

1261 return name if name or name is None else NN

1262

1263

1264 def _name1__(kwds_name, **name__or_nameof):

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

1266 '''

1267 if kwds_name or name__or_nameof:

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

1269 kwds_name.update(name=n)

1270 return kwds_name

1271

1272

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

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

1275 '''

1276 if name: # is given

1277 if isinstance(name, dict):

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

1279 n, kwds = _name2__(**kwds)

1280 else:

1281 n = str(name)

1282 elif name__ is not None:

1283 n = _dunder_nameof(name__, NN)

1284 else:

1285 n = name if name is None else NN

1286 if _or_nameof is not None and not n:

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

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

1289

1290

1291 def nameof(inst):

1292 '''Get the name of an instance.

1293

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

1295

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

1297 '''

1298 n = _xattr(inst, name=NN)

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

1300 try:

1301 n = inst.fget.__name__

1302 except AttributeError:

1303 n = NN

1304 return n

1305

1306

1307 def _notDecap(where):

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

1309 '''

1310 n = where.__name__

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

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

1313

1314

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

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

1317 '''

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

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

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

1321

1322

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

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

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

1326 '''

1327 if _std_NotImplemented:

1328 return NotImplemented

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

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

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

1332

1333

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

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

1336 property or for a missing caller feature.

1337

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

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

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

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

1342 C{B{up}=2}.

1343 '''

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

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

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

1347

1348

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

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

1351

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

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

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

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

1356 C{B{up}=2}.

1357 '''

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

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

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

1361

1362

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

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

1365 '''

1366 return arg

1367

1368

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

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

1371 '''

1372 if name__or_nameof:

1373 name = _name__(name, **name__or_nameof)

1374 if name and prefix:

1375 if enquote:

1376 name = repr(name)

1377 t = _SPACE_(prefix, name)

1378 else:

1379 t = prefix or name

1380 return t

1381

1382

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

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

1385

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

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

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

1389

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

1391 or if not named before.

1392 '''

1393 if name__or_nameof:

1394 name = _name__(name, **name__or_nameof)

1395 if name and isinstance(inst, _Named):

1396 if not inst.name:

1397 inst.name = name

1398 elif force:

1399 inst.rename(name)

1400 return inst

1401

1402

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

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

1405 '''

1406 if name_other: # and other is None

1407 name, other = _xkwds_item2(name_other)

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

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

1410 else:

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

1412 return other, name, up

1413

1414

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

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

1417 '''

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

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

1420

1421

1422 def _xvalid(name, underOK=False):

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

1424 '''

1425 return bool(name and isstr(name)

1426 and name != _name_

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

1428 and (not iskeyword(name))

1429 and isidentifier(name))

1430

1431

1432__all__ += _ALL_DOCS(_Named,

1433 _NamedBase, # _NamedDict,

1434 _NamedEnum, _NamedEnumItem,

1435 _NamedTuple)

1436

1437# **) MIT License

1438#

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

1440#

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

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

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

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

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

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

1447#

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

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

1450#

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

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

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

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

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

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

1457# OTHER DEALINGS IN THE SOFTWARE.