Пример #1
0
def html_escape(s):
    """HTML-escape a string or object

    This converts any non-string objects passed into it to strings
    (actually, using ``unicode()``).  All values returned are
    non-unicode strings (using ``&#num;`` entities for all non-ASCII
    characters).

    None is treated specially, and returns the empty string.
    """
    if s is None:
        return ''
    __html__ = getattr(s, '__html__', None)
    if __html__ is not None and callable(__html__):
        return s.__html__()
    if not isinstance(s, string_types):
        __unicode__ = getattr(s, '__unicode__', None)
        if __unicode__ is not None and callable(__unicode__):
            s = s.__unicode__()
        else:
            s = str(s)
    s = escape(s, True)
    if isinstance(s, text_type):
        s = s.encode('ascii', 'xmlcharrefreplace')
    return text_(s)
Пример #2
0
 def fire(self, callable, value):
     """
     Clear C{self.pending} to avoid a reference cycle and then invoke the
     callable with the value.
     """
     self.pending = None
     callable(value)
Пример #3
0
def _revlinkcfg(replace, templates):
    '''Helper function that returns suitable macros and functions
       for building revision links depending on replacement mechanism
'''

    assert not replace or callable(replace) or isinstance(replace, dict) or \
           isinstance(replace, str) or isinstance(replace, unicode)

    if not replace:
        return lambda rev, repo: None
    else:
        if callable(replace):
            return  lambda rev, repo: replace(rev, repo)
        elif isinstance(replace, dict): # TODO: test for [] instead
            def filter(rev, repo):
                url = replace.get(repo)
                if url:
                    return url % urllib.quote(rev)
                else:
                    return None

            return filter
        else:
            return lambda rev, repo: replace % urllib.quote(rev)

    assert False, '_replace has a bad type, but we should never get here'
Пример #4
0
		def collect_files():
			if callable(custom_files):
				try:
					files = custom_files()
					if files:
						return files
				except:
					_logger.exception("Error while trying to retrieve tracked files for plugin {}".format(key))

			templates = _get_all_templates()
			assets = _get_all_assets()
			translations = _get_all_translationfiles(g.locale.language if g.locale else "en",
			                                         "messages")

			files = templates + assets + translations

			if callable(additional_files):
				try:
					af = additional_files()
					if af:
						files += af
				except:
					_logger.exception("Error while trying to retrieve additional tracked files for plugin {}".format(key))

			return sorted(set(files))
Пример #5
0
def lookup_field(name, obj, model_admin=None):
    opts = obj._meta
    try:
        f = opts.get_field(name)
    except models.FieldDoesNotExist:
        # For non-field values, the value is either a method, property or
        # returned via a callable.
        if callable(name):
            attr = name
            value = attr(obj)
        elif (model_admin is not None and hasattr(model_admin, name) and
              not name == '__str__' and not name == '__unicode__'):
            attr = getattr(model_admin, name)
            value = attr(obj)
        else:
            if is_rel_field(name,obj):
                parts = name.split("__")
                rel_name,sub_rel_name = parts[0],"__".join(parts[1:])
                rel_obj =  getattr(obj,rel_name)
                if rel_obj is not None:
                    return lookup_field(sub_rel_name,rel_obj,model_admin)
            attr = getattr(obj, name)
            if callable(attr):
                value = attr()
            else:
                value = attr
        f = None
    else:
        attr = None
        value = getattr(obj, name)
    return f, attr, value
Пример #6
0
    def get_urls(self):
        """
        Unlike the standard MenuItemGroup usage, which would yield
        individual items, this one collects into a complete iterable so
        that it may be de-duplicated for any list urls.
        """
        queryset = self.get_queryset()
        final_urls = set()
        for obj in queryset:

            abs_url = getattr(obj, 'get_absolute_url')
            if callable(abs_url):
                abs_url = abs_url()

            abs_obj = ModelURI(path=abs_url, title=get_title(obj),
                               model_instance=obj)
            if abs_obj not in final_urls:
                final_urls.add(abs_obj)

            # there's no definitive way I'd like the title part of the next
            # bit to work, so we just shove the URL in as the title.
            list_url = getattr(obj, 'get_list_url', None)
            if list_url is None:
                continue

            if callable(list_url):
                list_url = list_url()

            list_title = get_list_title(obj, url=list_url)
            list_obj = URI(path=list_url, title=list_title)
            if list_title is not None and list_obj not in final_urls:
                final_urls.add(list_obj)
        return final_urls
Пример #7
0
def get_callable(lookup_view, can_fail=False):
    """
    Convert a string version of a function name to the callable object.

    If the lookup_view is not an import path, it is assumed to be a URL pattern
    label and the original string is returned.

    If can_fail is True, lookup_view might be a URL pattern label, so errors
    during the import fail and the string is returned.
    """
    if not callable(lookup_view):
        try:
            # Bail early for non-ASCII strings (they can't be functions).
            lookup_view = lookup_view.encode('ascii')
            mod_name, func_name = get_mod_func(lookup_view)
            if func_name != '':
                lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
                if not callable(lookup_view):
                    raise AttributeError("'%s.%s' is not a callable." % (mod_name, func_name))
        except (ImportError, AttributeError):
            if not can_fail:
                raise
        except UnicodeEncodeError:
            pass
    return lookup_view
def _get_estimator_spec(mode,
                        gan_model,
                        loss_fn,
                        get_eval_metric_ops_fn,
                        generator_optimizer,
                        discriminator_optimizer,
                        get_hooks_fn=None):
  """Get the EstimatorSpec for the current mode."""
  if mode == model_fn_lib.ModeKeys.PREDICT:
    estimator_spec = model_fn_lib.EstimatorSpec(
        mode=mode, predictions=gan_model.generated_data)
  else:
    gan_loss = loss_fn(gan_model)
    if mode == model_fn_lib.ModeKeys.EVAL:
      estimator_spec = _get_eval_estimator_spec(gan_model, gan_loss,
                                                get_eval_metric_ops_fn)
    else:  # model_fn_lib.ModeKeys.TRAIN:
      gopt = (
          generator_optimizer()
          if callable(generator_optimizer) else generator_optimizer)
      dopt = (
          discriminator_optimizer()
          if callable(discriminator_optimizer) else discriminator_optimizer)
      get_hooks_fn = get_hooks_fn or tfgan_train.get_sequential_train_hooks()
      estimator_spec = _get_train_estimator_spec(gan_model, gan_loss, gopt,
                                                 dopt, get_hooks_fn)

  return estimator_spec
Пример #9
0
    def register_action(self, event_name, action_object, *args, **kwargs):
        action_name = str(action_object)
        if ismethod(action_object) and callable(action_object):
            action_object = SingleAction(action_object, *args, **kwargs)
        elif isfunction(action_object) and callable(action_object):
            action_object = SingleAction(action_object, *args, **kwargs)
        elif not isinstance(action_object, SingleAction):
            action_object = SingleAction.from_string(action_object)

        if action_object is None:
            logger.error('action_object is None')
            return False

        if 'single_fire_action' in kwargs.keys() and kwargs['single_fire_action'] is True:
            action_object.single_fire_action = True
            del kwargs['single_fire_action']

        if event_name in self.__Actions.keys():
            self.__Actions[event_name][action_name] = action_object
            logger.trace("action %s was added to event %s", action_object, event_name)
        else:
            self.__Actions[event_name] = { action_name: action_object }
            logger.trace("action %s was added to new evententry %s", action_object, event_name)

        return action_object
Пример #10
0
    def _execute_task_pool(self, task, works, on_finished):
        if not callable(task):
            raise Exception(_("Invalid function to execute is passed as a parameter of the thread"))
        works_q = Queue.Queue()
        result_q = Queue.Queue()

        # Create the "thread pool"
        pool = [WorkerThread(task=task, works_q=works_q, result_q=result_q) for i in range(self.max_process)]

        # Start all threads
        for thread in pool:
            thread.start()

        # Give the workers some work to do
        self.work_count = 0
        for work in works:
            self.work_count += 1
            works_q.put((work, works[work]["params"]))

        # Ask threads to die and wait for them to do it
        for thread in pool:
            thread.join()

        # Now get all the results
        while not result_q.empty():
            # Blocking 'get' from a Queue.
            result = result_q.get()
            work = result[0]
            works[work]["result"] = result[1]
            works[work]["error"] = result[2]
            self.work_count -= 1

        if callable(on_finished):
            on_finished(works)
Пример #11
0
	def get_form_cfg(self, req, moddef, section, value=None):
		if self.read_cap and not req.has_permission(self.read_cap):
			return None
		field_name = '.'.join((moddef, section.name, self.name))
		loc = req.localizer
		if callable(self.field_cfg):
			cfg = self.field_cfg(req, moddef, section, value)
		else:
			cfg = self.field_cfg.copy()
		cfg.update({
			'name'        : field_name,
			'fieldLabel'  : loc.translate(self.title) if self.title else self.name,
			'description' : loc.translate(self.help_text) if self.help_text else None
		})
		if value is None and self.default is not None:
			value = self.default
		if value is not None and self.client_ok:
			cfg['value'] = self.format_param(value)
			if self.type == 'bool':
				cfg['checked'] = value
		extra = self.field_extra
		if extra:
			if callable(extra):
				extra = extra(req, moddef, section, value)
			cfg.update(extra)
		return cfg
Пример #12
0
    def dynamic_wrapper(wrapped, instance, args, kwargs):
        transaction = current_transaction()

        if transaction is None:
            return wrapped(*args, **kwargs)

        if callable(name):
            if instance is not None:
                _name = name(instance, *args, **kwargs)
            else:
                _name = name(*args, **kwargs)

        elif name is None:
            _name = callable_name(wrapped)

        else:
            _name = name

        if callable(group):
            if instance is not None:
                _group = group(instance, *args, **kwargs)
            else:
                _group = group(*args, **kwargs)

        else:
            _group = group

        transaction.set_transaction_name(_name, _group, priority)

        return wrapped(*args, **kwargs)
Пример #13
0
    def dispatch(self, request, *args, **kwargs):
        self.request = request
        self.args = args
        self.kwargs = kwargs

        if (self.response and not isinstance(self.response, HttpResponse)
           and not callable(self.response)):
            raise TypeError("The `response` keyword argument must "
                            "be a either HttpResponse instance or "
                            "callable with `request` argument")

        if self.request.method == self.method:
            key = self.cache_key()

            if cache.get(key):
                if callable(self.response):
                    return self.response(request)

                elif self.response:
                    return self.response

                else:
                    return HttpResponseForbidden('Try slowing down a little.')

            cache.set(key, 1, self.duration)

        return super(ThrottleMixin, self).dispatch(request, *args, **kwargs)
Пример #14
0
 def test_it(self):
     from zope.interface import Interface
     from substanced.interfaces import IUserLocator
     config = DummyConfigurator(registry=self.config.registry)
     class UserLocator(object):
         def __init__(self, context, request):
             self.group_ids = ['group1', 'group2']
         def get_groupids(self, userid):
             return self.group_ids
     self._callFUT(config, UserLocator)
     self.assertEqual(len(config.actions), 1)
     action = config.actions[0]
     self.assertEqual(
         action['discriminator'],
         ('sd-user-locator',)
         )
     self.assertEqual(
         action['introspectables'], (config.intr,)
         )
     self.assertEqual(config.intr['cls'], UserLocator)
     callable = action['callable']
     callable()
     adapter = self.config.registry.getMultiAdapter((Interface, Interface),
             IUserLocator)
     self.assertEqual(
             adapter.get_groupids('user'), ['group1', 'group2']
         )
Пример #15
0
def lookup_field(name, obj, model_admin=None):
    opts = obj._meta
    try:
        f = _get_non_gfk_field(opts, name)
    except (FieldDoesNotExist, FieldIsAForeignKeyColumnName):
        # For non-field values, the value is either a method, property or
        # returned via a callable.
        if callable(name):
            attr = name
            value = attr(obj)
        elif (model_admin is not None and
                hasattr(model_admin, name) and
                not name == '__str__' and
                not name == '__unicode__'):
            attr = getattr(model_admin, name)
            value = attr(obj)
        else:
            attr = getattr(obj, name)
            if callable(attr):
                value = attr()
            else:
                value = attr
        f = None
    else:
        attr = None
        value = getattr(obj, name)
    return f, attr, value
Пример #16
0
    def _extend_view(output, r, **attr):
        """
            Add additional view variables (invokes all callables)

            @param output: the output dict
            @param r: the S3Request
            @param attr: the view variables (e.g. 'rheader')

            @note: overload this method in subclasses if you don't want
                   additional view variables to be added automatically
        """

        if r.interactive and isinstance(output, dict):
            for key in attr:
                handler = attr[key]
                if callable(handler):
                    resolve = True
                    try:
                        display = handler(r)
                    except TypeError:
                        # Argument list failure
                        # => pass callable to the view as-is
                        display = handler
                        continue
                    except:
                        # Propagate all other errors to the caller
                        raise
                else:
                    display = handler
                if isinstance(display, dict) and resolve:
                    output.update(**display)
                elif display is not None:
                    output.update(**{key: display})
                elif key in output and callable(handler):
                    del output[key]
Пример #17
0
    def __init__(self, verbose_name=None, accessor=None, default=None,
                 visible=True, orderable=None, attrs=None, order_by=None,
                 sortable=None, empty_values=None, localize=None):
        if not (accessor is None or isinstance(accessor, six.string_types) or
                callable(accessor)):
            raise TypeError('accessor must be a string or callable, not %s' %
                            type(accessor).__name__)
        if callable(accessor) and default is not None:
            raise TypeError('accessor must be string when default is used, not callable')
        self.accessor = A(accessor) if accessor else None
        self._default = default
        self.verbose_name = verbose_name
        self.visible = visible
        if sortable is not None:
            warnings.warn('`sortable` is deprecated, use `orderable` instead.',
                          DeprecationWarning)
            # if orderable hasn't been specified, we'll use sortable's value
            if orderable is None:
                orderable = sortable
        self.orderable = orderable
        self.attrs = attrs or {}
        # massage order_by into an OrderByTuple or None
        order_by = (order_by, ) if isinstance(order_by, six.string_types) else order_by
        self.order_by = OrderByTuple(order_by) if order_by is not None else None
        if empty_values is not None:
            self.empty_values = empty_values

        self.localize = localize

        self.creation_counter = Column.creation_counter
        Column.creation_counter += 1
Пример #18
0
def normalize_subs_input(subs):
    "Accept a callable, a list, or a dict. Normalize to a dict of lists."
    normalized = {name: [] for name in SUBS_NAMES}
    if subs is None:
        pass
    elif callable(subs):
        normalized['all'].append(subs)
    elif hasattr(subs, 'items'):
        for key, funcs in list(subs.items()):
            if key not in SUBS_NAMES:
                raise KeyError("Keys must be one of {!r:0}".format(SUBS_NAMES))
            if callable(funcs):
                normalized[key].append(funcs)
            else:
                normalized[key].extend(funcs)
    elif isinstance(subs, Iterable):
        normalized['all'].extend(subs)
    else:
        raise ValueError("Subscriptions should be a callable, a list of "
                         "callables, or a dictionary mapping subscription "
                         "names to lists of callables.")
    # Validates that all entries are callables.
    for name, funcs in normalized.items():
        for func in funcs:
            if not callable(func):
                raise ValueError("subs values must be functions or lists "
                                "of functions. The offending entry is\n "
                                "{0}".format(func))
    return normalized
Пример #19
0
def help(player, string):
    """Display help a console command."""
    if string:
        try:
            func = globals()[string]
            if callable(func) and func.__doc__:
                for s in func.__doc__.split('\n'):
                    FantasyDemo.addChatMsg(-1, s)

            else:
                raise 'Not callable'
        except:
            FantasyDemo.addChatMsg(-1, 'No help for ' + string)

    else:
        isCallable = lambda x: callable(globals()[x])
        ignoreList = ('getV4FromString', 'help')
        notIgnored = lambda x: x not in ignoreList
        keys = filter(isCallable, globals().keys())
        keys = filter(notIgnored, keys)
        keys.sort()
        FantasyDemo.addChatMsg(-1, '/help {command} for more info.')
        stripper = lambda c: c not in '[]\'"'
        string = filter(stripper, str(keys))
        FantasyDemo.addChatMsg(-1, string)
Пример #20
0
 def __init__(self, tag="a", attr="href", unique=False, process_value=None):
     FixedSGMLParser.__init__(self)
     self.scan_tag = tag if callable(tag) else lambda t: t == tag
     self.scan_attr = attr if callable(attr) else lambda a: a == attr
     self.process_value = (lambda v: v) if process_value is None else process_value
     self.current_link = None
     self.unique = unique
Пример #21
0
    def walk_services(self, managername=None, **kwargs):
        """
        Walk a blueprint's services and execute callbacks.

        * `before_services(manager):`
          Executed before a service manager's dependencies are enumerated.
        * `service(manager, service):`
          Executed when a service is enumerated.
        * `after_services(manager):`
          Executed after a service manager's dependencies are enumerated.
        """

        # Unless otherwise specified, walk all service managers.
        if managername is None:
            for managername in sorted(self.services.iterkeys()):
                self.walk_services(managername, **kwargs)
            return

        manager = managers.ServiceManager(managername)

        kwargs.get('before_services', lambda *args: None)(manager)

        callable = kwargs.get('service', lambda *args: None)
        for service, deps in sorted(self.services.get(manager,
                                                      {}).iteritems()):
            callable(manager, service)
            self.walk_service_files(manager, service, **kwargs)
            self.walk_service_packages(manager, service, **kwargs)
            self.walk_service_sources(manager, service, **kwargs)

        kwargs.get('after_services', lambda *args: None)(manager)
Пример #22
0
    def _getFieldValues(self, obj):
        """Finds the specified field values and returns them if
        they contain file objects which are too large.  Specifically,
        it returns an iterator of tuples containing the type of storage,
        the field name, and the value stored"""
        max_size  = self.max_size

        # Search for fields stored via AnnotationStorage
        annotations = getattr(obj, '__annotations__', None)
        if annotations is not None:
            annotation_names = (ANNOTATION_PREFIX + name for name in
                                                              self.field_names)
            for name in annotation_names:
                val = annotations.get(name, None)
                # Skip linked Pdata chains too long for the pickler
                if hasattr(aq_base(val), 'getSize') and callable(val.getSize):
                    try:
                        size = val.getSize()
                    except (TypeError,AttributeError):
                        size = None
                    if isinstance(size, (int, long)) and size >= max_size:
                        yield 'annotation', name, val

        # Search for fields stored via AttributeStorage
        for name in self.field_names:
            val = getattr(obj, name, None)
            # Skip linked Pdata chains too long for the pickler
            if hasattr(aq_base(val), 'getSize') and callable(val.getSize):
                size = val.getSize()
                if isinstance(size, (int, long)) and size >= max_size:
                    yield 'attribute', name, val
Пример #23
0
def preprocess(func, c, expose_request=False):
    """
    A decorator that facilitates preprocessing per method. Setting
    C{expose_request} to C{True} will set the underlying request object (if
    there is one), usually HTTP and set it to the first argument of the
    preprocessing callable. If there is no request object, the default is
    C{None}.

    @raise TypeError: C{func} and preprocessor must be callable.
    """
    if not callable(func):
        raise TypeError('func must be callable')

    if not callable(c):
        raise TypeError('Preprocessor must be callable')

    attr = func

    if isinstance(func, types.UnboundMethodType):
        attr = func.im_func

    if expose_request is True:
        c = globals()['expose_request'](c)

    setattr(attr, '_pyamf_preprocessor', c)

    return func
Пример #24
0
 def __init__(self, stmt="pass", setup="pass", timer=default_timer):
     """Constructor.  See class doc string."""
     self.timer = timer
     ns = {}
     if isinstance(stmt, str):
         # Check that the code can be compiled outside a function
         if isinstance(setup, str):
             compile(setup, dummy_src_name, "exec")
             compile(setup + '\n' + stmt, dummy_src_name, "exec")
         else:
             compile(stmt, dummy_src_name, "exec")
         stmt = reindent(stmt, 8)
         if isinstance(setup, str):
             setup = reindent(setup, 4)
             src = template.format(stmt=stmt, setup=setup)
         elif callable(setup):
             src = template.format(stmt=stmt, setup='_setup()')
             ns['_setup'] = setup
         else:
             raise ValueError("setup is neither a string nor callable")
         self.src = src # Save for traceback display
         code = compile(src, dummy_src_name, "exec")
         exec(code, globals(), ns)
         self.inner = ns["inner"]
     elif callable(stmt):
         self.src = None
         if isinstance(setup, str):
             _setup = setup
             def setup():
                 exec(_setup, globals(), ns)
         elif not callable(setup):
             raise ValueError("setup is neither a string nor callable")
         self.inner = _template_func(setup, stmt)
     else:
         raise ValueError("stmt is neither a string nor callable")
Пример #25
0
def instance_cache_key(instance, label=None, version=None, args=None,
                       kwargs=None):
    """Extends the base `cache_key` function to include model instance metadata
    such as the type and primary key. In addition the `version` can be a
    property or method name on the instance which will be evaluated.
    """
    if not instance or not instance.pk:
        raise ValueError('model instances must have a primary key')

    # The version may be an attribute on the instance
    if isinstance(version, basestring) and hasattr(instance, version):
        version = getattr(instance, version)

        # Bound method
        if callable(version):
            version = version()

    # Plain function takes the instance
    elif callable(version):
        version = version(instance)

    opts = instance._meta
    key = [opts.app_label, opts.model_name, instance.pk]

    if label is not None:
        key.append(label)

    label = cache_key_func(key)

    return cache_key(label=label, version=version, args=args, kwargs=kwargs)
Пример #26
0
    def handle_command(self, string):
        """
            parse the commandstring and handle the command

            the string is parsed into commands, and they are searched in the commandList-tree
            the function returns an answerstring or that the command was ambigious
        """
        # necessary since shlex does not support unicode prior to python 2.7.3
        string = string.encode("utf-8")
        s = shlex.split(string)
        if not type(s) == list:
            s=[s,]
        if s[0] in self.commandList:
            if callable(self.commandList[s[0]]):
                return self._exec(self.commandList[s[0]], s[1:])
            else:
                return "Command \""+s[0]+"\" ambigious: "+", ".join(self.commandList[s[0]])
        s.reverse()
        cur=self.commandTree
        while len(s) > 0:
            n = s.pop() 
            if not n in cur:
                return None
            if callable(cur[n]):
                s.reverse()
                return self._exec(cur[n], s)
            else:
                cur=cur[n]
Пример #27
0
def init_spawn_value(value, validator=None):
    """
    Analyze the prototype value and produce a value useful at the point of spawning.

    Args:
        value (any): This can be:
            callable - will be called as callable()
            (callable, (args,)) - will be called as callable(*args)
            other - will be assigned depending on the variable type
            validator (callable, optional): If given, this will be called with the value to
                check and guarantee the outcome is of a given type.

    Returns:
        any (any): The (potentially pre-processed value to use for this prototype key)

    """
    value = protfunc_parser(value)
    validator = validator if validator else lambda o: o
    if callable(value):
        return validator(value())
    elif value and is_iter(value) and callable(value[0]):
        # a structure (callable, (args, ))
        args = value[1:]
        return validator(value[0](*make_iter(args)))
    else:
        return validator(value)
Пример #28
0
 def assertRaisesErrorWithMessage(self, error, message, callable,
     *args, **kwargs):
     self.assertRaises(error, callable, *args, **kwargs)
     try:
         callable(*args, **kwargs)
     except error, e:
         self.assertEqual(message, str(e))
Пример #29
0
def xop_request(ctx, request):
	# TODO: add optional redirect-to-site?
	if not ctx.can_access(request):
		raise HTTPForbidden('Access Denied')
	gw = ctx.get_gateway()
	if (not gw) or (not hasattr(gw, 'process_request')):
		raise HTTPForbidden('Access Denied')
	if not callable(gw.process_request):
		raise HTTPForbidden('Access Denied')

	try:
		sess = DBSession()
		xoplist = gw.process_request(request, sess)
	except Exception as e:
		# TODO: cancel and log?
		raise HTTPForbidden('Access Denied')
	for xop in xoplist:
		ctx.check_operation(xop)
		sess.add(xop)

	if hasattr(gw, 'generate_response') and callable(gw.generate_response):
		return gw.generate_response(request, xoplist)

	resp = Response(body='OK', content_type='text/plain', charset='UTF-8')
	return resp
Пример #30
0
 def prepareToShrinkSpray(spray, sprayProp, origin, target):
     if callable(target):
         target = target()
     if callable(origin):
         origin = origin()
     sprayProp.setPos(Point3(0.0, -SPRAY_LEN, 0.0))
     spray.setPos(target)
Пример #31
0
    def __init__(self,
                 model,
                 masker,
                 *,
                 partition_tree=None,
                 output_names=None,
                 link=links.identity):
        """ Uses the Partition SHAP method to explain the output of any function.

        Partition SHAP computes Shapley values recursively through a hierarchy of features, this
        hierarchy defines feature coalitions and results in the Owen values from game theory. The
        PartitionExplainer has two particularly nice properties: 1) PartitionExplainer is
        model-agnostic but when using a balanced partition tree only has quadradic exact runtime 
        (in term of the number of input features). This is in contrast to the exponential exact
        runtime of KernalExplainer or SamplingExplainer. 2) PartitionExplainer always assigns to groups of
        correlated features the credit that set of features would have had if treated as a group. This
        means if the hierarchical clustering given to PartitionExplainer groups correlated features
        together, then feature correlations are "accounted for" ... in the sense that the total credit assigned
        to a group of tightly dependent features does net depend on how they behave if their correlation
        structure was broken during the explanation's perterbation process. Note that for linear models
        the Owen values that PartitionExplainer returns are the same as the standard non-hierarchical
        Shapley values.


        Parameters
        ----------
        model : function
            User supplied function that takes a matrix of samples (# samples x # features) and
            computes the output of the model for those samples.

        masker : function or numpy.array or pandas.DataFrame or tokenizer
            The function used to "mask" out hidden features of the form `masker(mask, x)`. It takes a
            single input sample and a binary mask and returns a matrix of masked samples. These
            masked samples will then be evaluated using the model function and the outputs averaged.
            As a shortcut for the standard masking using by SHAP you can pass a background data matrix
            instead of a function and that matrix will be used for masking. Domain specific masking
            functions are available in shap such as shap.maksers.Image for images and shap.maskers.Text
            for text.

        partition_tree : None or function or numpy.array
            A hierarchical clustering of the input features represented by a matrix that follows the format
            used by scipy.cluster.hierarchy (see the notebooks_html/partition_explainer directory an example).
            If this is a function then the function produces a clustering matrix when given a single input
            example. If you are using a standard SHAP masker object then you can pass masker.clustering
            to use that masker's built-in clustering of the features, or if partition_tree is None then
            masker.clustering will be used by default.
        
        Examples
        --------
        See :ref:`Partition Explainer Examples <partition_explainer_examples>`
        """

        super(Partition, self).__init__(model, masker, algorithm="partition")

        warnings.warn(
            "explainers.Partition is still in an alpha state, so use with caution..."
        )

        # convert dataframes
        # if safe_isinstance(masker, "pandas.core.frame.DataFrame"):
        #     masker = TabularMasker(masker)
        # elif safe_isinstance(masker, "numpy.ndarray") and len(masker.shape) == 2:
        #     masker = TabularMasker(masker)
        # elif safe_isinstance(masker, "transformers.PreTrainedTokenizer"):
        #     masker = TextMasker(masker)
        # self.masker = masker

        # TODO: maybe? if we have a tabular masker then we build a PermutationExplainer that we
        # will use for sampling
        self.input_shape = masker.shape[1:] if hasattr(
            masker, "shape") and not callable(masker.shape) else None
        self.output_names = output_names

        self.model = lambda x: np.array(model(x))
        self.expected_value = None
        self._curr_base_value = None
        if getattr(self.masker, "clustering", None) is None:
            raise ValueError(
                "The passed masker must have a .clustering attribute defined! Try shap.maskers.Partition(data) for example."
            )
        # if partition_tree is None:
        #     if not hasattr(masker, "partition_tree"):
        #         raise ValueError("The passed masker does not have masker.clustering, so the partition_tree must be passed!")
        #     self.partition_tree = masker.clustering
        # else:
        #     self.partition_tree = partition_tree

        # handle higher dimensional tensor inputs
        if self.input_shape is not None and len(self.input_shape) > 1:
            self._reshaped_model = lambda x: self.model(
                x.reshape(x.shape[0], *self.input_shape))
        else:
            self._reshaped_model = self.model

        # if we don't have a dynamic clustering algorithm then can precowe mpute
        # a lot of information
        if not callable(self.masker.clustering):
            self._clustering = self.masker.clustering
            self._mask_matrix = make_masks(self._clustering)
Пример #32
0
def kernel(mf, mo_coeff, mo_occ, conv_tol=1e-10, conv_tol_grad=None,
           max_cycle=50, dump_chk=True,
           callback=None, verbose=logger.NOTE):
    cput0 = (time.clock(), time.time())
    log = logger.new_logger(mf, verbose)
    mol = mf._scf.mol
    if mol != mf.mol:
        logger.warn(mf, 'dual-basis SOSCF is an experimental feature. It is '
                    'still in testing.')

    if conv_tol_grad is None:
        conv_tol_grad = numpy.sqrt(conv_tol)
        log.info('Set conv_tol_grad to %g', conv_tol_grad)
    scf_conv = False
    e_tot = mf.e_tot

# call mf._scf.get_hcore, mf._scf.get_ovlp because they might be overloaded
    h1e = mf._scf.get_hcore(mol)
    s1e = mf._scf.get_ovlp(mol)
    dm = mf.make_rdm1(mo_coeff, mo_occ)
# call mf._scf.get_veff, to avoid "newton().density_fit()" polluting get_veff
    vhf = mf._scf.get_veff(mol, dm)
    e_tot = mf._scf.energy_tot(dm, h1e, vhf)
    fock = mf.get_fock(h1e, s1e, vhf, dm, level_shift_factor=0)
    log.info('Initial guess E= %.15g  |g|= %g', e_tot,
             numpy.linalg.norm(mf._scf.get_grad(mo_coeff, mo_occ, fock)))
# NOTE: DO NOT change the initial guess mo_occ, mo_coeff
    mo_energy, mo_tmp = mf.eig(fock, s1e)
    mf.get_occ(mo_energy, mo_tmp)

    if dump_chk and mf.chkfile:
        chkfile.save_mol(mol, mf.chkfile)

# Copy the integral file to soscf object to avoid the integrals being cached
# twice.
    if mol == mf.mol and not getattr(mf, 'with_df', None):
        mf._eri = mf._scf._eri

    rotaiter = rotate_orb_cc(mf, mo_coeff, mo_occ, fock, h1e, conv_tol_grad, log)
    u, g_orb, kfcount, jkcount = next(rotaiter)
    kftot = kfcount + 1
    jktot = jkcount
    cput1 = log.timer('initializing second order scf', *cput0)

    for imacro in range(max_cycle):
        dm_last = dm
        last_hf_e = e_tot
        norm_gorb = numpy.linalg.norm(g_orb)
        mo_coeff = mf.rotate_mo(mo_coeff, u, log)
        dm = mf.make_rdm1(mo_coeff, mo_occ)
        vhf = mf._scf.get_veff(mol, dm, dm_last=dm_last, vhf_last=vhf)
        fock = mf.get_fock(h1e, s1e, vhf, dm, level_shift_factor=0)
# NOTE: DO NOT change the initial guess mo_occ, mo_coeff
        if mf.verbose >= logger.DEBUG:
            mo_energy, mo_tmp = mf.eig(fock, s1e)
            mf.get_occ(mo_energy, mo_tmp)
# call mf._scf.energy_tot for dft, because the (dft).get_veff step saved _exc in mf._scf
        e_tot = mf._scf.energy_tot(dm, h1e, vhf)

        log.info('macro= %d  E= %.15g  delta_E= %g  |g|= %g  %d KF %d JK',
                 imacro, e_tot, e_tot-last_hf_e, norm_gorb,
                 kfcount+1, jkcount)
        cput1 = log.timer('cycle= %d'%(imacro+1), *cput1)

        if (abs((e_tot-last_hf_e)/e_tot)*1e2 < conv_tol and
            norm_gorb < conv_tol_grad):
            scf_conv = True

        if dump_chk:
            mf.dump_chk(locals())

        if callable(callback):
            callback(locals())

        if scf_conv:
            break

        u, g_orb, kfcount, jkcount = rotaiter.send((mo_coeff, mo_occ, fock))
        kftot += kfcount + 1
        jktot += jkcount

    if callable(callback):
        callback(locals())

    rotaiter.close()
    mo_energy, mo_coeff1 = mf._scf.canonicalize(mo_coeff, mo_occ, fock)
    if mf.canonicalization:
        log.info('Canonicalize SCF orbitals')
        mo_coeff = mo_coeff1
        if dump_chk:
            mf.dump_chk(locals())
    log.info('macro X = %d  E=%.15g  |g|= %g  total %d KF %d JK',
             imacro+1, e_tot, norm_gorb, kftot, jktot)
    if (numpy.any(mo_occ==0) and
        mo_energy[mo_occ>0].max() > mo_energy[mo_occ==0].min()):
        log.warn('H**O %s > LUMO %s was found in the canonicalized orbitals.',
                 mo_energy[mo_occ>0].max(), mo_energy[mo_occ==0].min())
    return scf_conv, e_tot, mo_energy, mo_coeff, mo_occ
Пример #33
0
 def __subclasshook(cls, subclass):
     return (hasattr(subclass, 'fit') and
             callable(subclass.fit) and
             hasattr(subclass, 'explain') and
             callable(subclass.explain) or
             NotImplemented)
Пример #34
0
def encapsulation_func(target_class, func):
    assert callable(func)
    setattr(target_class, func.__name__, func)
Пример #35
0
 def _get(self):
     if callable(self._value):
         value = self._value()
     else:
         value = self._value
     return value
    def __init__(self, transport=None, channel=None, credentials=None,
            client_config=None, client_info=None):
        """Constructor.

        Args:
            transport (Union[~.UserLocationViewServiceGrpcTransport,
                    Callable[[~.Credentials, type], ~.UserLocationViewServiceGrpcTransport]): A transport
                instance, responsible for actually making the API calls.
                The default transport uses the gRPC protocol.
                This argument may also be a callable which returns a
                transport instance. Callables will be sent the credentials
                as the first argument and the default transport class as
                the second argument.
            channel (grpc.Channel): DEPRECATED. A ``Channel`` instance
                through which to make calls. This argument is mutually exclusive
                with ``credentials``; providing both will raise an exception.
            credentials (google.auth.credentials.Credentials): The
                authorization credentials to attach to requests. These
                credentials identify this application to the service. If none
                are specified, the client will attempt to ascertain the
                credentials from the environment.
                This argument is mutually exclusive with providing a
                transport instance to ``transport``; doing so will raise
                an exception.
            client_config (dict): DEPRECATED. A dictionary of call options for
                each method. If not specified, the default configuration is used.
            client_info (google.api_core.gapic_v1.client_info.ClientInfo):
                The client info used to send a user-agent string along with
                API requests. If ``None``, then default info will be used.
                Generally, you only need to set this if you're developing
                your own client library.
        """
        # Raise deprecation warnings for things we want to go away.
        if client_config is not None:
            warnings.warn('The `client_config` argument is deprecated.',
                          PendingDeprecationWarning, stacklevel=2)
        else:
            client_config = user_location_view_service_client_config.config

        if channel:
            warnings.warn('The `channel` argument is deprecated; use '
                          '`transport` instead.',
                          PendingDeprecationWarning, stacklevel=2)

        # Instantiate the transport.
        # The transport is responsible for handling serialization and
        # deserialization and actually sending data to the service.
        if transport:
            if callable(transport):
                self.transport = transport(
                    credentials=credentials,
                    default_class=user_location_view_service_grpc_transport.UserLocationViewServiceGrpcTransport,
                )
            else:
                if credentials:
                    raise ValueError(
                        'Received both a transport instance and '
                        'credentials; these are mutually exclusive.'
                    )
                self.transport = transport
        else:
            self.transport = user_location_view_service_grpc_transport.UserLocationViewServiceGrpcTransport(
                address=self.SERVICE_ADDRESS,
                channel=channel,
                credentials=credentials,
            )

        if client_info is None:
            client_info = google.api_core.gapic_v1.client_info.ClientInfo(
                gapic_version=_GAPIC_LIBRARY_VERSION,
            )
        else:
            client_info.gapic_version = _GAPIC_LIBRARY_VERSION
        self._client_info = client_info

        # Parse out the default settings for retry and timeout for each RPC
        # from the client configuration.
        # (Ordinarily, these are the defaults specified in the `*_config.py`
        # file next to this one.)
        self._method_configs = google.api_core.gapic_v1.config.parse_method_configs(
            client_config['interfaces'][self._INTERFACE_NAME],
        )

        # Save a dictionary of cached API call functions.
        # These are the actual callables which invoke the proper
        # transport methods, wrapped with `wrap_method` to add retry,
        # timeout, and the like.
        self._inner_api_calls = {}
Пример #37
0
    def Init(self):
        self._PosX = self._Index * self._Screen._Width
        self._Width = self._Screen._Width
        self._Height = self._Screen._Height

        self._CanvasHWND = self._Screen._CanvasHWND

        ps = InfoPageSelector()
        ps._Parent = self
        ps._PosX = 2
        self._Ps = ps
        self._PsIndex = 0

        #                ""   pkgname, label
        alist = [["", "Airplane", "Airplane Mode"],
                 ["", "PowerOptions", "Power Options"], ["", "Wifi", "Wi-Fi"],
                 ["", "Bluetooth",
                  "Bluetooth"], ["", "Sound", "Sound  Volume"],
                 ["", "Brightness", "BackLight Brightness"],
                 ["", "Storage", ""], ["", "Time", "Timezone"],
                 ["", "Languages", "Languages"],
                 ["", "Notification", "Notification"],
                 ["", "Update", "Update Launcher"],
                 ["", "Cores", "Retroarch cores manager"],
                 ["", "About", "About"], ["", "PowerOFF", "Power OFF"],
                 ["", "ButtonsLayout", "Buttons Layout"],
                 ["", "Skins", "Theme Manager"],
                 ["", "LauncherGo", "Switch to LauncherGo"],
                 ["", "Lima", "GPU Driver Switch"],
                 ["", "GateWay", "Network gateway switch"]]

        start_x = 0
        start_y = 0

        sys.path.append(myvars.basepath)  # add self as import path
        for i, v in enumerate(alist):
            li = ListItem()
            li._Parent = self
            li._PosX = start_x
            li._PosY = start_y + i * ListItem._Height
            li._Width = Width
            li._Fonts["normal"] = self._ListFontObj

            if v[2] != "":
                li.Init(v[2])
            else:
                li.Init(v[1])

            #if v[1] == "Wifi" or v[1] == "Sound" or v[1] == "Brightness" or v[1] == "Storage" or v[1] == "Update" or v[1] == "About" or v[1] == "PowerOFF" or v[1] == "HelloWorld":
            if FileExists(myvars.basepath + "/" + v[1]):
                li._LinkObj = __import__(v[1])
                init_cb = getattr(li._LinkObj, "Init", None)
                if init_cb != None:
                    if callable(init_cb):
                        li._LinkObj.Init(self._Screen)

                self._MyList.append(li)

        self._Scroller = ListScroller()
        self._Scroller._Parent = self
        self._Scroller._PosX = self._Width - 10
        self._Scroller._PosY = 2
        self._Scroller.Init()
Пример #38
0
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data as data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import models.cifar as models
import dropbranch.layers as sb_nn
import custom_layer as custom_nn

from utils import Bar, Logger, AverageMeter, accuracy, mkdir_p, savefig


model_names = sorted(name for name in models.__dict__
    if name.islower() and not name.startswith("__")
    and callable(models.__dict__[name]))

parser = argparse.ArgumentParser(description='PyTorch CIFAR10/100 Training')
# Datasets
parser.add_argument('-d', '--dataset', default='cifar10', type=str)
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
                    help='number of data loading workers (default: 4)')
# Optimization options
parser.add_argument('--epochs', default=300, type=int, metavar='N',
                    help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
                    help='manual epoch number (useful on restarts)')
parser.add_argument('--train-batch', default=128, type=int, metavar='N',
                    help='train batchsize')
parser.add_argument('--test-batch', default=100, type=int, metavar='N',
                    help='test batchsize')
Пример #39
0
 def add_suffix(self, suffix, importFunc):
     assert callable(importFunc)
     self.fs_imp.add_suffix(suffix, importFunc)
Пример #40
0
    def explain_row(self, *row_args, max_evals, main_effects, error_bounds,
                    batch_size, outputs, silent):
        """ Explains a single row and returns the tuple (row_values, row_expected_values, row_mask_shapes).
        """

        # build a masked version of the model for the current input sample
        fm = MaskedModel(self.model, self.masker, self.link, *row_args)

        # make sure we have the base value and current value outputs
        M = len(fm)
        m00 = np.zeros(M, dtype=np.bool)
        if self._curr_base_value is None or getattr(self.masker,
                                                    "fixed_background", False):
            self._curr_base_value = fm(m00.reshape(1, -1))[0]
        f11 = fm(~m00.reshape(1, -1))[0]

        if callable(self.masker.clustering):
            self._clustering = self.masker.clustering(*row_args)
            self._mask_matrix = make_masks(self._clustering)

        if hasattr(self._curr_base_value,
                   'shape') and len(self._curr_base_value.shape) > 0:
            if outputs is None:
                outputs = np.arange(len(self._curr_base_value))
            elif isinstance(outputs, OpChain):
                outputs = outputs.apply(Explanation(f11)).values

            out_shape = (2 * self._clustering.shape[0] + 1, len(outputs))
        else:
            out_shape = (2 * self._clustering.shape[0] + 1, )

        if max_evals == "auto":
            max_evals = 100

        self.values = np.zeros(out_shape)
        self.dvalues = np.zeros(out_shape)

        fixed_context = 1
        self.owen(fm, self._curr_base_value, f11, max_evals // 2 - 2, outputs,
                  fixed_context, batch_size, silent)

        # if False:
        #     if self.multi_output:
        #         return [self.dvalues[:,i] for i in range(self.dvalues.shape[1])], oinds
        #     else:
        #         return self.dvalues.copy(), oinds
        # else:
        # drop the interaction terms down onto self.values
        self.values[:] = self.dvalues

        def lower_credit(i, value=0):
            if i < M:
                self.values[i] += value
                return
            li = int(self._clustering[i - M, 0])
            ri = int(self._clustering[i - M, 1])
            group_size = int(self._clustering[i - M, 3])
            lsize = int(self._clustering[li - M, 3]) if li >= M else 1
            rsize = int(self._clustering[ri - M, 3]) if ri >= M else 1
            assert lsize + rsize == group_size
            self.values[i] += value
            lower_credit(li, self.values[i] * lsize / group_size)
            lower_credit(ri, self.values[i] * rsize / group_size)

        lower_credit(len(self.dvalues) - 1)

        return {
            "values":
            self.values[:M].copy(),
            "expected_values":
            self._curr_base_value
            if outputs is None else self._curr_base_value[outputs],
            "mask_shapes": [s + out_shape[1:] for s in fm.mask_shapes],
            "main_effects":
            None,
            "hierarchical_values":
            self.dvalues.copy(),
            "clustering":
            self._clustering
        }
Пример #41
0
    def fit(self, X, y, sample_weight=None):
        """Fit the SVM model according to the given training data.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features) \
                or (n_samples, n_samples)
            Training vectors, where n_samples is the number of samples
            and n_features is the number of features.
            For kernel="precomputed", the expected shape of X is
            (n_samples, n_samples).

        y : array-like of shape (n_samples,)
            Target values (class labels in classification, real numbers in
            regression).

        sample_weight : array-like of shape (n_samples,), default=None
            Per-sample weights. Rescale C per sample. Higher weights
            force the classifier to put more emphasis on these points.

        Returns
        -------
        self : object

        Notes
        -----
        If X and y are not C-ordered and contiguous arrays of np.float64 and
        X is not a scipy.sparse.csr_matrix, X and/or y may be copied.

        If X is a dense array, then the other methods will not support sparse
        matrices as input.
        """

        rnd = check_random_state(self.random_state)

        sparse = sp.isspmatrix(X)
        if sparse and self.kernel == "precomputed":
            raise TypeError("Sparse precomputed kernels are not supported.")
        self._sparse = sparse and not callable(self.kernel)

        if hasattr(self, 'decision_function_shape'):
            if self.decision_function_shape not in ('ovr', 'ovo'):
                raise ValueError(
                    f"decision_function_shape must be either 'ovr' or 'ovo', "
                    f"got {self.decision_function_shape}."
                )

        if callable(self.kernel):
            check_consistent_length(X, y)
        else:
            X, y = self._validate_data(X, y, dtype=np.float64,
                                       order='C', accept_sparse='csr',
                                       accept_large_sparse=False)

        y = self._validate_targets(y)

        sample_weight = np.asarray([]
                                   if sample_weight is None
                                   else sample_weight, dtype=np.float64)
        solver_type = LIBSVM_IMPL.index(self._impl)

        # input validation
        n_samples = _num_samples(X)
        if solver_type != 2 and n_samples != y.shape[0]:
            raise ValueError("X and y have incompatible shapes.\n" +
                             "X has %s samples, but y has %s." %
                             (n_samples, y.shape[0]))

        if self.kernel == "precomputed" and n_samples != X.shape[1]:
            raise ValueError("Precomputed matrix must be a square matrix."
                             " Input is a {}x{} matrix."
                             .format(X.shape[0], X.shape[1]))

        if sample_weight.shape[0] > 0 and sample_weight.shape[0] != n_samples:
            raise ValueError("sample_weight and X have incompatible shapes: "
                             "%r vs %r\n"
                             "Note: Sparse matrices cannot be indexed w/"
                             "boolean masks (use `indices=True` in CV)."
                             % (sample_weight.shape, X.shape))

        kernel = 'precomputed' if callable(self.kernel) else self.kernel

        if kernel == 'precomputed':
            # unused but needs to be a float for cython code that ignores
            # it anyway
            self._gamma = 0.
        elif isinstance(self.gamma, str):
            if self.gamma == 'scale':
                # var = E[X^2] - E[X]^2 if sparse
                X_var = ((X.multiply(X)).mean() - (X.mean()) ** 2
                         if sparse else X.var())
                self._gamma = 1.0 / (X.shape[1] * X_var) if X_var != 0 else 1.0
            elif self.gamma == 'auto':
                self._gamma = 1.0 / X.shape[1]
            else:
                raise ValueError(
                    "When 'gamma' is a string, it should be either 'scale' or "
                    "'auto'. Got '{}' instead.".format(self.gamma)
                )
        else:
            self._gamma = self.gamma

        fit = self._sparse_fit if self._sparse else self._dense_fit
        if self.verbose:
            print('[LibSVM]', end='')

        seed = rnd.randint(np.iinfo('i').max)
        fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
        # see comment on the other call to np.iinfo in this file

        self.shape_fit_ = X.shape if hasattr(X, "shape") else (n_samples, )

        # In binary case, we need to flip the sign of coef, intercept and
        # decision function. Use self._intercept_ and self._dual_coef_
        # internally.
        self._intercept_ = self.intercept_.copy()
        self._dual_coef_ = self.dual_coef_
        if self._impl in ['c_svc', 'nu_svc'] and len(self.classes_) == 2:
            self.intercept_ *= -1
            self.dual_coef_ = -self.dual_coef_

        return self
Пример #42
0
 def add_suffix(self, suffix, importFunc):
     assert callable(importFunc)
     self.suffixes.append((suffix, importFunc))
Пример #43
0
    def finish(self):
        """ Transfer the data from the temp table to ir.translation """
        cr = self._cr

        # Step 0: insert rows in batch
        query = """ INSERT INTO %s (name, lang, res_id, src, type, imd_model,
                                    module, imd_name, value, state, comments)
                    VALUES """ % self._table
        for rows in cr.split_for_in_conditions(self._rows):
            cr.execute(query + ", ".join(["%s"] * len(rows)), rows)

        _logger.debug("ir.translation.cursor: We have %d entries to process", len(self._rows))

        # Step 1: resolve ir.model.data references to res_ids
        cr.execute(""" UPDATE %s AS ti
                          SET res_id = imd.res_id,
                              noupdate = imd.noupdate
                       FROM ir_model_data AS imd
                       WHERE ti.res_id IS NULL
                       AND ti.module IS NOT NULL AND ti.imd_name IS NOT NULL
                       AND ti.module = imd.module AND ti.imd_name = imd.name
                       AND ti.imd_model = imd.model; """ % self._table)

        if self._debug:
            cr.execute(""" SELECT module, imd_name, imd_model FROM %s
                           WHERE res_id IS NULL AND module IS NOT NULL """ % self._table)
            for row in cr.fetchall():
                _logger.info("ir.translation.cursor: missing res_id for %s.%s <%s> ", *row)

        # Records w/o res_id must _not_ be inserted into our db, because they are
        # referencing non-existent data.
        cr.execute("DELETE FROM %s WHERE res_id IS NULL AND module IS NOT NULL" % self._table)

        # detect the xml_translate fields, where the src must be the same
        env = api.Environment(cr, SUPERUSER_ID, {})
        src_relevant_fields = []
        for model in env:
            for field_name, field in env[model]._fields.items():
                if hasattr(field, 'translate') and callable(field.translate):
                    src_relevant_fields.append("%s,%s" % (model, field_name))

        count = 0
        # Step 2: insert new or upsert non-noupdate translations
        if self._overwrite:
            cr.execute(""" INSERT INTO %s(name, lang, res_id, src, type, value, module, state, comments)
                           SELECT name, lang, res_id, src, type, value, module, state, comments
                           FROM %s
                           WHERE type = 'code'
                           AND noupdate IS NOT TRUE
                           ON CONFLICT (type, lang, md5(src)) WHERE type = 'code'
                            DO UPDATE SET (name, lang, res_id, src, type, value, module, state, comments) = (EXCLUDED.name, EXCLUDED.lang, EXCLUDED.res_id, EXCLUDED.src, EXCLUDED.type, EXCLUDED.value, EXCLUDED.module, EXCLUDED.state, EXCLUDED.comments)
                            WHERE EXCLUDED.value IS NOT NULL AND EXCLUDED.value != '';
                       """ % (self._model_table, self._table))
            count += cr.rowcount
            cr.execute(""" INSERT INTO %s(name, lang, res_id, src, type, value, module, state, comments)
                           SELECT name, lang, res_id, src, type, value, module, state, comments
                           FROM %s
                           WHERE type = 'model'
                           AND noupdate IS NOT TRUE
                           ON CONFLICT (type, lang, name, res_id) WHERE type = 'model'
                            DO UPDATE SET (name, lang, res_id, src, type, value, module, state, comments) = (EXCLUDED.name, EXCLUDED.lang, EXCLUDED.res_id, EXCLUDED.src, EXCLUDED.type, EXCLUDED.value, EXCLUDED.module, EXCLUDED.state, EXCLUDED.comments)
                            WHERE EXCLUDED.value IS NOT NULL AND EXCLUDED.value != '';
                       """ % (self._model_table, self._table))
            count += cr.rowcount

            cr.execute(""" INSERT INTO %s(name, lang, res_id, src, type, value, module, state, comments)
                           SELECT name, lang, res_id, src, type, value, module, state, comments
                           FROM %s
                           WHERE type = 'model_terms'
                           AND noupdate IS NOT TRUE
                           ON CONFLICT (type, name, lang, res_id, md5(src))
                            DO UPDATE SET (name, lang, res_id, src, type, value, module, state, comments) = (EXCLUDED.name, EXCLUDED.lang, EXCLUDED.res_id, EXCLUDED.src, EXCLUDED.type, EXCLUDED.value, EXCLUDED.module, EXCLUDED.state, EXCLUDED.comments)
                            WHERE EXCLUDED.value IS NOT NULL AND EXCLUDED.value != '';
                       """ % (self._model_table, self._table))
            count += cr.rowcount
        cr.execute(""" INSERT INTO %s(name, lang, res_id, src, type, value, module, state, comments)
                       SELECT name, lang, res_id, src, type, value, module, state, comments
                       FROM %s
                       WHERE %s
                       ON CONFLICT DO NOTHING;
                   """ % (self._model_table, self._table, 'noupdate IS TRUE' if self._overwrite else 'TRUE'))
        count += cr.rowcount

        if self._debug:
            cr.execute("SELECT COUNT(*) FROM ONLY %s" % self._model_table)
            total = cr.fetchone()[0]
            _logger.debug("ir.translation.cursor: %d entries now in ir.translation, %d common entries with tmp", total, count)

        # Step 3: cleanup
        cr.execute("DROP TABLE %s" % self._table)
        self._rows.clear()
        return True
Пример #44
0
 def get_default(cls):
     assert cls.has_default(), (778,
                                f"{cls} haven't got a value/default value!")
     if callable(cls.validator.default):
         return cls.validator.default()
     return cls.validator.default
    def _check_input_parameters(self, X, y, groups):

        if self.scoring is not None and not (
            isinstance(self.scoring, str) or callable(self.scoring)
        ):
            raise ValueError(
                "scoring parameter must be a string, "
                "a callable or None. Multimetric scoring is not "
                "supported."
            )

        # We need to enforce that successive calls to cv.split() yield the same
        # splits: see https://github.com/scikit-learn/scikit-learn/issues/15149
        if not _yields_constant_splits(self._checked_cv_orig):
            raise ValueError(
                "The cv parameter must yield consistent folds across "
                "calls to split(). Set its random_state to an int, or set "
                "shuffle=False."
            )

        if (
            self.resource != "n_samples"
            and self.resource not in self.estimator.get_params()
        ):
            raise ValueError(
                f"Cannot use resource={self.resource} which is not supported "
                f"by estimator {self.estimator.__class__.__name__}"
            )

        if isinstance(self.max_resources, str) and self.max_resources != "auto":
            raise ValueError(
                "max_resources must be either 'auto' or a positive integer"
            )
        if self.max_resources != "auto" and (
            not isinstance(self.max_resources, Integral) or self.max_resources <= 0
        ):
            raise ValueError(
                "max_resources must be either 'auto' or a positive integer"
            )

        if self.min_resources not in ("smallest", "exhaust") and (
            not isinstance(self.min_resources, Integral) or self.min_resources <= 0
        ):
            raise ValueError(
                "min_resources must be either 'smallest', 'exhaust', "
                "or a positive integer "
                "no greater than max_resources."
            )

        if isinstance(self, HalvingRandomSearchCV):
            if self.min_resources == self.n_candidates == "exhaust":
                # for n_candidates=exhaust to work, we need to know what
                # min_resources is. Similarly min_resources=exhaust needs to
                # know the actual number of candidates.
                raise ValueError(
                    "n_candidates and min_resources cannot be both set to 'exhaust'."
                )
            if self.n_candidates != "exhaust" and (
                not isinstance(self.n_candidates, Integral) or self.n_candidates <= 0
            ):
                raise ValueError(
                    "n_candidates must be either 'exhaust' or a positive integer"
                )

        self.min_resources_ = self.min_resources
        if self.min_resources_ in ("smallest", "exhaust"):
            if self.resource == "n_samples":
                n_splits = self._checked_cv_orig.get_n_splits(X, y, groups)
                # please see https://gph.is/1KjihQe for a justification
                magic_factor = 2
                self.min_resources_ = n_splits * magic_factor
                if is_classifier(self.estimator):
                    y = self._validate_data(X="no_validation", y=y)
                    check_classification_targets(y)
                    n_classes = np.unique(y).shape[0]
                    self.min_resources_ *= n_classes
            else:
                self.min_resources_ = 1
            # if 'exhaust', min_resources_ might be set to a higher value later
            # in _run_search

        self.max_resources_ = self.max_resources
        if self.max_resources_ == "auto":
            if not self.resource == "n_samples":
                raise ValueError(
                    "max_resources can only be 'auto' if resource='n_samples'"
                )
            self.max_resources_ = _num_samples(X)

        if self.min_resources_ > self.max_resources_:
            raise ValueError(
                f"min_resources_={self.min_resources_} is greater "
                f"than max_resources_={self.max_resources_}."
            )

        if self.min_resources_ == 0:
            raise ValueError(
                f"min_resources_={self.min_resources_}: you might have passed "
                "an empty dataset X."
            )

        if not isinstance(self.refit, bool):
            raise ValueError(
                f"refit is expected to be a boolean. Got {type(self.refit)} instead."
            )
Пример #46
0
    def translate_fields(self, model, id, field=None):
        """ Open a view for translating the field(s) of the record (model, id). """
        main_lang = 'en_US'
        if not self.env['res.lang'].search_count([('code', '!=', main_lang)]):
            raise UserError(_("Translation features are unavailable until you install an extra translation."))

        # determine domain for selecting translations
        record = self.env[model].with_context(lang=main_lang).browse(id)
        domain = ['&', ('res_id', '=', id), ('name', '=like', model + ',%')]

        def make_domain(fld, rec):
            name = "%s,%s" % (fld.model_name, fld.name)
            return ['&', ('res_id', '=', rec.id), ('name', '=', name)]

        # insert missing translations, and extend domain for related fields
        for name, fld in record._fields.items():
            if not fld.translate:
                continue

            rec = record
            if fld.related:
                try:
                    # traverse related fields up to their data source
                    while fld.related:
                        rec, fld = fld.traverse_related(rec)
                    if rec:
                        domain = ['|'] + domain + make_domain(fld, rec)
                except AccessError:
                    continue

            assert fld.translate and rec._name == fld.model_name
            self.insert_missing(fld, rec)

        action = {
            'name': 'Translate',
            'res_model': 'ir.translation',
            'type': 'ir.actions.act_window',
            'view_mode': 'tree',
            'view_id': self.env.ref('base.view_translation_dialog_tree').id,
            'target': 'current',
            'flags': {'search_view': True, 'action_buttons': True},
            'domain': domain,
            'context': {},
        }
        if field:
            fld = record._fields[field]
            if not fld.related:
                action['context'] = {
                    'search_default_name': "%s,%s" % (fld.model_name, fld.name),
                }
            else:
                rec = record
                try:
                    while fld.related:
                        rec, fld = fld.traverse_related(rec)
                    if rec:
                        action['context'] = {'search_default_name': "%s,%s" % (fld.model_name, fld.name),}
                except AccessError:
                    pass

            action['target'] = 'new'
            action['context']['translation_type'] = 'text' if fld.type in ['text', 'html'] else 'char'
            action['context']['translation_show_src'] = False
            if callable(fld.translate):
                action['view_id'] = self.env.ref('base.view_translation_lang_src_value_tree').id,
                action['context']['translation_show_src'] = True
            else:
                action['view_id'] = self.env.ref('base.view_translation_lang_value_tree').id,

        return action
Пример #47
0
    def __call__(self,
                query,
                fields=None,
                field_id=None,
                left=None,
                headers={},
                columns=None,
                orderby=None,  # EXTENDED for permutation
                searchable=True,  # EXTENDED ex) [table.id, table.name, ...]
                sortable=True,  # EXTENDED ex) [table.id, table.name, ...]
                paginate=(10, 25, 50, 100),  # EXTENDED
                
                deletable=True,  # EXTENDED ex) lambda record_id: deleted
                editable=True,  # EXTENDED ex) [['id', 'name'], 'profile', ...] or lambda record: edit_form
                details=True,  # EXTENDED ex) [['id', 'name'], 'profile', ...] or lambda record: view_form
                selectable=None,  # TODO
                create=True,  # EXTENDED ex) [['id', 'name'], 'profile', ...] or lambda: create_form
                csv=True,
                
                links=None,
                links_in_grid=True,
             
                upload='<default>',
                args=[],
                user_signature=True,
                maxtextlengths={},  # NOT WORK
                maxtextlength=20,
                onvalidation=None,
                oncreate=None,
                onupdate=None,
                ondelete=None,
                sorter_icons=('[^]', '[v]'),  # NOT WORK
                ui='ui',  # ONLY WORK FOR "ui"
                showbuttontext=True,
                _class="web2py_grid",
                formname='web2py_grid',
                search_widget='default',  # NOT WORK
                extracolumns=None,  # CUSTOM (same as in SQLTABLE)
                search_queries={},  # CUSTOM
                showid=True,  # CUSTOM
                onpermute=None,  # CUSTOM
                virtualtable=None,  # CUSTOM
                virtualrecord=None,  # CUSTOM
                virtualset=None,  # CUSTOM
                hmac_key=None,  # CUSTOM
                scope=None,  # CUSTOM
                scope_default=None,  # CUSTOM
                groupby=None,  # CUSTOM
                search_vars=None, # CUSTOM
                ):
            
        from gluon.dal import SQLALL
        from plugin_solidform import SOLIDFORM
        from plugin_solidtable import SOLIDTABLE, OrderbySelector
        from plugin_paginator import Paginator, PaginateSelector, PaginateInfo
        gridbutton = self.settings.gridbutton
        recordbutton = self.settings.recordbutton
        
        request, response, session, T = current.request, current.response, current.session, current.T
        search_vars = search_vars or request.get_vars
        
        def __oncreate(form):
            session.flash = T('Created')

        def __onupdate(form):
            session.flash = T('Updated')

        def __ondelete(table, tablename):
            session.flash = T('Deleted')

        def __onpermute(table, tablename, ret):
            session.flash = T('Permuted')

        def redirect_patch():
            onupdate
            
        oncreate = oncreate or __oncreate
        onupdate = onupdate or __onupdate
        ondelete = ondelete or __ondelete
        onpermute = onpermute or __onpermute
        
        if ui == 'ui':
            ui = dict(widget='',
                      header='',
                      content='',
                      default='',
                      cornerall='',
                      cornertop='',
                      cornerbottom='',
                      button='',
                      buttontext='',
                      buttonadd='ui-icon-plusthick',
                      buttonback='ui-icon-arrowreturnthick-1-w',
                      buttonexport='ui-icon ui-icon-transferthick-e-w',
                      buttondelete='ui-icon-close',
                      buttonedit='ui-icon-pencil',
                      buttontable='',
                      buttonview='ui-icon-zoomin',
                      )
        elif not isinstance(ui, dict):
            raise RuntimeError('SQLFORM.grid ui argument must be a dictionary')
            
        wenabled = (not user_signature or (session.auth and session.auth.user))
        deletable = wenabled and deletable
        # if search_widget=='default':
            # search_widget = SQLFORM.search_menu
            
        url = self.url_factory(args, user_signature, hmac_key)
        
        db = query._db
        dbset = db(query)
        tables = [db[tablename] for tablename in db._adapter.tables(dbset.query)]
        if not fields:
            fields = reduce(lambda a, b: a + b,
                            [[field for field in table] for table in tables])
        
        new_fields = []
        for item in fields:
            if isinstance(item, SQLALL):
                new_fields += item.table
            else:
                new_fields.append(item)
        fields = new_fields
        
        main_table = tables[0]
        if not field_id:
            field_id = main_table._id
        
        table = field_id.table
        tablename = table._tablename
        referrer = session.get('_web2py_grid_referrer_' + formname, url())
        
        def __from_process_redirect_patch(func):
            def wrapper(form):
                func(form)
                redirect(referrer)
            return wrapper
        oncreate = __from_process_redirect_patch(oncreate)
        onupdate = __from_process_redirect_patch(onupdate)
            
        def check_authorization():
            if user_signature or hmac_key:
                if not URL.verify(request, user_signature=user_signature, hmac_key=hmac_key):
                    session.flash = T('not authorized')
                    redirect(referrer)

        if upload == '<default>':
            upload = lambda filename: url(args=['download', filename])
            if len(request.args) > 1 and request.args[-2] == 'download':
                check_authorization()
                stream = response.download(request, db)
                raise HTTP(200, stream, **response.headers)
        
        gridbuttons = [gridbutton('%(buttonback)s' % ui, T('Back'), referrer)]

        def _add_link_gridbuttons(record):
            if record and links:
                for link in links:
                    if isinstance(link, dict):
                        gridbuttons.append(link['body'](record))
                    elif link(record):
                        gridbuttons.append(link(record))
                     
        if create and len(request.args) > 1 and request.args[-2] == 'new':
            check_authorization()
            table = db[request.args[-1]]
            if orderby:
                inverted = (orderby.op == orderby.db._adapter.INVERT)
                field = orderby.first if inverted else orderby
                last = dbset.select(field_id, field, limitby=(0, 1), orderby=orderby.first if inverted else ~orderby).first()
                last_value = (last[field] or 0) if last else 0
                table[field.name].default = (-1 if inverted else 1) + last_value
                 
            self.mark_not_empty(virtualtable or table)
            
            if callable(create):
                create_form = create()
            else:
                create_form = SOLIDFORM(virtualtable or table,
                        fields=create if type(create) in (list, tuple) else None,
                        showid=showid,
                        _class='web2py_form',
                        submit_button=T('Create'),
                        ).process(  # next=referrer, for web2py-bug
                                  onvalidation=onvalidation,
                                  onsuccess=oncreate,
                                  formname=formname)
            
            self.unmark_not_empty(table)
            res = DIV(create_form, _class=_class)
            res.create_form = create_form
            res.gridbuttons = gridbuttons
            return res
            
        elif details and len(request.args) > 2 and request.args[-3] == 'view':
            check_authorization()
            table = db[request.args[-2]]
            record = table(request.args[-1]) or redirect(URL('error'))
            
            if callable(details):
                view_form = details(record)
            else:
                view_form = SOLIDFORM(virtualtable or table, virtualrecord or record,
                                 fields=details if type(details) in (list, tuple) else
                                            create if type(create) in (list, tuple) else None,
                                 upload=upload,
                                 readonly=True,
                                 showid=showid,
                                 _class='web2py_form')
            res = DIV(view_form, _class=_class)
            res.record = record  # CUSTOM
            res.view_form = view_form  # CUSTOM
            if editable:
                gridbuttons.append(
                    gridbutton('%(buttonedit)s' % ui, T('Edit'),
                                  url(args=['edit', tablename, record.id]))
                )
            _add_link_gridbuttons(record)
            res.gridbuttons = gridbuttons
            return res
            
        elif editable and len(request.args) > 2 and request.args[-3] == 'edit':
            check_authorization()
            table = db[request.args[-2]]
            record = table(request.args[-1]) or redirect(URL('error'))
            self.mark_not_empty(virtualtable or table)
            
            if callable(editable):
                edit_form = editable(record)
            else:
                edit_form = SOLIDFORM(virtualtable or table, virtualrecord or record,
                                fields=editable if type(editable) in (list, tuple) else
                                            create if type(create) in (list, tuple) else None,
                                upload=upload,
                                deletable=deletable is True,
                                showid=showid,
                                delete_label=T('Check to delete:'),
                                submit_button=T('Update'),
                                _class='web2py_form').process(
                                    formname=formname,
                                    onvalidation=onvalidation,
                                    onsuccess=onupdate,
                                    # #next=referrer, for web2py-bug
                                    )
            
            self.unmark_not_empty(table)
                
            res = DIV(edit_form, _class=_class)
            res.record = record  # CUSTOM
            res.edit_form = edit_form
            if details:
                gridbuttons.append(
                    gridbutton('%(buttonview)s' % ui, T('View'),
                                  url(args=['view', tablename, record.id]))
                )
            _add_link_gridbuttons(record)
            res.gridbuttons = gridbuttons
            return res
            
        elif deletable and len(request.args) > 2 and request.args[-3] == 'delete':
            check_authorization()
            table = db[request.args[-2]]
            if callable(deletable):
                deletable(request.args[-1])
            else:
                if ondelete:
                    ondelete(table, request.args[-1])
                db(table.id == request.args[-1]).delete()
            redirect(url())
            
        elif request.vars.records and not isinstance(request.vars.records, list):
            request.vars.records = [request.vars.records]
            
        elif not request.vars.records:
            request.vars.records = []
           
        error = None
        search_form = None
        table_el_id = formname + '_maintable'
        
        columns = columns or [str(f) for f in fields
                                if f.table == main_table and f.readable and (showid or f.type != 'id')]
        
        if searchable:
            field_sep = '___'
            
            if searchable is True:
                _exclude_types = ('upload', 'text') if showid else ('id', 'upload', 'text')
                searchable = [f for f in fields
                                if f.table == main_table and
                                   f.type not in _exclude_types and f.readable]
            
            _search_fields = []
            _from_tos = []

            for f in searchable:
                _requires = []
                if f.requires and type(f.requires) not in (list, tuple):
                    if isinstance(f.requires, IS_EMPTY_OR):
                        _requires = [f.requires.other]
                    else:
                        _requires = [f.requires]
                    
                _requires = [r for r in _requires if not isinstance(r, IS_NOT_IN_DB)]
                
                if _requires:
                    if len(_requires) == 1:
                        _requires = _requires[0]
                    _requires = IS_EMPTY_OR(_requires)
                else:
                    _requires = None
                
                _type = 'string' if f.type == 'text' else 'integer' if f.type == 'id' else f.type
                
                if (f.type in ('double', 'decimal', 'date', 'datetime') or
                        (f.type == 'integer' and _requires and
                         isinstance(_requires.other, (IS_INT_IN_RANGE)))):
                    _from_to = [Field(str(f).replace('.', field_sep) + field_sep + 'from',
                                      type=_type, requires=_requires,
                                      label=f.label, widget=f.widget),
                                Field(str(f).replace('.', field_sep) + field_sep + 'to',
                                      type=_type, requires=_requires,
                                      label=f.label, widget=f.widget)]
                    _from_tos.append(_from_to)
                    _search_fields += _from_to
                elif hasattr(f, 'table'):
                    _search_fields.append(Field(str(f).replace('.', field_sep),
                        type=_type, requires=_requires, label=f.label, widget=f.widget))
                else:
                    _search_fields.append(f)
            
            search_form = SQLFORM.factory(
                formstyle='divs', submit_button=T('Search'),
                _class='search_form',
                *_search_fields)
            
            for _from_to in _from_tos:
                self.inline(search_form, 'no_table',
                    [f.name for f in _from_to], LABEL(_from_to[0].label), SPAN(' - '))
            
            subquery = self._build_query_by_form(db, search_form,
                                                 search_vars,
                            queries=search_queries,
                            field_sep=field_sep,
                            formname='search_%s' % formname)
        else:
            subquery = None
            
        if subquery:
            dbset = dbset(subquery)
            
        if scope:
            from plugin_tablescope import TableScope
            scope_el = TableScope(dbset, scope, default=scope_default)
            dbset = scope_el.scoped_dataset
            
        if sortable is True:
            sortable = [~f if f.type in ('id', 'date', 'datetime') else f
                            for f in fields
                                if f.table == main_table and f.type not in ('text', 'upload')]
        if not sortable:
            sortable = []
        if orderby:
            sortable.insert(0, orderby)
        
        orderby_selector = OrderbySelector(sortable)
        
        if csv and len(request.args) > 1 and request.args[-2] == 'csv':
            if not dbset:
                raise HTTP(400)
            check_authorization()
            current.response.headers['Content-Type'] = 'text/csv; charset=Shift_JIS'
            current.response.headers['Content-Disposition'] = 'attachment;filename=rows.csv;'
            raise HTTP(200, str(dbset.select(limitby=(0, 1000))).encode('shift_jis', 'ignore'),
                       **current.response.headers)

        session['_web2py_grid_referrer_' + formname] = URL(
                r=request, args=request.args, vars=request.vars,
                user_signature=user_signature, hmac_key=hmac_key)

        current_orderby = orderby_selector.orderby()
        permutable = (orderby and (not subquery) and sortable and current_orderby is sortable[0])
           
        extracolumns = extracolumns or []
        
        _links_in_row_buttons = []

        def _get_link_extracolumn(link):
            return {'label': link['header'], 'class': ui.get('default', ''),
                    'content': lambda row, rc: link['body'](row)}
        if links and links_in_grid:
            for link in links:
                if isinstance(link, dict):
                    extracolumns.append(_get_link_extracolumn(link))
                else:
                    _links_in_row_buttons.append(link)
        
        if permutable:
            if len(request.args) > 2 and request.args[-3] in ('up', 'down'):
                check_authorization()
                table = db[request.args[-2]]
                record = table(request.args[-1]) or redirect(URL('error'))
                inverted = (orderby.op == orderby.db._adapter.INVERT)
                field = orderby.first if inverted else orderby
                 
                current_value = record[field]
                if current_value is None:
                    first = dbset.select(field_id, limitby=(0, 1), orderby=orderby).first()
                    current_value = (1 if inverted else -1) + (first.id if first else 0)
                    
                if (request.args[-3] == ('down' if inverted else 'up')):
                    target = dbset(field < current_value
                        ).select(field_id, field, limitby=(0, 1), orderby=orderby if inverted else ~orderby).first()
                elif (request.args[-3] == ('up' if inverted else 'down')):
                    target = dbset(field > current_value
                        ).select(field_id, field, limitby=(0, 1), orderby=orderby.first if inverted else orderby).first()
                else:
                    raise NotImplementedError
                if not target:
                    last = dbset.select(field_id, limitby=(0, 1), orderby=orderby.first if orderby.first else ~orderby).first()
                    target_value = (-1 if inverted else 1) + (last.id if last else 0)
                else:
                    target_value = target[field]
                    
                db(table.id == record[field_id]).update(**{field.name: target_value})
                if target:
                    db(table.id == target[field_id]).update(**{field.name: current_value})
                
                if onpermute:
                    onpermute(table, request.args[-2], (record, target))
                redirect(url())
                
            first = dbset.select(field_id, limitby=(0, 1), orderby=orderby).first()
            first_id = first.id if first else 0
            last = dbset.select(field_id, limitby=(0, 1), orderby=orderby.first if orderby.first else ~orderby).first()
            last_id = last.id if last else 0
            extracolumns.append(
                {'label': DIV(T('Move'), _style='text-align:center;'), 'width': '150px' if showbuttontext else '65px',
                'content': lambda row, rc:
                    DIV(recordbutton('ui-icon-triangle-1-n', T('Up'),
                                url(args=['up', tablename, row[field_id]]), showbuttontext)
                                if row[field_id] != first_id else '',
                          recordbutton('ui-icon-triangle-1-s', T('Down'),
                                url(args=['down', tablename, row[field_id]]), showbuttontext)
                                if row[field_id] != last_id else '',
                    _style='text-align:center;')}
            )
        
        if details or editable or deletable or _links_in_row_buttons:
            extracolumns.append(
                {'label': '',  # 'width':'%spx' % (_size + 12),
                'content': lambda row, rc:
                     DIV(_style='white-space:nowrap;',
                         *([link(row) or '' for link in _links_in_row_buttons] +
                           [recordbutton('%(buttonview)s' % ui, T('View'),
                                url(args=['view', tablename, row[field_id]]), showbuttontext)
                                    if details else '',
                          recordbutton('%(buttonedit)s' % ui, T('Edit'),
                                url(args=['edit', tablename, row[field_id]]), showbuttontext)
                                    if editable else '',
                          recordbutton('%(buttondelete)s' % ui, T('Delete'),
                                url(args=['delete', tablename, row[field_id]]), showbuttontext,
                                _onclick="""
if(confirm("%s")){return true;} else {jQuery(this).unbind('click').fadeOut();return false;}""" %
                                          T('Sure you want to delete them?'),)
                                    if deletable else '',
                          ])
                         )}
            )
            
        if paginate:
            paginate_selector = PaginateSelector(paginate if type(paginate) in (list, tuple) else [paginate])
            current_paginate = paginate_selector.paginate
            paginator = Paginator(paginate=current_paginate)
            # TODO for groupby
            paginator.records = virtualset(dbset.query).count() if virtualset else dbset.count()
            paginate_info = PaginateInfo(paginator.page, paginator.paginate, paginator.records)
            limitby = paginator.limitby()
        else:
            limitby = None
            current_paginate = None
            
        # TODO
        # if paginator.records == 0:
            # error = 'Not Found'
        if virtualset:
            records = virtualset(dbset.query).select(left=left, limitby=limitby,
                        orderby=current_orderby, groupby=groupby, *fields)
            records.db = virtualtable._db
        else:
            records = dbset.select(left=left, limitby=limitby,
                        orderby=current_orderby, groupby=groupby, *fields)
        
        table = SOLIDTABLE(records,
                    columns=columns,
                    headers=headers,
                    orderby=orderby_selector,
                    truncate=maxtextlength,  # TODO replace
                    extracolumns=extracolumns,
                    upload=upload)
        table.attributes['_class'] = 'solidtable'
        table.attributes['_id'] = table_el_id
        
        inner = []
        if scope:
            inner.append(scope_el)
        if current_paginate:
            inner.append(DIV(paginate_info, _class='pagination_information'))
        inner.append(table)
        if current_paginate and paginator.records > current_paginate:
            inner.append(DIV(paginate_selector, paginator, _class='index_footer'))
        
        res = DIV(_class=_class, *inner)
          
        res.records = records
        res.search_form = search_form
        res.error = error
        res.gridbuttons = []
        if create:
            res.gridbuttons.append(
                gridbutton('%(buttonadd)s' % ui, T('Add'), url(args=['new', tablename]))
            )

        if csv:
            res.gridbuttons.append(
                gridbutton('%(buttonexport)s' % ui, T('Export'), url(args=['csv', tablename], vars=request.vars))
            )
        return res
Пример #48
0
    def _sync_terms_translations(self, field, records):
        """ Synchronize the translations to the terms to translate, after the
        English value of a field is modified. The algorithm tries to match
        existing translations to the terms to translate, provided the distance
        between modified strings is not too large. It allows to not retranslate
        data where a typo has been fixed in the English value.
        """
        if not callable(field.translate):
            return

        Translation = self.env['ir.translation']
        outdated = Translation
        discarded = Translation

        for record in records:
            # get field value and terms to translate
            value = record[field.name]
            terms = set(field.get_trans_terms(value))
            translations = Translation.search([
                ('type', '=', 'model_terms'),
                ('name', '=', "%s,%s" % (field.model_name, field.name)),
                ('res_id', '=', record.id),
            ])

            if not terms:
                # discard all translations for that field
                discarded += translations
                continue

            # remap existing translations on terms when possible; each term
            # should be translated at most once per language
            done = set()                # {(src, lang), ...}
            translations_to_match = []

            for translation in translations:
                if not translation.value:
                    discarded += translation
                    # consider it done to avoid being matched against another term
                    done.add((translation.src, translation.lang))
                elif translation.src in terms:
                    done.add((translation.src, translation.lang))
                else:
                    translations_to_match.append(translation)

            for translation in translations_to_match:
                matches = get_close_matches(translation.src, terms, 1, 0.9)
                src = matches[0] if matches else None
                if not src:
                    outdated += translation
                elif (src, translation.lang) in done:
                    discarded += translation
                else:
                    vals = {'src': src, 'state': translation.state}
                    if translation.lang == records.env.lang:
                        vals['value'] = src
                    translation.write(vals)
                    done.add((src, translation.lang))

        # process outdated and discarded translations
        outdated.write({'state': 'to_translate'})
        discarded.unlink()
Пример #49
0
 def push(self):
     self.storesettings.insert(0,{key:value for key, value in self.__dict__.items() if not key.startswith('__') and not callable(key)})
Пример #50
0
    def get_empty_value(cls):
        if callable(cls.__empty_value__):
            return cls.__empty_value__()

        # otherwise...
        return cls.__empty_value__
Пример #51
0
 def test_task_manager_api_class(self):
     vertica_strategy = task_strategy()
     self.assertFalse(
         hasattr(vertica_strategy.task_manager_api_class, 'add_new_node'))
     self.assertTrue(callable(
         vertica_strategy.task_manager_api_class._cast))
Пример #52
0
Functions and methods in Python are higher order. Its mean they are can be uses 
as arguments
"""


class CountMissing:
    def __init__(self):
        self.added = 0

    def __call__(self, *args, **kwargs):
        self.added += 1
        return 0


counter = CountMissing()
counter() == 0
assert callable(counter)

increments = {
    ('product1', 1),
    ('product2', 2)
}

current = {
    ('product3', 10)
}
"""Example of usage"""
result = defaultdict(counter, current) # use and __call__ method from counter
for key, amount in increments:
    result[key] += amount
assert counter.added == 3
Пример #53
0
def SetGlobalFunc(mem, name, func):
  # type: (Mem, str, Union[Callable, ParameterizedArray, type]) -> None
  """Used by bin/oil.py to set split(), etc."""
  assert callable(func), func
  mem.SetVar(sh_lhs_expr.Name(name), value.Obj(func), scope_e.GlobalOnly)
Пример #54
0
    def write_page(self, site, page, filename):
        self.out('Writing sitemap %s.' % filename, 2)
        old_page_md5 = None
        urls = []

        if conf.MOCK_SITE:
            if conf.MOCK_SITE_NAME is None:
                raise ImproperlyConfigured(
                    "STATICSITEMAPS_MOCK_SITE_NAME must not be None. Try setting to www.yoursite.com"
                )
            from django.contrib.sites.requests import RequestSite
            from django.test.client import RequestFactory
            rs = RequestSite(RequestFactory().get(
                '/', SERVER_NAME=conf.MOCK_SITE_NAME))
        try:
            if callable(site):
                if conf.MOCK_SITE:
                    urls.extend(site().get_urls(
                        page, rs, protocol=conf.MOCK_SITE_PROTOCOL))
                else:
                    urls.extend(site().get_urls(page,
                                                protocol=conf.FORCE_PROTOCOL))
            else:
                if conf.MOCK_SITE:
                    urls.extend(
                        site.get_urls(page,
                                      rs,
                                      protocol=conf.MOCK_SITE_PROTOCOL))
                else:
                    urls.extend(
                        site.get_urls(page, protocol=conf.FORCE_PROTOCOL))
        except EmptyPage:
            self.out("Page %s empty" % page)
        except PageNotAnInteger:
            self.out("No page '%s'" % page)

        lastmods = [
            lastmod for lastmod in [u.get('lastmod') for u in urls]
            if lastmod is not None
        ]
        file_lastmod = max(lastmods) if len(lastmods) > 0 else None
        path = os.path.join(self.root_dir, filename)
        template = getattr(site, 'sitemap_template', 'sitemap.xml')

        if self.storage.exists(path):
            old_page_md5 = self.read_hash(path)
            self.storage.delete(path)

        output = smart_str(loader.render_to_string(template, {'urlset': urls}))
        self._write(path, output)

        with self.storage.open(path) as sitemap_page:
            if old_page_md5 != self.get_hash(sitemap_page.read()):
                self.has_changes = True

        if conf.USE_GZIP:
            if conf.GZIP_METHOD not in [
                    'python',
                    'system',
            ]:
                raise ImproperlyConfigured(
                    "STATICSITEMAPS_GZIP_METHOD must be in ['python', 'system']"
                )

            if conf.GZIP_METHOD == 'system' and not os.path.exists(
                    conf.SYSTEM_GZIP_PATH):
                raise ImproperlyConfigured(
                    'STATICSITEMAPS_SYSTEM_GZIP_PATH does not exist')

            if conf.GZIP_METHOD == 'system' and not isinstance(
                    self.storage, FileSystemStorage):
                raise ImproperlyConfigured(
                    'system gzip method can only be used with FileSystemStorage'
                )

            if conf.GZIP_METHOD == 'system':
                # GZIP with system gzip binary
                subprocess.call([
                    conf.SYSTEM_GZIP_PATH,
                    '-f',
                    path,
                ])
            else:
                # GZIP with python gzip lib
                try:
                    gzipped_path = '%s.gz' % path
                    if self.storage.exists(gzipped_path):
                        self.storage.delete(gzipped_path)

                    self.out('Compressing...', 2)
                    buf = BytesIO()
                    with gzip.GzipFile(fileobj=buf, mode="w") as f:
                        f.write(output.encode('utf-8'))
                    self.storage.save(gzipped_path,
                                      ContentFile(buf.getvalue()))
                except OSError:
                    self.out("Compress %s file error" % path)

        return file_lastmod
 def has_callable_default(self):
     return self.default is not None and callable(self.default)
Пример #56
0
def is_authenticated(user):
    if callable(user.is_authenticated):
        return user.is_authenticated()
    else:
        return user.is_authenticated
Пример #57
0
    def __new__(cls, name, bases, dct):
        default = dct.get('default')
        if callable(default):
            dct['default'] = staticmethod(default)

        return super(_Base, cls).__new__(cls, name, bases, dct)
Пример #58
0
 def callable_wrap(x):
     try:
         return callable(x)
     except Exception, msg:
         exceptions.append(msg)
Пример #59
0
def iso8601(datetime_: datetime.datetime = datetime.datetime.now):
    if (callable(datetime_)):
        datetime_ = datetime_()

    return datetime_.strftime("%Y-%m-%dT%H:%MZ")
 def has_constant_default(self):
     return self.default is not None and not callable(self.default)