Coverage for pygeodesy/basics.py : 96%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*-
'''
# all public contants, classes and functions
except ImportError: # PYCHOK no cover try: # _Ints imported by .utily _Ints = int, long #: (INTERNAL) Int objects (C{tuple}) except NameError: # Python 3+ _Ints = int, #: (INTERNAL) Int objects (C{tuple})
except ImportError: # PYCHOK no cover try: _Scalars = int, long, float #: (INTERNAL) Scalar objects (C{tuple}) except NameError: _Scalars = int, float #: (INTERNAL) Scalar objects (C{tuple})
# and isinstance(range(1), _Sequence): else: raise ImportError # AssertionError except ImportError: # PYCHOK no cover _Sequence = tuple # immutable for .points._Basequence _Seqs = list, _Sequence # , range for function len2 below
except AttributeError: # PYCHOK no cover EPS = 2.220446049250313e-16 #: Epsilon (C{float}) 2**-52? MANTIS = 53 #: Mantissa bits ≈53 (C{int}) MAX = pow(2.0, 1023) * (2 - EPS) #: Float max (C{float}) ≈10**308, 2**1024? MIN = pow(2.0, -1021) # Float min (C{float}) ≈10**-308, 2**-1021? # _1EPS = 1.0 + EPS #: M{1 + EPS} ≈1.0000000000000002 (C{float})
# R_M moved here to avoid circular imports
'''Error raised for mis-matching C{len} values. ''' '''New L{LenError}.
@arg where: Object with C{.__name__} attribute (C{class}, C{method}, or C{function}). @kwarg lens: Two or more C{name=len(name)} pairs (C{keyword arguments}). '''
'''Error raised for lat- or longitudinal deltas exceeding the B{C{limit}} in functions L{equirectangular} and L{equirectangular_} and C{nearestOn*} and C{simplify*} functions or methods. '''
'''Clip a string to the given length limit.
@arg bstr: String (C{bytes} or C{str}). @kwarg limit: Length limit (C{int}). @kwarg white: Whitespace replacement (C{str}).
@return: Un-/clipped B{C{bstr}}. '''
'''Split a string in 2 halfs.
@arg str2: String to split (C{str}).
@return: 2-Tuple (1st, 2nd) half (C{str}).
@raise ValueError: Zero or odd C{len}(B{str2}). ''' raise ValueError('%s invalid: %r' % ('str2', str2))
'''Check for C{Inf} and C{NaN} values.
@arg obj: Value (C{scalar}).
@return: C{False} if B{C{obj}} is C{INF} or C{NAN}, C{True} otherwise.
@raise TypeError: Non-scalar B{C{obj}}. ''' raise _isnotError(isscalar.__name__, obj=obj)
'''Check for integer type or an integer C{float}.
@arg obj: The object (any C{type}). @kwarg both: Optionally, check both type and value (C{bool}).
@return: C{True} if B{C{obj}} is C{int}, C{False} otherwise. ''' except AttributeError: return False # XXX float(int(obj)) == obj?
'''Check for L{NEG0}, negative 0.0.
@arg obj: Value (C{scalar}).
@return: C{True} if B{C{obj}} is C{NEG0} or -0.0, C{False} otherwise. ''' # and str(obj).rstrip('0') == '-0.'
'''(INTERNAL) Format a C{TypeError} for a C{name=value} pair. ''' else: n, v = 'pair', 'N/A'
'''Check for scalar types.
@arg obj: The object (any C{type}).
@return: C{True} if B{C{obj}} is C{scalar}, C{False} otherwise. '''
'''Check for sequence types.
@arg obj: The object (any C{type}). @arg excluded: Optional, exclusions (C{type}).
@note: Excluding C{tuple} implies excluding C{namedtuple}.
@return: C{True} if B{C{obj}} is a sequence, C{False} otherwise. ''' isinstance(obj, excluded) else: return isinstance(obj, _Seqs)
'''Check for string types.
@arg obj: The object (any C{type}).
@return: C{True} if B{C{obj}} is C{str}, C{False} otherwise. '''
'''Check whether a class is a subclass of a super class.
@arg sub: The subclass (C{class}). @arg sup: The super class (C{class}).
@return: C{True} if B{C{sub}} is a subclass of B{C{sup}}. '''
'''Make built-in function L{len} work for generators, iterators, etc. since those can only be started exactly once.
@arg items: Generator, iterator, list, range, tuple, etc.
@return: 2-Tuple C{(n, items)} of the number of items (C{int}) and the items (C{list} or C{tuple}). '''
'''Get/set the raising of limit errors.
@kwarg raiser: Choose C{True} to throw or C{False} to ignore L{LimitError} exceptions. Use C{None} to leave the setting unchanged.
@return: Previous setting (C{bool}). ''' global _limiterrors
'''Apply each argument to a single-argument function and return a C{tuple} of results.
@arg func: Function to apply (C{callable}). @arg xs: Arguments to apply (C{any positional}).
@return: Function results (C{tuple}). '''
'''Apply arguments to a function and return a C{tuple} of results.
Unlike Python 2's built-in L{map}, Python 3+ L{map} returns a L{map} object, an iterator-like object which generates the results only once. Converting the L{map} object to a tuple maintains Python 2 behavior.
@arg func: Function to apply (C{callable}). @arg xs: Arguments to apply (C{list, tuple, ...}).
@return: Function results (C{tuple}). '''
'''Decorator for a property with documentation.
@arg doc: The property documentation (C{str}).
@example:
>>> @property_doc_("documentation text.") >>> def name(self): >>> ... >>> >>> @name.setter >>> def name(self, value): >>> ... ''' # See Luciano Ramalho, "Fluent Python", page 212ff, O'Reilly, 2016, # "Parameterized Decorators", especially Example 7-23. Also, see # <https://Python-3-Patterns-Idioms-Test.ReadTheDocs.io/en/latest/PythonDecorators.html>
'''(INTERNAL) Return C{method} as documented C{property.getter}. '''
# No __doc__ on purpose
'''New immutable, read-only L{property_RO}.
@arg method: The callable to be decorated as C{property.getter}.
@note: Like standard Python C{property} without a C{property.setter}, but with a more descriptive error message when set. ''' # U{Descriptor HowTo Guide<https://docs.Python.org/3/howto/descriptor.html>} '''Throws an C{AttributeError}, always. ''' raise AttributeError('%s property: %r.%s = %r' % t)
# def property_RO(method): # OBSOLETE # '''An immutable property (C{Read Only}). # # @arg method: The callable to be decorated as C{property.getter}. # # @note: Like standard Python C{property} without a C{property.setter}, # but with a more descriptive error message when set. # ''' # def Read_Only(inst, value): # '''Throws an C{AttributeError}, always. # ''' # t = Read_Only.__name__, inst, method.__name__, value # raise AttributeError('%s property: %r.%s = %r' % t) # # return property(method, Read_Only, None, method.__doc__ or 'N/A')
'''Validate a scalar.
@arg value: The value (C{scalar}). @kwarg low: Optional lower bound (C{scalar}). @kwarg high: Optional upper bound (C{scalar}). @kwarg name: Optional name of value (C{str}). @kwarg Error: Exception to raise (C{ValueError}).
@return: New value (C{type} of B{C{low}}).
@raise TypeError: Non-scalar B{C{value}}.
@raise Error: Out-of-bounds B{C{value}}. ''' raise _isnotError(scalar.__name__, **{name: value}) else: raise ValueError except (TypeError, ValueError): raise _isnotError('valid', Error=Error, **{name: value})
'''Split an iterable into C{n} slices.
@arg iterable: Items to be spliced (C{list}, C{tuple}, ...). @kwarg n: Number of slices to generate (C{int}). @kwarg fill: Fill value for missing items.
@return: Generator of B{C{n}} slices M{iterable[i::n] for i=0..n}.
@note: Each generated slice is a C{tuple} or a C{list}, the latter only if the B{C{iterable}} is a C{list}.
@raise ValueError: Non-C{int} or non-positive B{C{n}}.
@example:
>>> from pygeodesy import splice
>>> a, b = splice(range(10)) >>> a, b ((0, 2, 4, 6, 8), (1, 3, 5, 7, 9))
>>> a, b, c = splice(range(10), n=3) >>> a, b, c ((0, 3, 6, 9), (1, 4, 7], [2, 5, 8))
>>> a, b, c = splice(range(10), n=3, fill=-1) >>> a, b, c ((0, 3, 6, 9), (1, 4, 7, -1), (2, 5, 8, -1))
>>> list(splice(range(12), n=5)) [(0, 5, 10), (1, 6, 11), (2, 7), (3, 8), (4, 9)]
>>> splice(range(9), n=1) <generator object splice at 0x0...> ''' raise ValueError('%s %s=%s' % ('splice', 'n', n))
else: yield t
'''(INTERNAL) Check C{Types} of all C{name=value} pairs. ''' # first letter of Type name I{pronounced} as vowel raise TypeError('%s not %s %s: %r' % (n, a, t, v))
'''(INTERNAL) Copy an instance, shallow or deep.
@arg inst: The instance to copy (C{_Named}). @kwarg deep: If C{True} make a deep, otherwise shallow copy (C{bool}).
@return: The copy (C{This class} or subclass thereof). '''
'''(INTERNAL) Override C{dflts} with C{kwds}. '''
# **) MIT License # # Copyright (C) 2016-2020 -- mrJean1 at Gmail -- All Rights Reserved. # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. |