Coverage for pygeodesy/basics.py: 91%
241 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-11-12 13:23 -0500
« prev ^ index » next coverage.py v7.2.2, created at 2023-11-12 13:23 -0500
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
17from pygeodesy.errors import _AssertionError, _AttributeError, _ImportError, \
18 _TypeError, _TypesError, _ValueError, _xkwds_get
19from pygeodesy.interns import MISSING, NN, _1_, _by_, _COMMA_, _DOT_, _ELLIPSIS4_, \
20 _enquote, _EQUAL_, _in_, _invalid_, _N_A_, _SPACE_, \
21 _UNDER_, _version_ # _utf_8_
22from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _FOR_DOCS, \
23 _getenv, _sys, _sys_version_info2
25from copy import copy as _copy, deepcopy as _deepcopy
26from math import copysign as _copysign
27import inspect as _inspect
29__all__ = _ALL_LAZY.basics
30__version__ = '23.10.15'
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'
41try: # Luciano Ramalho, "Fluent Python", O'Reilly, 2016 p. 395, 2022 p. 577+
42 from numbers import Integral as _Ints, Real as _Scalars
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,)
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
64try:
65 _Bytes = unicode, bytearray # PYCHOK expected
66 _Strs = basestring, str # XXX , bytes
68 def _pass(x): # == .utily._passarg
69 '''Pass thru, no-op'''
70 return x
72 str2ub = ub2str = _pass # avoids UnicodeDecodeError
74 def _Xstr(exc): # PYCHOK no cover
75 '''I{Invoke only with caught ImportError} B{C{exc}}.
77 C{... "can't import name _distributor_init" ...}
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
92except NameError: # Python 3+
93 from pygeodesy.interns import _utf_8_
95 _Bytes = bytes, bytearray
96 _Strs = str, # tuple
97 _Xstr = str
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
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
114def clips(sb, limit=50, white=NN):
115 '''Clip a string to the given length limit.
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}).
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
132def copysign0(x, y):
133 '''Like C{math.copysign(x, y)} except C{zero}, I{unsigned}.
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)
141def copytype(x, y):
142 '''Return the value of B{x} as C{type} of C{y}.
144 @return: C{type(B{y})(B{x})}.
145 '''
146 return type(y)(x if x else _0_0)
149def halfs2(str2):
150 '''Split a string in 2 halfs.
152 @arg str2: String to split (C{str}).
154 @return: 2-Tuple C{(_1st, _2nd)} half (C{str}).
156 @raise ValueError: Zero or odd C{len(B{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:]
164def int1s(x):
165 '''Count the number of 1-bits in an C{int}, I{unsigned}.
167 @note: C{int1s(-B{x}) == int1s(abs(B{x}))}.
168 '''
169 try:
170 return x.bit_count() # Python 3.10+
171 except AttributeError:
172 # bin(-x) = '-' + bin(abs(x))
173 return bin(x).count(_1_)
176def isbool(obj):
177 '''Check whether an object is C{bool}ean.
179 @arg obj: The object (any C{type}).
181 @return: C{True} if B{C{obj}} is C{bool}ean,
182 C{False} otherwise.
183 '''
184 return isinstance(obj, bool) # and (obj is False
185# or obj is True)
187if isbool(1) or isbool(0): # PYCHOK assert
188 raise _AssertionError(isbool=1)
190if _FOR_DOCS: # XXX avoid epidoc Python 2.7 error
192 def isclass(obj):
193 '''Return C{True} if B{C{obj}} is a C{class} or C{type}.
195 @see: Python's C{inspect.isclass}.
196 '''
197 return _inspect.isclass(obj)
198else:
199 isclass = _inspect.isclass
202def isCartesian(obj, ellipsoidal=None):
203 '''Is B{C{obj}} some C{Cartesian}?
205 @arg obj: The object (any C{type}).
206 @kwarg ellipsoidal: If C{None}, return the type of any C{Cartesian},
207 if C{True}, only an ellipsoidal C{Cartesian type}
208 or if C{False}, only a spherical C{Cartesian type}.
210 @return: C{type(B{obj}} if B{C{obj}} is a C{Cartesian} instance of
211 the required type, C{False} if a C{Cartesian} of an other
212 type or C{None} otherwise.
213 '''
214 if ellipsoidal is not None:
215 try:
216 return obj.ellipsoidalCartesian if ellipsoidal else obj.sphericalCartesian
217 except AttributeError:
218 return None
219 return isinstanceof(obj, _MODS.cartesianBase.CartesianBase)
222def iscomplex(obj):
223 '''Check whether an object is a C{complex} or complex C{str}.
225 @arg obj: The object (any C{type}).
227 @return: C{True} if B{C{obj}} is C{complex}, otherwise
228 C{False}.
229 '''
230 try: # hasattr('conjugate'), hasattr('real') and hasattr('imag')
231 return isinstance(obj, complex) or (isstr(obj)
232 and isinstance(complex(obj), complex)) # numbers.Complex?
233 except (TypeError, ValueError):
234 return False
237def isfloat(obj):
238 '''Check whether an object is a C{float} or float C{str}.
240 @arg obj: The object (any C{type}).
242 @return: C{True} if B{C{obj}} is a C{float}, otherwise
243 C{False}.
244 '''
245 try:
246 return isinstance( obj, float) or (isstr(obj)
247 and isinstance(float(obj), float))
248 except (TypeError, ValueError):
249 return False
252try:
253 isidentifier = str.isidentifier # Python 3, must be str
254except AttributeError: # Python 2-
256 def isidentifier(obj):
257 '''Return C{True} if B{C{obj}} is a Python identifier.
258 '''
259 return bool(obj and isstr(obj)
260 and obj.replace(_UNDER_, NN).isalnum()
261 and not obj[:1].isdigit())
264def isinstanceof(obj, *classes):
265 '''Is B{C{ob}} an intance of one of the C{classes}?
267 @arg obj: The instance (any C{type}).
268 @arg classes: One or more classes (C{class}).
270 @return: C{type(B{obj}} if B{C{obj}} is an instance
271 of the B{C{classes}}, C{None} otherwise.
272 '''
273 return type(obj) if isinstance(obj, classes) else None
276def isint(obj, both=False):
277 '''Check for C{int} type or an integer C{float} value.
279 @arg obj: The object (any C{type}).
280 @kwarg both: If C{true}, check C{float} and L{Fsum}
281 type and value (C{bool}).
283 @return: C{True} if B{C{obj}} is C{int} or I{integer}
284 C{float} or L{Fsum}, C{False} otherwise.
286 @note: Both C{isint(True)} and C{isint(False)} return
287 C{False} (and no longer C{True}).
288 '''
289 if isinstance(obj, _Ints) and not isbool(obj):
290 return True
291 elif both: # and isinstance(obj, (float, Fsum))
292 try: # NOT , _Scalars) to include Fsum!
293 return obj.is_integer()
294 except AttributeError:
295 pass # XXX float(int(obj)) == obj?
296 return False
299try:
300 from keyword import iskeyword # Python 2.7+
301except ImportError:
303 def iskeyword(unused):
304 '''Not Implemented, C{False} always.
305 '''
306 return False
309def isLatLon(obj, ellipsoidal=None):
310 '''Is B{C{obj}} some C{LatLon}?
312 @arg obj: The object (any C{type}).
313 @kwarg ellipsoidal: If C{None}, return the type of any C{LatLon},
314 if C{True}, only an ellipsoidal C{LatLon type}
315 or if C{False}, only a spherical C{LatLon type}.
317 @return: C{type(B{obj}} if B{C{obj}} is a C{LatLon} instance of
318 the required type, C{False} if a C{LatLon} of an other
319 type or {None} otherwise.
320 '''
321 if ellipsoidal is not None:
322 try:
323 return obj.ellipsoidalLatLon if ellipsoidal else obj.sphericalLatLon
324 except AttributeError:
325 return None
326 return isinstanceof(obj, _MODS.latlonBase.LatLonBase)
329def islistuple(obj, minum=0):
330 '''Check for list or tuple C{type} with a minumal length.
332 @arg obj: The object (any C{type}).
333 @kwarg minum: Minimal C{len} required C({int}).
335 @return: C{True} if B{C{obj}} is C{list} or C{tuple} with
336 C{len} at least B{C{minum}}, C{False} otherwise.
337 '''
338 return type(obj) in _list_tuple_types and len(obj) >= (minum or 0)
341def isNvector(obj, ellipsoidal=None):
342 '''Is B{C{obj}} some C{Nvector}?
344 @arg obj: The object (any C{type}).
345 @kwarg ellipsoidal: If C{None}, return the type of any C{Nvector},
346 if C{True}, only an ellipsoidal C{Nvector type}
347 or if C{False}, only a spherical C{Nvector type}.
349 @return: C{type(B{obj}} if B{C{obj}} is an C{Nvector} instance of
350 the required type, C{False} if an C{Nvector} of an other
351 type or {None} otherwise.
352 '''
353 if ellipsoidal is not None:
354 try:
355 return obj.ellipsoidalNvector if ellipsoidal else obj.sphericalNvector
356 except AttributeError:
357 return None
358 return isinstanceof(obj, _MODS.nvectorBase.NvectorBase)
361def isodd(x):
362 '''Is B{C{x}} odd?
364 @arg x: Value (C{scalar}).
366 @return: C{True} if B{C{x}} is odd,
367 C{False} otherwise.
368 '''
369 return bool(int(x) & 1) # == bool(int(x) % 2)
372def isscalar(obj):
373 '''Check for scalar types.
375 @arg obj: The object (any C{type}).
377 @return: C{True} if B{C{obj}} is C{scalar}, C{False} otherwise.
378 '''
379 return isinstance(obj, _Scalars) and not isbool(obj)
382def issequence(obj, *excls):
383 '''Check for sequence types.
385 @arg obj: The object (any C{type}).
386 @arg excls: Classes to exclude (C{type}), all positional.
388 @note: Excluding C{tuple} implies excluding C{namedtuple}.
390 @return: C{True} if B{C{obj}} is a sequence, C{False} otherwise.
391 '''
392 return isinstance(obj, _Seqs) and not (excls and isinstance(obj, excls))
395def isstr(obj):
396 '''Check for string types.
398 @arg obj: The object (any C{type}).
400 @return: C{True} if B{C{obj}} is C{str}, C{False} otherwise.
401 '''
402 return isinstance(obj, _Strs)
405def issubclassof(Sub, *Supers):
406 '''Check whether a class is a sub-class of some other class(es).
408 @arg Sub: The sub-class (C{class}).
409 @arg Supers: One or more C(super) classes (C{class}).
411 @return: C{True} if B{C{Sub}} is a sub-class of any B{C{Supers}},
412 C{False} if not (C{bool}) or C{None} if B{C{Sub}} is not
413 a class or if no B{C{Supers}} are given or none of those
414 are a class.
415 '''
416 if isclass(Sub):
417 t = tuple(S for S in Supers if isclass(S))
418 if t:
419 return bool(issubclass(Sub, t))
420 return None
423def len2(items):
424 '''Make built-in function L{len} work for generators, iterators,
425 etc. since those can only be started exactly once.
427 @arg items: Generator, iterator, list, range, tuple, etc.
429 @return: 2-Tuple C{(n, items)} of the number of items (C{int})
430 and the items (C{list} or C{tuple}).
431 '''
432 if not isinstance(items, _Seqs): # NOT hasattr(items, '__len__'):
433 items = list(items)
434 return len(items), items
437def map1(fun1, *xs): # XXX map_
438 '''Apply each B{C{xs}} to a single-argument function and
439 return a C{tuple} of results.
441 @arg fun1: 1-Arg function to apply (C{callable}).
442 @arg xs: Arguments to apply (C{any positional}).
444 @return: Function results (C{tuple}).
445 '''
446 return tuple(map(fun1, xs))
449def map2(func, *xs):
450 '''Apply arguments to a function and return a C{tuple} of results.
452 Unlike Python 2's built-in L{map}, Python 3+ L{map} returns a
453 L{map} object, an iterator-like object which generates the
454 results only once. Converting the L{map} object to a tuple
455 maintains the Python 2 behavior.
457 @arg func: Function to apply (C{callable}).
458 @arg xs: Arguments to apply (C{list, tuple, ...}).
460 @return: Function results (C{tuple}).
461 '''
462 return tuple(map(func, *xs))
465def neg(x, neg0=None):
466 '''Negate C{x} and optionally, negate C{0.0} amd C{-0.0}.
468 @kwarg neg0: Defines the return value for zero C{B{x}}: if C{None}
469 return C{0.0}, if C{True} return C{NEG0 if B{x}=0.0}
470 and C{0.0 if B{x}=NEG0} or if C{False} return C{B{x}}
471 I{as-is} (C{bool} or C{None}).
473 @return: C{-B{x} if B{x} else 0.0, NEG0 or B{x}}.
474 '''
475 return (-x) if x else (_0_0 if neg0 is None else (x if not neg0 else
476 (_0_0 if signBit(x) else _MODS.constants.NEG0)))
479def neg_(*xs):
480 '''Negate all C{xs} with L{neg}.
482 @return: A C{map(neg, B{xs})}.
483 '''
484 return map(neg, xs)
487def _reverange(n, stop=-1, step=-1):
488 '''(INTERNAL) Reversed range yielding C{n-1, n-1-step, ..., stop+1}.
489 '''
490 return range(n - 1, stop, step)
493def signBit(x):
494 '''Return C{signbit(B{x})}, like C++.
496 @return: C{True} if C{B{x} < 0} or C{NEG0} (C{bool}).
497 '''
498 return x < 0 or _MODS.constants.isneg0(x)
501def _signOf(x, ref): # in .fsums
502 '''(INTERNAL) Return the sign of B{C{x}} versus B{C{ref}}.
503 '''
504 return +1 if x > ref else (-1 if x < ref else 0)
507def signOf(x):
508 '''Return sign of C{x} as C{int}.
510 @return: -1, 0 or +1 (C{int}).
511 '''
512 try:
513 s = x.signOf() # Fsum instance?
514 except AttributeError:
515 s = _signOf(x, 0)
516 return s
519def _sizeof(inst):
520 '''(INTERNAL) Recursively size an C{inst}ance.
522 @return: Instance' size in bytes (C{int}),
523 ignoring class attributes and
524 counting duplicates only once or
525 C{None}.
527 @note: With C{PyPy}, the size is always C{None}.
528 '''
529 try:
530 _zB = _sys.getsizeof
531 _zD = _zB(None) # get some default
532 except TypeError: # PyPy3.10
533 return None
535 def _zR(s, iterable):
536 z, _s = 0, s.add
537 for o in iterable:
538 i = id(o)
539 if i not in s:
540 _s(i)
541 z += _zB(o, _zD)
542 if isinstance(o, dict):
543 z += _zR(s, o.keys())
544 z += _zR(s, o.values())
545 elif isinstance(o, _list_tuple_set_types):
546 z += _zR(s, o)
547 else:
548 try: # size instance' attr values only
549 z += _zR(s, o.__dict__.values())
550 except AttributeError: # None, int, etc.
551 pass
552 return z
554 return _zR(set(), (inst,))
557def splice(iterable, n=2, **fill):
558 '''Split an iterable into C{n} slices.
560 @arg iterable: Items to be spliced (C{list}, C{tuple}, ...).
561 @kwarg n: Number of slices to generate (C{int}).
562 @kwarg fill: Optional fill value for missing items.
564 @return: A generator for each of B{C{n}} slices,
565 M{iterable[i::n] for i=0..n}.
567 @raise TypeError: Invalid B{C{n}}.
569 @note: Each generated slice is a C{tuple} or a C{list},
570 the latter only if the B{C{iterable}} is a C{list}.
572 @example:
574 >>> from pygeodesy import splice
576 >>> a, b = splice(range(10))
577 >>> a, b
578 ((0, 2, 4, 6, 8), (1, 3, 5, 7, 9))
580 >>> a, b, c = splice(range(10), n=3)
581 >>> a, b, c
582 ((0, 3, 6, 9), (1, 4, 7), (2, 5, 8))
584 >>> a, b, c = splice(range(10), n=3, fill=-1)
585 >>> a, b, c
586 ((0, 3, 6, 9), (1, 4, 7, -1), (2, 5, 8, -1))
588 >>> tuple(splice(list(range(9)), n=5))
589 ([0, 5], [1, 6], [2, 7], [3, 8], [4])
591 >>> splice(range(9), n=1)
592 <generator object splice at 0x0...>
593 '''
594 if not isint(n):
595 raise _TypeError(n=n)
597 t = iterable
598 if not isinstance(t, _list_tuple_types):
599 t = tuple(t) # force tuple, also for PyPy3
601 if n > 1:
602 if fill:
603 fill = _xkwds_get(fill, fill=MISSING)
604 if fill is not MISSING:
605 m = len(t) % n
606 if m > 0: # same type fill
607 t += type(t)((fill,) * (n - m))
608 for i in range(n):
609 # XXX t[i::n] chokes PyChecker
610 yield t[slice(i, None, n)]
611 else:
612 yield t
615def _splituple(strs, *sep_splits): # in .mgrs, .osgr, .webmercator
616 '''(INTERNAL) Split a C{comma}- or C{whitespace}-separated
617 string into a C{tuple} of stripped strings.
618 '''
619 t = (strs.split(*sep_splits) if sep_splits else
620 strs.replace(_COMMA_, _SPACE_).split()) if strs else ()
621 return tuple(s.strip() for s in t if s)
624_XPACKAGES = _splituple(_getenv(_PYGEODESY_XPACKAGES_, NN))
627def unsigned0(x):
628 '''Unsign if C{0.0}.
630 @return: C{B{x}} if B{C{x}} else C{0.0}.
631 '''
632 return x if x else _0_0
635def _xargs_names(callabl):
636 '''(INTERNAL) Get the C{callabl}'s args names.
637 '''
638 try:
639 args_kwds = _inspect.signature(callabl).parameters.keys()
640 except AttributeError: # .signature new Python 3+
641 args_kwds = _inspect.getargspec(callabl).args
642 return tuple(args_kwds)
645def _xcopy(inst, deep=False):
646 '''(INTERNAL) Copy an object, shallow or deep.
648 @arg inst: The object to copy (any C{type}).
649 @kwarg deep: If C{True} make a deep, otherwise
650 a shallow copy (C{bool}).
652 @return: The copy of B{C{inst}}.
653 '''
654 return _deepcopy(inst) if deep else _copy(inst)
657def _xdup(inst, deep=False, **items):
658 '''(INTERNAL) Duplicate an object, replacing some attributes.
660 @arg inst: The object to copy (any C{type}).
661 @kwarg deep: If C{True} copy deep, otherwise shallow.
662 @kwarg items: Attributes to be changed (C{any}).
664 @return: A duplicate of B{C{inst}} with modified
665 attributes, if any B{C{items}}.
667 @raise AttributeError: Some B{C{items}} invalid.
668 '''
669 d = _xcopy(inst, deep=deep)
670 for n, v in items.items():
671 if getattr(d, n, v) != v:
672 setattr(d, n, v)
673 elif not hasattr(d, n):
674 t = _MODS.named.classname(inst)
675 t = _SPACE_(_DOT_(t, n), _invalid_)
676 raise _AttributeError(txt=t, this=inst, **items)
677 return d
680def _xgeographiclib(where, *required):
681 '''(INTERNAL) Import C{geographiclib} and check required version.
682 '''
683 try:
684 _xpackage(_xgeographiclib)
685 import geographiclib
686 except ImportError as x:
687 raise _xImportError(x, where)
688 return _xversion(geographiclib, where, *required)
691def _xImportError(x, where, **name):
692 '''(INTERNAL) Embellish an C{ImportError}.
693 '''
694 t = _SPACE_(_required_, _by_, _xwhere(where, **name))
695 return _ImportError(_Xstr(x), txt=t, cause=x)
698def _xinstanceof(*Types, **name_value_pairs):
699 '''(INTERNAL) Check C{Types} of all C{name=value} pairs.
701 @arg Types: One or more classes or types (C{class}),
702 all positional.
703 @kwarg name_value_pairs: One or more C{B{name}=value} pairs
704 with the C{value} to be checked.
706 @raise TypeError: One of the B{C{name_value_pairs}} is not
707 an instance of any of the B{C{Types}}.
708 '''
709 if Types and name_value_pairs:
710 for n, v in name_value_pairs.items():
711 if not isinstance(v, Types):
712 raise _TypesError(n, v, *Types)
713 else:
714 raise _AssertionError(Types=Types, name_value_pairs=name_value_pairs)
717def _xnumpy(where, *required):
718 '''(INTERNAL) Import C{numpy} and check required version.
719 '''
720 try:
721 _xpackage(_xnumpy)
722 import numpy
723 except ImportError as x:
724 raise _xImportError(x, where)
725 return _xversion(numpy, where, *required)
728def _xpackage(_xpkg):
729 '''(INTERNAL) Check dependency to be excluded.
730 '''
731 n = _xpkg.__name__[2:]
732 if n in _XPACKAGES:
733 x = _SPACE_(n, _in_, _PYGEODESY_XPACKAGES_)
734 e = _enquote(_getenv(_PYGEODESY_XPACKAGES_, NN))
735 raise ImportError(_EQUAL_(x, e))
738def _xor(x, *xs):
739 '''(INTERNAL) Exclusive-or C{x} and C{xs}.
740 '''
741 for x_ in xs:
742 x ^= x_
743 return x
746def _xscipy(where, *required):
747 '''(INTERNAL) Import C{scipy} and check required version.
748 '''
749 try:
750 _xpackage(_xscipy)
751 import scipy
752 except ImportError as x:
753 raise _xImportError(x, where)
754 return _xversion(scipy, where, *required)
757def _xsubclassof(*Classes, **name_value_pairs):
758 '''(INTERNAL) Check (super) class of all C{name=value} pairs.
760 @arg Classes: One or more classes or types (C{class}),
761 all positional.
762 @kwarg name_value_pairs: One or more C{B{name}=value} pairs
763 with the C{value} to be checked.
765 @raise TypeError: One of the B{C{name_value_pairs}} is not
766 a (sub-)class of any of the B{C{Classes}}.
767 '''
768 for n, v in name_value_pairs.items():
769 if not issubclassof(v, *Classes):
770 raise _TypesError(n, v, *Classes)
773def _xversion(package, where, *required, **name):
774 '''(INTERNAL) Check the C{package} version vs B{C{required}}.
775 '''
776 n = len(required)
777 if n:
778 t = _xversion_info(package)
779 if t[:n] < required:
780 t = _SPACE_(package.__name__, _version_, _DOT_(*t),
781 _below_, _DOT_(*required),
782 _required_, _by_, _xwhere(where, **name))
783 raise ImportError(t)
784 return package
787def _xversion_info(package): # in .karney
788 '''(INTERNAL) Get the C{package.__version_info__} as a 2- or
789 3-tuple C{(major, minor, revision)} if C{int}s.
790 '''
791 try:
792 t = package.__version_info__
793 except AttributeError:
794 t = package.__version__.strip()
795 t = t.replace(_DOT_, _SPACE_).split()[:3]
796 return map2(int, t)
799def _xwhere(where, **name):
800 '''(INTERNAL) Get the fully qualified name.
801 '''
802 m = _MODS.named.modulename(where, prefixed=True)
803 if name:
804 n = _xkwds_get(name, name=NN)
805 if n:
806 m = _DOT_(m, n)
807 return m
810if _sys_version_info2 < (3, 10): # see .errors
811 _zip = zip # PYCHOK exported
812else: # Python 3.10+
814 def _zip(*args):
815 return zip(*args, strict=True)
817# **) MIT License
818#
819# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.
820#
821# Permission is hereby granted, free of charge, to any person obtaining a
822# copy of this software and associated documentation files (the "Software"),
823# to deal in the Software without restriction, including without limitation
824# the rights to use, copy, modify, merge, publish, distribute, sublicense,
825# and/or sell copies of the Software, and to permit persons to whom the
826# Software is furnished to do so, subject to the following conditions:
827#
828# The above copyright notice and this permission notice shall be included
829# in all copies or substantial portions of the Software.
830#
831# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
832# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
833# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
834# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
835# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
836# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
837# OTHER DEALINGS IN THE SOFTWARE.