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

17 issubclassof, len2, _sizeof, _xcopy, _xdup, _zip

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

19 _IndexError, _IsnotError, itemsorted, LenError, \

20 _NameError, _NotImplementedError, _TypeError, \

21 _TypesError, _ValueError, UnitError, _xattr, \

22 _xkwds, _xkwds_get, _xkwds_pop, _xkwds_popitem

23 from pygeodesy.interns import NN, _at_, _AT_, _COLON_, _COLONSPACE_, _COMMA_, \

24 _COMMASPACE_, _doesn_t_exist_, _DOT_, _DUNDER_, \

25 _EQUAL_, _EQUALSPACED_, _exists_, _immutable_, _name_, \

26 _NL_, _NN_, _not_, _other_, _s_, _SPACE_, _std_, \

27 _UNDER_, _valid_, _vs_, _dunder_nameof, _isPyPy, _under

28 from pygeodesy.lazily import _ALL_DOCS, _ALL_LAZY, _caller3, _getenv

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

30 _update_all, property_doc_, Property_RO, property_RO, \

31 _update_attrs

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

33

34__all__ = _ALL_LAZY.named

35__version__ = '23.09.11'

36

37_COMMANL_ = _COMMA_ + _NL_

38_COMMASPACEDOT_ = _COMMASPACE_ + _DOT_

39_del_ = 'del'

40_item_ = 'item'

41_MRO_ = 'MRO'

42# __DUNDER gets mangled in class

43_name = _under(_name_)

44_Names_ = '_Names_'

45_registered_ = 'registered' # PYCHOK used!

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

47_Units_ = '_Units_'

48

49

50 def _xjoined_(prefix, name):

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

52 '''

53 return _SPACE_(prefix, repr(name)) if name and prefix else (prefix or name)

54

55

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

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

58

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

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

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

62

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

64 not named before.

65 '''

66 if name and isinstance(inst, _Named):

67 if not inst.name:

68 inst.name = name

69 elif force:

70 inst.rename(name)

71 return inst

72

73

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

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

76 '''

77 if name_other: # and not other and len(name_other) == 1

78 name, other = _xkwds_popitem(name_other)

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

80 other = other[0]

81 else:

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

83 return other, name, up

84

85

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

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

88 '''

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

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

91

92

93 def _xvalid(name, _OK=False):

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

95 '''

96 return True if (name and isstr(name)

97 and name != _name_

98 and (_OK or not name.startswith(_UNDER_))

99 and (not iskeyword(name))

100 and isidentifier(name)) else False

101

102

103 class _Dict(dict):

104 '''(INTERNAL) An C{dict} with both key I{and}

105 attribute access to the C{dict} items.

106 '''

107 def __getattr__(self, name):

108 '''Get an attribute or item by B{C{name}}.

109 '''

110 try:

111 return self[name]

112 except KeyError:

113 pass

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

115 raise _AttributeError(item=name, txt=_doesn_t_exist_)

116

117 def __repr__(self):

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

119 '''

120 return self.toRepr()

121

122 def __str__(self):

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

124 '''

125 return self.toStr()

126

127 def set_(self, **items): # PYCHOK signature

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

129

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

131 '''

132 dict.update(self, items)

133

134 def toRepr(self, **prec_fmt): # PYCHOK signature

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

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

137 '''

138 n = _xkwds_get(self, name=classname(self))

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

140

141 def toStr(self, **prec_fmt): # PYCHOK signature

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

143 function L{pygeodesy.fstr}.

144 '''

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

146

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

148 '''(INTERNAL) Helper for C{.toRepr} and C{.toStr}, also

149 in C{_NamedDict} below.

150 '''

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

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

153

154

155 class _Named(object):

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

157 '''

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

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

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

161# _updates = 0 # OBSOLETE Property/property updates

162

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

164 '''Not implemented.'''

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

166

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

168 '''Not implemented.'''

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

170

171 def __repr__(self):

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

173 '''

174 return Fmt.ANGLE(_SPACE_(self, _at_, hex(id(self))))

175

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

177 '''Not implemented.'''

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

179

180 def __str__(self):

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

182 '''

183 return self.named2

184

185 def attrs(self, *names, **sep_COMMASPACE__Nones_True__pairs_kwds):

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

187 function L{pygeodesy.fstr}.

188

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

190 @kwarg sep_COMMASPACE__Nones_True__pairs_kwds: Keyword argument for function

191 L{pygeodesy.pairs}, except C{B{sep}=", "} and C{B{Nones}=True} to

192 in- or exclude missing or C{None}-valued attributes.

193

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

195

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

197 '''

198 def _sep_kwds(sep=_COMMASPACE_, **kwds):

199 return sep, kwds

200

201 sep, kwds = _sep_kwds(**sep_COMMASPACE__Nones_True__pairs_kwds)

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

203

204 @Property_RO

205 def classname(self):

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

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

208 '''

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

210

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

212 def classnaming(self):

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

214 '''

215 return self._classnaming

216

217 @classnaming.setter # PYCHOK setter!

218 def classnaming(self, prefixed):

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

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

221 '''

222 b = bool(prefixed)

223 if self._classnaming != b:

224 self._classnaming = b

225 _update_attrs(self, *_Named_Property_ROs)

226

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

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

229

230 @arg args: Optional, positional arguments.

231 @kwarg kwds: Optional, keyword arguments.

232

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

234 '''

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

236

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

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

239

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

241 a shallow copy (C{bool}).

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

243

244 @return: The copy (C{This class} or sub-class thereof).

245 '''

246 c = _xcopy(self, deep=deep)

247 if name:

248 c.rename(name)

249 return c

250

251 def _DOT_(self, *names):

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

253 '''

254 return _DOT_(self.name, *names)

255

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

257 '''Duplicate this instance, replacing some items.

258

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

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

261

262 @return: The duplicate (C{This class} or sub-class thereof).

263

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

265 '''

266 d = _xdup(self, **items)

267 if name:

268 d.rename(name=name)

269 return d

270

271 def _instr(self, name, prec, *attrs, **props_kwds):

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

273 '''

274 def _props_kwds(props=(), **kwds):

275 return props, kwds

276

277 t = Fmt.EQUAL(_name_, repr(name or self.name)),

278 if attrs:

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

280 prec=prec, ints=True)

281 props, kwds =_props_kwds(**props_kwds)

282 if props:

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

284 prec=prec, ints=True)

285 if kwds:

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

287 return _COMMASPACE_.join(t[1:] if name is None else t)

288

289 @property_RO

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

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

292 if not available or not applicable.

293

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

295 several, nested functions.

296 '''

297 return self._iteration

298

299 def methodname(self, which):

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

301

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

303 '''

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

305

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

307 def name(self):

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

309 '''

310 return self._name

311

312 @name.setter # PYCHOK setter!

313 def name(self, name):

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

315

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

317 '''

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

319 if not m:

320 self._name = n

321 elif n != m:

322 n = repr(n)

323 c = self.classname

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

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

326 n = _DOT_(c, _EQUALSPACED_(_name_, n))

327 n = _SPACE_(n, m)

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

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

330 # self.name = name or

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

332 # _Named(self).name = name

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 named2(self):

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

343 '''

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

345

346 @Property_RO

347 def named3(self):

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

349 '''

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

351

352 @Property_RO

353 def named4(self):

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

355 '''

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

357

358 def rename(self, name):

359 '''Change the name.

360

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

362

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

364 '''

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

366 if n != m:

367 self._name = n

368 _update_attrs(self, *_Named_Property_ROs)

369 return m

370

371 @property_RO

372 def sizeof(self):

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

374 '''

375 return _sizeof(self)

376

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

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

379 '''

380 return repr(self)

381

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

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

384 '''

385 return str(self)

386

387 @deprecated_method

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

389 '''DEPRECATED, use method C{toRepr}.'''

390 return self.toRepr(**kwds)

391

392 def _unstr(self, which, *args, **kwds):

393 '''(INTERNAL) Return the string representation of a method

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

395

396 @see: Function L{pygeodesy.unstr}.

397 '''

398 n = _DOT_(self, which.__name__ if callable(which) else _NN_)

399 return unstr(n, *args, **kwds)

400

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

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

403

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

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

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

407

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

409 '''

410 return _xnamed(inst, name or self.name, force=force)

411

412 def _xrenamed(self, inst):

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

414

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

416

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

418

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

420 '''

421 if not isinstance(inst, _Named):

422 raise _IsnotError(_valid_, inst=inst)

423

424 inst.rename(self.name)

425 return inst

426

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

428

429

430 class _NamedBase(_Named):

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

432 '''

433 def __repr__(self):

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

435 '''

436 return self.toRepr()

437

438 def __str__(self):

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

440 '''

441 return self.toStr()

442

443# def notImplemented(self, attr):

444# '''Raise error for a missing method, function or attribute.

445#

446# @arg attr: Attribute name (C{str}).

447#

448# @raise NotImplementedError: No such attribute.

449# '''

450# c = self.__class__.__name__

451# return NotImplementedError(_DOT_(c, attr))

452

453 def others(self, *other, **name_other_up): # see .points.LatLon_.others

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

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

456

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

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

459 keyword arguments.

460

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

462 C{class} or C{type}.

463

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

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

466 '''

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

468 other0 = other[0]

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

470 isinstance(self, other0.__class__):

471 return other0

472

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

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

475 isinstance(other, self.__class__):

476 return _xnamed(other, name)

477

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

479

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

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

482

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

484

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

486 '''

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

488# if self.name:

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

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

491

492# def toRepr(self, **kwds)

493# if kwds:

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

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

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

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

498

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

500 '''(INTERNAL) I{Must be overloaded}, see function C{notOverloaded}.

501 '''

502 notOverloaded(self, **kwds)

503

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

505# if kwds:

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

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

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

509# return s

510

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

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

513 '''

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

515 if setters:

516 d = self.__dict__

517 # double-check that setters are Property_RO's

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

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

520 d[n] = v

521 else:

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

523 u += len(setters)

524 return u

525

526

527 class _NamedDict(_Dict, _Named):

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

529 access to the items.

530 '''

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

532 if args: # args override kwds

533 if len(args) != 1:

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

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

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

537 if _name_ in kwds:

538 _Named.name.fset(self, kwds.pop(_name_)) # see _Named.name

539 _Dict.__init__(self, kwds)

540

541 def __delattr__(self, name):

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

543 '''

544 if name in _Dict.keys(self):

545 _Dict.pop(name)

546 elif name in (_name_, _name):

547 # _Dict.__setattr__(self, name, NN)

548 _Named.rename(self, NN)

549 else:

550 _Dict.__delattr__(self, name)

551

552 def __getattr__(self, name):

553 '''Get an attribute or item by B{C{name}}.

554 '''

555 try:

556 return self[name]

557 except KeyError:

558 if name == _name_:

559 return _Named.name.fget(self)

560 raise _AttributeError(item=self._DOT_(name), txt=_doesn_t_exist_)

561

562 def __getitem__(self, key):

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

564 '''

565 if key == _name_:

566 raise KeyError(Fmt.SQUARE(self.classname, key))

567 return _Dict.__getitem__(self, key)

568

569 def __setattr__(self, name, value):

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

571 '''

572 if name in _Dict.keys(self):

573 _Dict.__setitem__(self, name, value) # self[name] = value

574 else:

575 _Dict.__setattr__(self, name, value)

576

577 def __setitem__(self, key, value):

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

579 '''

580 if key == _name_:

581 raise KeyError(_EQUAL_(Fmt.SQUARE(self.classname, key), repr(value)))

582 _Dict.__setitem__(self, key, value)

583

584 def toRepr(self, **prec_fmt): # PYCHOK signature

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

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

587 '''

588 return Fmt.PAREN(self.name, self._toT(_EQUAL_, **prec_fmt))

589

590 def toStr(self, **prec_fmt): # PYCHOK signature

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

592 function L{pygeodesy.fstr}.

593 '''

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

595

596

597 class _NamedEnum(_NamedDict):

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

599 restricted to valid keys.

600 '''

601 _item_Classes = ()

602

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

604 '''New C{_NamedEnum}.

605

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

607 values (C{type}).

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

609 '''

610 self._item_Classes = (Class,) + Classes

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

612 if n and _xvalid(n, _OK=True):

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

614

615 def __getattr__(self, name):

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

617 '''

618 try:

619 return self[name]

620 except KeyError:

621 if name == _name_:

622 return _NamedDict.name.fget(self)

623 raise _AttributeError(item=self._DOT_(name), txt=_doesn_t_exist_)

624

625 def __repr__(self):

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

627 '''

628 return self.toRepr()

629

630 def __str__(self):

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

632 '''

633 return self.toStr()

634

635 def _assert(self, **kwds):

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

637 '''

638 pypy = _isPyPy()

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

640 if isinstance(v, _LazyNamedEnumItem): # property

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

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

643 setattr(self.__class__, n, v)

644 elif isinstance(v, self._item_Classes): # PYCHOK no cover

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

646 and self.find(v) == n

647 else:

648 raise _TypeError(v, name=n)

649

650 def find(self, item, dflt=None):

651 '''Find a registered item.

652

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

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

655

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

657 there is no such I{registered} B{C{item}}.

658 '''

659 for k, v in self.items(): # or _Dict.items(self)

660 if v is item:

661 return k

662 return dflt

663

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

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

666

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

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

669

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

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

672 '''

673 # getattr needed to instantiate L{_LazyNamedEnumItem}

674 return getattr(self, name, dflt)

675

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

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

678

679 @kwarg all: Use C{True} to yield {all} items or C{False}

680 for only the currently I{registered} ones.

681 @kwarg asorted: If C{True}, yield the items sorted in

682 I{alphabetical, case-insensitive} order.

683 '''

684 if all: # instantiate any remaining L{_LazyNamedEnumItem} ...

685 # ... and remove the L{_LazyNamedEnumItem} from the class

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

687 if isinstance(p, _LazyNamedEnumItem)):

688 _ = getattr(self, n)

689 return itemsorted(self) if asorted else _Dict.items(self)

690

691 def keys(self, **all_asorted):

692 '''Yield the keys (C{str}) of all or only the I{registered} items,

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

694

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

696 '''

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

698 yield k

699

700 def popitem(self):

701 '''Remove I{an, any} curretly I{registed} item.

702

703 @return: The removed item.

704 '''

705 return self._zapitem(*_Dict.pop(self))

706

707 def register(self, item):

708 '''Registed a new item.

709

710 @arg item: The item (any C{type}).

711

712 @return: The item name (C{str}).

713

714 @raise NameError: An B{C{item}} already registered with

715 that name or the B{C{item}} has no, an

716 empty or an invalid name.

717

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

719 '''

720 try:

721 n = item.name

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

723 raise ValueError

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

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

726 if n in self:

727 raise _NameError(self._DOT_(n), item, txt=_exists_)

728 if not (self._item_Classes and isinstance(item, self._item_Classes)):

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

730 self[n] = item

731

732 def unregister(self, name_or_item):

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

734

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

736

737 @return: The unregistered item.

738

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

740

741 @raise ValueError: No such item.

742 '''

743 if isstr(name_or_item):

744 name = name_or_item

745 else:

746 name = self.find(name_or_item)

747 try:

748 item = _Dict.pop(self, name)

749 except KeyError:

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

751 return self._zapitem(name, item)

752

753 pop = unregister

754

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

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

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

758 '''

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

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

761

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

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

764 '''

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

766

767 def values(self, **all_asorted):

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

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

770

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

772 '''

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

774 yield v

775

776 def _zapitem(self, name, item):

777 # remove _LazyNamedEnumItem property value if still present

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

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

780 item._enum = None

781 return item

782

783

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

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

786 '''

787 pass

788

789

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

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

792

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

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

795 '''

796 def _fget(inst):

797 # assert isinstance(inst, _NamedEnum)

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

799 # item = inst.__dict__[name] # ... or _Dict

800 item = inst[name]

801 except KeyError:

802 # instantiate an _NamedEnumItem, it self-registers

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

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

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

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

807 inst.update({name: item}) # ... _Dict for Triaxials

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

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

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

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

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

813 if isinstance(p, _LazyNamedEnumItem):

814 delattr(inst.__class__, name)

815 # assert isinstance(item, _NamedEnumItem)

816 return item

817

818 p = _LazyNamedEnumItem(_fget)

819 p.name = name

820 return p

821

822

823 class _NamedEnumItem(_NamedBase):

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

825 '''

826 _enum = None

827

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

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

830#

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

832# '''

833# return not self.__eq__(other)

834

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

836 def name(self):

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

838 '''

839 return self._name

840

841 @name.setter # PYCHOK setter!

842 def name(self, name):

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

844 '''

845 if self._enum:

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

847 self._name = str(name)

848

849 def _register(self, enum, name):

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

851

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

853 start with a letter.

854 '''

855 if name and _xvalid(name, _OK=True):

856 self.name = name

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

858 enum.register(self)

859 self._enum = enum

860

861 def unregister(self):

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

863

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

865

866 @raise NameError: This item is unregistered.

867 '''

868 enum = self._enum

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

870 item = enum.unregister(self.name)

871 if item is not self:

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

873 raise _AssertionError(t)

874

875

876 class _NamedTuple(tuple, _Named):

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

878 attribute name access to the items.

879

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

881 but statically defined, lighter and limited.

882 '''

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

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

885

886 @note: Specify at least 2 item names.

887 '''

888 _Units_ = () # .units classes

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

890

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

892 '''

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

894

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

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

897

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

899 item of several more in other positional arguments.

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

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

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

903 I{silently} ignored.

904

905 @raise LenError: Unequal number of positional arguments and

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

907

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

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

910

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

912 or starts with C{underscore}.

913 '''

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

915 self = tuple.__new__(cls, args)

916 if not self._validated:

917 self._validate()

918

919 N = len(self._Names_)

920 if n != N:

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

922

923 if iteration_name:

924 self._kwdself(**iteration_name)

925 return self

926

927 def __delattr__(self, name):

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

929

930 @note: Items can not be deleted.

931 '''

932 if name in self._Names_:

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

934 elif name in (_name_, _name):

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

936 else:

937 tuple.__delattr__(self, name)

938

939 def __getattr__(self, name):

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

941 '''

942 try:

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

944 except IndexError:

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

946 except ValueError: # e.g. _iteration

947 return tuple.__getattribute__(self, name)

948

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

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

951# '''

952# return tuple.__getitem__(self, index)

953

954 def __repr__(self):

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

956 '''

957 return self.toRepr()

958

959 def __setattr__(self, name, value):

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

961 '''

962 if name in self._Names_:

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

964 elif name in (_name_, _name):

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

966 else: # e.g. _iteration

967 tuple.__setattr__(self, name, value)

968

969 def __str__(self):

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

971 '''

972 return self.toStr()

973

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

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

976

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

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

979

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

981

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

983 '''

984 tl = list(self)

985 if items:

986 _ix = self._Names_.index

987 try:

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

989 tl[_ix(n)] = v

990 except ValueError: # bad item name

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

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

993

994 def items(self):

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

996

997 @see: Method C{.units}.

998 '''

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

1000 yield n, v

1001

1002 iteritems = items

1003

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

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

1006 '''

1007 if iteration is not None:

1008 self._iteration = iteration

1009 if name:

1010 self.name = name

1011

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

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

1014

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

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

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

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

1019 @kwarg fmt: Optional, C{float} format (C{str}).

1020

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

1022 '''

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

1024# if self.name:

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

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

1027

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

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

1030

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

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

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

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

1035 @kwarg fmt: Optional C{float} format (C{str}).

1036

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

1038 '''

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

1040

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

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

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

1044

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

1046

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

1048

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

1050 '''

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

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

1053

1054 def units(self, Error=UnitError):

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

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

1057

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

1059

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

1061

1062 @see: Method C{.items}.

1063 '''

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

1065 if not (v is None or U is None

1066 or (isclass(U) and

1067 isinstance(v, U) and

1068 hasattr(v, _name_) and

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

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

1071 yield n, v

1072

1073 iterunits = units

1074

1075 def _validate(self, _OK=False): # see .EcefMatrix

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

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

1078 '''

1079 ns = self._Names_

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

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

1082 for i, n in enumerate(ns):

1083 if not _xvalid(n, _OK=_OK):

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

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

1086

1087 us = self._Units_

1088 if not isinstance(us, tuple):

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

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

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

1092 for i, u in enumerate(us):

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

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

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

1096

1097 self.__class__._validated = True

1098

1099 def _xtend(self, xTuple, *items):

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

1101 '''

1102 if (issubclassof(xTuple, _NamedTuple) and

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

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

1105 return self._xnamed(xTuple(self + items)) # *(self + items)

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

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

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

1109

1110

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

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

1113

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

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

1116 @kwarg source: Include source file name and line

1117 number (C{bool}).

1118 @kwarg underOK: Private, internal callables are OK (C{bool}).

1119

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

1121 '''

1122 try: # see .lazily._caller3

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

1124 n, f, s = _caller3(u)

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

1126 not n.startswith(_UNDER_)):

1127 if source:

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

1129 return n

1130 except (AttributeError, ValueError):

1131 pass

1132 return dflt

1133

1134

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

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

1137 '''

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

1139 if c:

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

1141 if self_name:

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

1143 return n

1144

1145

1146 def classname(inst, prefixed=None):

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

1148 module name.

1149

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

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

1152 function C{classnaming}.

1153

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

1155 '''

1156 if prefixed is None:

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

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

1159

1160

1161 def classnaming(prefixed=None):

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

1163

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

1165

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

1167 '''

1168 t = _Named._classnaming

1169 if prefixed in (True, False):

1170 _Named._classnaming = prefixed

1171 return t

1172

1173

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

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

1176 module name.

1177

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

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

1180 function C{classnaming}.

1181

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

1183 '''

1184 try:

1185 n = clas.__name__

1186 except AttributeError:

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

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

1189 try:

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

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

1192 except AttributeError:

1193 pass

1194 return n

1195

1196

1197 def nameof(inst):

1198 '''Get the name of an instance.

1199

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

1201

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

1203 '''

1204 n = _xattr(inst, name=NN)

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

1206 try:

1207 n = inst.fget.__name__

1208 except AttributeError:

1209 n = NN

1210 return n

1211

1212

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

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

1215 '''

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

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

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

1219

1220

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

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

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

1224 '''

1225 if _std_NotImplemented:

1226 return NotImplemented

1227 u = _xkwds_pop(kwds, up=2)

1228 n = _DOT_(classname(inst), callername(up=u, underOK=True)) # source=True

1229 raise _NotImplementedError(unstr(n, *other, **kwds), txt=repr(inst))

1230

1231

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

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

1234 property or for a missing caller feature.

1235

1236 @arg inst: Instance (C{any}) or C{None} for caller.

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

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

1239 '''

1240 u = _xkwds_pop(kwds, up=2)

1241 n = _xkwds_pop(kwds, callername=NN) or callername(up=u)

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

1243 raise _NotImplementedError(t, txt=notImplemented.__name__.replace('I', ' i'))

1244

1245

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

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

1248

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

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

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

1252 '''

1253 u = _xkwds_pop(kwds, up=2)

1254 n = _xkwds_pop(kwds, callername=NN) or callername(up=u)

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

1256 raise _AssertionError(t, txt=notOverloaded.__name__.replace('O', ' o'))

1257

1258

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

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

1261 '''

1262 return arg

1263

1264

1265__all__ += _ALL_DOCS(_Named,

1266 _NamedBase, # _NamedDict,

1267 _NamedEnum, _NamedEnumItem,

1268 _NamedTuple)

1269

1270# **) MIT License

1271#

1272# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.

1273#

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

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

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

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

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

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

1280#

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

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

1283#

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

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

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

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

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

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

1290# OTHER DEALINGS IN THE SOFTWARE.

1291

1292# % env PYGEODESY_FOR_DOCS=1 python -m pygeodesy.named

1293# all 71 locals OK