Coverage for pygeodesy/streprs.py: 96%

269 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-12-02 13:46 -0500

1 

2# -*- coding: utf-8 -*- 

3 

4u'''Floating point and other formatting utilities. 

5''' 

6 

7from pygeodesy.basics import _0_0, isint, islistuple, isscalar, isstr, _zip 

8# from pygeodesy.constants import _0_0 

9from pygeodesy.errors import _AttributeError, _IsnotError, itemsorted, _or, \ 

10 _TypeError, _ValueError, _xkwds_get, _xkwds_pop 

11from pygeodesy.interns import NN, _0_, _0to9_, MISSING, _BAR_, _COMMASPACE_, \ 

12 _DOT_, _dunder_nameof, _E_, _ELLIPSIS_, _EQUAL_, \ 

13 _H_, _LR_PAIRS, _N_, _name_, _not_, _not_scalar_, \ 

14 _PERCENT_, _SPACE_, _STAR_, _UNDER_ 

15from pygeodesy.interns import _convergence_, _distant_, _e_, _eps_, _exceeds_, \ 

16 _EQUALSPACED_, _f_, _F_, _g_, _limit_, _no_, \ 

17 _tolerance_ # PYCHOK used! 

18from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _getenv 

19 

20from math import fabs, log10 as _log10 

21 

22__all__ = _ALL_LAZY.streprs 

23__version__ = '23.06.27' 

24 

25_EN_PREC = 6 # max MGRS/OSGR precision, 1 micrometer 

26_EN_WIDE = 5 # number of MGRS/OSGR units, log10(_100km) 

27_OKd_ = '._-' # acceptable name characters 

28_PAREN_g = '(%g)' # PYCHOK used! 

29_threshold_ = 'threshold' # PYCHOK used! 

30 

31 

32class _Fmt(str): # in .streprs 

33 '''(INTERNAL) Callable formatting. 

34 ''' 

35 name = NN 

36 

37 def __call__(self, *name_value_, **name_value): 

38 '''Format a C{name=value} pair or C{name, value} pair 

39 or just a single C{value}. 

40 ''' 

41 for n, v in name_value.items(): 

42 break 

43 else: 

44 if len(name_value_) > 1: 

45 n, v = name_value_[:2] 

46 elif name_value_: 

47 n, v = NN, name_value_[0] 

48 else: 

49 n, v = NN, MISSING 

50 t = str.__mod__(self, v) 

51 return NN(n, t) if n else t 

52 

53# def __mod__(self, arg, **unused): 

54# '''Regular C{%} operator. 

55# ''' 

56# return str.__mod__(self, arg) 

57 

58 

59class Fstr(str): 

60 '''(INTERNAL) C{float} format. 

61 ''' 

62 name = NN 

63 

64 def __call__(self, flt, prec=None, ints=False): 

65 '''Format the B{C{flt}} like function L{fstr}. 

66 ''' 

67 # see also function C{fstr} if isscalar case below 

68 t = str.__mod__(_pct(self), flt) if prec is None else next( 

69 _streprs(prec, (flt,), self, ints, True, None)) 

70 return t 

71 

72 def __mod__(self, arg, **unused): 

73 '''Regular C{%} operator. 

74 

75 @arg arg: A C{scalar} value to be formatted (either 

76 the C{scalar}, or a 1-tuple C{(scalar,)}, 

77 or 2-tuple C{(prec, scalar)}. 

78 

79 @raise TypeError: Non-scalar B{C{arg}} value. 

80 

81 @raise ValueError: Invalid B{C{arg}}. 

82 ''' 

83 def _error(arg): 

84 n = _DOT_(Fstr.__name__, self.name or self) 

85 return _SPACE_(n, _PERCENT_, repr(arg)) 

86 

87 prec = 6 # default std %f and %F 

88 if islistuple(arg): 

89 n = len(arg) 

90 if n == 1: 

91 arg = arg[0] 

92 elif n == 2: 

93 prec, arg = arg 

94 else: 

95 raise _ValueError(_error(arg)) 

96 

97 if not isscalar(arg): 

98 raise _TypeError(_error(arg)) 

99 return self(arg, prec=prec) 

100 

101 

102class _Sub(str): 

103 '''(INTERNAL) Class list formatter. 

104 ''' 

105 # see .ellipsoidalNvector.LatLon.deltaTo 

106 def __call__(self, *Classes): 

107 t = _or(*(C.__name__ for C in Classes)) 

108 return str.__mod__(self, t or MISSING) 

109 

110 

111class Fmt(object): 

112 '''Formatting options. 

113 ''' 

114 ANGLE = _Fmt('<%s>') 

115 COLON = _Fmt(':%s') 

116# COLONSPACE = _Fmt(': %s') # == _COLONSPACE_(n, v) 

117# COMMASPACE = _Fmt(', %s') # == _COMMASPACE_(n, v) 

118 convergence = _Fmt(_convergence_(_PAREN_g)) 

119 CURLY = _Fmt('{%s}') # BRACES 

120 distant = _Fmt(_distant_('(%.3g)')) 

121 DOT = _Fmt('.%s') # == NN(_DOT_, n) 

122 e = Fstr(_e_) 

123 E = Fstr(_E_) 

124 EQUAL = _Fmt(_EQUAL_(NN, '%s')) 

125 EQUALg = _Fmt(_EQUAL_(NN, '%g')) 

126 EQUALSPACED = _Fmt(_EQUALSPACED_(NN, '%s')) 

127 exceeds_eps = _Fmt(_exceeds_(_eps_, _PAREN_g)) 

128 exceeds_limit = _Fmt(_exceeds_(_limit_, _PAREN_g)) 

129 f = Fstr(_f_) 

130 form = _getenv('PYGEODESY_FMT_FORM', NN) 

131 F = Fstr(_F_) 

132 g = Fstr(_g_) 

133 G = Fstr('G') 

134 h = Fstr('%+.*f') # height, .streprs.hstr 

135 limit = _Fmt(' %s limit') # .units 

136 LOPEN = _Fmt('(%s]') # left-open range (L, R] 

137 PAREN = _Fmt('(%s)') 

138 PAREN_g = _Fmt(_PAREN_g) 

139 PARENSPACED = _Fmt(' (%s)') 

140 QUOTE2 = _Fmt('"%s"') 

141 ROPEN = _Fmt('[%s)') # right-open range [L, R) 

142# SPACE = _Fmt(' %s') # == _SPACE_(n, v) 

143 SQUARE = _Fmt('[%s]') # BRACKETS 

144 sub_class = _Sub('%s (sub-)class') 

145 TAG = ANGLE 

146 TAGEND = _Fmt('</%s>') 

147 tolerance = _Fmt(_tolerance_(_PAREN_g)) 

148 zone = _Fmt('%02d') # .epsg, .mgrs, .utmupsBase 

149 

150 def __init__(self): 

151 for n, a in self.__class__.__dict__.items(): 

152 if isinstance(a, (Fstr, _Fmt)): 

153 setattr(a, _name_, n) 

154 

155 def __call__(self, obj, prec=9): 

156 '''Return C{str(B{obj})} or C{repr(B{obj})}. 

157 ''' 

158 return str(obj) if isint(obj) else next( 

159 _streprs(prec, (obj,), Fmt.g, False, False, repr)) 

160 

161 def no_convergence(self, _d, *tol, **thresh): 

162 t = Fmt.convergence(fabs(_d)) 

163 if tol: 

164 t = _COMMASPACE_(t, Fmt.tolerance(tol[0])) 

165 if thresh and _xkwds_get(thresh, thresh=False): 

166 t = t.replace(_tolerance_, _threshold_) 

167 return _no_(t) 

168 

169Fmt = Fmt() # PYCHOK singleton 

170Fmt.__name__ = Fmt.__class__.__name__ 

171 

172_DOTSTAR_ = Fmt.DOT(_STAR_) 

173# formats %G and %g drop all trailing zeros and the 

174# decimal point, making the float appear as an int 

175_Gg = (Fmt.G, Fmt.g) 

176_FfEeGg = (Fmt.F, Fmt.f, Fmt.E, Fmt.e) + _Gg # float formats 

177_Fspec_ = NN('[%[<flags>][<width>]', _DOTSTAR_, ']', _BAR_.join(_FfEeGg)) # in testStreprs 

178 

179 

180def anstr(name, OKd=_OKd_, sub=_UNDER_): 

181 '''Make a valid name of alphanumeric and OKd characters. 

182 

183 @arg name: The original name (C{str}). 

184 @kwarg OKd: Other acceptable characters (C{str}). 

185 @kwarg sub: Substitute for invalid charactes (C{str}). 

186 

187 @return: The modified name (C{str}). 

188 

189 @note: Leading and trailing whitespace characters are removed, 

190 intermediate whitespace characters are coalesced and 

191 substituted. 

192 ''' 

193 s = n = str(name).strip() 

194 for c in n: 

195 if not (c.isalnum() or c in OKd or c in sub): 

196 s = s.replace(c, _SPACE_) 

197 return sub.join(s.strip().split()) 

198 

199 

200def attrs(inst, *names, **Nones_True__pairs_kwds): # prec=6, fmt=Fmt.F, ints=False, Nones=True, sep=_EQUAL_ 

201 '''Get instance attributes as I{name=value} strings, with C{float}s 

202 formatted by function L{fstr}. 

203 

204 @arg inst: The instance (any C{type}). 

205 @arg names: The attribute names, all other positional (C{str}). 

206 @kwarg Nones_True__pairs_kwds: Keyword argument for function L{pairs}, except 

207 C{B{Nones}=True} to in-/exclude missing or C{None}-valued attributes. 

208 

209 @return: A C{tuple(B{sep}.join(t) for t in zip(B{names}, reprs(values, ...)))} 

210 of C{str}s. 

211 ''' 

212 def _items(inst, names, Nones): 

213 for n in names: 

214 v = getattr(inst, n, None) 

215 if Nones or v is not None: 

216 yield n, v 

217 

218 def _Nones_kwds(Nones=True, **kwds): 

219 return Nones, kwds 

220 

221 Nones, kwds = _Nones_kwds(**Nones_True__pairs_kwds) 

222 return pairs(_items(inst, names, Nones), **kwds) 

223 

224 

225def enstr2(easting, northing, prec, *extras, **wide_dot): 

226 '''Return an MGRS/OSGR easting, northing string representations. 

227 

228 @arg easting: Easting from false easting (C{meter}). 

229 @arg northing: Northing from from false northing (C{meter}). 

230 @arg prec: Precision, the number of I{decimal} digits (C{int}) or if 

231 negative, the number of I{units to drop}, like MGRS U{PRECISION 

232 <https://GeographicLib.SourceForge.io/C++/doc/GeoConvert.1.html#PRECISION>}. 

233 @arg extras: Optional leading items (C{str}s). 

234 @kwarg wide_dot: Optional keword argument C{B{wide}=%d} for the number of I{unit digits} 

235 (C{int}) and C{B{dot}=False} (C{bool}) to insert a decimal point. 

236 

237 @return: B{C{extras}} + 2-tuple C{(str(B{easting}), str(B{northing}))} or 

238 + 2-tuple C{("", "")} for C{B{prec} <= -B{wide}}. 

239 

240 @raise ValueError: Invalid B{C{easting}}, B{C{northing}} or B{C{prec}}. 

241 

242 @note: The B{C{easting}} and B{C{northing}} values are I{truncated, not rounded}. 

243 ''' 

244 t = extras 

245 try: # like .dms.compassPoint 

246 p = min(int(prec), _EN_PREC) 

247 w = p + _xkwds_get(wide_dot, wide=_EN_WIDE) 

248 if w > 0: 

249 f = 10**p # truncate 

250 d = (-p) if p > 0 and _xkwds_get(wide_dot, dot=False) else 0 

251 t += (_0wdot(w, int(easting * f), d), 

252 _0wdot(w, int(northing * f), d)) 

253 else: # prec <= -_EN_WIDE 

254 t += (NN, NN) 

255 except (TypeError, ValueError) as x: 

256 raise _ValueError(easting=easting, northing=northing, prec=prec, cause=x) 

257 return t 

258 

259if enstr2.__doc__: # PYCHOK expected 

260 enstr2.__doc__ %= (_EN_WIDE,) 

261 

262 

263def _enstr2m3(estr, nstr, wide=_EN_WIDE): # in .mgrs, .osgr 

264 '''(INTERNAL) Convert east- and northing C{str}s to meter and resolution. 

265 ''' 

266 def _s2m2(s, m): # e or n str to float meter 

267 if _DOT_ in s: 

268 m = 1 # meter 

269 else: 

270 s += _0_ * wide 

271 s = _DOT_(s[:wide], s[wide:wide+_EN_PREC]) 

272 return float(s), m 

273 

274 e, m = _s2m2(estr, 0) 

275 n, m = _s2m2(nstr, m) 

276 if not m: 

277 p = max(len(estr), len(nstr)) # 2 = Km, 5 = m, 7 = cm 

278 m = 10**max(-_EN_PREC, wide - p) # resolution, meter 

279 return e, n, m 

280 

281 

282def fstr(floats, prec=6, fmt=Fmt.F, ints=False, sep=_COMMASPACE_, strepr=None): 

283 '''Convert one or more floats to string, optionally stripped of trailing zero decimals. 

284 

285 @arg floats: Single or a list, sequence, tuple, etc. (C{scalar}s). 

286 @kwarg prec: The C{float} precision, number of decimal digits (0..9). 

287 Trailing zero decimals are stripped if B{C{prec}} is 

288 positive, but kept for negative B{C{prec}} values. In 

289 addition, trailing decimal zeros are stripped for U{alternate, 

290 form '#'<https://docs.Python.org/3/library/stdtypes.html 

291 #printf-style-string-formatting>}. 

292 @kwarg fmt: Optional, C{float} format (C{str}). 

293 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}). 

294 @kwarg sep: Separator joining the B{C{floats}} (C{str}). 

295 @kwarg strepr: Optional callable to format non-C{floats} (typically 

296 C{repr}, C{str}) or C{None} to raise a TypeError. 

297 

298 @return: The C{sep.join(strs(floats, ...)} joined (C{str}) or single 

299 C{strs((floats,), ...)} (C{str}) if B{C{floats}} is C{scalar}. 

300 ''' 

301 if isscalar(floats): # see Fstr.__call__ above 

302 return next(_streprs(prec, (floats,), fmt, ints, True, strepr)) 

303 else: 

304 return sep.join(_streprs(prec, floats, fmt, ints, True, strepr)) 

305 

306 

307def _fstrENH2(inst, prec, m): # in .css, .lcc, .utmupsBase 

308 # (INTERNAL) For C{Css.} and C{Lcc.} C{toRepr} and C{toStr} and C{UtmUpsBase._toStr}. 

309 t = inst.easting, inst.northing 

310 t = tuple(_streprs(prec, t, Fmt.F, False, True, None)) 

311 T = _E_, _N_ 

312 if m is not None and fabs(inst.height): # fabs(self.height) > EPS 

313 t += hstr(inst.height, prec=-2, m=m), 

314 T += _H_, 

315 return t, T 

316 

317 

318def _fstrLL0(inst, prec, toRepr): # in .azimuthal, .css 

319 # (INTERNAL) For C{_AlbersBase.}, C{_AzimuthalBase.} and C{CassiniSoldner.} 

320 t = tuple(_streprs(prec, inst.latlon0, Fmt.F, False, True, None)) 

321 if toRepr: 

322 n = inst.name 

323 if n: 

324 t += Fmt.EQUAL(_name_, repr(n)), 

325 t = Fmt.PAREN(inst.classname, _COMMASPACE_.join(t)) 

326 return t 

327 

328 

329def fstrzs(efstr, ap1z=False): 

330 '''Strip trailing zero decimals from a C{float} string. 

331 

332 @arg efstr: Float with or without exponent (C{str}). 

333 @kwarg ap1z: Append the decimal point and one zero decimal 

334 if the B{C{efstr}} is all digits (C{bool}). 

335 

336 @return: Float (C{str}). 

337 ''' 

338 s = efstr.find(_DOT_) 

339 if s >= 0: 

340 e = efstr.rfind(Fmt.e) 

341 if e < 0: 

342 e = efstr.rfind(Fmt.E) 

343 if e < 0: 

344 e = len(efstr) 

345 s += 2 # keep 1st _DOT_ + _0_ 

346 if s < e and efstr[e-1] == _0_: 

347 efstr = NN(efstr[:s], efstr[s:e].rstrip(_0_), efstr[e:]) 

348 

349 elif ap1z: 

350 # %.G and %.g formats may drop the decimal 

351 # point and all trailing zeros, ... 

352 if efstr.isdigit(): 

353 efstr += _DOT_ + _0_ # ... append or ... 

354 else: # ... insert one dot and zero 

355 e = efstr.rfind(Fmt.e) 

356 if e < 0: 

357 e = efstr.rfind(Fmt.E) 

358 if e > 0: 

359 efstr = NN(efstr[:e], _DOT_, _0_, efstr[e:]) 

360 

361 return efstr 

362 

363 

364def hstr(height, prec=2, fmt=Fmt.h, ints=False, m=NN): 

365 '''Return a string for the height value. 

366 

367 @arg height: Height value (C{float}). 

368 @kwarg prec: The C{float} precision, number of decimal digits (0..9). 

369 Trailing zero decimals are stripped if B{C{prec}} is 

370 positive, but kept for negative B{C{prec}} values. 

371 @kwarg fmt: Optional, C{float} format (C{str}). 

372 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}). 

373 @kwarg m: Optional unit of the height (C{str}). 

374 ''' 

375 h = next(_streprs(prec, (height,), fmt, ints, True, None)) 

376 return NN(h, str(m)) if m else h 

377 

378 

379def instr(inst, *args, **kwds): 

380 '''Return the string representation of an instantiation. 

381 

382 @arg inst: The instance (any C{type}). 

383 @arg args: Optional positional arguments. 

384 @kwarg kwds: Optional keyword arguments. 

385 

386 @return: Representation (C{str}). 

387 ''' 

388 return unstr(_MODS.named.classname(inst), *args, **kwds) 

389 

390 

391def lrstrip(txt, lrpairs=_LR_PAIRS): 

392 '''Left- I{and} right-strip parentheses, brackets, etc. from a string. 

393 

394 @arg txt: String to be stripped (C{str}). 

395 @kwarg lrpairs: Parentheses, etc. to remove (C{dict} of one or several 

396 C{(Left, Right)} pairs). 

397 

398 @return: Stripped B{C{txt}} (C{str}). 

399 ''' 

400 _e, _s, _n = str.endswith, str.startswith, len 

401 while _n(txt) > 2: 

402 for L, R in lrpairs.items(): 

403 if _e(txt, R) and _s(txt, L): 

404 txt = txt[_n(L):-_n(R)] 

405 break # restart 

406 else: 

407 return txt 

408 

409 

410def pairs(items, prec=6, fmt=Fmt.F, ints=False, sep=_EQUAL_): 

411 '''Convert items to I{name=value} strings, with C{float}s handled like L{fstr}. 

412 

413 @arg items: Name-value pairs (C{dict} or 2-{tuple}s of any C{type}s). 

414 @kwarg prec: The C{float} precision, number of decimal digits (0..9). 

415 Trailing zero decimals are stripped if B{C{prec}} is 

416 positive, but kept for negative B{C{prec}} values. 

417 @kwarg fmt: Optional, C{float} format (C{str}). 

418 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}). 

419 @kwarg sep: Separator joining I{names} and I{values} (C{str}). 

420 

421 @return: A C{tuple(B{sep}.join(t) for t in B{items}))} of C{str}s. 

422 ''' 

423 try: 

424 if isinstance(items, dict): 

425 items = itemsorted(items) 

426 elif not islistuple(items): 

427 items = tuple(items) 

428 # can't unzip empty items tuple, list, etc. 

429 n, v = _zip(*items) if items else ((), ()) # strict=True 

430 except (TypeError, ValueError): 

431 raise _IsnotError(dict.__name__, '2-tuples', items=items) 

432 v = _streprs(prec, v, fmt, ints, False, repr) 

433 return tuple(sep.join(t) for t in _zip(map(str, n), v)) # strict=True 

434 

435 

436def _pct(fmt): 

437 '''(INTERNAL) Prefix C{%} if needed. 

438 ''' 

439 return fmt if _PERCENT_ in fmt else NN(_PERCENT_, fmt) 

440 

441 

442def reprs(objs, prec=6, fmt=Fmt.F, ints=False): 

443 '''Convert objects to C{repr} strings, with C{float}s handled like L{fstr}. 

444 

445 @arg objs: List, sequence, tuple, etc. (any C{type}s). 

446 @kwarg prec: The C{float} precision, number of decimal digits (0..9). 

447 Trailing zero decimals are stripped if B{C{prec}} is 

448 positive, but kept for negative B{C{prec}} values. 

449 @kwarg fmt: Optional, C{float} format (C{str}). 

450 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}). 

451 

452 @return: A C{tuple(map(fstr|repr, objs))} of C{str}s. 

453 ''' 

454 return tuple(_streprs(prec, objs, fmt, ints, False, repr)) if objs else () 

455 

456 

457def _resolution10(resolution, Error=ValueError): # in .mgrs, .osgr 

458 '''(INTERNAL) Validate C{resolution} in C{meter}. 

459 ''' 

460 try: 

461 r = int(_log10(resolution)) 

462 if _EN_WIDE < r or r < -_EN_PREC: 

463 raise ValueError 

464 except (ValueError, TypeError): 

465 raise Error(resolution=resolution) 

466 return _MODS.units.Meter(resolution=10**r) 

467 

468 

469def _streprs(prec, objs, fmt, ints, force, strepr): 

470 '''(INTERNAL) Helper for C{fstr}, C{pairs}, C{reprs} and C{strs} 

471 ''' 

472 # <https://docs.Python.org/3/library/stdtypes.html#printf-style-string-formatting> 

473 if fmt in _FfEeGg: 

474 fGg = fmt in _Gg 

475 fmt = NN(_PERCENT_, _DOT_, abs(prec), fmt) 

476 

477 elif fmt.startswith(_PERCENT_): 

478 fGg = False 

479 try: # to make sure fmt is valid 

480 f = fmt.replace(_DOTSTAR_, Fmt.DOT(abs(prec))) 

481 _ = f % (_0_0,) 

482 except (TypeError, ValueError): 

483 raise _ValueError(fmt=fmt, txt=_not_(repr(_DOTSTAR_))) 

484 fmt = f 

485 

486 else: 

487 raise _ValueError(fmt=fmt, txt=_not_(repr(_Fspec_))) 

488 

489 for i, o in enumerate(objs): 

490 if force or isinstance(o, float): 

491 t = fmt % (float(o),) 

492 if ints and t.rstrip(_0to9_ if isint(o, both=True) else 

493 _0_).endswith(_DOT_): 

494 t = t.split(_DOT_)[0] 

495 elif prec > 1: 

496 t = fstrzs(t, ap1z=fGg) 

497 elif strepr: 

498 t = strepr(o) 

499 else: 

500 t = Fmt.PARENSPACED(Fmt.SQUARE(objs=i), o) 

501 raise TypeError(_SPACE_(t, _not_scalar_)) 

502 yield t 

503 

504 

505def strs(objs, prec=6, fmt=Fmt.F, ints=False): 

506 '''Convert objects to C{str} strings, with C{float}s handled like L{fstr}. 

507 

508 @arg objs: List, sequence, tuple, etc. (any C{type}s). 

509 @kwarg prec: The C{float} precision, number of decimal digits (0..9). 

510 Trailing zero decimals are stripped if B{C{prec}} is 

511 positive, but kept for negative B{C{prec}} values. 

512 @kwarg fmt: Optional, C{float} format (C{str}). 

513 @kwarg ints: Optionally, remove the decimal dot for C{int} values (C{bool}). 

514 

515 @return: A C{tuple(map(fstr|str, objs))} of C{str}s. 

516 ''' 

517 return tuple(_streprs(prec, objs, fmt, ints, False, str)) if objs else () 

518 

519 

520def unstr(where, *args, **kwds): 

521 '''Return the string representation of an invokation. 

522 

523 @arg where: Class, function, method (C{type}) or name (C{str}). 

524 @arg args: Optional positional arguments. 

525 @kwarg kwds: Optional keyword arguments, except 

526 C{B{_ELLIPSIS}=False}. 

527 

528 @return: Representation (C{str}). 

529 ''' 

530 t = reprs(args, fmt=Fmt.g) if args else () 

531 if kwds and _xkwds_pop(kwds, _ELLIPSIS=False): 

532 t += _ELLIPSIS_, 

533 if kwds: 

534 t += pairs(itemsorted(kwds), fmt=Fmt.g) 

535 n = where if isstr(where) else _dunder_nameof(where) 

536 return Fmt.PAREN(n, _COMMASPACE_.join(t)) 

537 

538 

539def _0wd(*w_i): # in .osgr, .wgrs 

540 '''(INTERNAL) Int formatter'. 

541 ''' 

542 return '%0*d' % w_i 

543 

544 

545def _0wdot(w, f, dot=0): 

546 '''(INTERNAL) Int and Float formatter'. 

547 ''' 

548 s = _0wd(w, int(f)) 

549 if dot: 

550 s = _DOT_(s[:dot], s[dot:]) 

551 return s 

552 

553 

554def _0wpF(*w_p_f): # in .dms, .osgr 

555 '''(INTERNAL) Float deg, min, sec formatter'. 

556 ''' 

557 return '%0*.*f' % w_p_f # XXX was F 

558 

559 

560def _xattrs(insto, other, *attrs): # see .errors._xattr 

561 '''(INTERNAL) Copy attribute values from B{C{other}} to B{C{insto}}. 

562 

563 @arg insto: Object to copy attribute values to (any C{type}). 

564 @arg other: Object to copy attribute values from (any C{type}). 

565 @arg attrs: One or more attribute names (C{str}s). 

566 

567 @return: Object B{C{insto}}, updated. 

568 

569 @raise AttributeError: An B{C{attrs}} doesn't exist 

570 or is not settable. 

571 ''' 

572 def _getattr(o, a): 

573 if hasattr(o, a): 

574 return getattr(o, a) 

575 try: 

576 n = o._DOT_(a) 

577 except AttributeError: 

578 n = Fmt.DOT(a) 

579 raise _AttributeError(o, name=n) 

580 

581 for a in attrs: 

582 s = _getattr(other, a) 

583 g = _getattr(insto, a) 

584 if (g is None and s is not None) or g != s: 

585 setattr(insto, a, s) # not settable? 

586 return insto 

587 

588 

589def _xzipairs(names, values, sep=_COMMASPACE_, fmt=NN, pair_fmt=Fmt.COLON): 

590 '''(INTERNAL) Zip C{names} and C{values} into a C{str}, joined and bracketed. 

591 ''' 

592 try: 

593 t = sep.join(pair_fmt(*t) for t in _zip(names, values)) # strict=True 

594 except Exception as x: 

595 raise _ValueError(names=names, values=values, cause=x) 

596 return (fmt % (t,)) if fmt else t 

597 

598# **) MIT License 

599# 

600# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved. 

601# 

602# Permission is hereby granted, free of charge, to any person obtaining a 

603# copy of this software and associated documentation files (the "Software"), 

604# to deal in the Software without restriction, including without limitation 

605# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

606# and/or sell copies of the Software, and to permit persons to whom the 

607# Software is furnished to do so, subject to the following conditions: 

608# 

609# The above copyright notice and this permission notice shall be included 

610# in all copies or substantial portions of the Software. 

611# 

612# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

613# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

614# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

615# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

616# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

617# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

618# OTHER DEALINGS IN THE SOFTWARE.