Coverage for pygeodesy/streprs.py : 95%

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 -*-
'''
# from pygeodesy.constants import _0_0 _TypeError, _ValueError, _xkwds_get, _xkwds_pop _E_, _ELLIPSIS_, _EQUAL_, _H_, _N_, _name_, _not_, \ _not_scalar_, _PERCENT_, _SPACE_, _STAR_, _UNDER_ _exceeds_, _f_, _F_, _g_, _tolerance_ # PYCHOK used!
'''(INTERNAL) Callable formatting. '''
'''Format a C{name=value} pair or C{name, value} pair or just a single C{value}. ''' else: else: n, v = NN, MISSING
# def __mod__(self, arg, **unused): # '''Regular C{%} operator. # ''' # return str.__mod__(self, arg)
'''(INTERNAL) C{float} format. '''
'''Format the B{C{flt}} like function L{fstr}. ''' # see also function C{fstr} if isscalar case below _streprs(prec, (flt,), self, ints, True, None))
'''Regular C{%} operator.
@arg arg: A C{scalar} value to be formatted (either the C{scalar}, or a 1-tuple C{(scalar,)}, or 2-tuple C{(prec, scalar)}.
@raise TypeError: Non-scalar B{C{arg}} value.
@raise ValueError: Invalid B{C{arg}}. ''' n = _DOT_(Fstr.__name__, self.name or self) return _SPACE_(n, _PERCENT_, repr(arg))
else: raise _ValueError(_error(arg))
raise _TypeError(_error(arg))
'''(INTERNAL) Class list formatter. ''' # see .ellipsoidalNvector.LatLon.deltaTo t = _or(*(C.__name__ for C in Classes)) return str.__mod__(self, t or MISSING)
'''Formatting options. ''' # COLONSPACE = _Fmt(': %s') # == _COLONSPACE_(n, v) # COMMASPACE = _Fmt(', %s') # == _COMMASPACE_(n, v) # SPACE = _Fmt(' %s') # == _SPACE_(n, v)
'''Return C{str(B{obj})} or C{repr(B{obj})}. ''' _streprs(prec, (obj,), Fmt.g, False, False, repr))
t = _COMMASPACE_(t, Fmt.tolerance(tol[0])) if thresh and _xkwds_get(thresh, thresh=False): t = t.replace(_tolerance_, _threshold_)
# formats %G and %g drop all trailing zeros and the # decimal point, making the float appear as an int
'''Make a valid name of alphanumeric and OKd characters.
@arg name: The original name (C{str}). @kwarg OKd: Other acceptable characters (C{str}). @kwarg sub: Substitute for invalid charactes (C{str}).
@return: The modified name (C{str}).
@note: Leading and trailing whitespace characters are removed, intermediate whitespace characters are coalesced and substituted. '''
'''Get instance attributes as I{name=value} strings, with C{float}s formatted by function L{fstr}.
@arg inst: The instance (any C{type}). @arg names: The attribute names, all other positional (C{str}). @kwarg Nones_True__pairs_kwds: Keyword argument for function L{pairs}, except C{B{Nones}=True} to in-/exclude missing or C{None}-valued attributes.
@return: A C{tuple(B{sep}.join(t) for t in zip(B{names}, reprs(values, ...)))} of C{str}s. '''
'''(INTERNAL) Set applicable C{bool} properties/attributes. ''' t = _SPACE_(_EQUAL_(n, repr(v)), 'for', inst.__class__.__name__) # XXX .classname raise _AttributeError(t, txt=_not_('applicable'))
'''Return an MGRS/OSGR easting, northing string representations.
@arg easting: Easting from false easting (C{meter}). @arg northing: Northing from from false northing (C{meter}). @arg prec: Precision, the number of I{decimal} digits (C{int}) or if negative, the number of I{units to drop}, like MGRS U{PRECISION <https://GeographicLib.SourceForge.io/C++/doc/GeoConvert.1.html#PRECISION>}. @arg extras: Optional leading items (C{str}s). @kwarg wide_dot: Optional keword argument C{B{wide}=%d} for the number of I{unit digits} (C{int}) and C{B{dot}=False} (C{bool}) to insert a decimal point.
@return: B{C{extras}} + 2-tuple C{(str(B{easting}), str(B{northing}))} or + 2-tuple C{("", "")} for C{B{prec} <= -B{wide}}.
@raise ValueError: Invalid B{C{easting}}, B{C{northing}} or B{C{prec}}.
@note: The B{C{easting}} and B{C{northing}} values are I{truncated, not rounded}. ''' _0wdot(w, int(northing * f), d)) else: # prec <= -_EN_WIDE t += (NN, NN) except (TypeError, ValueError) as x: raise _ValueError(easting=easting, northing=northing, prec=prec, txt=str(x))
'''(INTERNAL) Convert east- and northing C{str}s to meter and resolution. ''' else:
'''Convert one or more floats to string, optionally stripped of trailing zero decimals.
@arg floats: Single or a list, sequence, tuple, etc. (C{scalar}s). @kwarg prec: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped if B{C{prec}} is positive, but kept for negative B{C{prec}} values. In addition, trailing decimal zeros are stripped for U{alternate, form '#'<https://docs.Python.org/3/library/stdtypes.html #printf-style-string-formatting>}. @kwarg fmt: Optional, C{float} format (C{str}). @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}). @kwarg sep: Separator joining the B{C{floats}} (C{str}). @kwarg strepr: Optional callable to format non-C{floats} (typically C{repr}, C{str}) or C{None} to raise a TypeError.
@return: The C{sep.join(strs(floats, ...)} joined (C{str}) or single C{strs((floats,), ...)} (C{str}) if B{C{floats}} is C{scalar}. ''' else:
# (INTERNAL) For C{Css.} and C{Lcc.} C{toRepr} and C{toStr} and C{UtmUpsBase._toStr}.
# (INTERNAL) For C{_AzimuthalBase.} and C{CassiniSoldner.} C{toStr} and C{toRepr}.
'''Strip trailing zero decimals from a C{float} string.
@arg efstr: Float with or without exponent (C{str}). @kwarg ap1z: Append the decimal point and one zero decimal if the B{C{efstr}} is all digits (C{bool}).
@return: Float (C{str}). '''
# %.G and %.g formats may drop the decimal # point and all trailing zeros, ... else: # ... insert one dot and zero
'''Return a string for the height value.
@arg height: Height value (C{float}). @kwarg prec: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped if B{C{prec}} is positive, but kept for negative B{C{prec}} values. @kwarg fmt: Optional, C{float} format (C{str}). @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}). @kwarg m: Optional unit of the height (C{str}). '''
'''Return the string representation of an instantiation.
@arg inst: The instance (any C{type}). @arg args: Optional positional arguments. @kwarg kwds: Optional keyword arguments.
@return: Representation (C{str}). '''
'''Convert items to I{name=value} strings, with C{float}s handled like L{fstr}.
@arg items: Name-value pairs (C{dict} or 2-{tuple}s of any C{type}s). @kwarg prec: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped if B{C{prec}} is positive, but kept for negative B{C{prec}} values. @kwarg fmt: Optional, C{float} format (C{str}). @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}). @kwarg sep: Separator joining I{names} and I{values} (C{str}).
@return: A C{tuple(B{sep}.join(t) for t in B{items}))} of C{str}s. ''' # can't unzip empty items tuple, list, etc. except (TypeError, ValueError): raise _IsnotError(dict.__name__, '2-tuples', items=items)
'''(INTERNAL) Prefix C{%} if needed. ''' return fmt if _PERCENT_ in fmt else NN(_PERCENT_, fmt)
'''Convert objects to C{repr} strings, with C{float}s handled like L{fstr}.
@arg objs: List, sequence, tuple, etc. (any C{type}s). @kwarg prec: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped if B{C{prec}} is positive, but kept for negative B{C{prec}} values. @kwarg fmt: Optional, C{float} format (C{str}). @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}).
@return: A C{tuple(map(fstr|repr, objs))} of C{str}s. '''
'''(INTERNAL) Validate C{resolution} in C{meter}. ''' raise ValueError except (ValueError, TypeError): raise Error(resolution=resolution)
'''(INTERNAL) Helper for C{fstr}, C{pairs}, C{reprs} and C{strs} ''' # <https://docs.Python.org/3/library/stdtypes.html#printf-style-string-formatting>
except (TypeError, ValueError): raise _ValueError(fmt=fmt, txt=_not_(repr(_DOTSTAR_)))
else: raise _ValueError(fmt=fmt, txt=_not_(repr(_Fspec_)))
# corner case testLcc lon0=-96.0 t.rstrip(_0_).endswith(_DOT_)): else: t = Fmt.PARENSPACED(Fmt.SQUARE(objs=i), o) raise TypeError(_SPACE_(t, _not_scalar_))
'''Convert objects to C{str} strings, with C{float}s handled like L{fstr}.
@arg objs: List, sequence, tuple, etc. (any C{type}s). @kwarg prec: The C{float} precision, number of decimal digits (0..9). Trailing zero decimals are stripped if B{C{prec}} is positive, but kept for negative B{C{prec}} values. @kwarg fmt: Optional, C{float} format (C{str}). @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}).
@return: A C{tuple(map(fstr|str, objs))} of C{str}s. '''
'''Return the string representation of an invokation.
@arg named: Function, method or class name (C{str}). @arg args: Optional positional arguments. @kwarg kwds: Optional keyword arguments, except C{B{_ELLIPSIS}=False}.
@return: Representation (C{str}). '''
'''(INTERNAL) Int formatter'. '''
'''(INTERNAL) Int and Float formatter'. '''
'''(INTERNAL) Float deg, min, sec formatter'. '''
'''(INTERNAL) Copy attribute values from B{C{other}} to B{C{insto}}.
@arg insto: Object to copy attribute values to (any C{type}). @arg other: Object to copy attribute values from (any C{type}). @arg attrs: One or more attribute names (C{str}s).
@return: Object B{C{insto}}, updated.
@raise AttributeError: An B{C{attrs}} doesn't exist or is not settable. ''' try: n = o._DOT_(a) except AttributeError: n = Fmt.DOT(a) raise _AttributeError(o, name=n)
'''(INTERNAL) Zip C{names} and C{values} into a C{str}, joined and bracketed. ''' except Exception as x: raise _ValueError(names=names, values=values, txt=str(x))
# **) MIT License # # Copyright (C) 2016-2022 -- 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. |