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, _getPYGEODESY, _isPyPy, \

24 _sizeof, _under

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

26 _COMMASPACE_, _doesn_t_exist_, _DOT_, _DUNDER_, \

27 _DUNDER_name_, _EQUAL_, _exists_, _immutable_, _name_, \

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

29 _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__ = '24.10.14'

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 = _getPYGEODESY('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.__class__.__name__, 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 toRepr(self, **prec_fmt):

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

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

119 '''

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

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

122

123 def toStr(self, **prec_fmt):

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

125 function L{pygeodesy.fstr}.

126 '''

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

128

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

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

131 '''

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

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

134

135

136 class _Named(object):

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

138 '''

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

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

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

142# _updates = 0 # OBSOLETE Property/property updates

143

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

145 '''Not implemented.'''

146 return _NotImplemented(self, fmt)

147

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

149 '''Not implemented.'''

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

151

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

153 '''Not implemented.'''

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

155

156 def __repr__(self):

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

158 '''

159 return Fmt.repr_at(self)

160

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

162 '''Not implemented.'''

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

164

165 def __str__(self):

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

167 '''

168 return self.named2

169

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

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

172 function L{pygeodesy.fstr}.

173

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

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

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

177 or C{None}-valued attributes.

178

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

180

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

182 '''

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

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

185

186 @Property_RO

187 def classname(self):

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

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

190 '''

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

192

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

194 def classnaming(self):

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

196 '''

197 return self._classnaming

198

199 @classnaming.setter # PYCHOK setter!

200 def classnaming(self, prefixed):

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

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

203 '''

204 b = bool(prefixed)

205 if self._classnaming != b:

206 self._classnaming = b

207 _update_attrs(self, *_Named_Property_ROs)

208

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

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

211

212 @arg args: Optional, positional arguments.

213 @kwarg kwds: Optional, keyword arguments.

214

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

216 '''

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

218

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

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

221

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

223 copy (C{bool}).

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

225

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

227 '''

228 c = _xcopy(self, deep=deep)

229 if name:

230 _ = c.rename(name)

231 return c

232

233 def _DOT_(self, *names):

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

235 '''

236 return _DOT_(self.name, *names)

237

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

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

240

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

242 (C{bool}).

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

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

245

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

247

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

249 '''

250 n = self.name

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

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

253 if m != n:

254 d.rename(m) # zap _Named_Property_ROs

255# if items:

256# _update_all(d)

257 return d

258

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

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

261 '''

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

263 return fmt, prec, props, sep, kwds

264

265 name, kwds = _name2__(**fmt_prec_props_sep_name__kwds)

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

267

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

269 if attrs:

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

271 prec=prec, ints=True, fmt=fmt)

272 if props:

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

274 prec=prec, ints=True)

275 if kwds:

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

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

278

279 @property_RO

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

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

282 if not available or not applicable.

283

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

285 several, nested functions.

286 '''

287 return self._iteration

288

289 def methodname(self, which):

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

291

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

293 '''

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

295

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

297 def name(self):

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

299 '''

300 return self._name

301

302 @name.setter # PYCHOK setter!

303 def name(self, name):

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

305

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

307 '''

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

309 if not m:

310 self._name = n

311 elif n != m:

312 n = repr(n)

313 c = self.classname

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

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

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

317 n = _SPACE_(n, m)

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

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

320 # self.name = name or

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

322 # _Named(self).name = name

323

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

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

326 '''

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

328

329 def _name1__(self, kwds):

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

331 '''

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

333

334 @Property_RO

335 def named(self):

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

337 '''

338 return self.name or self.classname

339

340# @Property_RO

341# def named_(self):

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

343# '''

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

345

346 @Property_RO

347 def named2(self):

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

349 '''

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

351

352 @Property_RO

353 def named3(self):

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

355 '''

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

357

358 @Property_RO

359 def named4(self):

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

361 '''

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

363

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

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

366 '''

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

368

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

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

371 '''

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

373

374 def rename(self, name):

375 '''Change the name.

376

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

378

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

380 '''

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

382 if n != m:

383 self._name = n

384 _update_attrs(self, *_Named_Property_ROs)

385 return m

386

387 def renamed(self, name):

388 '''Change the name.

389

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

391

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

393 '''

394 _ = self.rename(name)

395 return self

396

397 @property_RO

398 def sizeof(self):

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

400 '''

401 return _sizeof(self)

402

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

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

405 '''

406 return repr(self)

407

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

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

410 '''

411 return str(self)

412

413 @deprecated_method

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

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

416 return self.toRepr(**kwds)

417

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

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

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

421#

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

423# '''

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

425

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

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

428

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

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

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

432

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

434 or if not named before.

435 '''

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

437

438 def _xrenamed(self, inst):

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

440

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

442

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

444

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

446 '''

447 _xinstanceof(_Named, inst=inst) # assert

448 return inst.renamed(self.name)

449

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

451

452

453 class _NamedBase(_Named):

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

455 '''

456 def __repr__(self):

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

458 '''

459 return self.toRepr()

460

461 def __str__(self):

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

463 '''

464 return self.toStr()

465

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

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

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

469

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

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

472 keyword arguments.

473

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

475 C{class} or C{type}.

476

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

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

479 '''

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

481 other0 = other[0]

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

483 isinstance(self, other0.__class__):

484 return other0

485

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

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

488 isinstance(other, self.__class__):

489 return _xnamed(other, name)

490

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

492

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

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

495

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

497

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

499 '''

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

501# if self.name:

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

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

504

505# def toRepr(self, **kwds)

506# if kwds:

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

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

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

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

511

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

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

514 notOverloaded(self, **kwds)

515

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

517# if kwds:

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

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

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

521# return s

522

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

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

525 '''

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

527 if setters:

528 d = self.__dict__

529 # double-check that setters are Property_RO's

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

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

532 d[n] = v

533 else:

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

535 u += len(setters)

536 return u

537

538

539 class _NamedDict(ADict, _Named):

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

541 to the items.

542 '''

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

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

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

546 kwds = _name1__(kwds)

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

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

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

550 n, kwds = _name2__(**kwds)

551 if n:

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

553 ADict.__init__(self, kwds)

554

555 def __delattr__(self, name):

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

557 '''

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

559 ADict.pop(self, name)

560 elif name in (_name_, _name):

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

562 _Named.rename(self, NN)

563 else:

564 ADict.__delattr__(self, name)

565

566 def __getattr__(self, name):

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

568 '''

569 try:

570 return self[name]

571 except KeyError:

572 if name == _name_:

573 return _Named.name.fget(self)

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

575

576 def __getitem__(self, key):

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

578 '''

579 if key == _name_:

580 raise self._KeyError(key)

581 return ADict.__getitem__(self, key)

582

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

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

585 '''

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

587 t = Fmt.SQUARE(n, key)

588 if value:

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

590 return _KeyError(t)

591

592 def __setattr__(self, name, value):

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

594 '''

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

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

597 else:

598 ADict.__setattr__(self, name, value)

599

600 def __setitem__(self, key, value):

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

602 '''

603 if key == _name_:

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

605 ADict.__setitem__(self, key, value)

606

607

608 class _NamedEnum(_NamedDict):

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

610 restricted to valid keys.

611 '''

612 _item_Classes = ()

613

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

615 '''New C{_NamedEnum}.

616

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

618 values (C{type}).

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

620 '''

621 self._item_Classes = (Class,) + Classes

622 n = _name__(**name) or NN(Class.__name__, _s_) # _DUNDER_nameof

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

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

625

626 def __getattr__(self, name):

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

628 '''

629 return _NamedDict.__getattr__(self, name)

630

631 def __repr__(self):

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

633 '''

634 return self.toRepr()

635

636 def __str__(self):

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

638 '''

639 return self.toStr()

640

641 def _assert(self, **kwds):

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

643 '''

644 pypy = _isPyPy()

645 _isa = isinstance

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

647 if _isa(v, _LazyNamedEnumItem): # property

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

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

650 setattr(self.__class__, n, v)

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

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

653 and self.find(v) == n

654 else:

655 raise _TypeError(v, name=n)

656

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

658 '''Find a registered item.

659

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

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

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

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

664

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

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

667 '''

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

669 if v is item:

670 return k

671 return dflt

672

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

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

675

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

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

678

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

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

681 '''

682 # getattr needed to instantiate L{_LazyNamedEnumItem}

683 return getattr(self, name, dflt)

684

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

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

687

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

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

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

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

692 '''

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

694 _isa = isinstance

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

696 if _isa(p, _LazyNamedEnumItem):

697 _ = getattr(self, n)

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

699

700 def keys(self, **all_asorted):

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

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

703

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

705 '''

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

707 yield k

708

709 def popitem(self):

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

711

712 @return: The removed item.

713 '''

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

715

716 def register(self, item):

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

718

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

720

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

722

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

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

725 invalid name.

726

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

728 '''

729 if item is all or item is any:

730 _ = self.items(all=True)

731 n = item.__name__

732 else:

733 try:

734 n = item.name

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

736 raise ValueError()

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

738 n = _DOT_(_item_, _name_)

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

740 if n in self:

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

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

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

744 n = self._DOT_(n)

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

746 self[n] = item

747 return n

748

749 def unregister(self, name_or_item):

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

751

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

753

754 @return: The unregistered item.

755

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

757

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

759 '''

760 if isstr(name_or_item):

761 name = name_or_item

762 else:

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

764 if name is MISSING:

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

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

767 try:

768 item = ADict.pop(self, name)

769 except KeyError:

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

771 return self._zapitem(name, item)

772

773 pop = unregister

774

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

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

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

778 '''

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

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

781

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

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

784 '''

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

786

787 def values(self, **all_asorted):

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

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

790

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

792 '''

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

794 yield v

795

796 def _zapitem(self, name, item):

797 # remove _LazyNamedEnumItem property value if still present

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

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

800 item._enum = None

801 return item

802

803

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

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

806 '''

807 pass

808

809

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

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

812

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

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

815 '''

816 def _fget(inst):

817 # assert isinstance(inst, _NamedEnum)

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

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

820 item = inst[name]

821 except KeyError:

822 # instantiate an _NamedEnumItem, it self-registers

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

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

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

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

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

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

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

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

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

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

833 if isinstance(p, _LazyNamedEnumItem):

834 delattr(inst.__class__, name)

835 # assert isinstance(item, _NamedEnumItem)

836 return item

837

838 p = _LazyNamedEnumItem(_fget)

839 p.name = name

840 return p

841

842

843 class _NamedEnumItem(_NamedBase):

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

845 '''

846 _enum = None

847

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

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

850#

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

852# '''

853# return not self.__eq__(other)

854

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

856 def name(self):

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

858 '''

859 return self._name

860

861 @name.setter # PYCHOK setter!

862 def name(self, name):

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

864 '''

865 name = _name__(name) or _NN_

866 if self._enum:

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

868 if name:

869 self._name = name

870

871 def _register(self, enum, name):

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

873

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

875 start with a letter.

876 '''

877 name = _name__(name)

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

879 self.name = name

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

881 enum.register(self)

882 self._enum = enum

883

884 def unregister(self):

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

886

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

888

889 @raise NameError: This item is unregistered.

890 '''

891 enum = self._enum

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

893 item = enum.unregister(self.name)

894 if item is not self: # PYCHOK no cover

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

896 raise _AssertionError(t)

897

898

899# from pygeodesy.props import _NamedProperty

900

901

902 class _NamedTuple(tuple, _Named):

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

904 attribute name access to the items.

905

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

907 but statically defined, lighter and limited.

908 '''

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

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

911

912 @note: Specify at least 2 item names.

913 '''

914 _Units_ = () # .units classes

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

916

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

918 '''

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

920

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

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

923

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

925 item of several more in other positional arguments.

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

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

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

929 I{silently} ignored.

930

931 @raise LenError: Unequal number of positional arguments and

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

933

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

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

936

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

938 or starts with C{underscore}.

939 '''

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

941 self = tuple.__new__(cls, args)

942 if not self._validated:

943 self._validate()

944

945 N = len(self._Names_)

946 if n != N:

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

948

949 if iteration_name:

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

951 if i is not None:

952 self._iteration = i

953 if name:

954 self.name = name

955 return self

956

957 def __delattr__(self, name):

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

959

960 @note: Items can not be deleted.

961 '''

962 if name in self._Names_:

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

964 raise _TypeError(t, txt=_immutable_)

965 elif name in (_name_, _name):

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

967 else:

968 tuple.__delattr__(self, name)

969

970 def __getattr__(self, name):

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

972 '''

973 try:

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

975 except IndexError as x:

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

977 except ValueError: # e.g. _iteration

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

979

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

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

982# '''

983# return tuple.__getitem__(self, index)

984

985 def __hash__(self):

986 return tuple.__hash__(self)

987

988 def __repr__(self):

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

990 '''

991 return self.toRepr()

992

993 def __setattr__(self, name, value):

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

995 '''

996 if name in self._Names_:

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

998 raise _TypeError(t, txt=_immutable_)

999 elif name in (_name_, _name):

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

1001 else: # e.g. _iteration

1002 tuple.__setattr__(self, name, value)

1003

1004 def __str__(self):

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

1006 '''

1007 return self.toStr()

1008

1009 def _DOT_(self, *names):

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

1011 '''

1012 return _DOT_(self.classname, *names)

1013

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

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

1016

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

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

1019

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

1021

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

1023 '''

1024 t = list(self)

1025 U = self._Units_

1026 if items:

1027 _ix = self._Names_.index

1028 _2U = _MODS.units._toUnit

1029 try:

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

1031 i = _ix(n)

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

1033 except ValueError: # bad item name

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

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

1036

1037 def items(self):

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

1039

1040 @see: Method C{.units}.

1041 '''

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

1043 yield n, v

1044

1045 iteritems = items

1046

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

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

1049

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

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

1052

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

1054

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

1056 '''

1057 U = self._Units_

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

1059 if n:

1060 R = Units + U[n:]

1061 if R != U:

1062 self._Units_ = R

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

1064

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

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

1067

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

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

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

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

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

1073

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

1075 '''

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

1077# if self.name:

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

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

1080

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

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

1083

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

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

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

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

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

1089

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

1091 '''

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

1093

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

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

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

1097

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

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

1100

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

1102

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

1104 '''

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

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

1107

1108 def units(self, **Error):

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

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

1111

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

1113

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

1115

1116 @see: Method C{.items}.

1117 '''

1118 _2U = _MODS.units._toUnit

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

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

1121

1122 iterunits = units

1123

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

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

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

1127 '''

1128 ns = self._Names_

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

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

1131 for i, n in enumerate(ns):

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

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

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

1135

1136 us = self._Units_

1137 if not isinstance(us, tuple):

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

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

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

1141 for i, u in enumerate(us):

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

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

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

1145

1146 self.__class__._validated = True

1147

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

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

1150 '''

1151 _xsubclassof(_NamedTuple, xTuple=xTuple)

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

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

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

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

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

1157 t = self + items

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

1159

1160

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

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

1163

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

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

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

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

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

1169

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

1171 '''

1172 try: # see .lazily._caller3

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

1174 n, f, s = _caller3(u)

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

1176 not n.startswith(_UNDER_)):

1177 if source:

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

1179 return n

1180 except (AttributeError, ValueError):

1181 pass

1182 return dflt

1183

1184

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

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

1187 '''

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

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

1190 return n, kwds

1191

1192

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

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

1195 '''

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

1197 if c:

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

1199 if self_name:

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

1201 return n

1202

1203

1204 def classname(inst, prefixed=None):

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

1206 module name.

1207

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

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

1210 function C{classnaming}.

1211

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

1213 '''

1214 if prefixed is None:

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

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

1217

1218

1219 def classnaming(prefixed=None):

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

1221

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

1223

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

1225 '''

1226 t = _Named._classnaming

1227 if prefixed in (True, False):

1228 _Named._classnaming = prefixed

1229 return t

1230

1231

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

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

1234 module name.

1235

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

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

1238 function C{classnaming}.

1239

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

1241 '''

1242 try:

1243 n = clas.__name__

1244 except AttributeError:

1245 n = clas if isstr(clas) else _DUNDER_name_

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

1247 try:

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

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

1250 except AttributeError:

1251 pass

1252 return n

1253

1254

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

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

1257# '''

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

1259# m = _MODS.errors

1260# raise m._UnexpectedError(**kwds)

1261# if name: # is given

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

1263# elif name__ is not None:

1264# n = getattr(name__, _DUNDER_name_, NN) # _xattr(name__, __name__=NN)

1265# else:

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

1267# if _or_nameof is not None and not n:

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

1269# return n # str or None or {}

1270

1271

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

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

1274 '''

1275 if name or kwds:

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

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

1278 raise _UnexpectedError(**kwds)

1279 return name if name or name is None else NN

1280

1281

1282 def _name1__(kwds_name, **name__or_nameof):

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

1284 '''

1285 if kwds_name or name__or_nameof:

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

1287 kwds_name.update(name=n)

1288 return kwds_name

1289

1290

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

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

1293 '''

1294 if name: # is given

1295 if isinstance(name, dict):

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

1297 n, kwds = _name2__(**kwds)

1298 else:

1299 n = str(name)

1300 elif name__ is not None:

1301 n = _DUNDER_nameof(name__, NN)

1302 else:

1303 n = name if name is None else NN

1304 if _or_nameof is not None and not n:

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

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

1307

1308

1309 def nameof(inst):

1310 '''Get the name of an instance.

1311

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

1313

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

1315 '''

1316 n = _xattr(inst, name=NN)

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

1318 try:

1319 n = inst.fget.__name__

1320 except AttributeError:

1321 n = NN

1322 return n

1323

1324

1325 def _notDecap(where):

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

1327 '''

1328 n = where.__name__

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

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

1331

1332

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

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

1335 '''

1336 n = _DOT_(classname(inst, prefixed=True), _DUNDER_nameof(name, name))

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

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

1339

1340

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

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

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

1344 '''

1345 if _std_NotImplemented:

1346 return NotImplemented

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

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

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

1350

1351

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

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

1354 property or for a missing caller feature.

1355

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

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

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

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

1360 C{B{up}=2}.

1361 '''

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

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

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

1365

1366

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

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

1369

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

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

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

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

1374 C{B{up}=2}.

1375 '''

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

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

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

1379

1380

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

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

1383 '''

1384 return arg

1385

1386

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

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

1389 '''

1390 if name__or_nameof:

1391 name = _name__(name, **name__or_nameof)

1392 if name and prefix:

1393 if enquote:

1394 name = repr(name)

1395 t = _SPACE_(prefix, name)

1396 else:

1397 t = prefix or name

1398 return t

1399

1400

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

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

1403

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

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

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

1407

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

1409 or if not named before.

1410 '''

1411 if name__or_nameof:

1412 name = _name__(name, **name__or_nameof)

1413 if name and isinstance(inst, _Named):

1414 if not inst.name:

1415 inst.name = name

1416 elif force:

1417 inst.rename(name)

1418 return inst

1419

1420

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

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

1423 '''

1424 if name_other: # and other is None

1425 name, other = _xkwds_item2(name_other)

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

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

1428 else:

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

1430 return other, name, up

1431

1432

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

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

1435 '''

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

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

1438

1439

1440 def _xvalid(name, underOK=False):

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

1442 '''

1443 return bool(name and isstr(name)

1444 and name != _name_

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

1446 and (not iskeyword(name))

1447 and isidentifier(name))

1448

1449

1450__all__ += _ALL_DOCS(_Named,

1451 _NamedBase, # _NamedDict,

1452 _NamedEnum, _NamedEnumItem,

1453 _NamedTuple)

1454

1455# **) MIT License

1456#

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

1458#

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

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

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

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

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

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

1465#

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

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

1468#

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

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

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

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

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

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

1475# OTHER DEALINGS IN THE SOFTWARE.