1

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

3

4 u'''Some, basic definitions, functions and dependencies.

5

6 Use env variable C{PYGEODESY_XPACKAGES} to avoid import of dependencies

7 C{geographiclib}, C{numpy} and/or C{scipy}. Set C{PYGEODESY_XPACKAGES}

8 to a comma-separated list of package names to be excluded from import.

9'''

10# make sure int/int division yields float quotient

11 from __future__ import division

12 division = 1 / 2 # .albers, .azimuthal, .constants, etc., .utily

13 if not division:

14 raise ImportError('%s 1/2 == %s' % ('division', division))

15 del division

16

17# from pygeodesy.cartesianBase import CartesianBase # _MODS

18# from pygeodesy.constants import isneg0, NEG0 # _MODS

19 from pygeodesy.errors import _AttributeError, _ImportError, _NotImplementedError, \

20 _TypeError, _TypesError, _ValueError, _xAssertionError, \

21 _xkwds_get1

22# from pygeodesy.fsums import _isFsum_2Tuple # _MODS

23 from pygeodesy.internals import _0_0, _enquote, _passarg, _version_info

24 from pygeodesy.interns import MISSING, NN, _1_, _by_, _COMMA_, _DOT_, _DEPRECATED_, \

25 _ELLIPSIS4_, _EQUAL_, _in_, _invalid_, _N_A_, _not_, \

26 _not_scalar_, _odd_, _SPACE_, _UNDER_, _version_

27# from pygeodesy.latlonBase import LatLonBase # _MODS

28 from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _FOR_DOCS, _getenv, \

29 LazyImportError, _sys_version_info2

30# from pygeodesy.named import classname, modulename, _name__ # _MODS

31# from pygeodesy.nvectorBase import NvectorBase # _MODS

32# from pygeodesy.props import _update_all # _MODS

33# from pygeodesy.streprs import Fmt # _MODS

34# from pygeodesy.unitsBase import _NamedUnit, Str # _MODS

35

36 from copy import copy as _copy, deepcopy as _deepcopy

37 from math import copysign as _copysign

38 import inspect as _inspect

39

40__all__ = _ALL_LAZY.basics

41__version__ = '24.09.28'

42

43_below_ = 'below'

44_list_tuple_types = (list, tuple)

45_PYGEODESY_XPACKAGES_ = 'PYGEODESY_XPACKAGES'

46_required_ = 'required'

47

48 try: # Luciano Ramalho, "Fluent Python", O'Reilly, 2016 p. 395, 2022 p. 577+

49 from numbers import Integral as _Ints, Real as _Scalars # .units

50 except ImportError:

51 try:

52 _Ints = int, long # int objects (C{tuple})

53 except NameError: # Python 3+

54 _Ints = int, # int objects (C{tuple})

55 _Scalars = (float,) + _Ints

56

57 try:

58 try: # use C{from collections.abc import ...} in Python 3.9+

59 from collections.abc import Sequence as _Sequence # in .points

60 except ImportError: # no .abc in Python 3.8- and 2.7-

61 from collections import Sequence as _Sequence # in .points

62 if isinstance([], _Sequence) and isinstance((), _Sequence):

63 # and isinstance(range(1), _Sequence):

64 _Seqs = _Sequence

65 else:

66 raise ImportError() # _AssertionError

67 except ImportError:

68 _Sequence = tuple # immutable for .points._Basequence

69 _Seqs = list, _Sequence # range for function len2 below

70

71 try:

72 _Bytes = unicode, bytearray # PYCHOK in .internals

73 _Strs = basestring, str # XXX str == bytes

74 str2ub = ub2str = _passarg # avoids UnicodeDecodeError

75

76 def _Xstr(exc): # PYCHOK no cover

77 '''I{Invoke only with caught ImportError} B{C{exc}}.

78

79 C{... "can't import name _distributor_init" ...}

80

81 only for C{numpy}, C{scipy} import errors occurring

82 on arm64 Apple Silicon running macOS' Python 2.7.16?

83 '''

84 t = str(exc)

85 if '_distributor_init' in t:

86 from sys import exc_info

87 from traceback import extract_tb

88 tb = exc_info()[2] # 3-tuple (type, value, traceback)

89 t4 = extract_tb(tb, 1)[0] # 4-tuple (file, line, name, 'import ...')

90 t = _SPACE_("can't", t4[3] or _N_A_)

91 del tb, t4

92 return t

93

94 except NameError: # Python 3+

95 from pygeodesy.interns import _utf_8_

96

97 _Bytes = bytes, bytearray # in .internals

98 _Strs = str, # tuple

99 _Xstr = str

100

101 def str2ub(sb):

102 '''Convert C{str} to C{unicode bytes}.

103 '''

104 if isinstance(sb, _Strs):

105 sb = sb.encode(_utf_8_)

106 return sb

107

108 def ub2str(ub):

109 '''Convert C{unicode bytes} to C{str}.

110 '''

111 if isinstance(ub, _Bytes):

112 ub = str(ub.decode(_utf_8_))

113 return ub

114

115

116 def _args_kwds_count2(func, exelf=True):

117 '''(INTERNAL) Get a C{func}'s args and kwds count as 2-tuple

118 C{(nargs, nkwds)}, including arg C{self} for methods.

119

120 @kwarg exelf: If C{True}, exclude C{self} in the C{args}

121 of a method (C{bool}).

122 '''

123 try: # PYCHOK no cover

124 a = k = 0

125 for _, p in _inspect.signature(func).parameters.items():

126 if p.kind is p.POSITIONAL_OR_KEYWORD:

127 if p.default is p.empty:

128 a += 1

129 else:

130 k += 1

131 except AttributeError: # .signature new Python 3+

132 s = _inspect.getargspec(func)

133 k = len(s.defaults or ())

134 a = len(s.args) - k

135 if exelf and a > 0 and _inspect.ismethod(func):

136 a -= 1

137 return a, k

138

139

140 def _args_kwds_names(func, splast=False):

141 '''(INTERNAL) Get a C{func}'s args and kwds names, including

142 C{self} for methods.

143

144 @kwarg splast: If C{True}, split the last keyword argument

145 at UNDERscores (C{bool}).

146

147 @note: Python 2 may I{not} include the C{*args} nor the

148 C{**kwds} names.

149 '''

150 try:

151 args_kwds = _inspect.signature(func).parameters.keys()

152 except AttributeError: # .signature new Python 3+

153 args_kwds = _inspect.getargspec(func).args

154 if splast and args_kwds: # PYCHOK no cover

155 args_kwds = list(args_kwds)

156 t = args_kwds[-1:]

157 if t:

158 s = t[0].strip(_UNDER_).split(_UNDER_)

159 if len(s) > 1 or s != t:

160 args_kwds += s

161 return tuple(args_kwds)

162

163

164 def clips(sb, limit=50, white=NN, length=False):

165 '''Clip a string to the given length limit.

166

167 @arg sb: String (C{str} or C{bytes}).

168 @kwarg limit: Length limit (C{int}).

169 @kwarg white: Optionally, replace all whitespace (C{str}).

170 @kwarg length: If C{True}, append the original I{[length]} (C{bool}).

171

172 @return: The clipped or unclipped B{C{sb}}.

173 '''

174 T, n = type(sb), len(sb)

175 if n > limit > 8:

176 h = limit // 2

177 sb = T(_ELLIPSIS4_).join((sb[:h], sb[-h:]))

178 if length:

179 n = _MODS.streprs.Fmt.SQUARE(n)

180 sb = T(NN).join((sb, n))

181 if white: # replace whitespace

182 sb = T(white).join(sb.split())

183 return sb

184

185

186 def copysign0(x, y):

187 '''Like C{math.copysign(x, y)} except C{zero}, I{unsigned}.

188

189 @return: C{math.copysign(B{x}, B{y})} if B{C{x}} else

190 C{type(B{x})(0)}.

191 '''

192 return _copysign(x, (y if y else 0)) if x else copytype(0, x)

193

194

195 def copytype(x, y):

196 '''Return the value of B{x} as C{type} of C{y}.

197

198 @return: C{type(B{y})(B{x})}.

199 '''

200 return type(y)(x if x else _0_0)

201

202

203 def _enumereverse(iterable):

204 '''(INTERNAL) Reversed C{enumberate}.

205 '''

206 for j in _reverange(len(iterable)):

207 yield j, iterable[j]

208

209

210 def halfs2(str2):

211 '''Split a string in 2 halfs.

212

213 @arg str2: String to split (C{str}).

214

215 @return: 2-Tuple C{(_1st, _2nd)} half (C{str}).

216

217 @raise ValueError: Zero or odd C{len(B{str2})}.

218 '''

219 h, r = divmod(len(str2), 2)

220 if r or not h:

221 raise _ValueError(str2=str2, txt=_odd_)

222 return str2[:h], str2[h:]

223

224

225 def int1s(x):

226 '''Count the number of 1-bits in an C{int}, I{unsigned}.

227

228 @note: C{int1s(-B{x}) == int1s(abs(B{x}))}.

229 '''

230 try:

231 return x.bit_count() # Python 3.10+

232 except AttributeError:

233 # bin(-x) = '-' + bin(abs(x))

234 return bin(x).count(_1_)

235

236

237 def isbool(obj):

238 '''Is B{C{obj}}ect a C{bool}ean?

239

240 @arg obj: The object (any C{type}).

241

242 @return: C{True} if C{bool}ean, C{False} otherwise.

243 '''

244 return isinstance(obj, bool) # and (obj is False

245# or obj is True)

246

247 assert not (isbool(1) or isbool(0) or isbool(None)) # PYCHOK 2

248

249

250 def isCartesian(obj, ellipsoidal=None):

251 '''Is B{C{obj}}ect some C{Cartesian}?

252

253 @arg obj: The object (any C{type}).

254 @kwarg ellipsoidal: If C{None}, return the type of any C{Cartesian},

255 if C{True}, only an ellipsoidal C{Cartesian type}

256 or if C{False}, only a spherical C{Cartesian type}.

257

258 @return: C{type(B{obj}} if a C{Cartesian} of the required type, C{False}

259 if a C{Cartesian} of an other type or {None} otherwise.

260 '''

261 if ellipsoidal is not None:

262 try:

263 return obj.ellipsoidalCartesian if ellipsoidal else obj.sphericalCartesian

264 except AttributeError:

265 return None

266 return isinstanceof(obj, _MODS.cartesianBase.CartesianBase)

267

268

269 if _FOR_DOCS: # XXX avoid epydoc Python 2.7 error

270

271 def isclass(obj):

272 '''Is B{C{obj}}ect a C{Class} or C{type}?

273 '''

274 return _inspect.isclass(obj)

275 else:

276 isclass = _inspect.isclass

277

278

279 def iscomplex(obj, both=False):

280 '''Is B{C{obj}}ect a C{complex} or complex literal C{str}?

281

282 @arg obj: The object (any C{type}).

283 @kwarg both: If C{True}, check complex C{str} (C{bool}).

284

285 @return: C{True} if C{complex}, C{False} otherwise.

286 '''

287 try: # hasattr('conjugate', 'real' and 'imag')

288 return isinstance(obj, complex) or bool(both and isstr(obj) and

289 isinstance(complex(obj), complex)) # numbers.Complex?

290 except (TypeError, ValueError):

291 return False

292

293

294 def isDEPRECATED(obj):

295 '''Is B{C{obj}}ect a C{DEPRECATED} class, method or function?

296

297 @return: C{True} if C{DEPRECATED}, {False} if not or

298 C{None} if undetermined.

299 '''

300 try: # XXX inspect.getdoc(obj) or obj.__doc__

301 doc = obj.__doc__.lstrip()

302 return bool(doc and doc.startswith(_DEPRECATED_))

303 except AttributeError:

304 return None

305

306

307 def isfloat(obj, both=False):

308 '''Is B{C{obj}}ect a C{float} or float literal C{str}?

309

310 @arg obj: The object (any C{type}).

311 @kwarg both: If C{True}, check float C{str} (C{bool}).

312

313 @return: C{True} if C{float}, C{False} otherwise.

314 '''

315 try:

316 return isinstance(obj, float) or bool(both and

317 isstr(obj) and isinstance(float(obj), float))

318 except (TypeError, ValueError):

319 return False

320

321

322 try:

323 isidentifier = str.isidentifier # Python 3, must be str

324 except AttributeError: # Python 2-

325

326 def isidentifier(obj):

327 '''Is B{C{obj}}ect a Python identifier?

328 '''

329 return bool(obj and isstr(obj)

330 and obj.replace(_UNDER_, NN).isalnum()

331 and not obj[:1].isdigit())

332

333

334 def isinstanceof(obj, *Classes):

335 '''Is B{C{obj}}ect an instance of one of the C{Classes}?

336

337 @arg obj: The object (any C{type}).

338 @arg Classes: One or more classes (C{Class}).

339

340 @return: C{type(B{obj}} if one of the B{C{Classes}},

341 C{None} otherwise.

342 '''

343 return type(obj) if isinstance(obj, Classes) else None

344

345

346 def isint(obj, both=False):

347 '''Is B{C{obj}}ect an C{int} or integer C{float} value?

348

349 @arg obj: The object (any C{type}).

350 @kwarg both: If C{True}, check C{float} and L{Fsum}

351 type and value (C{bool}).

352

353 @return: C{True} if C{int} or I{integer} C{float}

354 or L{Fsum}, C{False} otherwise.

355

356 @note: Both C{isint(True)} and C{isint(False)} return

357 C{False} (and no longer C{True}).

358 '''

359 if isinstance(obj, _Ints):

360 return not isbool(obj)

361 elif both: # and isinstance(obj, (float, Fsum))

362 try: # NOT , _Scalars) to include Fsum!

363 return obj.is_integer()

364 except AttributeError:

365 pass # XXX float(int(obj)) == obj?

366 return False

367

368

369 def isiterable(obj):

370 '''Is B{C{obj}}ect C{iterable}?

371

372 @arg obj: The object (any C{type}).

373

374 @return: C{True} if C{iterable}, C{False} otherwise.

375 '''

376 # <https://PyPI.org/project/isiterable/>

377 return hasattr(obj, '__iter__') # map, range, set

378

379

380 def isiterablen(obj):

381 '''Is B{C{obj}}ect C{iterable} and has C{len}gth?

382

383 @arg obj: The object (any C{type}).

384

385 @return: C{True} if C{iterable} with C{len}gth, C{False} otherwise.

386 '''

387 return hasattr(obj, '__len__') and hasattr(obj, '__getitem__')

388

389

390 try:

391 from keyword import iskeyword # Python 2.7+

392 except ImportError:

393

394 def iskeyword(unused):

395 '''Not Implemented, C{False} always.

396 '''

397 return False

398

399

400 def isLatLon(obj, ellipsoidal=None):

401 '''Is B{C{obj}}ect some C{LatLon}?

402

403 @arg obj: The object (any C{type}).

404 @kwarg ellipsoidal: If C{None}, return the type of any C{LatLon},

405 if C{True}, only an ellipsoidal C{LatLon type}

406 or if C{False}, only a spherical C{LatLon type}.

407

408 @return: C{type(B{obj}} if a C{LatLon} of the required type, C{False}

409 if a C{LatLon} of an other type or {None} otherwise.

410 '''

411 if ellipsoidal is not None:

412 try:

413 return obj.ellipsoidalLatLon if ellipsoidal else obj.sphericalLatLon

414 except AttributeError:

415 return None

416 return isinstanceof(obj, _MODS.latlonBase.LatLonBase)

417

418

419 def islistuple(obj, minum=0):

420 '''Is B{C{obj}}ect a C{list} or C{tuple} with non-zero length?

421

422 @arg obj: The object (any C{type}).

423 @kwarg minum: Minimal C{len} required C({int}).

424

425 @return: C{True} if a C{list} or C{tuple} with C{len} at

426 least B{C{minum}}, C{False} otherwise.

427 '''

428 return isinstance(obj, _list_tuple_types) and len(obj) >= minum

429

430

431 def isNvector(obj, ellipsoidal=None):

432 '''Is B{C{obj}}ect some C{Nvector}?

433

434 @arg obj: The object (any C{type}).

435 @kwarg ellipsoidal: If C{None}, return the type of any C{Nvector},

436 if C{True}, only an ellipsoidal C{Nvector type}

437 or if C{False}, only a spherical C{Nvector type}.

438

439 @return: C{type(B{obj}} if an C{Nvector} of the required type, C{False}

440 if an C{Nvector} of an other type or {None} otherwise.

441 '''

442 if ellipsoidal is not None:

443 try:

444 return obj.ellipsoidalNvector if ellipsoidal else obj.sphericalNvector

445 except AttributeError:

446 return None

447 return isinstanceof(obj, _MODS.nvectorBase.NvectorBase)

448

449

450 def isodd(x):

451 '''Is B{C{x}} odd?

452

453 @arg x: Value (C{scalar}).

454

455 @return: C{True} if odd, C{False} otherwise.

456 '''

457 return bool(int(x) & 1) # == bool(int(x) % 2)

458

459

460 def isscalar(obj, both=False):

461 '''Is B{C{obj}}ect an C{int} or integer C{float} value?

462

463 @arg obj: The object (any C{type}).

464 @kwarg both: If C{True}, check L{Fsum} and L{Fsum2Tuple}

465 residuals.

466

467 @return: C{True} if C{int}, C{float} or C{Fsum/-2Tuple}

468 with zero residual, C{False} otherwise.

469 '''

470 if isinstance(obj, _Scalars):

471 return not isbool(obj) # exclude bool

472 elif both and _MODS.fsums._isFsum_2Tuple(obj):

473 return bool(obj.residual == 0)

474 return False

475

476

477 def issequence(obj, *excls):

478 '''Is B{C{obj}}ect some sequence type?

479

480 @arg obj: The object (any C{type}).

481 @arg excls: Classes to exclude (C{type}), all positional.

482

483 @note: Excluding C{tuple} implies excluding C{namedtuple}.

484

485 @return: C{True} if a sequence, C{False} otherwise.

486 '''

487 return isinstance(obj, _Seqs) and not (excls and isinstance(obj, excls))

488

489

490 def isstr(obj):

491 '''Is B{C{obj}}ect some string type?

492

493 @arg obj: The object (any C{type}).

494

495 @return: C{True} if a C{str}, C{bytes}, ...,

496 C{False} otherwise.

497 '''

498 return isinstance(obj, _Strs)

499

500

501 def issubclassof(Sub, *Supers):

502 '''Is B{C{Sub}} a class and sub-class of some other class(es)?

503

504 @arg Sub: The sub-class (C{Class}).

505 @arg Supers: One or more C(super) classes (C{Class}).

506

507 @return: C{True} if a sub-class of any B{C{Supers}}, C{False}

508 if not (C{bool}) or C{None} if not a class or if no

509 B{C{Supers}} are given or none of those are a class.

510 '''

511 if isclass(Sub):

512 t = tuple(S for S in Supers if isclass(S))

513 if t:

514 return bool(issubclass(Sub, t))

515 return None

516

517

518 def itemsorted(adict, *items_args, **asorted_reverse):

519 '''Return the items of C{B{adict}} sorted I{alphabetically,

520 case-insensitively} and in I{ascending} order.

521

522 @arg items_args: Optional positional argument(s) for method

523 C{B{adict}.items(B*{items_args})}.

524 @kwarg asorted_reverse: Use C{B{asorted}=False} for I{alphabetical,

525 case-sensitive} sorting and C{B{reverse}=True} for

526 sorting in C{descending} order.

527 '''

528 def _ins(item): # functools.cmp_to_key

529 k, v = item

530 return k.lower()

531

532 def _reverse_key(asorted=True, reverse=False):

533 return dict(reverse=reverse, key=_ins if asorted else None)

534

535 items = adict.items(*items_args) if items_args else adict.items()

536 return sorted(items, **_reverse_key(**asorted_reverse))

537

538

539 def len2(items):

540 '''Make built-in function L{len} work for generators, iterators,

541 etc. since those can only be started exactly once.

542

543 @arg items: Generator, iterator, list, range, tuple, etc.

544

545 @return: 2-Tuple C{(n, items)} of the number of items (C{int})

546 and the items (C{list} or C{tuple}).

547 '''

548 if not isinstance(items, _Seqs): # NOT hasattr(items, '__len__'):

549 items = list(items)

550 return len(items), items

551

552

553 def map1(fun1, *xs): # XXX map_

554 '''Call a single-argument function to each B{C{xs}}

555 and return a C{tuple} of results.

556

557 @arg fun1: 1-Arg function (C{callable}).

558 @arg xs: Arguments (C{any positional}).

559

560 @return: Function results (C{tuple}).

561 '''

562 return tuple(map(fun1, xs))

563

564

565 def map2(fun, *xs):

566 '''Like Python's B{C{map}} but returning a C{tuple} of results.

567

568 Unlike Python 2's built-in L{map}, Python 3+ L{map} returns a

569 L{map} object, an iterator-like object which generates the

570 results only once. Converting the L{map} object to a tuple

571 maintains the Python 2 behavior.

572

573 @arg fun: Function (C{callable}).

574 @arg xs: Arguments (C{all positional}).

575

576 @return: Function results (C{tuple}).

577 '''

578 return tuple(map(fun, *xs))

579

580

581 def neg(x, neg0=None):

582 '''Negate C{x} and optionally, negate C{0.0} and C{-0.0}.

583

584 @kwarg neg0: Defines the return value for zero C{B{x}}: if C{None}

585 return C{0.0}, if C{True} return C{NEG0 if B{x}=0.0}

586 and C{0.0 if B{x}=NEG0} or if C{False} return C{B{x}}

587 I{as-is} (C{bool} or C{None}).

588

589 @return: C{-B{x} if B{x} else 0.0, NEG0 or B{x}}.

590 '''

591 return (-x) if x else (

592 _0_0 if neg0 is None else (

593 x if not neg0 else (

594 _0_0 if signBit(x) else _MODS.constants.

595 NEG0))) # PYCHOK indent

596

597

598 def neg_(*xs):

599 '''Negate all C{xs} with L{neg}.

600

601 @return: A C{map(neg, B{xs})}.

602 '''

603 return map(neg, xs)

604

605

606 def _neg0(x):

607 '''(INTERNAL) Return C{NEG0 if x < 0 else _0_0},

608 unlike C{_copysign_0_0} which returns C{_N_0_0}.

609 '''

610 return _MODS.constants.NEG0 if x < 0 else _0_0

611

612

613 def _req_d_by(where, **name):

614 '''(INTERNAL) Get the fully qualified name.

615 '''

616 m = _MODS.named

617 n = m._name__(**name)

618 m = m.modulename(where, prefixed=True)

619 if n:

620 m = _DOT_(m, n)

621 return _SPACE_(_required_, _by_, m)

622

623

624 def _reverange(n, stop=-1, step=-1):

625 '''(INTERNAL) Reversed range yielding C{n-1, n-1-step, ..., stop+1}.

626 '''

627 return range(n - 1, stop, step)

628

629

630 def signBit(x):

631 '''Return C{signbit(B{x})}, like C++.

632

633 @return: C{True} if C{B{x} < 0} or C{NEG0} (C{bool}).

634 '''

635 return x < 0 or _MODS.constants.isneg0(x)

636

637

638 def _signOf(x, ref): # in .fsums

639 '''(INTERNAL) Return the sign of B{C{x}} versus B{C{ref}}.

640 '''

641 return (-1) if x < ref else (+1 if x > ref else 0)

642

643

644 def signOf(x):

645 '''Return sign of C{x} as C{int}.

646

647 @return: -1, 0 or +1 (C{int}).

648 '''

649 try:

650 s = x.signOf() # Fsum instance?

651 except AttributeError:

652 s = _signOf(x, 0)

653 return s

654

655

656 def splice(iterable, n=2, **fill):

657 '''Split an iterable into C{n} slices.

658

659 @arg iterable: Items to be spliced (C{list}, C{tuple}, ...).

660 @kwarg n: Number of slices to generate (C{int}).

661 @kwarg fill: Optional fill value for missing items.

662

663 @return: A generator for each of B{C{n}} slices,

664 M{iterable[i::n] for i=0..n}.

665

666 @raise TypeError: Invalid B{C{n}}.

667

668 @note: Each generated slice is a C{tuple} or a C{list},

669 the latter only if the B{C{iterable}} is a C{list}.

670

671 @example:

672

673 >>> from pygeodesy import splice

674

675 >>> a, b = splice(range(10))

676 >>> a, b

677 ((0, 2, 4, 6, 8), (1, 3, 5, 7, 9))

678

679 >>> a, b, c = splice(range(10), n=3)

680 >>> a, b, c

681 ((0, 3, 6, 9), (1, 4, 7), (2, 5, 8))

682

683 >>> a, b, c = splice(range(10), n=3, fill=-1)

684 >>> a, b, c

685 ((0, 3, 6, 9), (1, 4, 7, -1), (2, 5, 8, -1))

686

687 >>> tuple(splice(list(range(9)), n=5))

688 ([0, 5], [1, 6], [2, 7], [3, 8], [4])

689

690 >>> splice(range(9), n=1)

691 <generator object splice at 0x0...>

692 '''

693 if not isint(n):

694 raise _TypeError(n=n)

695

696 t = _xiterablen(iterable)

697 if not isinstance(t, _list_tuple_types):

698 t = tuple(t)

699

700 if n > 1:

701 if fill:

702 fill = _xkwds_get1(fill, fill=MISSING)

703 if fill is not MISSING:

704 m = len(t) % n

705 if m > 0: # same type fill

706 t = t + type(t)((fill,) * (n - m))

707 for i in range(n):

708 # XXX t[i::n] chokes PyChecker

709 yield t[slice(i, None, n)]

710 else:

711 yield t # 1 slice, all

712

713

714 def _splituple(strs, *sep_splits): # in .mgrs, .osgr, .webmercator

715 '''(INTERNAL) Split a C{comma}- or C{whitespace}-separated

716 string into a C{tuple} of stripped strings.

717 '''

718 t = (strs.split(*sep_splits) if sep_splits else

719 strs.replace(_COMMA_, _SPACE_).split()) if strs else ()

720 return tuple(s.strip() for s in t if s)

721

722

723 def unsigned0(x):

724 '''Unsign if C{0.0}.

725

726 @return: C{B{x}} if B{C{x}} else C{0.0}.

727 '''

728 return x if x else _0_0

729

730

731 def _xcopy(obj, deep=False):

732 '''(INTERNAL) Copy an object, shallow or deep.

733

734 @arg obj: The object to copy (any C{type}).

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

736 a shallow copy (C{bool}).

737

738 @return: The copy of B{C{obj}}.

739 '''

740 return _deepcopy(obj) if deep else _copy(obj)

741

742

743 def _xcoverage(where, *required):

744 '''(INTERNAL) Import C{coverage} and check required version.

745 '''

746 try:

747 _xpackage(_xcoverage)

748 import coverage

749 except ImportError as x:

750 raise _xImportError(x, where)

751 return _xversion(coverage, where, *required)

752

753

754 def _xdup(obj, deep=False, **items):

755 '''(INTERNAL) Duplicate an object, replacing some attributes.

756

757 @arg obj: The object to copy (any C{type}).

758 @kwarg deep: If C{True}, copy deep, otherwise shallow (C{bool}).

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

760

761 @return: A duplicate of B{C{obj}} with modified

762 attributes, if any B{C{items}}.

763

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

765 '''

766 d = _xcopy(obj, deep=deep)

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

768 if getattr(d, n, v) != v:

769 setattr(d, n, v)

770 elif not hasattr(d, n):

771 t = _MODS.named.classname(obj)

772 t = _SPACE_(_DOT_(t, n), _invalid_)

773 raise _AttributeError(txt=t, obj=obj, **items)

774# if items:

775# _MODS.props._update_all(d)

776 return d

777

778

779 def _xgeographiclib(where, *required):

780 '''(INTERNAL) Import C{geographiclib} and check required version.

781 '''

782 try:

783 _xpackage(_xgeographiclib)

784 import geographiclib

785 except ImportError as x:

786 raise _xImportError(x, where, Error=LazyImportError)

787 return _xversion(geographiclib, where, *required)

788

789

790 def _xImportError(exc, where, Error=_ImportError, **name):

791 '''(INTERNAL) Embellish an C{Lazy/ImportError}.

792 '''

793 t = _req_d_by(where, **name)

794 return Error(_Xstr(exc), txt=t, cause=exc)

795

796

797 def _xinstanceof(*Types, **names_values):

798 '''(INTERNAL) Check C{Types} of all C{name=value} pairs.

799

800 @arg Types: One or more classes or types (C{class}), all

801 positional.

802 @kwarg names_values: One or more C{B{name}=value} pairs

803 with the C{value} to be checked.

804

805 @raise TypeError: One B{C{names_values}} pair is not an

806 instance of any of the B{C{Types}}.

807 '''

808 if not (Types and names_values):

809 raise _xAssertionError(_xinstanceof, *Types, **names_values)

810

811 for n, v in names_values.items():

812 if not isinstance(v, Types):

813 raise _TypesError(n, v, *Types)

814

815

816 def _xiterable(obj):

817 '''(INTERNAL) Return C{obj} if iterable, otherwise raise C{TypeError}.

818 '''

819 return obj if isiterable(obj) else _xiterror(obj, _xiterable) # PYCHOK None

820

821

822 def _xiterablen(obj):

823 '''(INTERNAL) Return C{obj} if iterable with C{__len__}, otherwise raise C{TypeError}.

824 '''

825 return obj if isiterablen(obj) else _xiterror(obj, _xiterablen) # PYCHOK None

826

827

828 def _xiterror(obj, _xwhich):

829 '''(INTERNAL) Helper for C{_xinterable} and C{_xiterablen}.

830 '''

831 t = _not_(_xwhich.__name__[2:]) # _dunder_nameof

832 raise _TypeError(repr(obj), txt=t)

833

834

835 def _xnumpy(where, *required):

836 '''(INTERNAL) Import C{numpy} and check required version.

837 '''

838 try:

839 _xpackage(_xnumpy)

840 import numpy

841 except ImportError as x:

842 raise _xImportError(x, where)

843 return _xversion(numpy, where, *required)

844

845

846 def _xor(x, *xs):

847 '''(INTERNAL) Exclusive-or C{x} and C{xs}.

848 '''

849 for x_ in xs:

850 x ^= x_

851 return x

852

853

854 def _xpackage(_xpkg):

855 '''(INTERNAL) Check dependency to be excluded.

856 '''

857 n = _xpkg.__name__[2:] # _dunder_nameof

858 if n in _XPACKAGES:

859 x = _SPACE_(n, _in_, _PYGEODESY_XPACKAGES_)

860 e = _enquote(_getenv(_PYGEODESY_XPACKAGES_, NN))

861 raise ImportError(_EQUAL_(x, e))

862

863

864 def _xscalar(**names_values):

865 '''(INTERNAL) Check all C{name=value} pairs to be C{scalar}.

866 '''

867 for n, v in names_values.items():

868 if not isscalar(v):

869 raise _TypeError(n, v, txt=_not_scalar_)

870

871

872 def _xscipy(where, *required):

873 '''(INTERNAL) Import C{scipy} and check required version.

874 '''

875 try:

876 _xpackage(_xscipy)

877 import scipy

878 except ImportError as x:

879 raise _xImportError(x, where)

880 return _xversion(scipy, where, *required)

881

882

883 def _xsubclassof(*Classes, **names_values):

884 '''(INTERNAL) Check (super) class of all C{name=value} pairs.

885

886 @arg Classes: One or more classes or types (C{class}), all

887 positional.

888 @kwarg names_values: One or more C{B{name}=value} pairs

889 with the C{value} to be checked.

890

891 @raise TypeError: One B{C{names_values}} pair is not a

892 (sub-)class of any of the B{C{Classes}}.

893 '''

894 if not (Classes and names_values):

895 raise _xAssertionError(_xsubclassof, *Classes, **names_values)

896

897 for n, v in names_values.items():

898 if not issubclassof(v, *Classes):

899 raise _TypesError(n, v, *Classes)

900

901

902 def _xversion(package, where, *required, **name):

903 '''(INTERNAL) Check the C{package} version vs B{C{required}}.

904 '''

905 if required:

906 t = _version_info(package)

907 if t[:len(required)] < required:

908 t = _SPACE_(package.__name__, # _dunder_nameof

909 _version_, _DOT_(*t),

910 _below_, _DOT_(*required),

911 _req_d_by(where, **name))

912 raise ImportError(t)

913 return package

914

915

916 def _xzip(*args, **strict): # PYCHOK no cover

917 '''(INTERNAL) Standard C{zip(..., strict=True)}.

918 '''

919 s = _xkwds_get1(strict, strict=True)

920 if s:

921 if _zip is zip: # < (3, 10)

922 t = _MODS.streprs.unstr(_xzip, *args, strict=s)

923 raise _NotImplementedError(t, txt=None)

924 return _zip(*args)

925 return zip(*args)

926

927

928 if _sys_version_info2 < (3, 10): # see .errors

929 _zip = zip # PYCHOK exported

930 else: # Python 3.10+

931

932 def _zip(*args):

933 return zip(*args, strict=True)

934

935_XPACKAGES = _splituple(_getenv(_PYGEODESY_XPACKAGES_, NN).lower())

936

937# **) MIT License

938#

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

940#

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

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

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

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

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

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

947#

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

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

950#

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

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

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

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

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

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

957# OTHER DEALINGS IN THE SOFTWARE.