Coverage for pygeodesy/basics.py: 90%
218 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-07 07:28 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-07 07:28 -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
17from pygeodesy.errors import _AssertionError, _AttributeError, _ImportError, \
18 _TypeError, _TypesError, _ValueError, _xkwds_get
19from pygeodesy.interns import MISSING, NN, _by_, _DOT_, _ELLIPSIS4_, _enquote, \
20 _EQUAL_, _in_, _invalid_, _N_A_, _SPACE_, \
21 _splituple, _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.06.23'
32_0_0 = 0.0 # in .constants
33_below_ = 'below'
34_cannot_ = 'cannot'
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_XPACKAGES = _splituple(_getenv(_PYGEODESY_XPACKAGES_, NN))
42try: # Luciano Ramalho, "Fluent Python", page 395, O'Reilly, 2016
43 from numbers import Integral as _Ints, Real as _Scalars
44except ImportError:
45 try:
46 _Ints = int, long # int objects (C{tuple})
47 except NameError: # Python 3+
48 _Ints = int, # int objects (C{tuple})
49 _Scalars = _Ints + (float,)
51try:
52 try: # use C{from collections.abc import ...} in Python 3.9+
53 from collections.abc import Sequence as _Sequence # in .points
54 except ImportError: # no .abc in Python 3.8- and 2.7-
55 from collections import Sequence as _Sequence # in .points
56 if isinstance([], _Sequence) and isinstance((), _Sequence):
57 # and isinstance(range(1), _Sequence):
58 _Seqs = _Sequence
59 else:
60 raise ImportError # _AssertionError
61except ImportError:
62 _Sequence = tuple # immutable for .points._Basequence
63 _Seqs = list, _Sequence # , range for function len2 below
65try:
66 _Bytes = unicode, bytearray # PYCHOK expected
67 _Strs = basestring, str # XXX , bytes
69 def _NOP(x):
70 '''NOP, pass thru.'''
71 return x
73 str2ub = ub2str = _NOP # avoids UnicodeDecodeError
75 def _Xstr(exc): # PYCHOK no cover
76 '''I{Invoke only with caught ImportError} B{C{exc}}.
78 C{... "cannot import name _distributor_init" ...}
80 only for C{numpy}, C{scipy} import errors occurring
81 on arm64 Apple Silicon running macOS' Python 2.7.16?
82 '''
83 t = str(exc)
84 if '_distributor_init' in t:
85 from sys import exc_info
86 from traceback import extract_tb
87 tb = exc_info()[2] # 3-tuple (type, value, traceback)
88 t4 = extract_tb(tb, 1)[0] # 4-tuple (file, line, name, 'import ...')
89 t = _SPACE_(_cannot_, t4[3] or _N_A_)
90 del tb, t4
91 return t
93except NameError: # Python 3+
94 from pygeodesy.interns import _utf_8_
96 _Bytes = bytes, bytearray
97 _Strs = str, # tuple
98 _Xstr = str
100 def str2ub(sb):
101 '''Convert C{str} to C{unicode bytes}.
102 '''
103 if isinstance(sb, _Strs):
104 sb = sb.encode(_utf_8_)
105 return sb
107 def ub2str(ub):
108 '''Convert C{unicode bytes} to C{str}.
109 '''
110 if isinstance(ub, _Bytes):
111 ub = str(ub.decode(_utf_8_))
112 return ub
115def clips(sb, limit=50, white=NN):
116 '''Clip a string to the given length limit.
118 @arg sb: String (C{str} or C{bytes}).
119 @kwarg limit: Length limit (C{int}).
120 @kwarg white: Optionally, replace all whitespace (C{str}).
122 @return: The clipped or unclipped B{C{sb}}.
123 '''
124 T = type(sb)
125 if len(sb) > limit > 8:
126 h = limit // 2
127 sb = T(_ELLIPSIS4_).join((sb[:h], sb[-h:]))
128 if white: # replace whitespace
129 sb = T(white).join(sb.split())
130 return sb
133def copysign0(x, y):
134 '''Like C{math.copysign(x, y)} except C{zero}, I{unsigned}.
136 @return: C{math.copysign(B{x}, B{y})} if B{C{x}} else
137 C{type(B{x})(0)}.
138 '''
139 return _copysign(x, (y if y else 0)) if x else copytype(0, x)
142def copytype(x, y):
143 '''Return the value of B{x} as C{type} of C{y}.
145 @return: C{type(B{y})(B{x})}.
146 '''
147 return type(y)(x if x else _0_0)
150def halfs2(str2):
151 '''Split a string in 2 halfs.
153 @arg str2: String to split (C{str}).
155 @return: 2-Tuple C{(_1st, _2nd)} half (C{str}).
157 @raise ValueError: Zero or odd C{len(B{str2})}.
158 '''
159 h, r = divmod(len(str2), 2)
160 if r or not h:
161 raise _ValueError(str2=str2, txt=_odd_)
162 return str2[:h], str2[h:]
165def isbool(obj):
166 '''Check whether an object is C{bool}ean.
168 @arg obj: The object (any C{type}).
170 @return: C{True} if B{C{obj}} is C{bool}ean,
171 C{False} otherwise.
172 '''
173 return isinstance(obj, bool) # and (obj is False
174# or obj is True)
176if isbool(1) or isbool(0): # PYCHOK assert
177 raise _AssertionError(isbool=1)
179if _FOR_DOCS: # XXX avoid epidoc Python 2.7 error
181 def isclass(obj):
182 '''Return C{True} if B{C{obj}} is a C{class} or C{type}.
184 @see: Python's C{inspect.isclass}.
185 '''
186 return _inspect.isclass(obj)
187else:
188 isclass = _inspect.isclass
191def iscomplex(obj):
192 '''Check whether an object is a C{complex} or complex C{str}.
194 @arg obj: The object (any C{type}).
196 @return: C{True} if B{C{obj}} is C{complex}, otherwise
197 C{False}.
198 '''
199 try: # hasattr('conjugate'), hasattr('real') and hasattr('imag')
200 return isinstance(obj, complex) or (isstr(obj)
201 and isinstance(complex(obj), complex)) # numbers.Complex?
202 except (TypeError, ValueError):
203 return False
206def isfloat(obj):
207 '''Check whether an object is a C{float} or float C{str}.
209 @arg obj: The object (any C{type}).
211 @return: C{True} if B{C{obj}} is a C{float}, otherwise
212 C{False}.
213 '''
214 try:
215 return isinstance( obj, float) or (isstr(obj)
216 and isinstance(float(obj), float))
217 except (TypeError, ValueError):
218 return False
221try:
222 isidentifier = str.isidentifier # Python 3, must be str
223except AttributeError: # Python 2-
225 def isidentifier(obj):
226 '''Return C{True} if B{C{obj}} is a Python identifier.
227 '''
228 return bool(obj and isstr(obj)
229 and obj.replace(_UNDER_, NN).isalnum()
230 and not obj[:1].isdigit())
233def isinstanceof(obj, *classes):
234 '''Check an instance of one or several C{classes}.
236 @arg obj: The instance (C{any}).
237 @arg classes: One or more classes (C{class}).
239 @return: C{True} if B{C{obj}} is in instance of
240 one of the B{C{classes}}.
241 '''
242 return isinstance(obj, classes)
245def isint(obj, both=False):
246 '''Check for C{int} type or an integer C{float} value.
248 @arg obj: The object (any C{type}).
249 @kwarg both: If C{true}, check C{float} and L{Fsum}
250 type and value (C{bool}).
252 @return: C{True} if B{C{obj}} is C{int} or I{integer}
253 C{float} or L{Fsum}, C{False} otherwise.
255 @note: Both C{isint(True)} and C{isint(False)} return
256 C{False} (and no longer C{True}).
257 '''
258 if isinstance(obj, _Ints) and not isbool(obj):
259 return True
260 elif both: # and isinstance(obj, (float, Fsum))
261 try: # NOT , _Scalars) to include Fsum!
262 return obj.is_integer()
263 except AttributeError:
264 pass # XXX float(int(obj)) == obj?
265 return False
268try:
269 from keyword import iskeyword # Python 2.7+
270except ImportError:
272 def iskeyword(unused):
273 '''Not Implemented, C{False} always.
274 '''
275 return False
278def islistuple(obj, minum=0):
279 '''Check for list or tuple C{type} with a minumal length.
281 @arg obj: The object (any C{type}).
282 @kwarg minum: Minimal C{len} required C({int}).
284 @return: C{True} if B{C{obj}} is C{list} or C{tuple} with
285 C{len} at least B{C{minum}}, C{False} otherwise.
286 '''
287 return type(obj) in _list_tuple_types and len(obj) >= (minum or 0)
290def isodd(x):
291 '''Is B{C{x}} odd?
293 @arg x: Value (C{scalar}).
295 @return: C{True} if B{C{x}} is odd,
296 C{False} otherwise.
297 '''
298 return bool(int(x) & 1) # == bool(int(x) % 2)
301def isscalar(obj):
302 '''Check for scalar types.
304 @arg obj: The object (any C{type}).
306 @return: C{True} if B{C{obj}} is C{scalar}, C{False} otherwise.
307 '''
308 return isinstance(obj, _Scalars) and not isbool(obj)
311def issequence(obj, *excls):
312 '''Check for sequence types.
314 @arg obj: The object (any C{type}).
315 @arg excls: Classes to exclude (C{type}), all positional.
317 @note: Excluding C{tuple} implies excluding C{namedtuple}.
319 @return: C{True} if B{C{obj}} is a sequence, C{False} otherwise.
320 '''
321 return isinstance(obj, _Seqs) and not (excls and isinstance(obj, excls))
324def isstr(obj):
325 '''Check for string types.
327 @arg obj: The object (any C{type}).
329 @return: C{True} if B{C{obj}} is C{str}, C{False} otherwise.
330 '''
331 return isinstance(obj, _Strs)
334def issubclassof(Sub, *Supers):
335 '''Check whether a class is a sub-class of some other class(es).
337 @arg Sub: The sub-class (C{class}).
338 @arg Supers: One or more C(super) classes (C{class}).
340 @return: C{True} if B{C{Sub}} is a sub-class of any B{C{Supers}},
341 C{False} if not (C{bool}) or C{None} if B{C{Sub}} is not
342 a class or if no B{C{Supers}} are given or none of those
343 are a class.
344 '''
345 if isclass(Sub):
346 t = tuple(S for S in Supers if isclass(S))
347 if t:
348 return bool(issubclass(Sub, t))
349 return None
352def len2(items):
353 '''Make built-in function L{len} work for generators, iterators,
354 etc. since those can only be started exactly once.
356 @arg items: Generator, iterator, list, range, tuple, etc.
358 @return: 2-Tuple C{(n, items)} of the number of items (C{int})
359 and the items (C{list} or C{tuple}).
360 '''
361 if not isinstance(items, _Seqs): # NOT hasattr(items, '__len__'):
362 items = list(items)
363 return len(items), items
366def map1(fun1, *xs): # XXX map_
367 '''Apply each B{C{xs}} to a single-argument function and
368 return a C{tuple} of results.
370 @arg fun1: 1-Arg function to apply (C{callable}).
371 @arg xs: Arguments to apply (C{any positional}).
373 @return: Function results (C{tuple}).
374 '''
375 return tuple(map(fun1, xs))
378def map2(func, *xs):
379 '''Apply arguments to a function and return a C{tuple} of results.
381 Unlike Python 2's built-in L{map}, Python 3+ L{map} returns a
382 L{map} object, an iterator-like object which generates the
383 results only once. Converting the L{map} object to a tuple
384 maintains the Python 2 behavior.
386 @arg func: Function to apply (C{callable}).
387 @arg xs: Arguments to apply (C{list, tuple, ...}).
389 @return: Function results (C{tuple}).
390 '''
391 return tuple(map(func, *xs))
394def neg(x):
395 '''Negate C{x} unless C{zero} or C{NEG0}.
397 @return: C{-B{x}} if B{C{x}} else C{0.0}.
398 '''
399 return (-x) if x else _0_0
402def neg_(*xs):
403 '''Negate all C{xs} with L{neg}.
405 @return: A C{map(neg, B{xs})}.
406 '''
407 return map(neg, xs)
410def _reverange(n):
411 '''(INTERNAL) Reversed range yielding (n-1, n-2, ..., 1, 0).
412 '''
413 return range(n - 1, -1, -1)
416def signBit(x):
417 '''Return C{signbit(B{x})}, like C++.
419 @return: C{True} if C{B{x} < 0} or C{NEG0} (C{bool}).
420 '''
421 return x < 0 or _MODS.constants.isneg0(x)
424def _signOf(x, ref): # in .fsums
425 '''(INTERNAL) Return the sign of B{C{x}} versus B{C{ref}}.
426 '''
427 return +1 if x > ref else (-1 if x < ref else 0)
430def signOf(x):
431 '''Return sign of C{x} as C{int}.
433 @return: -1, 0 or +1 (C{int}).
434 '''
435 try:
436 s = x.signOf() # Fsum instance?
437 except AttributeError:
438 s = _signOf(x, 0)
439 return s
442def _sizeof(inst):
443 '''(INTERNAL) Recursively size an C{inst}ance.
445 @return: Instance' size in bytes (C{int}),
446 ignoring class attributes and
447 counting duplicates only once.
448 '''
449 _zB = _sys.getsizeof
450 _zD = _zB(None) # some default
452 def _zR(s, iterable):
453 z, _s = 0, s.add
454 for o in iterable:
455 i = id(o)
456 if i not in s:
457 _s(i)
458 z += _zB(o, _zD)
459 if isinstance(o, dict):
460 z += _zR(s, o.keys())
461 z += _zR(s, o.values())
462 elif isinstance(o, _list_tuple_set_types):
463 z += _zR(s, o)
464 else:
465 try: # size instance' attr values only
466 z += _zR(s, o.__dict__.values())
467 except AttributeError: # None, int, etc.
468 pass
469 return z
471 return _zR(set(), (inst,))
474def splice(iterable, n=2, **fill):
475 '''Split an iterable into C{n} slices.
477 @arg iterable: Items to be spliced (C{list}, C{tuple}, ...).
478 @kwarg n: Number of slices to generate (C{int}).
479 @kwarg fill: Optional fill value for missing items.
481 @return: A generator for each of B{C{n}} slices,
482 M{iterable[i::n] for i=0..n}.
484 @raise TypeError: Invalid B{C{n}}.
486 @note: Each generated slice is a C{tuple} or a C{list},
487 the latter only if the B{C{iterable}} is a C{list}.
489 @example:
491 >>> from pygeodesy import splice
493 >>> a, b = splice(range(10))
494 >>> a, b
495 ((0, 2, 4, 6, 8), (1, 3, 5, 7, 9))
497 >>> a, b, c = splice(range(10), n=3)
498 >>> a, b, c
499 ((0, 3, 6, 9), (1, 4, 7), (2, 5, 8))
501 >>> a, b, c = splice(range(10), n=3, fill=-1)
502 >>> a, b, c
503 ((0, 3, 6, 9), (1, 4, 7, -1), (2, 5, 8, -1))
505 >>> tuple(splice(list(range(9)), n=5))
506 ([0, 5], [1, 6], [2, 7], [3, 8], [4])
508 >>> splice(range(9), n=1)
509 <generator object splice at 0x0...>
510 '''
511 if not isint(n):
512 raise _TypeError(n=n)
514 t = iterable
515 if not isinstance(t, _list_tuple_types):
516 t = tuple(t) # force tuple, also for PyPy3
518 if n > 1:
519 if fill:
520 fill = _xkwds_get(fill, fill=MISSING)
521 if fill is not MISSING:
522 m = len(t) % n
523 if m > 0: # same type fill
524 t += type(t)((fill,) * (n - m))
525 for i in range(n):
526 # XXX t[i::n] chokes PyChecker
527 yield t[slice(i, None, n)]
528 else:
529 yield t
532def unsigned0(x):
533 '''Unsign if C{0.0}.
535 @return: C{B{x}} if B{C{x}} else C{0.0}.
536 '''
537 return x if x else _0_0
540def _xargs_names(callabl):
541 '''(INTERNAL) Get the C{callabl}'s args names.
542 '''
543 try:
544 args_kwds = _inspect.signature(callabl).parameters.keys()
545 except AttributeError: # .signature new Python 3+
546 args_kwds = _inspect.getargspec(callabl).args
547 return tuple(args_kwds)
550def _xcopy(inst, deep=False):
551 '''(INTERNAL) Copy an object, shallow or deep.
553 @arg inst: The object to copy (any C{type}).
554 @kwarg deep: If C{True} make a deep, otherwise
555 a shallow copy (C{bool}).
557 @return: The copy of B{C{inst}}.
558 '''
559 return _deepcopy(inst) if deep else _copy(inst)
562def _xdup(inst, **items):
563 '''(INTERNAL) Duplicate an object, replacing some attributes.
565 @arg inst: The object to copy (any C{type}).
566 @kwarg items: Attributes to be changed (C{any}).
568 @return: Shallow duplicate of B{C{inst}} with modified
569 attributes, if any B{C{items}}.
571 @raise AttributeError: Some B{C{items}} invalid.
572 '''
573 d = _xcopy(inst, deep=False)
574 for n, v in items.items():
575 if not hasattr(d, n):
576 t = _MODS.named.classname(inst)
577 t = _SPACE_(_DOT_(t, n), _invalid_)
578 raise _AttributeError(txt=t, this=inst, **items)
579 setattr(d, n, v)
580 return d
583def _xgeographiclib(where, *required):
584 '''(INTERNAL) Import C{geographiclib} and check required version.
585 '''
586 try:
587 _xpackage(_xgeographiclib)
588 import geographiclib
589 except ImportError as x:
590 raise _xImportError(x, where)
591 return _xversion(geographiclib, where, *required)
594def _xImportError(x, where, **name):
595 '''(INTERNAL) Embellish an C{ImportError}.
596 '''
597 t = _SPACE_(_required_, _by_, _xwhere(where, **name))
598 return _ImportError(_Xstr(x), txt=t, cause=x)
601def _xinstanceof(*Types, **name_value_pairs):
602 '''(INTERNAL) Check C{Types} of all C{name=value} pairs.
604 @arg Types: One or more classes or types (C{class}),
605 all positional.
606 @kwarg name_value_pairs: One or more C{B{name}=value} pairs
607 with the C{value} to be checked.
609 @raise TypeError: One of the B{C{name_value_pairs}} is not
610 an instance of any of the B{C{Types}}.
611 '''
612 if Types and name_value_pairs:
613 for n, v in name_value_pairs.items():
614 if not isinstance(v, Types):
615 raise _TypesError(n, v, *Types)
616 else:
617 raise _AssertionError(Types=Types, name_value_pairs=name_value_pairs)
620def _xnumpy(where, *required):
621 '''(INTERNAL) Import C{numpy} and check required version.
622 '''
623 try:
624 _xpackage(_xnumpy)
625 import numpy
626 except ImportError as x:
627 raise _xImportError(x, where)
628 return _xversion(numpy, where, *required)
631def _xpackage(_xpkg):
632 '''(INTERNAL) Check dependency to be excluded.
633 '''
634 n = _xpkg.__name__[2:]
635 if n in _XPACKAGES:
636 x = _SPACE_(n, _in_, _PYGEODESY_XPACKAGES_)
637 e = _enquote(_getenv(_PYGEODESY_XPACKAGES_, NN))
638 raise ImportError(_EQUAL_(x, e))
641def _xor(x, *xs):
642 '''(INTERNAL) Exclusive-or C{x} and C{xs}.
643 '''
644 for x_ in xs:
645 x ^= x_
646 return x
649def _xscipy(where, *required):
650 '''(INTERNAL) Import C{scipy} and check required version.
651 '''
652 try:
653 _xpackage(_xscipy)
654 import scipy
655 except ImportError as x:
656 raise _xImportError(x, where)
657 return _xversion(scipy, where, *required)
660def _xsubclassof(*Classes, **name_value_pairs):
661 '''(INTERNAL) Check (super) class of all C{name=value} pairs.
663 @arg Classes: One or more classes or types (C{class}),
664 all positional.
665 @kwarg name_value_pairs: One or more C{B{name}=value} pairs
666 with the C{value} to be checked.
668 @raise TypeError: One of the B{C{name_value_pairs}} is not
669 a (sub-)class of any of the B{C{Classes}}.
670 '''
671 for n, v in name_value_pairs.items():
672 if not issubclassof(v, *Classes):
673 raise _TypesError(n, v, *Classes)
676def _xversion(package, where, *required, **name):
677 '''(INTERNAL) Check the C{package} version vs B{C{required}}.
678 '''
679 n = len(required)
680 if n:
681 t = _xversion_info(package)
682 if t[:n] < required:
683 t = _SPACE_(package.__name__, _version_, _DOT_(*t),
684 _below_, _DOT_(*required),
685 _required_, _by_, _xwhere(where, **name))
686 raise ImportError(t)
687 return package
690def _xversion_info(package): # in .karney
691 '''(INTERNAL) Get the C{package.__version_info__} as a 2- or
692 3-tuple C{(major, minor, revision)} if C{int}s.
693 '''
694 try:
695 t = package.__version_info__
696 except AttributeError:
697 t = package.__version__.strip()
698 t = t.replace(_DOT_, _SPACE_).split()[:3]
699 return map2(int, t)
702def _xwhere(where, **name):
703 '''(INTERNAL) Get the fully qualified name.
704 '''
705 m = _MODS.named.modulename(where, prefixed=True)
706 if name:
707 n = _xkwds_get(name, name=NN)
708 if n:
709 m = _DOT_(m, n)
710 return m
713if _sys_version_info2 < (3, 10): # see .errors
714 _zip = zip # PYCHOK exported
715else: # Python 3.10+
717 def _zip(*args):
718 return zip(*args, strict=True)
720# **) MIT License
721#
722# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.
723#
724# Permission is hereby granted, free of charge, to any person obtaining a
725# copy of this software and associated documentation files (the "Software"),
726# to deal in the Software without restriction, including without limitation
727# the rights to use, copy, modify, merge, publish, distribute, sublicense,
728# and/or sell copies of the Software, and to permit persons to whom the
729# Software is furnished to do so, subject to the following conditions:
730#
731# The above copyright notice and this permission notice shall be included
732# in all copies or substantial portions of the Software.
733#
734# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
735# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
736# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
737# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
738# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
739# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
740# OTHER DEALINGS IN THE SOFTWARE.