Coverage for pygeodesy/streprs.py: 96%
270 statements
« prev ^ index » next coverage.py v7.2.2, created at 2024-04-24 16:06 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2024-04-24 16:06 -0400
2# -*- coding: utf-8 -*-
4u'''Floating point and other formatting utilities.
5'''
7from pygeodesy.basics import isint, islistuple, isscalar, isstr, itemsorted, \
8 _zip, _0_0
9# from pygeodesy.constants import _0_0
10from pygeodesy.errors import _or, _AttributeError, _IsnotError, _TypeError, \
11 _ValueError, _xkwds_get, _xkwds_item2, _xkwds_pop2
12from pygeodesy.interns import NN, _0_, _0to9_, MISSING, _BAR_, _COMMASPACE_, \
13 _DOT_, _E_, _ELLIPSIS_, _EQUAL_, _H_, _LR_PAIRS, \
14 _N_, _name_, _not_, _not_scalar_, _PERCENT_, \
15 _SPACE_, _STAR_, _UNDER_, _dunder_nameof
16from pygeodesy.interns import _convergence_, _distant_, _e_, _eps_, _exceeds_, \
17 _EQUALSPACED_, _f_, _F_, _g_, _limit_, _no_, \
18 _tolerance_ # PYCHOK used!
19from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS
21from math import fabs, log10 as _log10
23__all__ = _ALL_LAZY.streprs
24__version__ = '24.03.20'
26_EN_PREC = 6 # max MGRS/OSGR precision, 1 micrometer
27_EN_WIDE = 5 # number of MGRS/OSGR units, log10(_100km)
28_OKd_ = '._-' # acceptable name characters
29_PAREN_g = '(%g)' # PYCHOK used!
30_threshold_ = 'threshold' # PYCHOK used!
33class _Fmt(str):
34 '''(INTERNAL) Callable formatting.
35 '''
36 name = NN
38 def __call__(self, *name_value_, **name_value):
39 '''Format a C{name=value} pair or C{name, value} pair or
40 just a single C{value}.
41 '''
42 for n, v in name_value.items():
43 break
44 else:
45 n, v = name_value_[:2] if len(name_value_) > 1 else (NN,
46 (name_value_[0] if name_value_ else MISSING))
47 t = str.__mod__(self, v)
48 return NN(n, t) if n else t
50# def __mod__(self, arg, **unused):
51# '''Regular C{%} operator.
52# '''
53# return str.__mod__(self, arg)
56class Fstr(str):
57 '''(INTERNAL) C{float} format.
58 '''
59 name = NN
61 def __call__(self, flt, prec=None, ints=False):
62 '''Format the B{C{flt}} like function L{fstr}.
63 '''
64 # see also function C{fstr} if isscalar case below
65 t = str.__mod__(_pct(self), flt) if prec is None else next(
66 _streprs(prec, (flt,), self, ints, True, None))
67 return t
69 def __mod__(self, arg, **unused):
70 '''Regular C{%} operator.
72 @arg arg: A C{scalar} value to be formatted (either
73 the C{scalar}, or a 1-tuple C{(scalar,)},
74 or 2-tuple C{(prec, scalar)}.
76 @raise TypeError: Non-scalar B{C{arg}} value.
78 @raise ValueError: Invalid B{C{arg}}.
79 '''
80 def _error(arg):
81 n = _DOT_(Fstr.__name__, self.name or self)
82 return _SPACE_(n, _PERCENT_, repr(arg))
84 prec = 6 # default std %f and %F
85 if islistuple(arg):
86 n = len(arg)
87 if n == 1:
88 arg = arg[0]
89 elif n == 2:
90 prec, arg = arg
91 else:
92 raise _ValueError(_error(arg))
94 if not isscalar(arg):
95 raise _TypeError(_error(arg))
96 return self(arg, prec=prec) # Fstr.__call__(self, arg, prec=prec)
99class _Sub(str):
100 '''(INTERNAL) Class list formatter.
101 '''
102 # see .ellipsoidalNvector.LatLon.deltaTo
103 def __call__(self, *Classes):
104 t = _or(*(C.__name__ for C in Classes))
105 return str.__mod__(self, t or MISSING)
108class Fmt(object):
109 '''Formatting options.
110 '''
111 ANGLE = _Fmt('<%s>')
112 COLON = _Fmt(':%s')
113# COLONSPACE = _Fmt(': %s') # == _COLONSPACE_(n, v)
114# COMMASPACE = _Fmt(', %s') # == _COMMASPACE_(n, v)
115 convergence = _Fmt(_convergence_(_PAREN_g))
116 CURLY = _Fmt('{%s}') # BRACES
117 distant = _Fmt(_distant_('(%.3g)'))
118 DOT = _Fmt('.%s') # == NN(_DOT_, n)
119 e = Fstr(_e_)
120 E = Fstr(_E_)
121 EQUAL = _Fmt(_EQUAL_(NN, '%s'))
122 EQUALg = _Fmt(_EQUAL_(NN, '%g'))
123 EQUALSPACED = _Fmt(_EQUALSPACED_(NN, '%s'))
124 exceeds_eps = _Fmt(_exceeds_(_eps_, _PAREN_g))
125 exceeds_limit = _Fmt(_exceeds_(_limit_, _PAREN_g))
126 f = Fstr(_f_)
127 F = Fstr(_F_)
128 g = Fstr(_g_)
129 G = Fstr('G')
130 h = Fstr('%+.*f') # height, .streprs.hstr
131 limit = _Fmt(' %s limit') # .units
132 LOPEN = _Fmt('(%s]') # left-open range (L, R]
133 PAREN = _Fmt('(%s)')
134 PAREN_g = _Fmt(_PAREN_g)
135 PARENSPACED = _Fmt(' (%s)')
136 QUOTE2 = _Fmt('"%s"')
137 ROPEN = _Fmt('[%s)') # right-open range [L, R)
138# SPACE = _Fmt(' %s') # == _SPACE_(n, v)
139 SQUARE = _Fmt('[%s]') # BRACKETS
140 sub_class = _Sub('%s (sub-)class')
141 TAG = ANGLE
142 TAGEND = _Fmt('</%s>')
143 tolerance = _Fmt(_tolerance_(_PAREN_g))
144 zone = _Fmt('%02d') # .epsg, .mgrs, .utmupsBase
146 def __init__(self):
147 for n, a in self.__class__.__dict__.items():
148 if isinstance(a, (Fstr, _Fmt)):
149 setattr(a, _name_, n)
151 def __call__(self, obj, prec=9):
152 '''Return C{str(B{obj})} or C{repr(B{obj})}.
153 '''
154 return str(obj) if isint(obj) else next(
155 _streprs(prec, (obj,), Fmt.g, False, False, repr))
157 def INDEX(self, name=NN, i=None, **name_i):
158 '''Return C{"B{name}" if B{i} is None else "B{name}[B{i}]"}.
159 '''
160 if name_i:
161 name, i = _xkwds_item2(name_i)
162 return name if i is None else self.SQUARE(name, i)
164 def no_convergence(self, _d, *tol, **thresh):
165 '''Return C{"no convergence (B{_d})"}, C{"no convergence
166 (B{_d}), tolerance (B{tol})"} or C{"no convergence
167 (B{_d}), threshold (B{tol})"}.
168 '''
169 t = Fmt.convergence(fabs(_d))
170 if tol:
171 t = _COMMASPACE_(t, Fmt.tolerance(tol[0]))
172 if thresh and _xkwds_get(thresh, thresh=False):
173 t = t.replace(_tolerance_, _threshold_)
174 return _no_(t)
176Fmt = Fmt() # PYCHOK singleton
177Fmt.__name__ = Fmt.__class__.__name__
179_DOTSTAR_ = Fmt.DOT(_STAR_)
180# formats %G and %g drop all trailing zeros and the
181# decimal point, making the float appear as an int
182_Gg = (Fmt.G, Fmt.g)
183_FfEeGg = (Fmt.F, Fmt.f, Fmt.E, Fmt.e) + _Gg # float formats
184_Fspec_ = NN('[%[<flags>][<width>]', _DOTSTAR_, ']', _BAR_.join(_FfEeGg)) # in testStreprs
187def anstr(name, OKd=_OKd_, sub=_UNDER_):
188 '''Make a valid name of alphanumeric and OKd characters.
190 @arg name: The original name (C{str}).
191 @kwarg OKd: Other acceptable characters (C{str}).
192 @kwarg sub: Substitute for invalid charactes (C{str}).
194 @return: The modified name (C{str}).
196 @note: Leading and trailing whitespace characters are removed,
197 intermediate whitespace characters are coalesced and
198 substituted.
199 '''
200 s = n = str(name).strip()
201 for c in n:
202 if not (c.isalnum() or c in OKd or c in sub):
203 s = s.replace(c, _SPACE_)
204 return sub.join(s.strip().split())
207def attrs(inst, *names, **Nones_True__pairs_kwds): # prec=6, fmt=Fmt.F, ints=False, Nones=True, sep=_EQUAL_
208 '''Get instance attributes as I{name=value} strings, with C{float}s
209 formatted by function L{fstr}.
211 @arg inst: The instance (any C{type}).
212 @arg names: The attribute names, all other positional (C{str}).
213 @kwarg Nones_True__pairs_kwds: Keyword argument for function L{pairs}, except
214 C{B{Nones}=True} to in-/exclude missing or C{None}-valued attributes.
216 @return: A C{tuple(B{sep}.join(t) for t in zip(B{names}, reprs(values, ...)))}
217 of C{str}s.
218 '''
219 def _items(inst, names, Nones):
220 for n in names:
221 v = getattr(inst, n, None)
222 if Nones or v is not None:
223 yield n, v
225 def _Nones_kwds(Nones=True, **kwds):
226 return Nones, kwds
228 Nones, kwds = _Nones_kwds(**Nones_True__pairs_kwds)
229 return pairs(_items(inst, names, Nones), **kwds)
232def enstr2(easting, northing, prec, *extras, **wide_dot):
233 '''Return an MGRS/OSGR easting, northing string representations.
235 @arg easting: Easting from false easting (C{meter}).
236 @arg northing: Northing from from false northing (C{meter}).
237 @arg prec: Precision, the number of I{decimal} digits (C{int}) or if
238 negative, the number of I{units to drop}, like MGRS U{PRECISION
239 <https://GeographicLib.SourceForge.io/C++/doc/GeoConvert.1.html#PRECISION>}.
240 @arg extras: Optional leading items (C{str}s).
241 @kwarg wide_dot: Optional keword argument C{B{wide}=%d} for the number of I{unit digits}
242 (C{int}) and C{B{dot}=False} (C{bool}) to insert a decimal point.
244 @return: B{C{extras}} + 2-tuple C{(str(B{easting}), str(B{northing}))} or
245 + 2-tuple C{("", "")} for C{B{prec} <= -B{wide}}.
247 @raise ValueError: Invalid B{C{easting}}, B{C{northing}} or B{C{prec}}.
249 @note: The B{C{easting}} and B{C{northing}} values are I{truncated, not rounded}.
250 '''
251 t = extras
252 try: # like .dms.compassPoint
253 p = min(int(prec), _EN_PREC)
254 w = p + _xkwds_get(wide_dot, wide=_EN_WIDE)
255 if w > 0:
256 f = 10**p # truncate
257 d = (-p) if p > 0 and _xkwds_get(wide_dot, dot=False) else 0
258 t += (_0wdot(w, int(easting * f), d),
259 _0wdot(w, int(northing * f), d))
260 else: # prec <= -_EN_WIDE
261 t += (NN, NN)
262 except (TypeError, ValueError) as x:
263 raise _ValueError(easting=easting, northing=northing, prec=prec, cause=x)
264 return t
266if enstr2.__doc__: # PYCHOK expected
267 enstr2.__doc__ %= (_EN_WIDE,)
270def _enstr2m3(estr, nstr, wide=_EN_WIDE): # in .mgrs, .osgr
271 '''(INTERNAL) Convert east- and northing C{str}s to meter and resolution.
272 '''
273 def _s2m2(s, m): # e or n str to float meter
274 if _DOT_ in s:
275 m = 1 # meter
276 else:
277 s += _0_ * wide
278 s = _DOT_(s[:wide], s[wide:wide+_EN_PREC])
279 return float(s), m
281 e, m = _s2m2(estr, 0)
282 n, m = _s2m2(nstr, m)
283 if not m:
284 p = max(len(estr), len(nstr)) # 2 = Km, 5 = m, 7 = cm
285 m = 10**max(-_EN_PREC, wide - p) # resolution, meter
286 return e, n, m
289def fstr(floats, prec=6, fmt=Fmt.F, ints=False, sep=_COMMASPACE_, strepr=None):
290 '''Convert one or more floats to string, optionally stripped of trailing zero decimals.
292 @arg floats: Single or a list, sequence, tuple, etc. (C{scalar}s).
293 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
294 Trailing zero decimals are stripped if B{C{prec}} is
295 positive, but kept for negative B{C{prec}} values. In
296 addition, trailing decimal zeros are stripped for U{alternate,
297 form '#'<https://docs.Python.org/3/library/stdtypes.html
298 #printf-style-string-formatting>}.
299 @kwarg fmt: Optional C{float} format (C{letter}).
300 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}).
301 @kwarg sep: Separator joining the B{C{floats}} (C{str}).
302 @kwarg strepr: Optional callable to format non-C{floats} (typically
303 C{repr}, C{str}) or C{None} to raise a TypeError.
305 @return: The C{sep.join(strs(floats, ...)} joined (C{str}) or single
306 C{strs((floats,), ...)} (C{str}) if B{C{floats}} is C{scalar}.
307 '''
308 if isscalar(floats): # see Fstr.__call__ above
309 return next(_streprs(prec, (floats,), fmt, ints, True, strepr))
310 else:
311 return sep.join(_streprs(prec, floats, fmt, ints, True, strepr))
314def _fstrENH2(inst, prec, m, fmt=Fmt.F): # in .css, .lcc, .utmupsBase
315 # (INTERNAL) For C{Css.} and C{Lcc.} C{toRepr} and C{toStr} and C{UtmUpsBase._toStr}.
316 t = inst.easting, inst.northing
317 t = tuple(_streprs(prec, t, fmt, False, True, None))
318 T = _E_, _N_
319 if m is not None and fabs(inst.height): # fabs(self.height) > EPS
320 t += hstr(inst.height, prec=-2, m=m),
321 T += _H_,
322 return t, T
325def _fstrLL0(inst, prec, toRepr): # in .azimuthal, .css
326 # (INTERNAL) For C{_AlbersBase.}, C{_AzimuthalBase.} and C{CassiniSoldner.}
327 t = tuple(_streprs(prec, inst.latlon0, Fmt.F, False, True, None))
328 if toRepr:
329 n = inst.name
330 if n:
331 t += Fmt.EQUAL(_name_, repr(n)),
332 t = Fmt.PAREN(inst.classname, _COMMASPACE_.join(t))
333 return t
336def fstrzs(efstr, ap1z=False):
337 '''Strip trailing zero decimals from a C{float} string.
339 @arg efstr: Float with or without exponent (C{str}).
340 @kwarg ap1z: Append the decimal point and one zero decimal
341 if the B{C{efstr}} is all digits (C{bool}).
343 @return: Float (C{str}).
344 '''
345 s = efstr.find(_DOT_)
346 if s >= 0:
347 e = efstr.rfind(Fmt.e)
348 if e < 0:
349 e = efstr.rfind(Fmt.E)
350 if e < 0:
351 e = len(efstr)
352 s += 2 # keep 1st _DOT_ + _0_
353 if s < e and efstr[e-1] == _0_:
354 efstr = NN(efstr[:s], efstr[s:e].rstrip(_0_), efstr[e:])
356 elif ap1z:
357 # %.G and %.g formats may drop the decimal
358 # point and all trailing zeros, ...
359 if efstr.isdigit():
360 efstr += _DOT_ + _0_ # ... append or ...
361 else: # ... insert one dot and zero
362 e = efstr.rfind(Fmt.e)
363 if e < 0:
364 e = efstr.rfind(Fmt.E)
365 if e > 0:
366 efstr = NN(efstr[:e], _DOT_, _0_, efstr[e:])
368 return efstr
371def hstr(height, prec=2, fmt=Fmt.h, ints=False, m=NN):
372 '''Return a string for the height value.
374 @arg height: Height value (C{float}).
375 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
376 Trailing zero decimals are stripped if B{C{prec}} is
377 positive, but kept for negative B{C{prec}} values.
378 @kwarg fmt: Optional C{float} format (C{letter}).
379 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}).
380 @kwarg m: Optional unit of the height (C{str}).
381 '''
382 h = next(_streprs(prec, (height,), fmt, ints, True, None))
383 return NN(h, str(m)) if m else h
386def instr(inst, *args, **kwds):
387 '''Return the string representation of an instantiation.
389 @arg inst: The instance (any C{type}).
390 @arg args: Optional positional arguments.
391 @kwarg kwds: Optional keyword arguments.
393 @return: Representation (C{str}).
394 '''
395 return unstr(_MODS.named.classname(inst), *args, **kwds)
398def lrstrip(txt, lrpairs=_LR_PAIRS):
399 '''Left- I{and} right-strip parentheses, brackets, etc. from a string.
401 @arg txt: String to be stripped (C{str}).
402 @kwarg lrpairs: Parentheses, etc. to remove (C{dict} of one or several
403 C{(Left, Right)} pairs).
405 @return: Stripped B{C{txt}} (C{str}).
406 '''
407 _e, _s, _n = str.endswith, str.startswith, len
408 while _n(txt) > 2:
409 for L, R in lrpairs.items():
410 if _e(txt, R) and _s(txt, L):
411 txt = txt[_n(L):-_n(R)]
412 break # restart
413 else:
414 return txt
417def pairs(items, prec=6, fmt=Fmt.F, ints=False, sep=_EQUAL_):
418 '''Convert items to I{name=value} strings, with C{float}s handled like L{fstr}.
420 @arg items: Name-value pairs (C{dict} or 2-{tuple}s of any C{type}s).
421 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
422 Trailing zero decimals are stripped if B{C{prec}} is
423 positive, but kept for negative B{C{prec}} values.
424 @kwarg fmt: Optional C{float} format (C{letter}).
425 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}).
426 @kwarg sep: Separator joining I{names} and I{values} (C{str}).
428 @return: A C{tuple(B{sep}.join(t) for t in B{items}))} of C{str}s.
429 '''
430 try:
431 if isinstance(items, dict):
432 items = itemsorted(items)
433 elif not islistuple(items):
434 items = tuple(items)
435 # can't unzip empty items tuple, list, etc.
436 n, v = _zip(*items) if items else ((), ()) # strict=True
437 except (TypeError, ValueError):
438 raise _IsnotError(dict.__name__, '2-tuples', items=items)
439 v = _streprs(prec, v, fmt, ints, False, repr)
440 return tuple(sep.join(t) for t in _zip(map(str, n), v)) # strict=True
443def _pct(fmt):
444 '''(INTERNAL) Prefix C{%} if needed.
445 '''
446 return fmt if _PERCENT_ in fmt else NN(_PERCENT_, fmt)
449def reprs(objs, prec=6, fmt=Fmt.F, ints=False):
450 '''Convert objects to C{repr} strings, with C{float}s handled like L{fstr}.
452 @arg objs: List, sequence, tuple, etc. (any C{type}s).
453 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
454 Trailing zero decimals are stripped if B{C{prec}} is
455 positive, but kept for negative B{C{prec}} values.
456 @kwarg fmt: Optional C{float} format (C{letter}).
457 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}).
459 @return: A C{tuple(map(fstr|repr, objs))} of C{str}s.
460 '''
461 return tuple(_streprs(prec, objs, fmt, ints, False, repr)) if objs else ()
464def _resolution10(resolution, Error=ValueError): # in .mgrs, .osgr
465 '''(INTERNAL) Validate C{resolution} in C{meter}.
466 '''
467 try:
468 r = int(_log10(resolution))
469 if _EN_WIDE < r or r < -_EN_PREC:
470 raise ValueError
471 except (ValueError, TypeError):
472 raise Error(resolution=resolution)
473 return _MODS.units.Meter(resolution=10**r)
476def _streprs(prec, objs, fmt, ints, force, strepr):
477 '''(INTERNAL) Helper for C{fstr}, C{pairs}, C{reprs} and C{strs}
478 '''
479 # <https://docs.Python.org/3/library/stdtypes.html#printf-style-string-formatting>
480 if fmt in _FfEeGg:
481 fGg = fmt in _Gg
482 fmt = NN(_PERCENT_, _DOT_, abs(prec), fmt)
484 elif fmt.startswith(_PERCENT_):
485 fGg = False
486 try: # to make sure fmt is valid
487 f = fmt.replace(_DOTSTAR_, Fmt.DOT(abs(prec)))
488 _ = f % (_0_0,)
489 except (TypeError, ValueError):
490 raise _ValueError(fmt=fmt, txt=_not_(repr(_DOTSTAR_)))
491 fmt = f
493 else:
494 raise _ValueError(fmt=fmt, txt=_not_(repr(_Fspec_)))
496 for i, o in enumerate(objs):
497 if force or isinstance(o, float):
498 t = fmt % (float(o),)
499 if ints and t.rstrip(_0to9_ if isint(o, both=True) else
500 _0_).endswith(_DOT_):
501 t = t.split(_DOT_)[0]
502 elif prec > 1:
503 t = fstrzs(t, ap1z=fGg)
504 elif strepr:
505 t = strepr(o)
506 else:
507 t = Fmt.PARENSPACED(Fmt.SQUARE(objs=i), o)
508 raise TypeError(_SPACE_(t, _not_scalar_))
509 yield t
512def strs(objs, prec=6, fmt=Fmt.F, ints=False):
513 '''Convert objects to C{str} strings, with C{float}s handled like L{fstr}.
515 @arg objs: List, sequence, tuple, etc. (any C{type}s).
516 @kwarg prec: The C{float} precision, number of decimal digits (0..9).
517 Trailing zero decimals are stripped if B{C{prec}} is
518 positive, but kept for negative B{C{prec}} values.
519 @kwarg fmt: Optional C{float} format (C{letter}).
520 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}).
522 @return: A C{tuple(map(fstr|str, objs))} of C{str}s.
523 '''
524 return tuple(_streprs(prec, objs, fmt, ints, False, str)) if objs else ()
527def unstr(where, *args, **kwds):
528 '''Return the string representation of an invokation.
530 @arg where: Class, function, method (C{type}) or name (C{str}).
531 @arg args: Optional positional arguments.
532 @kwarg kwds: Optional keyword arguments, except C{B{_fmt}=Fmt.g}
533 and C{B{_ELLIPSIS}=False}.
535 @return: Representation (C{str}).
536 '''
537 e, kwds = _xkwds_pop2(kwds, _ELLIPSIS=False)
538 g, kwds = _xkwds_pop2(kwds, _fmt=Fmt.g)
539 t = reprs(args, fmt=g) if args else ()
540 if e:
541 t += _ELLIPSIS_,
542 if kwds:
543 t += pairs(itemsorted(kwds), fmt=g)
544 n = where if isstr(where) else _dunder_nameof(where) # _NN_
545 return Fmt.PAREN(n, _COMMASPACE_.join(t))
548def _0wd(*w_i): # in .osgr, .wgrs
549 '''(INTERNAL) Int formatter'.
550 '''
551 return '%0*d' % w_i
554def _0wdot(w, f, dot=0):
555 '''(INTERNAL) Int and Float formatter'.
556 '''
557 s = _0wd(w, int(f))
558 if dot:
559 s = _DOT_(s[:dot], s[dot:])
560 return s
563def _0wpF(*w_p_f): # in .dms, .osgr
564 '''(INTERNAL) Float deg, min, sec formatter'.
565 '''
566 return '%0*.*f' % w_p_f # XXX was F
569def _xattrs(insto, other, *attrs): # see .errors._xattr
570 '''(INTERNAL) Copy attribute values from B{C{other}} to B{C{insto}}.
572 @arg insto: Object to copy attribute values to (any C{type}).
573 @arg other: Object to copy attribute values from (any C{type}).
574 @arg attrs: One or more attribute names (C{str}s).
576 @return: Object B{C{insto}}, updated.
578 @raise AttributeError: An B{C{attrs}} doesn't exist
579 or is not settable.
580 '''
581 def _getattr(o, a):
582 if hasattr(o, a):
583 return getattr(o, a)
584 try:
585 n = o._DOT_(a)
586 except AttributeError:
587 n = Fmt.DOT(a)
588 raise _AttributeError(o, name=n)
590 for a in attrs:
591 s = _getattr(other, a)
592 g = _getattr(insto, a)
593 if (g is None and s is not None) or g != s:
594 setattr(insto, a, s) # not settable?
595 return insto
598def _xzipairs(names, values, sep=_COMMASPACE_, fmt=NN, pair_fmt=Fmt.COLON):
599 '''(INTERNAL) Zip C{names} and C{values} into a C{str}, joined and bracketed.
600 '''
601 try:
602 t = sep.join(pair_fmt(*t) for t in _zip(names, values)) # strict=True
603 except Exception as x:
604 raise _ValueError(names=names, values=values, cause=x)
605 return (fmt % (t,)) if fmt else t # enc
607# **) MIT License
608#
609# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
610#
611# Permission is hereby granted, free of charge, to any person obtaining a
612# copy of this software and associated documentation files (the "Software"),
613# to deal in the Software without restriction, including without limitation
614# the rights to use, copy, modify, merge, publish, distribute, sublicense,
615# and/or sell copies of the Software, and to permit persons to whom the
616# Software is furnished to do so, subject to the following conditions:
617#
618# The above copyright notice and this permission notice shall be included
619# in all copies or substantial portions of the Software.
620#
621# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
622# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
623# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
624# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
625# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
626# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
627# OTHER DEALINGS IN THE SOFTWARE.