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 isbool, isidentifier, iskeyword, isstr, itemsorted, \

17 len2, _xcopy, _xdup, _xinstanceof, _xsubclassof, _zip

18# from pygeodesy.ecef import EcefKarney # _MODS

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

20 _IndexError, _KeyError, LenError, _NameError, \

21 _NotImplementedError, _TypeError, _TypesError, \

22 _UnexpectedError, UnitError, _ValueError, \

23 _xattr, _xkwds, _xkwds_item2, _xkwds_pop2

24 from pygeodesy.internals import _caller3, _envPYGEODESY, _isPyPy, _sizeof, \

25 typename, _under

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

27 _COMMASPACE_, _DNAME_, _doesn_t_exist_, _DOT_, _DUNDER_, \

28 _EQUAL_, _exists_, _immutable_, _name_, _NL_, _NN_, \

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

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

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

32 _update_all, property_doc_, Property_RO, property_RO, \

33 _update_attrs

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

35# from pygeodesy.units import _toUnit # _MODS

36

37__all__ = _ALL_LAZY.named

38__version__ = '25.04.28'

39

40_COMMANL_ = _COMMA_ + _NL_

41_COMMASPACEDOT_ = _COMMASPACE_ + _DOT_

42_del_ = 'del'

43_item_ = 'item'

44_MRO_ = 'MRO'

45# __DUNDER gets mangled in class

46_name = _under(_name_)

47_Names_ = '_Names_'

48_registered_ = 'registered' # PYCHOK used!

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

50_such_ = 'such'

51_Units_ = '_Units_'

52_UP = 2

53

54

55 class ADict(dict):

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

57 the C{dict} items.

58 '''

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

60

61 def __getattr__(self, name):

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

63 '''

64 try:

65 return self[name]

66 except KeyError:

67 if name == _name_:

68 return NN

69 raise self._AttributeError(name)

70

71 def __repr__(self):

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

73 '''

74 return self.toRepr()

75

76 def __setattr__(self, name, value):

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

78 '''

79 try:

80 if self[name] != value:

81 self[name] = value

82 except KeyError:

83 dict.__setattr__(self, name, value)

84

85 def __str__(self):

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

87 '''

88 return self.toStr()

89

90 def _AttributeError(self, name):

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

92 '''

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

94 name = _DOT_(self.typename, name)

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

96

97 @property_RO

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

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

100 C{None} if not available/applicable.

101 '''

102 return self._iteration

103

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

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

106

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

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

109 '''

110 if iteration is not None:

111 self._iteration = iteration

112 if items:

113 dict.update(self, items)

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

115

116 def _toL(self):

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

118 '''

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

120

121 def toRepr(self, **prec_fmt):

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

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

124 '''

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

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

127

128 def toStr(self, **prec_fmt):

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

130 function L{pygeodesy.fstr}.

131 '''

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

133

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

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

136 '''

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

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

139

140 @property_RO

141 def typename(self):

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

143 '''

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

145

146

147 class _Named(object):

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

149 '''

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

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

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

153# _updates = 0 # OBSOLETE Property/property updates

154

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

156 '''Not implemented.'''

157 return _NotImplemented(self, fmt)

158

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

160 '''Not implemented.'''

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

162

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

164 '''Not implemented.'''

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

166

167 def __repr__(self):

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

169 '''

170 return Fmt.repr_at(self)

171

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

173 '''Not implemented.'''

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

175

176 def __str__(self):

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

178 '''

179 return self.named2

180

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

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

183 function L{pygeodesy.fstr}.

184

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

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

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

188 or C{None}-valued attributes.

189

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

191

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

193 '''

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

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

196

197 @Property_RO

198 def classname(self):

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

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

201 '''

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

203

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

205 def classnaming(self):

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

207 '''

208 return self._classnaming

209

210 @classnaming.setter # PYCHOK setter!

211 def classnaming(self, prefixed):

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

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

214 '''

215 b = bool(prefixed)

216 if self._classnaming != b:

217 self._classnaming = b

218 _update_attrs(self, *_Named_Property_ROs)

219

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

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

222

223 @arg args: Optional, positional arguments.

224 @kwarg kwds: Optional, keyword arguments.

225

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

227 '''

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

229

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

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

232

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

234 copy (C{bool}).

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

236

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

238 '''

239 c = _xcopy(self, deep=deep)

240 if name:

241 _ = c.rename(name)

242 return c

243

244 def _DOT_(self, *names):

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

246 '''

247 return _DOT_(self.name, *names)

248

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

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

251

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

253 (C{bool}).

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

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

256

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

258

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

260 '''

261 n = self.name

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

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

264 if m != n:

265 d.rename(m) # zap _Named_Property_ROs

266# if items:

267# _update_all(d)

268 return d

269

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

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

272 '''

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

274 return fmt, prec, props, sep, kwds

275

276 name, kwds = _name2__(**fmt_prec_props_sep_name__kwds)

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

278

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

280 if attrs:

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

282 prec=prec, ints=True, fmt=fmt)

283 if props:

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

285 prec=prec, ints=True)

286 if kwds:

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

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

289

290 @property_RO

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

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

293 if not available or not applicable.

294

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

296 several, nested functions.

297 '''

298 return self._iteration

299

300 def methodname(self, which):

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

302

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

304 '''

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

306

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

308 def name(self):

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

310 '''

311 return self._name

312

313 @name.setter # PYCHOK setter!

314 def name(self, name):

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

316

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

318 '''

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

320 if not m:

321 self._name = n

322 elif n != m:

323 n = repr(n)

324 c = self.classname

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

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

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

328 n = _SPACE_(n, m)

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

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

331 # self.name = name or

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

333 # _Named(self).name = name

334

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

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

337 '''

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

339

340 def _name1__(self, kwds):

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

342 '''

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

344

345 @Property_RO

346 def named(self):

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

348 '''

349 return self.name or self.classname

350

351# @Property_RO

352# def named_(self):

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

354# '''

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

356

357 @Property_RO

358 def named2(self):

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

360 '''

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

362

363 @Property_RO

364 def named3(self):

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

366 '''

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

368

369 @Property_RO

370 def named4(self):

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

372 '''

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

374

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

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

377 '''

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

379

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

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

382 '''

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

384

385 def rename(self, name):

386 '''Change the name.

387

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

389

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

391 '''

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

393 if n != m:

394 self._name = n

395 _update_attrs(self, *_Named_Property_ROs)

396 return m

397

398 def renamed(self, name):

399 '''Change the name.

400

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

402

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

404 '''

405 _ = self.rename(name)

406 return self

407

408 @property_RO

409 def sizeof(self):

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

411 '''

412 return _sizeof(self)

413

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

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

416 '''

417 return repr(self)

418

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

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

421 '''

422 return str(self)

423

424 @deprecated_method

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

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

427 return self.toRepr(**kwds)

428

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

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

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

432#

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

434# '''

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

436

437 @property_RO

438 def typename(self):

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

440 '''

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

442

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

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

445

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

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

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

449

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

451 or if not named before.

452 '''

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

454

455 def _xrenamed(self, inst):

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

457

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

459

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

461

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

463 '''

464 _xinstanceof(_Named, inst=inst) # assert

465 return inst.renamed(self.name)

466

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

468

469

470 class _NamedBase(_Named):

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

472 '''

473 def __repr__(self):

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

475 '''

476 return self.toRepr()

477

478 def __str__(self):

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

480 '''

481 return self.toStr()

482

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

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

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

486

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

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

489 keyword arguments.

490

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

492 C{class} or C{type}.

493

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

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

496 '''

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

498 other0 = other[0]

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

500 isinstance(self, other0.__class__):

501 return other0

502

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

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

505 isinstance(other, self.__class__):

506 return _xnamed(other, name)

507

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

509

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

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

512

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

514

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

516 '''

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

518# if self.name:

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

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

521

522# def toRepr(self, **kwds)

523# if kwds:

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

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

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

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

528

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

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

531 notOverloaded(self, **kwds)

532

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

534# if kwds:

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

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

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

538# return s

539

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

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

542 '''

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

544 if setters:

545 d = self.__dict__

546 # double-check that setters are Property_RO's

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

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

549 d[n] = v

550 else:

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

552 u += len(setters)

553 return u

554

555

556 class _NamedDict(ADict, _Named):

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

558 to the items.

559 '''

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

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

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

563 kwds = _name1__(kwds)

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

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

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

567 n, kwds = _name2__(**kwds)

568 if n:

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

570 ADict.__init__(self, kwds)

571

572 def __delattr__(self, name):

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

574 '''

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

576 ADict.pop(self, name)

577 elif name in (_name_, _name):

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

579 _Named.rename(self, NN)

580 else:

581 ADict.__delattr__(self, name)

582

583 def __getattr__(self, name):

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

585 '''

586 try:

587 return self[name]

588 except KeyError:

589 if name == _name_:

590 return _Named.name.fget(self)

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

592

593 def __getitem__(self, key):

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

595 '''

596 if key == _name_:

597 raise self._KeyError(key)

598 return ADict.__getitem__(self, key)

599

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

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

602 '''

603 n = self.name or self.typename

604 t = Fmt.SQUARE(n, key)

605 if value:

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

607 return _KeyError(t)

608

609 def __setattr__(self, name, value):

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

611 '''

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

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

614 else:

615 ADict.__setattr__(self, name, value)

616

617 def __setitem__(self, key, value):

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

619 '''

620 if key == _name_:

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

622 ADict.__setitem__(self, key, value)

623

624

625 class _NamedEnum(_NamedDict):

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

627 restricted to valid keys.

628 '''

629 _item_Classes = ()

630

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

632 '''New C{_NamedEnum}.

633

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

635 values (C{type}).

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

637 '''

638 self._item_Classes = (Class,) + Classes

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

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

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

642

643 def __getattr__(self, name):

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

645 '''

646 return _NamedDict.__getattr__(self, name)

647

648 def __repr__(self):

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

650 '''

651 return self.toRepr()

652

653 def __str__(self):

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

655 '''

656 return self.toStr()

657

658 def _assert(self, **kwds):

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

660 '''

661 pypy = _isPyPy()

662 _isa = isinstance

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

664 if _isa(v, _LazyNamedEnumItem): # property

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

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

667 setattr(self.__class__, n, v)

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

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

670 and self.find(v) == n

671 else:

672 raise _TypeError(v, name=n)

673

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

675 '''Find a registered item.

676

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

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

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

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

681

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

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

684 '''

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

686 if v is item:

687 return k

688 return dflt

689

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

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

692

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

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

695

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

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

698 '''

699 # getattr needed to instantiate L{_LazyNamedEnumItem}

700 return getattr(self, name, dflt)

701

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

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

704

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

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

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

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

709 '''

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

711 _isa = isinstance

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

713 if _isa(p, _LazyNamedEnumItem):

714 _ = getattr(self, n)

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

716

717 def keys(self, **all_asorted):

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

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

720

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

722 '''

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

724 yield k

725

726 def popitem(self):

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

728

729 @return: The removed item.

730 '''

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

732

733 def register(self, item):

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

735

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

737

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

739

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

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

742 invalid name.

743

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

745 '''

746 if item is all or item is any:

747 _ = self.items(all=True)

748 n = typename(item)

749 else:

750 try:

751 n = item.name

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

753 raise ValueError()

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

755 n = _DOT_(_item_, _name_)

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

757 if n in self:

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

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

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

761 n = self._DOT_(n)

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

763 self[n] = item

764 return n

765

766 def unregister(self, name_or_item):

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

768

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

770

771 @return: The unregistered item.

772

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

774

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

776 '''

777 if isstr(name_or_item):

778 name = name_or_item

779 else:

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

781 if name is MISSING:

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

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

784 try:

785 item = ADict.pop(self, name)

786 except KeyError:

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

788 return self._zapitem(name, item)

789

790 pop = unregister

791

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

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

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

795 '''

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

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

798

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

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

801 '''

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

803

804 def values(self, **all_asorted):

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

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

807

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

809 '''

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

811 yield v

812

813 def _zapitem(self, name, item):

814 # remove _LazyNamedEnumItem property value if still present

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

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

817 item._enum = None

818 return item

819

820

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

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

823 '''

824 pass

825

826

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

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

829

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

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

832 '''

833 def _fget(inst):

834 # assert isinstance(inst, _NamedEnum)

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

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

837 item = inst[name]

838 except KeyError:

839 # instantiate an _NamedEnumItem, it self-registers

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

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

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

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

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

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

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

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

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

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

850 if isinstance(p, _LazyNamedEnumItem):

851 delattr(inst.__class__, name)

852 # assert isinstance(item, _NamedEnumItem)

853 return item

854

855 p = _LazyNamedEnumItem(_fget)

856 p.name = name

857 return p

858

859

860 class _NamedEnumItem(_NamedBase):

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

862 '''

863 _enum = None

864

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

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

867#

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

869# '''

870# return not self.__eq__(other)

871

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

873 def name(self):

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

875 '''

876 return self._name

877

878 @name.setter # PYCHOK setter!

879 def name(self, name):

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

881 '''

882 name = _name__(name) or _NN_

883 if self._enum:

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

885 if name:

886 self._name = name

887

888 def _register(self, enum, name):

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

890

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

892 start with a letter.

893 '''

894 name = _name__(name)

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

896 self.name = name

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

898 enum.register(self)

899 self._enum = enum

900

901 def unregister(self):

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

903

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

905

906 @raise NameError: This item is unregistered.

907 '''

908 enum = self._enum

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

910 item = enum.unregister(self.name)

911 if item is not self: # PYCHOK no cover

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

913 raise _AssertionError(t)

914

915

916# from pygeodesy.props import _NamedProperty

917

918

919 class _NamedTuple(tuple, _Named):

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

921 attribute name access to the items.

922

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

924 but statically defined, lighter and limited.

925 '''

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

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

928

929 @note: Specify at least 2 item names.

930 '''

931 _Units_ = () # .units classes

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

933

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

935 '''

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

937

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

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

940

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

942 item of several more in other positional arguments.

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

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

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

946 I{silently} ignored.

947

948 @raise LenError: Unequal number of positional arguments and

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

950

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

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

953

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

955 or starts with C{underscore}.

956 '''

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

958 self = tuple.__new__(cls, args)

959 if not self._validated:

960 self._validate()

961

962 N = len(self._Names_)

963 if n != N:

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

965

966 if iteration_name:

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

968 if i is not None:

969 self._iteration = i

970 if name:

971 self.name = name

972 return self

973

974 def __delattr__(self, name):

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

976

977 @note: Items can not be deleted.

978 '''

979 if name in self._Names_:

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

981 raise _TypeError(t, txt=_immutable_)

982 elif name in (_name_, _name):

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

984 else:

985 tuple.__delattr__(self, name)

986

987 def __getattr__(self, name):

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

989 '''

990 try:

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

992 except IndexError as x:

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

994 except ValueError: # e.g. _iteration

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

996

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

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

999# '''

1000# return tuple.__getitem__(self, index)

1001

1002 def __hash__(self):

1003 return tuple.__hash__(self)

1004

1005 def __repr__(self):

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

1007 '''

1008 return self.toRepr()

1009

1010 def __setattr__(self, name, value):

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

1012 '''

1013 if name in self._Names_:

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

1015 raise _TypeError(t, txt=_immutable_)

1016 elif name in (_name_, _name):

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

1018 else: # e.g. _iteration

1019 tuple.__setattr__(self, name, value)

1020

1021 def __str__(self):

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

1023 '''

1024 return self.toStr()

1025

1026 def _DOT_(self, *names):

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

1028 '''

1029 return _DOT_(self.classname, *names)

1030

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

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

1033

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

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

1036

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

1038

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

1040 '''

1041 t = list(self)

1042 U = self._Units_

1043 if items:

1044 _ix = self._Names_.index

1045 _2U = _MODS.units._toUnit

1046 try:

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

1048 i = _ix(n)

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

1050 except ValueError: # bad item name

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

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

1053

1054 def items(self):

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

1056

1057 @see: Method C{.units}.

1058 '''

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

1060 yield n, v

1061

1062 iteritems = items

1063

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

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

1066

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

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

1069

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

1071

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

1073 '''

1074 U = self._Units_

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

1076 if n:

1077 R = Units + U[n:]

1078 if R != U:

1079 self._Units_ = R

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

1081

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

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

1084

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

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

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

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

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

1090

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

1092 '''

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

1094# if self.name:

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

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

1097

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

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

1100

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

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

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

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

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

1106

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

1108 '''

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

1110

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

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

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

1114

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

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

1117

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

1119

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

1121 '''

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

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

1124

1125 def units(self, **Error):

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

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

1128

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

1130

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

1132

1133 @see: Method C{.items}.

1134 '''

1135 _2U = _MODS.units._toUnit

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

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

1138

1139 iterunits = units

1140

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

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

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

1144 '''

1145 ns = self._Names_

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

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

1148 for i, n in enumerate(ns):

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

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

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

1152

1153 us = self._Units_

1154 if not isinstance(us, tuple):

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

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

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

1158 for i, u in enumerate(us):

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

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

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

1162

1163 type(self)._validated = True

1164

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

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

1167 '''

1168 _xsubclassof(_NamedTuple, xTuple=xTuple)

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

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

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

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

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

1174 t = self + items

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

1176

1177

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

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

1180

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

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

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

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

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

1186

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

1188 '''

1189 try: # see .lazily._caller3

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

1191 n, f, s = _caller3(u)

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

1193 not n.startswith(_UNDER_)):

1194 if source:

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

1196 return n

1197 except (AttributeError, ValueError):

1198 pass

1199 return dflt

1200

1201

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

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

1204 '''

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

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

1207 return n, kwds

1208

1209

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

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

1212 '''

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

1214 if c:

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

1216 if self_name:

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

1218 return n

1219

1220

1221 def classname(inst, prefixed=None):

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

1223 module name.

1224

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

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

1227 function C{classnaming}.

1228

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

1230 '''

1231 if prefixed is None:

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

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

1234

1235

1236 def classnaming(prefixed=None):

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

1238

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

1240

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

1242 '''

1243 t = _Named._classnaming

1244 if isbool(prefixed):

1245 _Named._classnaming = prefixed

1246 return t

1247

1248

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

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

1251 module name.

1252

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

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

1255 function C{classnaming}.

1256

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

1258 '''

1259 try:

1260 n = typename(clas)

1261 except AttributeError:

1262 n = clas if isstr(clas) else _DNAME_

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

1264 try:

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

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

1267 except AttributeError:

1268 pass

1269 return n

1270

1271

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

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

1274# '''

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

1276# m = _MODS.errors

1277# raise m._UnexpectedError(**kwds)

1278# if name: # is given

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

1280# elif name__ is not None:

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

1282# else:

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

1284# if _or_nameof is not None and not n:

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

1286# return n # str or None or {}

1287

1288

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

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

1291 '''

1292 if name or kwds:

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

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

1295 raise _UnexpectedError(**kwds)

1296 return name if name or name is None else NN

1297

1298

1299 def _name1__(kwds_name, **name__or_nameof):

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

1301 '''

1302 if kwds_name or name__or_nameof:

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

1304 kwds_name.update(name=n)

1305 return kwds_name

1306

1307

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

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

1310 '''

1311 if name: # is given

1312 if isinstance(name, dict):

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

1314 n, kwds = _name2__(**kwds)

1315 else:

1316 n = str(name)

1317 elif name__ is not None:

1318 n = typename(name__, NN)

1319 else:

1320 n = name if name is None else NN

1321 if _or_nameof is not None and not n:

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

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

1324

1325

1326 def nameof(inst):

1327 '''Get the name of an instance.

1328

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

1330

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

1332 '''

1333 n = _xattr(inst, name=NN)

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

1335 try:

1336 n = typename(inst.fget)

1337 except AttributeError:

1338 n = NN

1339 return n

1340

1341

1342 def _notDecap(where):

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

1344 '''

1345 n = typename(where)

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

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

1348

1349

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

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

1352 '''

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

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

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

1356

1357

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

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

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

1361 '''

1362 if _std_NotImplemented:

1363 return NotImplemented

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

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

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

1367

1368

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

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

1371 property or for a missing caller feature.

1372

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

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

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

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

1377 C{B{up}=2}.

1378 '''

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

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

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

1382

1383

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

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

1386

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

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

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

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

1391 C{B{up}=2}.

1392 '''

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

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

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

1396

1397

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

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

1400 '''

1401 return arg

1402

1403

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

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

1406 '''

1407 if name__or_nameof:

1408 name = _name__(name, **name__or_nameof)

1409 if name and prefix:

1410 if enquote:

1411 name = repr(name)

1412 t = _SPACE_(prefix, name)

1413 else:

1414 t = prefix or name

1415 return t

1416

1417

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

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

1420

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

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

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

1424

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

1426 or if not named before.

1427 '''

1428 if name__or_nameof:

1429 name = _name__(name, **name__or_nameof)

1430 if name and isinstance(inst, _Named):

1431 if not inst.name:

1432 inst.name = name

1433 elif force:

1434 inst.rename(name)

1435 return inst

1436

1437

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

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

1440 '''

1441 if name_other: # and other is None

1442 name, other = _xkwds_item2(name_other)

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

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

1445 else:

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

1447 return other, name, up

1448

1449

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

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

1452 '''

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

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

1455

1456

1457 def _xvalid(name, underOK=False):

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

1459 '''

1460 return bool(name and isstr(name)

1461 and name != _name_

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

1463 and (not iskeyword(name))

1464 and isidentifier(name))

1465

1466

1467__all__ += _ALL_DOCS(_Named,

1468 _NamedBase, # _NamedDict,

1469 _NamedEnum, _NamedEnumItem,

1470 _NamedTuple)

1471

1472# **) MIT License

1473#

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

1475#

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

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

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

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

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

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

1482#

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

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

1485#

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

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

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

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

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

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

1492# OTHER DEALINGS IN THE SOFTWARE.