Coverage for pygeodesy/basics.py: 95%

246 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-12-29 12:35 -0500

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 

17from pygeodesy.errors import _AssertionError, _AttributeError, _ImportError, \ 

18 _TypeError, _TypesError, _ValueError, _xkwds_get 

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

20 _ELLIPSIS4_, _enquote, _EQUAL_, _in_, _invalid_, _N_A_, \ 

21 _SPACE_, _UNDER_, _version_ # _utf_8_ 

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

23 _getenv, LazyImportError, _sys, _sys_version_info2 

24 

25from copy import copy as _copy, deepcopy as _deepcopy 

26from math import copysign as _copysign 

27import inspect as _inspect 

28 

29__all__ = _ALL_LAZY.basics 

30__version__ = '23.12.10' 

31 

32_0_0 = 0.0 # in .constants 

33_below_ = 'below' 

34_can_t_ = "can't" 

35_list_tuple_types = (list, tuple) 

36_list_tuple_set_types = (list, tuple, set) 

37_odd_ = 'odd' 

38_required_ = 'required' 

39_PYGEODESY_XPACKAGES_ = 'PYGEODESY_XPACKAGES' 

40 

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

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

43except 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 

50try: 

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 

60except ImportError: 

61 _Sequence = tuple # immutable for .points._Basequence 

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

63 

64 

65def _passarg(arg): # in .auxilats.auxLat 

66 '''(INTERNAL) Helper, no-op. 

67 ''' 

68 return arg 

69 

70 

71def _passargs(*args): # in .utily 

72 '''(INTERNAL) Helper, no-op. 

73 ''' 

74 return args 

75 

76 

77try: 

78 _Bytes = unicode, bytearray # PYCHOK expected 

79 _Strs = basestring, str # XXX , bytes 

80 str2ub = ub2str = _passarg # avoids UnicodeDecodeError 

81 

82 def _Xstr(exc): # PYCHOK no cover 

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

84 

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

86 

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

88 on arm64 Apple Silicon running macOS' Python 2.7.16? 

89 ''' 

90 t = str(exc) 

91 if '_distributor_init' in t: 

92 from sys import exc_info 

93 from traceback import extract_tb 

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

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

96 t = _SPACE_(_can_t_, t4[3] or _N_A_) 

97 del tb, t4 

98 return t 

99 

100except NameError: # Python 3+ 

101 from pygeodesy.interns import _utf_8_ 

102 

103 _Bytes = bytes, bytearray 

104 _Strs = str, # tuple 

105 _Xstr = str 

106 

107 def str2ub(sb): 

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

109 ''' 

110 if isinstance(sb, _Strs): 

111 sb = sb.encode(_utf_8_) 

112 return sb 

113 

114 def ub2str(ub): 

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

116 ''' 

117 if isinstance(ub, _Bytes): 

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

119 return ub 

120 

121 

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

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

124 

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

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

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

128 

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

130 ''' 

131 T = type(sb) 

132 if len(sb) > limit > 8: 

133 h = limit // 2 

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

135 if white: # replace whitespace 

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

137 return sb 

138 

139 

140def copysign0(x, y): 

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

142 

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

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

145 ''' 

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

147 

148 

149def copytype(x, y): 

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

151 

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

153 ''' 

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

155 

156 

157def halfs2(str2): 

158 '''Split a string in 2 halfs. 

159 

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

161 

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

163 

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

165 ''' 

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

167 if r or not h: 

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

169 return str2[:h], str2[h:] 

170 

171 

172def int1s(x): 

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

174 

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

176 ''' 

177 try: 

178 return x.bit_count() # Python 3.10+ 

179 except AttributeError: 

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

181 return bin(x).count(_1_) 

182 

183 

184def isbool(obj): 

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

186 

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

188 

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

190 C{False} otherwise. 

191 ''' 

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

193# or obj is True) 

194 

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

196 raise _AssertionError(isbool=1) 

197 

198if _FOR_DOCS: # XXX avoid epidoc Python 2.7 error 

199 

200 def isclass(obj): 

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

202 

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

204 ''' 

205 return _inspect.isclass(obj) 

206else: 

207 isclass = _inspect.isclass 

208 

209 

210def isCartesian(obj, ellipsoidal=None): 

211 '''Is B{C{obj}} some C{Cartesian}? 

212 

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

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

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

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

217 

218 @return: C{type(B{obj}} if B{C{obj}} is a C{Cartesian} instance of 

219 the required type, C{False} if a C{Cartesian} of an other 

220 type or C{None} otherwise. 

221 ''' 

222 if ellipsoidal is not None: 

223 try: 

224 return obj.ellipsoidalCartesian if ellipsoidal else obj.sphericalCartesian 

225 except AttributeError: 

226 return None 

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

228 

229 

230def iscomplex(obj): 

231 '''Check whether an object is a C{complex} or complex C{str}. 

232 

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

234 

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

236 C{False}. 

237 ''' 

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

239 return isinstance(obj, complex) or (isstr(obj) 

240 and isinstance(complex(obj), complex)) # numbers.Complex? 

241 except (TypeError, ValueError): 

242 return False 

243 

244 

245def isDEPRECATED(obj): 

246 '''Return C{True} if C{B{obj}} is a C{DEPRECATED} class, method 

247 or function, C{False} if not or C{None} if undetermined. 

248 ''' 

249 try: # XXX inspect.getdoc(obj) 

250 return bool(obj.__doc__.lstrip().startswith(_DEPRECATED_)) 

251 except AttributeError: 

252 return None 

253 

254 

255def isfloat(obj): 

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

257 

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

259 

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

261 C{False}. 

262 ''' 

263 try: 

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

265 and isinstance(float(obj), float)) 

266 except (TypeError, ValueError): 

267 return False 

268 

269 

270try: 

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

272except AttributeError: # Python 2- 

273 

274 def isidentifier(obj): 

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

276 ''' 

277 return bool(obj and isstr(obj) 

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

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

280 

281 

282def isinstanceof(obj, *classes): 

283 '''Is B{C{ob}} an intance of one of the C{classes}? 

284 

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

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

287 

288 @return: C{type(B{obj}} if B{C{obj}} is an instance 

289 of the B{C{classes}}, C{None} otherwise. 

290 ''' 

291 return type(obj) if isinstance(obj, classes) else None 

292 

293 

294def isint(obj, both=False): 

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

296 

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

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

299 type and value (C{bool}). 

300 

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

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

303 

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

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

306 ''' 

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

308 return True 

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

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

311 return obj.is_integer() 

312 except AttributeError: 

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

314 return False 

315 

316 

317try: 

318 from keyword import iskeyword # Python 2.7+ 

319except ImportError: 

320 

321 def iskeyword(unused): 

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

323 ''' 

324 return False 

325 

326 

327def isLatLon(obj, ellipsoidal=None): 

328 '''Is B{C{obj}} some C{LatLon}? 

329 

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

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

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

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

334 

335 @return: C{type(B{obj}} if B{C{obj}} is a C{LatLon} instance of 

336 the required type, C{False} if a C{LatLon} of an other 

337 type or {None} otherwise. 

338 ''' 

339 if ellipsoidal is not None: 

340 try: 

341 return obj.ellipsoidalLatLon if ellipsoidal else obj.sphericalLatLon 

342 except AttributeError: 

343 return None 

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

345 

346 

347def islistuple(obj, minum=0): 

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

349 

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

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

352 

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

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

355 ''' 

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

357 

358 

359def isNvector(obj, ellipsoidal=None): 

360 '''Is B{C{obj}} some C{Nvector}? 

361 

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

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

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

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

366 

367 @return: C{type(B{obj}} if B{C{obj}} is an C{Nvector} instance of 

368 the required type, C{False} if an C{Nvector} of an other 

369 type or {None} otherwise. 

370 ''' 

371 if ellipsoidal is not None: 

372 try: 

373 return obj.ellipsoidalNvector if ellipsoidal else obj.sphericalNvector 

374 except AttributeError: 

375 return None 

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

377 

378 

379def isodd(x): 

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

381 

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

383 

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

385 C{False} otherwise. 

386 ''' 

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

388 

389 

390def isscalar(obj): 

391 '''Check for scalar types. 

392 

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

394 

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

396 ''' 

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

398 

399 

400def issequence(obj, *excls): 

401 '''Check for sequence types. 

402 

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

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

405 

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

407 

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

409 ''' 

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

411 

412 

413def isstr(obj): 

414 '''Check for string types. 

415 

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

417 

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

419 ''' 

420 return isinstance(obj, _Strs) 

421 

422 

423def issubclassof(Sub, *Supers): 

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

425 

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

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

428 

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

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

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

432 are a class. 

433 ''' 

434 if isclass(Sub): 

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

436 if t: 

437 return bool(issubclass(Sub, t)) 

438 return None 

439 

440 

441def len2(items): 

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

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

444 

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

446 

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

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

449 ''' 

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

451 items = list(items) 

452 return len(items), items 

453 

454 

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

456 '''Apply each B{C{xs}} to a single-argument function and 

457 return a C{tuple} of results. 

458 

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

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

461 

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

463 ''' 

464 return tuple(map(fun1, xs)) 

465 

466 

467def map2(func, *xs): 

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

469 

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

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

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

473 maintains the Python 2 behavior. 

474 

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

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

477 

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

479 ''' 

480 return tuple(map(func, *xs)) 

481 

482 

483def neg(x, neg0=None): 

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

485 

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

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

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

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

490 

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

492 ''' 

493 return (-x) if x else (_0_0 if neg0 is None else (x if not neg0 else 

494 (_0_0 if signBit(x) else _MODS.constants.NEG0))) 

495 

496 

497def neg_(*xs): 

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

499 

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

501 ''' 

502 return map(neg, xs) 

503 

504 

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

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

507 ''' 

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

509 

510 

511def signBit(x): 

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

513 

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

515 ''' 

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

517 

518 

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

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

521 ''' 

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

523 

524 

525def signOf(x): 

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

527 

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

529 ''' 

530 try: 

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

532 except AttributeError: 

533 s = _signOf(x, 0) 

534 return s 

535 

536 

537def _sizeof(inst): 

538 '''(INTERNAL) Recursively size an C{inst}ance. 

539 

540 @return: Instance' size in bytes (C{int}), 

541 ignoring class attributes and 

542 counting duplicates only once or 

543 C{None}. 

544 

545 @note: With C{PyPy}, the size is always C{None}. 

546 ''' 

547 try: 

548 _zB = _sys.getsizeof 

549 _zD = _zB(None) # get some default 

550 except TypeError: # PyPy3.10 

551 return None 

552 

553 def _zR(s, iterable): 

554 z, _s = 0, s.add 

555 for o in iterable: 

556 i = id(o) 

557 if i not in s: 

558 _s(i) 

559 z += _zB(o, _zD) 

560 if isinstance(o, dict): 

561 z += _zR(s, o.keys()) 

562 z += _zR(s, o.values()) 

563 elif isinstance(o, _list_tuple_set_types): 

564 z += _zR(s, o) 

565 else: 

566 try: # size instance' attr values only 

567 z += _zR(s, o.__dict__.values()) 

568 except AttributeError: # None, int, etc. 

569 pass 

570 return z 

571 

572 return _zR(set(), (inst,)) 

573 

574 

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

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

577 

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

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

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

581 

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

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

584 

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

586 

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

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

589 

590 @example: 

591 

592 >>> from pygeodesy import splice 

593 

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

595 >>> a, b 

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

597 

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

599 >>> a, b, c 

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

601 

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

603 >>> a, b, c 

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

605 

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

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

608 

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

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

611 ''' 

612 if not isint(n): 

613 raise _TypeError(n=n) 

614 

615 t = iterable 

616 if not isinstance(t, _list_tuple_types): 

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

618 

619 if n > 1: 

620 if fill: 

621 fill = _xkwds_get(fill, fill=MISSING) 

622 if fill is not MISSING: 

623 m = len(t) % n 

624 if m > 0: # same type fill 

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

626 for i in range(n): 

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

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

629 else: 

630 yield t 

631 

632 

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

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

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

636 ''' 

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

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

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

640 

641 

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

643 

644 

645def unsigned0(x): 

646 '''Unsign if C{0.0}. 

647 

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

649 ''' 

650 return x if x else _0_0 

651 

652 

653def _xargs_kwds_names(func): 

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

655 C{self} for methods. 

656 

657 @note: Python 2 does I{not} include the C{*args} nor the 

658 C{**kwds} names. 

659 ''' 

660 try: 

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

662 except AttributeError: # .signature new Python 3+ 

663 args_kwds = _inspect.getargspec(func).args 

664 return tuple(args_kwds) 

665 

666 

667def _xcopy(inst, deep=False): 

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

669 

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

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

672 a shallow copy (C{bool}). 

673 

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

675 ''' 

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

677 

678 

679def _xdup(inst, deep=False, **items): 

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

681 

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

683 @kwarg deep: If C{True} copy deep, otherwise shallow. 

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

685 

686 @return: A duplicate of B{C{inst}} with modified 

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

688 

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

690 ''' 

691 d = _xcopy(inst, deep=deep) 

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

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

694 setattr(d, n, v) 

695 elif not hasattr(d, n): 

696 t = _MODS.named.classname(inst) 

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

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

699 return d 

700 

701 

702def _xgeographiclib(where, *required): 

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

704 ''' 

705 try: 

706 _xpackage(_xgeographiclib) 

707 import geographiclib 

708 except ImportError as x: 

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

710 return _xversion(geographiclib, where, *required) 

711 

712 

713def _xImportError(x, where, Error=_ImportError, **name): 

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

715 ''' 

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

717 return Error(_Xstr(x), txt=t, cause=x) 

718 

719 

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

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

722 

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

724 all positional. 

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

726 with the C{value} to be checked. 

727 

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

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

730 ''' 

731 if Types and name_value_pairs: 

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

733 if not isinstance(v, Types): 

734 raise _TypesError(n, v, *Types) 

735 else: 

736 raise _AssertionError(Types=Types, name_value_pairs=name_value_pairs) 

737 

738 

739def _xnumpy(where, *required): 

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

741 ''' 

742 try: 

743 _xpackage(_xnumpy) 

744 import numpy 

745 except ImportError as x: 

746 raise _xImportError(x, where) 

747 return _xversion(numpy, where, *required) 

748 

749 

750def _xpackage(_xpkg): 

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

752 ''' 

753 n = _xpkg.__name__[2:] 

754 if n in _XPACKAGES: 

755 x = _SPACE_(n, _in_, _PYGEODESY_XPACKAGES_) 

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

757 raise ImportError(_EQUAL_(x, e)) 

758 

759 

760def _xor(x, *xs): 

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

762 ''' 

763 for x_ in xs: 

764 x ^= x_ 

765 return x 

766 

767 

768def _xscipy(where, *required): 

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

770 ''' 

771 try: 

772 _xpackage(_xscipy) 

773 import scipy 

774 except ImportError as x: 

775 raise _xImportError(x, where) 

776 return _xversion(scipy, where, *required) 

777 

778 

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

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

781 

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

783 all positional. 

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

785 with the C{value} to be checked. 

786 

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

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

789 ''' 

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

791 if not issubclassof(v, *Classes): 

792 raise _TypesError(n, v, *Classes) 

793 

794 

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

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

797 ''' 

798 n = len(required) 

799 if n: 

800 t = _xversion_info(package) 

801 if t[:n] < required: 

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

803 _below_, _DOT_(*required), 

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

805 raise ImportError(t) 

806 return package 

807 

808 

809def _xversion_info(package): # in .karney 

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

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

812 ''' 

813 try: 

814 t = package.__version_info__ 

815 except AttributeError: 

816 t = package.__version__.strip() 

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

818 return map2(int, t) 

819 

820 

821def _xwhere(where, **name): 

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

823 ''' 

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

825 if name: 

826 n = _xkwds_get(name, name=NN) 

827 if n: 

828 m = _DOT_(m, n) 

829 return m 

830 

831 

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

833 _zip = zip # PYCHOK exported 

834else: # Python 3.10+ 

835 

836 def _zip(*args): 

837 return zip(*args, strict=True) 

838 

839# **) MIT License 

840# 

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

842# 

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

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

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

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

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

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

849# 

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

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

852# 

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

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

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

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

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

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

859# OTHER DEALINGS IN THE SOFTWARE.