def register(self, item): '''Registed a new item. @arg item: The item (any C{type}). @return: The item name (C{str}). @raise NameError: An B{C{item}} already registered with that name or the B{C{item}} has no, an empty or an invalid name. @raise TypeError: The B{C{item}} type invalid. ''' if not (self._item_classes and isinstance(item, self._item_classes)): raise TypeError('%s: %r' % (_dot_('item', 'type'), item)) try: n = item.name if not (n and n.replace('_', '').isalnum() and isstr(n)): raise ValueError except (AttributeError, ValueError): raise NameError('%s %s: %r' % (_dot_('item', 'name'), 'invalid', item)) if n in self: raise NameError('%s %s: %r' % (self._dot_(n), 'exists', item)) self[n] = item
def _all(globalocals): from pygeodesy.lazily import _dot_ # PYCHOK expected # collect all public module and attribute names and check # that modules are imported from this package, 'pygeodesy' # (but the latter only when not bundled with PyInstaller or # Py2Exe, since the file-layout is different. Courtesy of # GilderGeek<https://GitHub.com/mrJean1/PyGeodesy/issues/31>) ns = list(lazily._ALL_INIT) # XXX ps = () if _isfrozen else set([_pygeodesy] + __name__.split('.')) for mod, attrs in lazily._ALL_LAZY.enums(): if mod not in globalocals: raise ImportError('missing %s: %s' % ('module', _dot_(_pygeodesy, mod))) ns.append(mod) # check that all other public attributes do exist if attrs and isinstance(attrs, tuple): for a in attrs: if a not in globalocals: raise ImportError('missing %s: %s' % ('attribute', _dot_(mod, a))) ns.extend(attrs) # XXX if ps: # check that mod is a _pygeodesy module # XXX m = globalocals[mod] # assert(m.__name__ == mod) # XXX f = getattr(m, '__file__', '') # XXX d = dirname(abspath(f)) if f else pygeodesy_abspath # XXX p = getattr(m, '__package__', '') or _pygeodesy # XXX if p not in ps or d != pygeodesy_abspath: # XXX raise ImportError('foreign module: %s from %r' % (_dot_(p, mod), f or p)) return tuple(set(ns)) # remove duplicates, only R_M?
def __new__(cls, *args): '''New L{_NamedTuple} initialized with B{C{positional}} arguments. ''' self = tuple.__new__(cls, args) ns = self._Names_ if not (isinstance(ns, tuple) and len(ns) > 1): # XXX > 0 raise TypeError('%s invalid: %r' % (_dot_(self.classname, '_Names_'), ns)) if len(ns) != len(args) or not ns: raise ValueError( '%s(%s) invalid: %r[%s] vs %s' % (self.classname, 'args', args, len(args), len(ns))) if _NAME_ in ns: raise NameError('%s invalid: %r' % (_dot_(self.classname, '_Names_'), _NAME_)) return self
def __getattr__(self, name): try: return tuple.__getitem__(self, self._Names_.index(name)) except IndexError: raise IndexError('%s not valid: %r' % (_dot_(self.classname, '<name>'), name)) except ValueError: return tuple.__getattribute__(self, name)
def clipCS3(points, lowerleft, upperright, closed=False, inull=False): '''Clip a path against a rectangular clip box using the U{Cohen-Sutherland <https://WikiPedia.org/wiki/Cohen-Sutherland_algorithm>} algorithm. @arg points: The points (C{LatLon}[]). @arg lowerleft: Bottom-left corner of the clip box (C{LatLon}). @arg upperright: Top-right corner of the clip box (C{LatLon}). @kwarg closed: Optionally, close the path (C{bool}). @kwarg inull: Optionally, include null edges if inside (C{bool}). @return: Yield a L{ClipCS3Tuple}C{(start, end, index)} for each edge of the clipped path. @raise ClipError: The B{C{lowerleft}} and B{C{upperright}} corners specify an invalid clip box. @raise PointsError: Insufficient number of B{C{points}}. ''' cs = _CS(lowerleft, upperright, clipCS3.__name__) n, pts = _points2(points, closed, inull) i, m = _imdex2(closed, n) cmbp = cs.code4(pts[i]) for i in range(m, n): c1, m1, b1, p1 = cmbp c2, m2, b2, p2 = cmbp = cs.code4(pts[i]) if c1 & c2: # edge outside continue if not cs.edge(p1, p2): if inull: # null edge if not c1: yield ClipCS3Tuple(p1, p1, i) elif not c2: yield ClipCS3Tuple(p2, p2, i) continue for _ in range(5): if c1: # clip p1 c1, m1, b1, p1 = m1(b1, p1) elif c2: # clip p2 c2, m2, b2, p2 = m2(b2, p2) else: # inside if inull or _neq(p1, p2): yield ClipCS3Tuple(p1, p2, i) break if c1 & c2: # edge outside break else: # PYCHOK no cover raise AssertionError(_dot_(cs._name, 'for_else'))
def notOverloaded(inst, name, *args, **kwds): # PYCHOK no cover '''Raise an C{AssertionError} for a method or property not overloaded. @arg name: Method, property or name (C{str}). @arg args: Method or property positional arguments (any C{type}s). @arg kwds: Method or property keyword arguments (any C{type}s). ''' n = getattr(name, '__name__', name) n = '%s %s' % (notOverloaded.__name__, _dot_(classname(inst, prefixed=True), n)) m = ', '.join( modulename(c, prefixed=True) for c in inst.__class__.__mro__[1:-1]) raise AssertionError('%s, MRO(%s)' % (unstr(n, *args, **kwds), m))
def intersect(self, p1, p2, edge): # compute intersection # of polygon edge p1 to p2 and the current clip edge, # where p1 and p2 are known to NOT be located on the # same side or on top of the current clip edge # <https://StackOverflow.com/questions/563198/ # how-do-you-detect-where-two-line-segments-intersect> fy = float(p2.lat - p1.lat) fx = float(p2.lon - p1.lon) fp = fy * self._dx - fx * self._dy if abs(fp) < EPS: # PYCHOK no cover raise AssertionError(_dot_(self._name, self.intersect.__name__)) r = fsum_(self._xy, -p1.lat * self._dx, p1.lon * self._dy) / fp y = p1.lat + r * fy x = p1.lon + r * fx return _LLi_(y, x, p1.classof, edge)
def nop4(self, b, p): # PYCHOK no cover if p: # should never get here raise AssertionError(_dot_(self._name, self.nop4.__name__)) return _CS._IN, self.nop4, b, p
def _dot_(self, name): '''(INTERNAL) Period-join C{self.name} and C{name}. ''' return _dot_(self.name, name)
import sys from pygeodesy.lazily import _FOR_DOCS if not _FOR_DOCS: sys.exit( '%s\n' % (' '.join('usage: env PYGEODESY_FOR_DOCS=1 python -m'.split() + sys.argv), )) ls = locals() for n in __all__: if n not in ls: raise NameError('%s %r not in %s' % ('__all__', n, _dot_('named', 'locals'))) for n, o in ls.items(): if n not in __all__ and not n.startswith('_') \ and getattr(o, '__module__', '') == __name__: raise NameError('%s %r not in %s' % ('locals', n, _dot_('named', '__all__'))) print('%s: %s vs %s OK' % (sys.argv[0], '__all__', 'locals')) # **) 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