def exec_module(self, module): toolz_mods = self._load_toolz(module.__name__) fast_mod = toolz_mods.get('cytoolz') or toolz_mods['toolz'] slow_mod = toolz_mods.get('toolz') or toolz_mods['cytoolz'] module.__dict__.update(toolz.merge(fast_mod.__dict__, module.__dict__)) package = fast_mod.__package__ if package is not None: package, dot, submodules = package.partition('.') module.__package__ = ''.join(['tlz', dot, submodules]) if not module.__doc__: module.__doc__ = fast_mod.__doc__ # show file from toolz during introspection module.__file__ = slow_mod.__file__ for k, v in fast_mod.__dict__.items(): tv = slow_mod.__dict__.get(k) try: hash(tv) except TypeError: tv = None if tv in self.always_from_toolz: module.__dict__[k] = tv elif (isinstance(v, types.ModuleType) and v.__package__ == fast_mod.__name__): package, dot, submodules = v.__name__.partition('.') module_name = ''.join(['tlz', dot, submodules]) submodule = import_module(module_name) module.__dict__[k] = submodule
def _load_toolz(self, fullname): rv = {} package, dot, submodules = fullname.partition('.') try: module_name = ''.join(['cytoolz', dot, submodules]) rv['cytoolz'] = import_module(module_name) except ImportError: pass try: module_name = ''.join(['toolz', dot, submodules]) rv['toolz'] = import_module(module_name) except ImportError: pass if not rv: raise ImportError(fullname) return rv
def exec_module(self, module): toolz_mods = self._load_toolz(module.__name__) fast_mod = toolz_mods.get("cytoolz") or toolz_mods["toolz"] slow_mod = toolz_mods.get("toolz") or toolz_mods["cytoolz"] module.__dict__.update(toolz.merge(fast_mod.__dict__, module.__dict__)) package = fast_mod.__package__ if package is not None: package, dot, submodules = package.partition(".") module.__package__ = "".join(["tlz", dot, submodules]) if not module.__doc__: module.__doc__ = fast_mod.__doc__ # show file from toolz during introspection module.__file__ = slow_mod.__file__ for k, v in fast_mod.__dict__.items(): tv = slow_mod.__dict__.get(k) try: hash(tv) except TypeError: tv = None if tv in self.always_from_toolz: module.__dict__[k] = tv elif isinstance(v, types.ModuleType) and v.__package__ == fast_mod.__name__: package, dot, submodules = v.__name__.partition(".") module_name = "".join(["tlz", dot, submodules]) submodule = import_module(module_name) module.__dict__[k] = submodule
def _load_toolz(self, fullname): rv = {} package, dot, submodules = fullname.partition(".") try: module_name = "".join(["cytoolz", dot, submodules]) rv["cytoolz"] = import_module(module_name) except ImportError: pass try: module_name = "".join(["toolz", dot, submodules]) rv["toolz"] = import_module(module_name) except ImportError: pass if not rv: raise ImportError(fullname) return rv
def test_curried_namespace(): exceptions = import_module('toolz.curried.exceptions') namespace = {} def should_curry(func): if not callable(func) or isinstance(func, toolz.curry): return False nargs = toolz.functoolz.num_required_args(func) if nargs is None or nargs > 1: return True return nargs == 1 and toolz.functoolz.has_keywords(func) def curry_namespace(ns): return dict((name, toolz.curry(f) if should_curry(f) else f) for name, f in ns.items() if '__' not in name) from_toolz = curry_namespace(vars(toolz)) from_exceptions = curry_namespace(vars(exceptions)) namespace.update(toolz.merge(from_toolz, from_exceptions)) namespace = toolz.valfilter(callable, namespace) curried_namespace = toolz.valfilter(callable, toolz.curried.__dict__) if namespace != curried_namespace: missing = set(namespace) - set(curried_namespace) if missing: raise AssertionError( 'There are missing functions in toolz.curried:\n %s' % ' \n'.join(sorted(missing))) extra = set(curried_namespace) - set(namespace) if extra: raise AssertionError( 'There are extra functions in toolz.curried:\n %s' % ' \n'.join(sorted(extra))) unequal = toolz.merge_with(list, namespace, curried_namespace) unequal = toolz.valfilter(lambda x: x[0] != x[1], unequal) messages = [] for name, (orig_func, auto_func) in sorted(unequal.items()): if name in from_exceptions: messages.append( '%s should come from toolz.curried.exceptions' % name) elif should_curry(getattr(toolz, name)): messages.append('%s should be curried from toolz' % name) else: messages.append( '%s should come from toolz and NOT be curried' % name) raise AssertionError('\n'.join(messages))
def test_curried_namespace(): exceptions = import_module('toolz.curried.exceptions') namespace = {} def should_curry(func): if not callable(func) or isinstance(func, toolz.curry): return False nargs = toolz.functoolz.num_required_args(func) if nargs is None or nargs > 1: return True return nargs == 1 and toolz.functoolz.has_keywords(func) def curry_namespace(ns): return dict( (name, toolz.curry(f) if should_curry(f) else f) for name, f in ns.items() if '__' not in name ) from_toolz = curry_namespace(vars(toolz)) from_exceptions = curry_namespace(vars(exceptions)) namespace.update(toolz.merge(from_toolz, from_exceptions)) namespace = toolz.valfilter(callable, namespace) curried_namespace = toolz.valfilter(callable, toolz.curried.__dict__) if namespace != curried_namespace: missing = set(namespace) - set(curried_namespace) if missing: raise AssertionError('There are missing functions in toolz.curried:\n %s' % ' \n'.join(sorted(missing))) extra = set(curried_namespace) - set(namespace) if extra: raise AssertionError('There are extra functions in toolz.curried:\n %s' % ' \n'.join(sorted(extra))) unequal = toolz.merge_with(list, namespace, curried_namespace) unequal = toolz.valfilter(lambda x: x[0] != x[1], unequal) messages = [] for name, (orig_func, auto_func) in sorted(unequal.items()): if name in from_exceptions: messages.append('%s should come from toolz.curried.exceptions' % name) elif should_curry(getattr(toolz, name)): messages.append('%s should be curried from toolz' % name) else: messages.append('%s should come from toolz and NOT be curried' % name) raise AssertionError('\n'.join(messages))