Coverage for pygeodesy/basics.py: 83%
287 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-09-25 17:40 -0400
« prev ^ index » next coverage.py v7.6.0, created at 2024-09-25 17:40 -0400
2# -*- coding: utf-8 -*-
4u'''Some, basic definitions, functions and dependencies.
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
17# from pygeodesy.cartesianBase import CartesianBase # _MODS
18# from pygeodesy.constants import isneg0, NEG0 # _MODS
19from pygeodesy.errors import _AttributeError, _ImportError, _NotImplementedError, \
20 _TypeError, _TypesError, _ValueError, _xAssertionError, \
21 _xkwds_get1
22# from pygeodesy.fsums import _isFsumTuple # _MODS
23from pygeodesy.internals import _0_0, _enquote, _passarg, _version_info
24from 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
28from 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
36from copy import copy as _copy, deepcopy as _deepcopy
37from math import copysign as _copysign
38import inspect as _inspect
40__all__ = _ALL_LAZY.basics
41__version__ = '24.09.12'
43_below_ = 'below'
44_list_tuple_types = (list, tuple)
45_PYGEODESY_XPACKAGES_ = 'PYGEODESY_XPACKAGES'
46_required_ = 'required'
48try: # Luciano Ramalho, "Fluent Python", O'Reilly, 2016 p. 395, 2022 p. 577+
49 from numbers import Integral as _Ints, Real as _Scalars # .units
50except 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
57try:
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
67except ImportError:
68 _Sequence = tuple # immutable for .points._Basequence
69 _Seqs = list, _Sequence # range for function len2 below
71try:
72 _Bytes = unicode, bytearray # PYCHOK in .internals
73 _Strs = basestring, str # XXX str == bytes
74 str2ub = ub2str = _passarg # avoids UnicodeDecodeError
76 def _Xstr(exc): # PYCHOK no cover
77 '''I{Invoke only with caught ImportError} B{C{exc}}.
79 C{... "can't import name _distributor_init" ...}
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
94except NameError: # Python 3+
95 from pygeodesy.interns import _utf_8_
97 _Bytes = bytes, bytearray # in .internals
98 _Strs = str, # tuple
99 _Xstr = str
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
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
116def _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.
120 @kwarg exelf: If C{True}, exclude C{self} in the C{args}
121 of a method (C{bool}).
122 '''
123 try:
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
140def _args_kwds_names(func, splast=False):
141 '''(INTERNAL) Get a C{func}'s args and kwds names, including
142 C{self} for methods.
144 @kwarg splast: If C{True}, split the last keyword argument
145 at UNDERscores (C{bool}).
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:
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)
164def clips(sb, limit=50, white=NN, length=False):
165 '''Clip a string to the given length limit.
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}).
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
186def copysign0(x, y):
187 '''Like C{math.copysign(x, y)} except C{zero}, I{unsigned}.
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)
195def copytype(x, y):
196 '''Return the value of B{x} as C{type} of C{y}.
198 @return: C{type(B{y})(B{x})}.
199 '''
200 return type(y)(x if x else _0_0)
203def _enumereverse(iterable):
204 '''(INTERNAL) Reversed C{enumberate}.
205 '''
206 for j in _reverange(len(iterable)):
207 yield j, iterable[j]
210def halfs2(str2):
211 '''Split a string in 2 halfs.
213 @arg str2: String to split (C{str}).
215 @return: 2-Tuple C{(_1st, _2nd)} half (C{str}).
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:]
225def int1s(x):
226 '''Count the number of 1-bits in an C{int}, I{unsigned}.
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_)
237def isbool(obj):
238 '''Is B{C{obj}}ect a C{bool}ean?
240 @arg obj: The object (any C{type}).
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)
247assert not (isbool(1) or isbool(0) or isbool(None)) # PYCHOK 2
250def isCartesian(obj, ellipsoidal=None):
251 '''Is B{C{obj}}ect some C{Cartesian}?
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}.
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)
269if _FOR_DOCS: # XXX avoid epydoc Python 2.7 error
271 def isclass(obj):
272 '''Is B{C{obj}}ect a C{Class} or C{type}?
273 '''
274 return _inspect.isclass(obj)
275else:
276 isclass = _inspect.isclass
279def iscomplex(obj, both=False):
280 '''Is B{C{obj}}ect a C{complex} or complex literal C{str}?
282 @arg obj: The object (any C{type}).
283 @kwarg both: If C{True}, check complex C{str} (C{bool}).
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
294def isDEPRECATED(obj):
295 '''Is B{C{obj}}ect a C{DEPRECATED} class, method or function?
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
307def isfloat(obj, both=False):
308 '''Is B{C{obj}}ect a C{float} or float literal C{str}?
310 @arg obj: The object (any C{type}).
311 @kwarg both: If C{True}, check float C{str} (C{bool}).
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
322try:
323 isidentifier = str.isidentifier # Python 3, must be str
324except AttributeError: # Python 2-
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())
334def isinstanceof(obj, *Classes):
335 '''Is B{C{obj}}ect an instance of one of the C{Classes}?
337 @arg obj: The object (any C{type}).
338 @arg Classes: One or more classes (C{Class}).
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
346def isint(obj, both=False):
347 '''Is B{C{obj}}ect an C{int} or integer C{float} value?
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}).
353 @return: C{True} if C{int} or I{integer} C{float}
354 or L{Fsum}, C{False} otherwise.
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
369def isiterable(obj):
370 '''Is B{C{obj}}ect C{iterable}?
372 @arg obj: The object (any C{type}).
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
380def isiterablen(obj):
381 '''Is B{C{obj}}ect C{iterable} and has C{len}gth?
383 @arg obj: The object (any C{type}).
385 @return: C{True} if C{iterable} with C{len}gth, C{False} otherwise.
386 '''
387 return hasattr(obj, '__len__') and hasattr(obj, '__getitem__')
390try:
391 from keyword import iskeyword # Python 2.7+
392except ImportError:
394 def iskeyword(unused):
395 '''Not Implemented, C{False} always.
396 '''
397 return False
400def isLatLon(obj, ellipsoidal=None):
401 '''Is B{C{obj}}ect some C{LatLon}?
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}.
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)
419def islistuple(obj, minum=0):
420 '''Is B{C{obj}}ect a C{list} or C{tuple} with non-zero length?
422 @arg obj: The object (any C{type}).
423 @kwarg minum: Minimal C{len} required C({int}).
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
431def isNvector(obj, ellipsoidal=None):
432 '''Is B{C{obj}}ect some C{Nvector}?
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}.
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)
450def isodd(x):
451 '''Is B{C{x}} odd?
453 @arg x: Value (C{scalar}).
455 @return: C{True} if odd, C{False} otherwise.
456 '''
457 return bool(int(x) & 1) # == bool(int(x) % 2)
460def isscalar(obj, both=False):
461 '''Is B{C{obj}}ect an C{int} or integer C{float} value?
463 @arg obj: The object (any C{type}).
464 @kwarg both: If C{True}, check L{Fsum} and L{Fsum2Tuple}
465 residuals.
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._isFsumTuple(obj):
473 return bool(obj.residual == 0)
474 return False
477def issequence(obj, *excls):
478 '''Is B{C{obj}}ect some sequence type?
480 @arg obj: The object (any C{type}).
481 @arg excls: Classes to exclude (C{type}), all positional.
483 @note: Excluding C{tuple} implies excluding C{namedtuple}.
485 @return: C{True} if a sequence, C{False} otherwise.
486 '''
487 return isinstance(obj, _Seqs) and not (excls and isinstance(obj, excls))
490def isstr(obj):
491 '''Is B{C{obj}}ect some string type?
493 @arg obj: The object (any C{type}).
495 @return: C{True} if a C{str}, C{bytes}, ...,
496 C{False} otherwise.
497 '''
498 return isinstance(obj, _Strs)
501def issubclassof(Sub, *Supers):
502 '''Is B{C{Sub}} a class and sub-class of some other class(es)?
504 @arg Sub: The sub-class (C{Class}).
505 @arg Supers: One or more C(super) classes (C{Class}).
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
518def 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.
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()
532 def _reverse_key(asorted=True, reverse=False):
533 return dict(reverse=reverse, key=_ins if asorted else None)
535 items = adict.items(*items_args) if items_args else adict.items()
536 return sorted(items, **_reverse_key(**asorted_reverse))
539def len2(items):
540 '''Make built-in function L{len} work for generators, iterators,
541 etc. since those can only be started exactly once.
543 @arg items: Generator, iterator, list, range, tuple, etc.
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
553def map1(fun1, *xs): # XXX map_
554 '''Call a single-argument function to each B{C{xs}}
555 and return a C{tuple} of results.
557 @arg fun1: 1-Arg function (C{callable}).
558 @arg xs: Arguments (C{any positional}).
560 @return: Function results (C{tuple}).
561 '''
562 return tuple(map(fun1, xs))
565def map2(fun, *xs):
566 '''Like Python's B{C{map}} but returning a C{tuple} of results.
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.
573 @arg fun: Function (C{callable}).
574 @arg xs: Arguments (C{all positional}).
576 @return: Function results (C{tuple}).
577 '''
578 return tuple(map(fun, *xs))
581def neg(x, neg0=None):
582 '''Negate C{x} and optionally, negate C{0.0} and C{-0.0}.
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}).
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
598def neg_(*xs):
599 '''Negate all C{xs} with L{neg}.
601 @return: A C{map(neg, B{xs})}.
602 '''
603 return map(neg, xs)
606def _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
613def _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)
624def _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)
630def signBit(x):
631 '''Return C{signbit(B{x})}, like C++.
633 @return: C{True} if C{B{x} < 0} or C{NEG0} (C{bool}).
634 '''
635 return x < 0 or _MODS.constants.isneg0(x)
638def _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)
644def signOf(x):
645 '''Return sign of C{x} as C{int}.
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
656def splice(iterable, n=2, **fill):
657 '''Split an iterable into C{n} slices.
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.
663 @return: A generator for each of B{C{n}} slices,
664 M{iterable[i::n] for i=0..n}.
666 @raise TypeError: Invalid B{C{n}}.
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}.
671 @example:
673 >>> from pygeodesy import splice
675 >>> a, b = splice(range(10))
676 >>> a, b
677 ((0, 2, 4, 6, 8), (1, 3, 5, 7, 9))
679 >>> a, b, c = splice(range(10), n=3)
680 >>> a, b, c
681 ((0, 3, 6, 9), (1, 4, 7), (2, 5, 8))
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))
687 >>> tuple(splice(list(range(9)), n=5))
688 ([0, 5], [1, 6], [2, 7], [3, 8], [4])
690 >>> splice(range(9), n=1)
691 <generator object splice at 0x0...>
692 '''
693 if not isint(n):
694 raise _TypeError(n=n)
696 t = _xiterablen(iterable)
697 if not isinstance(t, _list_tuple_types):
698 t = tuple(t)
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
714def _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)
723def unsigned0(x):
724 '''Unsign if C{0.0}.
726 @return: C{B{x}} if B{C{x}} else C{0.0}.
727 '''
728 return x if x else _0_0
731def _xcopy(obj, deep=False):
732 '''(INTERNAL) Copy an object, shallow or deep.
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}).
738 @return: The copy of B{C{obj}}.
739 '''
740 return _deepcopy(obj) if deep else _copy(obj)
743def _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)
754def _xdup(obj, deep=False, **items):
755 '''(INTERNAL) Duplicate an object, replacing some attributes.
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}).
761 @return: A duplicate of B{C{obj}} with modified
762 attributes, if any B{C{items}}.
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
779def _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)
790def _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)
797def _xinstanceof(*Types, **names_values):
798 '''(INTERNAL) Check C{Types} of all C{name=value} pairs.
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.
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)
811 for n, v in names_values.items():
812 if not isinstance(v, Types):
813 raise _TypesError(n, v, *Types)
816def _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
822def _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
828def _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)
835def _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)
846def _xor(x, *xs):
847 '''(INTERNAL) Exclusive-or C{x} and C{xs}.
848 '''
849 for x_ in xs:
850 x ^= x_
851 return x
854def _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))
864def _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_)
872def _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)
883def _xsubclassof(*Classes, **names_values):
884 '''(INTERNAL) Check (super) class of all C{name=value} pairs.
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.
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)
897 for n, v in names_values.items():
898 if not issubclassof(v, *Classes):
899 raise _TypesError(n, v, *Classes)
902def _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
916def _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)
928if _sys_version_info2 < (3, 10): # see .errors
929 _zip = zip # PYCHOK exported
930else: # Python 3.10+
932 def _zip(*args):
933 return zip(*args, strict=True)
935_XPACKAGES = _splituple(_getenv(_PYGEODESY_XPACKAGES_, NN).lower())
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.