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.errors import _AssertionError, _AttributeError, _ImportError, \

18 _TypeError, _TypesError, _ValueError, _xkwds_get

19 from pygeodesy.interns import MISSING, NN, _by_, _DOT_, _ELLIPSIS4_, _enquote, \

20 _EQUAL_, _in_, _invalid_, _N_A_, _name_, _SPACE_, \

21 _splituple, _UNDER_, _version_

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

23 _getenv, _sys_version_info2

24

25 from copy import copy as _copy, deepcopy as _deepcopy

26 from math import copysign as _copysign

27 import inspect as _inspect

28

29__all__ = _ALL_LAZY.basics

30__version__ = '23.03.29'

31

32_0_0 = 0.0 # see .constants

33_below_ = 'below'

34_cannot_ = 'cannot'

35_list_tuple_types = (list, tuple)

36_odd_ = 'odd'

37_required_ = 'required'

38_PYGEODESY_XPACKAGES_ = 'PYGEODESY_XPACKAGES'

39_XPACKAGES = _splituple(_getenv(_PYGEODESY_XPACKAGES_, NN))

40

41 try: # Luciano Ramalho, "Fluent Python", page 395, O'Reilly, 2016

42 from numbers import Integral as _Ints, Real as _Scalars

43 except ImportError:

44 try:

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

46 except NameError: # Python 3+

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

48 _Scalars = _Ints + (float,)

49

50 try:

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

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

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

54 from collections import Sequence as _Sequence # in .points

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

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

57 _Seqs = _Sequence

58 else:

59 raise ImportError # _AssertionError

60 except ImportError:

61 _Sequence = tuple # immutable for .points._Basequence

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

63

64 try:

65 _Bytes = unicode, bytearray # PYCHOK expected

66 _Strs = basestring, str # XXX , bytes

67

68 def _NOP(x):

69 '''NOP, pass thru.'''

70 return x

71

72 str2ub = ub2str = _NOP # avoids UnicodeDecodeError

73

74 def _Xstr(exc): # PYCHOK no cover

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

76

77 C{... "cannot import name _distributor_init" ...}

78

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

80 on arm64 Apple Silicon running macOS' Python 2.7.16?

81 '''

82 t = str(exc)

83 if '_distributor_init' in t:

84 from sys import exc_info

85 from traceback import extract_tb

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

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

88 t = _SPACE_(_cannot_, t4[3] or _N_A_)

89 del tb, t4

90 return t

91

92 except NameError: # Python 3+

93 from pygeodesy.interns import _utf_8_

94

95 _Bytes = bytes, bytearray

96 _Strs = str, # tuple

97 _Xstr = str

98

99 def str2ub(sb):

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

101 '''

102 if isinstance(sb, _Strs):

103 sb = sb.encode(_utf_8_)

104 return sb

105

106 def ub2str(ub):

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

108 '''

109 if isinstance(ub, _Bytes):

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

111 return ub

112

113

114 def clips(sb, limit=50, white=NN):

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

116

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

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

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

120

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

122 '''

123 T = type(sb)

124 if len(sb) > limit > 8:

125 h = limit // 2

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

127 if white: # replace whitespace

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

129 return sb

130

131

132 def copysign0(x, y):

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

134

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

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

137 '''

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

139

140

141 def copytype(x, y):

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

143

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

145 '''

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

147

148

149 def halfs2(str2):

150 '''Split a string in 2 halfs.

151

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

153

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

155

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

157 '''

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

159 if r or not h:

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

161 return str2[:h], str2[h:]

162

163

164 def isbool(obj):

165 '''Check whether an object is C{bool}ean.

166

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

168

169 @return: C{True} if B{C{obj}} is C{bool}ean,

170 C{False} otherwise.

171 '''

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

173# or obj is True)

174

175 if isbool(1) or isbool(0): # PYCHOK assert

176 raise _AssertionError(isbool=1)

177

178 if _FOR_DOCS: # XXX avoid epidoc Python 2.7 error

179

180 def isclass(obj):

181 '''Return C{True} if B{C{obj}} is a C{class} or C{type}.

182

183 @see: Python's C{inspect.isclass}.

184 '''

185 return _inspect.isclass(obj)

186 else:

187 isclass = _inspect.isclass

188

189

190 def iscomplex(obj):

191 '''Check whether an object is C{complex}.

192

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

194

195 @return: C{True} if B{C{obj}} is C{complex},

196 C{False} otherwise.

197 '''

198 # hasattr('conjugate'), hasattr('real') and hasattr('imag')

199 return isinstance(obj, complex) # numbers.Complex?

200

201

202 def isfloat(obj):

203 '''Check whether an object is a C{float} or float C{str}.

204

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

206

207 @return: C{True} if B{C{obj}} is a C{float},

208 C{False} otherwise.

209 '''

210 try:

211 return isinstance( obj, float) or (isstr(obj)

212 and isinstance(float(obj), float))

213 except (TypeError, ValueError):

214 return False

215

216

217 try:

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

219 except AttributeError: # Python 2-

220

221 def isidentifier(obj):

222 '''Return C{True} if B{C{obj}} is a valid Python identifier.

223 '''

224 return bool(obj and obj.replace(_UNDER_, NN).isalnum()

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

226

227

228 def isinstanceof(obj, *classes):

229 '''Check an instance of one or several C{classes}.

230

231 @arg obj: The instance (C{any}).

232 @arg classes: One or more classes (C{class}).

233

234 @return: C{True} if B{C{obj}} is in instance of

235 one of the B{C{classes}}.

236 '''

237 return isinstance(obj, classes)

238

239

240 def isint(obj, both=False):

241 '''Check for C{int} type or an integer C{float} value.

242

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

244 @kwarg both: If C{true}, check C{float} and L{Fsum}

245 type and value (C{bool}).

246

247 @return: C{True} if B{C{obj}} is C{int} or I{integer}

248 C{float} or L{Fsum}, C{False} otherwise.

249

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

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

252 '''

253 if isinstance(obj, _Ints) and not isbool(obj):

254 return True

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

256 try: # ... NOT , Scalars) to include Fsum!

257 return obj.is_integer()

258 except AttributeError:

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

260 return False

261

262

263 try:

264 from keyword import iskeyword # Python 2.7+

265 except ImportError:

266

267 def iskeyword(unused):

268 '''Not Implemented. Return C{False}, always.

269 '''

270 return False

271

272

273 def islistuple(obj, minum=0):

274 '''Check for list or tuple C{type} with a minumal length.

275

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

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

278

279 @return: C{True} if B{C{obj}} is C{list} or C{tuple} with

280 C{len} at least B{C{minum}}, C{False} otherwise.

281 '''

282 return type(obj) in _list_tuple_types and len(obj) >= (minum or 0)

283

284

285 def isodd(x):

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

287

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

289

290 @return: C{True} if B{C{x}} is odd,

291 C{False} otherwise.

292 '''

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

294

295

296 def isscalar(obj):

297 '''Check for scalar types.

298

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

300

301 @return: C{True} if B{C{obj}} is C{scalar}, C{False} otherwise.

302 '''

303 return isinstance(obj, _Scalars) and not isbool(obj)

304

305

306 def issequence(obj, *excls):

307 '''Check for sequence types.

308

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

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

311

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

313

314 @return: C{True} if B{C{obj}} is a sequence, C{False} otherwise.

315 '''

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

317

318

319 def isstr(obj):

320 '''Check for string types.

321

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

323

324 @return: C{True} if B{C{obj}} is C{str}, C{False} otherwise.

325 '''

326 return isinstance(obj, _Strs)

327

328

329 def issubclassof(Sub, *Supers):

330 '''Check whether a class is a sub-class of some other class(es).

331

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

333 @arg Supers: One or more C(super) classes (C{class}).

334

335 @return: C{True} if B{C{Sub}} is a sub-class of any B{C{Supers}},

336 C{False} if not (C{bool}) or C{None} if B{C{Sub}} is not

337 a class or if no B{C{Supers}} are given or none of those

338 are a class.

339 '''

340 if isclass(Sub):

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

342 if t:

343 return bool(issubclass(Sub, t))

344 return None

345

346

347 def len2(items):

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

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

350

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

352

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

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

355 '''

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

357 items = list(items)

358 return len(items), items

359

360

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

362 '''Apply each argument to a single-argument function and

363 return a C{tuple} of results.

364

365 @arg fun1: 1-Arg function to apply (C{callable}).

366 @arg xs: Arguments to apply (C{any positional}).

367

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

369 '''

370 return tuple(map(fun1, xs)) # note xs, not *xs

371

372

373 def map2(func, *xs):

374 '''Apply arguments to a function and return a C{tuple} of results.

375

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

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

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

379 maintains the Python 2 behavior.

380

381 @arg func: Function to apply (C{callable}).

382 @arg xs: Arguments to apply (C{list, tuple, ...}).

383

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

385 '''

386 return tuple(map(func, *xs)) # note *xs, not xs

387

388

389 def neg(x):

390 '''Negate C{x} unless C{zero} or C{NEG0}.

391

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

393 '''

394 return -x if x else _0_0

395

396

397 def neg_(*xs):

398 '''Negate all of C{xs} with L{neg}.

399

400 @return: A C{tuple(neg(x) for x in B{xs})}.

401 '''

402 return tuple(map(neg, xs)) # like map1

403

404

405 def signBit(x):

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

407

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

409 '''

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

411

412

413 def _signOf(x, off):

414 '''(INTERNAL) Return the sign of B{C{x}} versus B{C{off}}.

415 '''

416 return +1 if x > off else (-1 if x < off else 0)

417

418

419 def signOf(x):

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

421

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

423 '''

424 try:

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

426 except AttributeError:

427 s = _signOf(x, 0)

428 return s

429

430

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

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

433

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

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

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

437

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

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

440

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

442

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

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

445

446 @example:

447

448 >>> from pygeodesy import splice

449

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

451 >>> a, b

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

453

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

455 >>> a, b, c

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

457

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

459 >>> a, b, c

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

461

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

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

464

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

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

467 '''

468 if not isint(n):

469 raise _TypeError(n=n)

470

471 t = iterable

472 if not isinstance(t, (list, tuple)):

473 t = tuple(t) # force tuple, also for PyPy3

474

475 if n > 1:

476 if fill:

477 fill = _xkwds_get(fill, fill=MISSING)

478 if fill is not MISSING:

479 m = len(t) % n

480 if m > 0: # same type fill

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

482 for i in range(n):

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

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

485 else:

486 yield t

487

488

489 def unsigned0(x):

490 '''Return C{0.0} unsigned.

491

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

493 '''

494 return x if x else _0_0

495

496

497 def _xargs_names(callabl):

498 '''(INTERNAL) Get the C{callabl}'s args names.

499 '''

500 try:

501 args_kwds = _inspect.signature(callabl).parameters.keys()

502 except AttributeError: # .signature new Python 3+

503 args_kwds = _inspect.getargspec(callabl).args

504 return tuple(args_kwds)

505

506

507 def _xcopy(inst, deep=False):

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

509

510 @arg inst: The object to copy (any C{type}).

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

512 a shallow copy (C{bool}).

513

514 @return: The copy of B{C{inst}}.

515 '''

516 return _deepcopy(inst) if deep else _copy(inst)

517

518

519 def _xdup(inst, **items):

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

521

522 @arg inst: The object to copy (any C{type}).

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

524

525 @return: Shallow duplicate of B{C{inst}} with modified

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

527

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

529 '''

530 d = _xcopy(inst, deep=False)

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

532 if not hasattr(d, n):

533 t = _MODS.named.classname(inst)

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

535 raise _AttributeError(txt=t, this=inst, **items)

536 setattr(d, n, v)

537 return d

538

539

540 def _xgeographiclib(where, *required):

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

542 '''

543 try:

544 _xpackage(_xgeographiclib)

545 import geographiclib

546 except ImportError as x:

547 raise _xImportError(x, where)

548 return _xversion(geographiclib, where, *required)

549

550

551 def _xImportError(x, where, **name):

552 '''(INTERNAL) Embellish an C{ImportError}.

553 '''

554 t = _SPACE_(_required_, _by_, _xwhere(where, **name))

555 return _ImportError(_Xstr(x), txt=t, cause=x)

556

557

558 def _xinstanceof(*Types, **name_value_pairs):

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

560

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

562 all positional.

563 @kwarg name_value_pairs: One or more C{B{name}=value} pairs

564 with the C{value} to be checked.

565

566 @raise TypeError: One of the B{C{name_value_pairs}} is not

567 an instance of any of the B{C{Types}}.

568 '''

569 for n, v in name_value_pairs.items():

570 if not isinstance(v, Types):

571 raise _TypesError(n, v, *Types)

572

573

574 def _xnumpy(where, *required):

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

576 '''

577 try:

578 _xpackage(_xnumpy)

579 import numpy

580 except ImportError as x:

581 raise _xImportError(x, where)

582 return _xversion(numpy, where, *required)

583

584

585 def _xpackage(_xpkg):

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

587 '''

588 n = _xpkg.__name__[2:]

589 if n in _XPACKAGES:

590 x = _SPACE_(n, _in_, _PYGEODESY_XPACKAGES_)

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

592 raise ImportError(_EQUAL_(x, e))

593

594

595 def _xor(x, *xs):

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

597 '''

598 for x_ in xs:

599 x ^= x_

600 return x

601

602

603 def _xscipy(where, *required):

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

605 '''

606 try:

607 _xpackage(_xscipy)

608 import scipy

609 except ImportError as x:

610 raise _xImportError(x, where)

611 return _xversion(scipy, where, *required)

612

613

614 def _xsubclassof(*Classes, **name_value_pairs):

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

616

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

618 all positional.

619 @kwarg name_value_pairs: One or more C{B{name}=value} pairs

620 with the C{value} to be checked.

621

622 @raise TypeError: One of the B{C{name_value_pairs}} is not

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

624 '''

625 for n, v in name_value_pairs.items():

626 if not issubclassof(v, *Classes):

627 raise _TypesError(n, v, *Classes)

628

629

630 def _xversion(package, where, *required, **name): # in .karney

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

632 '''

633 n = len(required)

634 if n:

635 t = _xversion_info(package)

636 if t[:n] < required:

637 t = _SPACE_(package.__name__, _version_, _DOT_(*t),

638 _below_, _DOT_(*required),

639 _required_, _by_, _xwhere(where, **name))

640 raise ImportError(t)

641 return package

642

643

644 def _xversion_info(package): # in .karney

645 '''(INTERNAL) Get the C{package.__version_info__} as a 2- or

646 3-tuple C{(major, minor, revision)} if C{int}s.

647 '''

648 try:

649 t = package.__version_info__

650 except AttributeError:

651 t = package.__version__.strip()

652 t = t.replace(_DOT_, _SPACE_).split()[:3]

653 return map2(int, t)

654

655

656 def _xwhere(where, **name):

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

658 '''

659 m = _MODS.named.modulename(where, prefixed=True)

660 n = name.get(_name_, NN)

661 if n:

662 m = _DOT_(m, n)

663 return m

664

665

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

667 _zip = zip # PYCHOK exported

668 else: # Python 3.10+

669

670 def _zip(*args):

671 return zip(*args, strict=True)

672

673# **) MIT License

674#

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

676#

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

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

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

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

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

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

683#

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

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

686#

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

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

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

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

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

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

693# OTHER DEALINGS IN THE SOFTWARE.