Coverage for pygeodesy / basics.py: 88%

279 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-06-24 12:50 -0400

1 

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

3 

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

5 

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

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

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

9''' 

10# make sure int/int division yields float quotient 

11from __future__ import division 

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

13if not division: 

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

15del division 

16 

17# from pygeodesy.cartesianBase import CartesianBase # _MODS 

18# from pygeodesy.constants import NEG0 # _MODS 

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

20 _TypeError, _TypesError, _ValueError, _xAssertionError, \ 

21 _xkwds_get1 

22# from pygeodesy.fsums import _isFsum_2Tuple # _MODS 

23from pygeodesy.internals import _0_0, _enquote, _envPYGEODESY, _getenv, _passarg, \ 

24 _PYGEODESY_ENV, typename, _version_info 

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

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

27 _not_scalar_, _odd_, _SPACE_, _UNDER_, _version_ 

28# from pygeodesy.latlonBase import LatLonBase # _MODS 

29from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, LazyImportError 

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 

35from copy import copy as _copy, deepcopy as _deepcopy 

36from math import copysign as _copysign 

37# import inspect as _inspect # _MODS 

38 

39__all__ = _ALL_LAZY.basics 

40__version__ = '26.06.24' 

41 

42_below_ = 'below' 

43_list_tuple_types = (list, tuple) 

44_required_ = 'required' 

45 

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

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

48except ImportError: 

49 try: 

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

51 except NameError: # Python 3+ 

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

53 _Scalars = (float,) + _Ints 

54 

55try: 

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

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

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

59 from collections import Sequence as _Sequence # in .points 

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

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

62 _Seqs = _Sequence 

63 else: 

64 raise ImportError() # _AssertionError 

65except ImportError: 

66 _Sequence = tuple # immutable for .points._Basequence 

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

68 

69try: 

70 _Bytes = unicode, bytearray # PYCHOK in .internals 

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

72 str2ub = ub2str = _passarg # avoids UnicodeDecodeError 

73 

74 def _Xstr(exc): # PYCHOK no cover 

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

76 

77 C{... "can't 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_("can't", t4[3] or _N_A_) 

89 del tb, t4 

90 return t 

91 

92except NameError: # Python 3+ 

93 from pygeodesy.interns import _utf_8_ 

94 

95 _Bytes = bytes, bytearray # in .internals 

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 _args_kwds_count2(func, exelf=True): # in .formy 

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

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

117# 

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

119# of a method (C{bool}). 

120# ''' 

121# i = _MODS.inspect 

122# try: 

123# a = k = 0 

124# for _, p in i.signature(func).parameters.items(): 

125# if p.kind is p.POSITIONAL_OR_KEYWORD: 

126# if p.default is p.empty: 

127# a += 1 

128# else: 

129# k += 1 

130# except AttributeError: # Python 2- 

131# s = i.getargspec(func) 

132# k = len(s.defaults or ()) 

133# a = len(s.args) - k 

134# if exelf and a > 0 and i.ismethod(func): 

135# a -= 1 

136# return a, k 

137 

138 

139def _args_kwds_names(func, splast=False): 

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

141 C{self} for methods. 

142 

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

144 at UNDERscores (C{bool}). 

145 

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

147 C{**kwds} names. 

148 ''' 

149 i = _MODS.inspect 

150 try: 

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

152 except AttributeError: # Python 2- 

153 args_kwds = i.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 

164def 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 

186def 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 

195def 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 

203def _enumereverse(iterable): 

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

205 ''' 

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

207 yield j, iterable[j] 

208 

209 

210def _float0d(f): 

211 # numpy 2.5.0 numpy.linalg.lstsq, numpy.pseudo_inverse.dot and 

212 # scipy 1.18.0 scipy.BivariateSpline.ev return a "0-d float" or 

213 # 1-list of a float (instead of previously a float) causing error 

214 # "only 0-dimensional arrays can be converted to Python scalars" 

215 try: 

216 return float(f) 

217 except TypeError: # 0-d or 1-list 

218 return f.item() # or float(f[()])? 

219 

220 

221try: 

222 from math import gcd as _gcd 

223except ImportError: # 3.4- 

224 

225 def _gcd(a, b): # PYCHOK redef 

226 # <https://WikiPedia.org/wiki/Greatest_common_divisor> 

227 a, b = int(a), int(b) 

228 if b > a: 

229 a, b = b, a 

230# if b <= 0: 

231# return 1 

232 while b: 

233 a, b = b, (a % b) 

234 return a 

235 

236 

237def halfs2(str2): 

238 '''Split a string in 2 halfs. 

239 

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

241 

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

243 

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

245 ''' 

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

247 if r or not h: 

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

249 return str2[:h], str2[h:] 

250 

251 

252def _integer_ratio2(x): # PYCHOK no cover 

253 '''(INTERNAL) Return C{B{x}.as_interger_ratio()}. 

254 ''' 

255 try: # int.as_integer_ratio in 3.8+ 

256 return x.as_integer_ratio() 

257 except (AttributeError, OverflowError, TypeError, ValueError): 

258 return (x if isint(x) else float(x)), 1 

259 

260 

261def int1s(x): # PYCHOK no cover 

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

263 

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

265 ''' 

266 try: 

267 return x.bit_count() # Python 3.10+ 

268 except AttributeError: 

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

270 return bin(x).count(_1_) 

271 

272 

273def isbool(obj): 

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

275 

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

277 

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

279 ''' 

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

281# or obj is True) 

282 

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

284 

285 

286def isCartesian(obj, ellipsoidal=None): 

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

288 

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

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

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

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

293 

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

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

296 ''' 

297 if ellipsoidal is not None: 

298 try: 

299 return obj.ellipsoidalCartesian if ellipsoidal else obj.sphericalCartesian 

300 except AttributeError: 

301 return None 

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

303 

304 

305def isclass(obj): # XXX avoid epydoc Python 2.7 error 

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

307 ''' 

308 return _MODS.inspect.isclass(obj) 

309 

310 

311def iscomplex(obj, both=False): 

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

313 

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

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

316 

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

318 ''' 

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

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

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

322 except (TypeError, ValueError): 

323 return False 

324 

325 

326def isDEPRECATED(obj, outer=1): 

327 '''Is B{C{obj}}ect or its outer C{type} a C{DEPRECATED} 

328 class, constant, method or function? 

329 

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

331 C{None} if undetermined. 

332 ''' 

333 r = None 

334 for _ in range(max(0, outer) + 1): 

335 try: # inspect.getdoc(obj) 

336 if _DEPRECATED_ in obj.__doc__: 

337 return True 

338 r = False 

339 except AttributeError: 

340 pass 

341 obj = type(obj) 

342 return r 

343 

344 

345def isfloat(obj, both=False): 

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

347 

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

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

350 

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

352 ''' 

353 try: 

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

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

356 except (TypeError, ValueError): 

357 return False 

358 

359 

360try: 

361 isidentifier = str.isidentifier # must be str 

362except AttributeError: # 2.0- 

363 

364 def isidentifier(obj): 

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

366 ''' 

367 return bool(obj and isstr(obj) 

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

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

370 

371 

372def _isin(obj, *objs): 

373 '''(INTERNAL) Return C{bool(obj in objs)} with C{True} and C{False} matching. 

374 ''' 

375 return any(o is obj for o in objs) or \ 

376 any(o == obj for o in objs if not isbool(o)) 

377 

378 

379def isinstanceof(obj, *Classes): 

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

381 

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

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

384 

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

386 C{None} otherwise. 

387 ''' 

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

389 

390 

391def isint(obj, both=False): 

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

393 

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

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

396 type and value (C{bool}). 

397 

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

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

400 

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

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

403 ''' 

404 if isinstance(obj, _Ints): 

405 return not isbool(obj) 

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

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

408 return obj.is_integer() 

409 except AttributeError: 

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

411 return False 

412 

413 

414def isiterable(obj, strict=False): 

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

416 

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

418 @kwarg strict: If C{True}, check class attributes (C{bool}). 

419 

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

421 ''' 

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

423 return bool(isiterabletype(obj)) if strict else hasattr(obj, '__iter__') # map, range, set 

424 

425 

426def isiterablen(obj, strict=False): 

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

428 

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

430 @kwarg strict: If C{True}, check class attributes (C{bool}). 

431 

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

433 ''' 

434 _has = isiterabletype if strict else hasattr 

435 return bool(_has(obj, '__len__') and _has(obj, '__getitem__')) 

436 

437 

438def isiterabletype(obj, method='__iter__'): 

439 '''Is B{C{obj}}ect an instance of an C{iterable} class or type? 

440 

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

442 @kwarg method: The name of the required method (C{str}). 

443 

444 @return: The C{base-class} if C{iterable}, C{None} otherwise. 

445 ''' 

446 try: # <https://StackOverflow.com/questions/73568964> 

447 for b in type(obj).__mro__[:-1]: # ignore C{object} 

448 try: 

449 if callable(b.__dict__[method]): 

450 return b 

451 except (AttributeError, KeyError): 

452 pass 

453 except (AttributeError, TypeError): 

454 pass 

455 return None 

456 

457 

458try: 

459 from keyword import iskeyword # 2.7+ 

460except ImportError: 

461 

462 def iskeyword(unused): 

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

464 ''' 

465 return False 

466 

467 

468def isLatLon(obj, ellipsoidal=None): 

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

470 

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

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

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

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

475 

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

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

478 ''' 

479 if ellipsoidal is not None: 

480 try: 

481 return obj.ellipsoidalLatLon if ellipsoidal else obj.sphericalLatLon 

482 except AttributeError: 

483 return None 

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

485 

486 

487def islistuple(obj, minum=0): 

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

489 

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

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

492 

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

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

495 ''' 

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

497 

498 

499def isNvector(obj, ellipsoidal=None): 

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

501 

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

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

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

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

506 

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

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

509 ''' 

510 if ellipsoidal is not None: 

511 try: 

512 return obj.ellipsoidalNvector if ellipsoidal else obj.sphericalNvector 

513 except AttributeError: 

514 return None 

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

516 

517 

518def isodd(x): 

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

520 

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

522 

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

524 ''' 

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

526 

527 

528def isscalar(obj, both=False): 

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

530 

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

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

533 residuals. 

534 

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

536 with zero residual, C{False} otherwise. 

537 ''' 

538 if isinstance(obj, _Scalars): 

539 return not isbool(obj) # exclude bool 

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

541 return bool(obj.residual == 0) 

542 return False 

543 

544 

545def issequence(obj, *excls): 

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

547 

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

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

550 

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

552 

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

554 ''' 

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

556 

557 

558def isstr(obj): 

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

560 

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

562 

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

564 C{False} otherwise. 

565 ''' 

566 return isinstance(obj, _Strs) 

567 

568 

569def issubclassof(Sub, *Supers): 

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

571 

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

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

574 

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

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

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

578 ''' 

579 if isclass(Sub): 

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

581 if t: 

582 return bool(issubclass(Sub, t)) # built-in 

583 return None 

584 

585 

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

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

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

589 

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

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

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

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

594 sorting in C{descending} order. 

595 ''' 

596 def _ins(item): # functools.cmp_to_key 

597 k, v = item 

598 return k.lower() 

599 

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

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

602 

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

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

605 

606 

607def len2(items): 

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

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

610 

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

612 

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

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

615 ''' 

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

617 items = list(items) 

618 return len(items), items 

619 

620 

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

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

623 and return a C{tuple} of results. 

624 

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

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

627 

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

629 ''' 

630 return tuple(map(fun1, xs)) # if len(xs) != 1 else fun1(xs[0]) 

631 

632 

633def map2(fun, *xs, **strict): 

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

635 

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

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

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

639 maintains the Python 2 behavior. 

640 

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

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

643 @kwarg strict: See U{Python 3.14+ map<https://docs.Python.org/ 

644 3.14/library/functions.html#map>} (C{bool}). 

645 

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

647 ''' 

648 return tuple(map(fun, *xs, **strict) if strict else map(fun, *xs)) 

649 

650 

651def max2(*xs): 

652 '''Return 2-tuple C{(max(xs), xs.index(max(xs)))}. 

653 ''' 

654 return _max2min2(xs, max, max2) 

655 

656 

657def _max2min2(xs, _m, _m2): 

658 '''(INTERNAL) Helper for C{max2} and C{min2}. 

659 ''' 

660 if len(xs) == 1: 

661 x = xs[0] 

662 if isiterable(x) or isiterablen(x): 

663 x, i = _m2(*x) 

664 else: 

665 i = 0 

666 else: 

667 x = _m(xs) # max or min 

668 i = xs.index(x) 

669 return x, i 

670 

671 

672def min2(*xs): 

673 '''Return 2-tuple C{(min(xs), xs.index(min(xs)))}. 

674 ''' 

675 return _max2min2(xs, min, min2) 

676 

677 

678def neg(x, neg0=None): 

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

680 

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

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

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

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

685 

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

687 ''' 

688 return (-x) if x else ( 

689 _0_0 if neg0 is None else ( 

690 x if not neg0 else ( 

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

692 NEG0))) # PYCHOK indent 

693 

694 

695def neg_(*xs): 

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

697 

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

699 ''' 

700 return map(neg, xs) 

701 

702 

703def _neg0(x): 

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

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

706 ''' 

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

708 

709 

710def _req_d_by(where, **name): 

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

712 ''' 

713 m = _MODS.named 

714 n = m._name__(**name) 

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

716 if n: 

717 m = _DOT_(m, n) 

718 return _SPACE_(_required_, _by_, m) 

719 

720 

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

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

723 ''' 

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

725 

726 

727try: 

728 from math import signbit as signBit # 3.15+ 

729except ImportError: 

730 

731 def signBit(x): 

732 '''Return C{signbit(B{x})}, like C++, see also L{isneg}. 

733 

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

735 ''' 

736 return (x or _copysign(1, x)) < 0 

737 

738 

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

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

741 ''' 

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

743 

744 

745def signOf(x): 

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

747 

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

749 ''' 

750 try: 

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

752 except AttributeError: 

753 s = _signOf(x, 0) 

754 return s 

755 

756 

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

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

759 

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

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

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

763 

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

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

766 

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

768 

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

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

771 

772 @example: 

773 

774 >>> from pygeodesy import splice 

775 

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

777 >>> a, b 

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

779 

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

781 >>> a, b, c 

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

783 

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

785 >>> a, b, c 

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

787 

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

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

790 

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

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

793 ''' 

794 if not isint(n): 

795 raise _TypeError(n=n) 

796 

797 t = _xiterablen(iterable) 

798 if not isinstance(t, _list_tuple_types): 

799 t = tuple(t) 

800 

801 if n > 1: 

802 if fill: 

803 fill = _xkwds_get1(fill, fill=MISSING) 

804 if fill is not MISSING: 

805 m = len(t) % n 

806 if m > 0: # same type fill 

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

808 for i in range(n): 

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

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

811 else: 

812 yield t # 1 slice, all 

813 

814 

815def _splituple(strs, *sep_splits): # in .mgrs, ... 

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

817 string into a C{tuple} of stripped C{str}ings. 

818 ''' 

819 if sep_splits: 

820 t = (t.strip() for t in strs.split(*sep_splits)) 

821 else: 

822 t = strs.strip() 

823 if t: 

824 t = t.replace(_COMMA_, _SPACE_).split() 

825 return tuple(t) if t else () 

826 

827 

828def unsigned0(x): 

829 '''Unsign if C{0.0}. 

830 

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

832 ''' 

833 return x if x else _0_0 

834 

835 

836def _xcopy(obj, deep=False): 

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

838 

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

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

841 a shallow copy (C{bool}). 

842 

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

844 ''' 

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

846 

847 

848def _xcoverage(where, *required): # in .__main__ # PYCHOK no cover 

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

850 ''' 

851 try: 

852 _xpackages(_xcoverage) 

853 import coverage 

854 except ImportError as x: 

855 raise _xImportError(x, where) 

856 return _xversion(coverage, where, *required) 

857 

858 

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

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

861 

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

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

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

865 

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

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

868 

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

870 ''' 

871 d = _xcopy(obj, deep=deep) 

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

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

874 setattr(d, n, v) 

875 elif not hasattr(d, n): 

876 t = _MODS.named.classname(obj) 

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

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

879# if items: 

880# _MODS.props._update_all(d) 

881 return d 

882 

883 

884def _xgeographiclib(where, *required): 

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

886 ''' 

887 try: 

888 _xpackages(_xgeographiclib) 

889 import geographiclib 

890 except ImportError as x: 

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

892 return _xversion(geographiclib, where, *required) 

893 

894 

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

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

897 ''' 

898 t = _req_d_by(where, **name) 

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

900 

901 

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

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

904 

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

906 positional. 

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

908 with the C{value} to be checked. 

909 

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

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

912 ''' 

913 if not (Types and names_values): 

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

915 

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

917 if not isinstance(v, Types): 

918 raise _TypesError(n, v, *Types) 

919 

920 

921def _xiterable(obj): 

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

923 ''' 

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

925 

926 

927def _xiterablen(obj): 

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

929 ''' 

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

931 

932 

933def _xiterror(obj, _xwhich): 

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

935 ''' 

936 t = typename(_xwhich)[2:] # less '_x' 

937 raise _TypeError(repr(obj), txt=_not_(t)) 

938 

939 

940def _xnumpy(where, *required): 

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

942 ''' 

943 try: 

944 _xpackages(_xnumpy) 

945 import numpy 

946 except ImportError as x: 

947 raise _xImportError(x, where) 

948 return _xversion(numpy, where, *required) 

949 

950 

951def _xor(x, *xs): 

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

953 ''' 

954 for x_ in xs: 

955 x ^= x_ 

956 return x 

957 

958 

959def _xpackages(_xhich): 

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

961 ''' 

962 if _XPACKAGES: # PYCHOK no cover 

963 n = typename(_xhich)[2:] # less '_x' 

964 if n.lower() in _XPACKAGES: 

965 E = _PYGEODESY_ENV(_xpackages_) 

966 x = _SPACE_(n, _in_, E) 

967 e = _enquote(_getenv(E, NN)) 

968 raise ImportError(_EQUAL_(x, e)) 

969 

970 

971def _xscalar(**names_values): 

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

973 ''' 

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

975 if not isscalar(v): 

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

977 

978 

979def _xscipy(where, *required): 

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

981 ''' 

982 try: 

983 _xpackages(_xscipy) 

984 import scipy 

985 except ImportError as x: 

986 raise _xImportError(x, where) 

987 return _xversion(scipy, where, *required) 

988 

989 

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

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

992 

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

994 positional. 

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

996 with the C{value} to be checked. 

997 

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

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

1000 ''' 

1001 if not (Classes and names_values): 

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

1003 

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

1005 if not issubclassof(v, *Classes): 

1006 raise _TypesError(n, v, *Classes) 

1007 

1008 

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

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

1011 ''' 

1012 if required: 

1013 t = _version_info(package) 

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

1015 t = _SPACE_(typename(package), 

1016 _version_, _DOT_(*t), 

1017 _below_, _DOT_(*required), 

1018 _req_d_by(where, **name)) 

1019 raise ImportError(t) 

1020 return package 

1021 

1022 

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

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

1025 ''' 

1026 s = _xkwds_get1(strict, strict=True) 

1027 if s: 

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

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

1030 raise _NotImplementedError(t, txt=None) 

1031 return _zip(*args) 

1032 return zip(*args) 

1033 

1034 

1035if _MODS.sys_version_info2 < (3, 10): # see .errors 

1036 _zip = zip # PYCHOK exported 

1037else: # Python 3.10+ 

1038 

1039 def _zip(*args): 

1040 return zip(*args, strict=True) 

1041 

1042_xpackages_ = typename(_xpackages).lstrip(_UNDER_) 

1043_XPACKAGES = _splituple(_envPYGEODESY(_xpackages_).lower()) # test/bases._X_OK 

1044 

1045# **) MIT License 

1046# 

1047# Copyright (C) 2016-2026 -- mrJean1 at Gmail -- All Rights Reserved. 

1048# 

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

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

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

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

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

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

1055# 

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

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

1058# 

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

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

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

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

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

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

1065# OTHER DEALINGS IN THE SOFTWARE.