Пример #1
0
def mounet1(with_key_filter=False):
    pw_calc = Group.get(pk=1139193).nodes.next()
    structure = pw_calc.out.output_structure
    qstruc = StructureData.query(children__pk=structure.pk)
    attr_filters = models.DbAttribute.objects.filter(tval__endswith='alvarez')

    # Because we can't reproduce a filter on the value only with a JSON table,
    # a fairer comparison would be with a filter on the key too.
    if with_key_filter:
        attr_filters = attr_filters.filter(
            Q(key="radii_source") | Q(key="lowdim_dict.radii_source"))

    qic = InlineCalculation.query(inputs__in=qstruc).filter(
        inputs__dbattributes__in=attr_filters).distinct()

    return qic.count()
Пример #2
0
def mounet1():
    pw_calc = Group.get(pk=1139193).nodes.next()
    structure = pw_calc.out.output_structure
    qstruc = StructureData.query(children__pk=structure.pk).with_entities(
        DbNode.id)
    n_children = aliased(DbNode)
    qic = (InlineCalculation.query(
    ).join(DbLink, DbNode.id == DbLink.output_id).filter(
        DbLink.input_id.in_(qstruc)).join(n_children, DbNode.inputs).filter(
            or_(
                n_children.attributes["radii_source"].astext.like("%alvarez"),
                n_children.attributes[(
                    "lowdim_dict",
                    "radii_source")].astext.like("%alvarez"))).distinct())

    return qic.with_entities(func.count(DbNode.id)).scalar()
Пример #3
0
def mounet2():
    StructureData = DataFactory('structure')
    structure = load_node(2304207)
    qstruc = StructureData.query(children__pk=structure.pk).with_entities(
        DbNode.id)

    n_children = aliased(DbNode)
    qic = (InlineCalculation.query().filter(
        DbNode.attributes["function_name"].astext == "lowdimfinder_inline"
    ).join(DbLink, DbNode.id == DbLink.output_id).filter(
        DbLink.input_id.in_(qstruc)).join(n_children, DbNode.inputs).filter(
            or_(
                n_children.attributes["radii_source"].astext.like("%alvarez"),
                n_children.attributes[(
                    "lowdim_dict",
                    "radii_source")].astext.like("%alvarez"))).distinct())

    return qic.with_entities(func.count(DbNode.id)).scalar()
Пример #4
0
def mounet2(with_key_filter=False):
    StructureData = DataFactory('structure')
    structure = load_node(2304207)
    qstruc = StructureData.query(children__pk=structure.pk)
    qattr = models.DbAttribute.objects.filter(key='function_name',
                                              tval='lowdimfinder_inline',
                                              dbnode__inputs__in=qstruc)

    attr_filters = models.DbAttribute.objects.filter(tval__endswith='alvarez')

    if with_key_filter:
        attr_filters = attr_filters.filter(
            Q(key="radii_source") | Q(key="lowdim_dict.radii_source"))

    qic = InlineCalculation.query(
        inputs__in=qstruc, dbattributes__in=qattr).filter(
            inputs__dbattributes__in=attr_filters).distinct()

    return qic.count()
Пример #5
0
    def wrapped_function(*args, **kwargs):
        """
        This wrapper function is the actual function that is called.
        """
        from django.db import transaction

        # Note: if you pass a lambda function, the name will be <lambda>; moreover
        # if you define a function f, and then do "h=f", h.__name__ will
        # still return 'f'!
        function_name = func.__name__
        if not function_name.endswith('_inline'):
            raise ValueError(
                "The function name that is wrapped must end "
                "with '_inline', while its name is '{}'".format(function_name))

        if args:
            print args
            raise ValueError("Arguments of inline function should be "
                             "passed as key=value")

        # Check the input values
        for k, v in kwargs.iteritems():
            if not isinstance(v, Data):
                raise TypeError("Input data to a wrapped inline calculation "
                                "must be Data nodes")
                # kwargs should always be strings, no need to check
                # if not isinstance(k, basestring):
                #    raise TypeError("")

        # Create the calculation (unstored)
        c = InlineCalculation()
        # Add data input nodes as links
        for k, v in kwargs.iteritems():
            c.add_link_from(v, label=k)

        # Try to get the source code
        add_source_info(c, func)

        # Run the wrapped function
        retval = func(**kwargs)

        # Check the output values
        if not isinstance(retval, dict):
            raise TypeError("The wrapped function did not return a dictionary")
        for k, v in retval.iteritems():
            if not isinstance(k, basestring):
                raise TypeError("One of the key of the dictionary returned by "
                                "the wrapped function is not a string: "
                                "'{}'".format(k))
            if not isinstance(v, Data):
                raise TypeError("One of the values (for key '{}') of the "
                                "dictionary returned by the wrapped function "
                                "is not a Data node".format(k))
            if v.is_stored:
                raise ModificationNotAllowed(
                    "One of the values (for key '{}') of the "
                    "dictionary returned by the wrapped function "
                    "is already stored! Note that this node (and "
                    "any other side effect of the function) are "
                    "not going to be undone!".format(k))

        # Add link to output data nodes
        for k, v in retval.iteritems():
            v.add_link_from(c, label=k, link_type=LinkType.RETURN)

        with transaction.atomic():
            # I call store_all for the Inline calculation;
            # this will store also the inputs, if needed.
            c.store_all(with_transaction=False)
            # As c is already stored, I just call store (and not store_all)
            # on each output
            for v in retval.itervalues():
                v.store(with_transaction=False)

        # Return the calculation and the return values
        return c, retval