Python django.utils.datastructures.OrderedSet() Examples

The following are 30 code examples of django.utils.datastructures.OrderedSet(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.utils.datastructures , or try the search function .
Example #1
Source File: graph.py    From python with Apache License 2.0 6 votes vote down vote up
def iterative_dfs(self, start, forwards=True):
        """
        Iterative depth first search, for finding dependencies.
        """
        visited = deque()
        visited.append(start)
        if forwards:
            stack = deque(sorted(start.parents))
        else:
            stack = deque(sorted(start.children))
        while stack:
            node = stack.popleft()
            visited.appendleft(node)
            if forwards:
                children = sorted(node.parents, reverse=True)
            else:
                children = sorted(node.children, reverse=True)
            # reverse sorting is needed because prepending using deque.extendleft
            # also effectively reverses values
            stack.extendleft(children)

        return list(OrderedSet(visited)) 
Example #2
Source File: runner.py    From Hands-On-Application-Development-with-PyCharm with MIT License 6 votes vote down vote up
def reorder_suite(suite, classes, reverse=False):
    """
    Reorder a test suite by test type.

    `classes` is a sequence of types

    All tests of type classes[0] are placed first, then tests of type
    classes[1], etc. Tests with no match in classes are placed last.

    If `reverse` is True, sort tests within classes in opposite order but
    don't reverse test classes.
    """
    class_count = len(classes)
    suite_class = type(suite)
    bins = [OrderedSet() for i in range(class_count + 1)]
    partition_suite_by_type(suite, classes, bins, reverse=reverse)
    reordered_suite = suite_class()
    for i in range(class_count + 1):
        reordered_suite.addTests(bins[i])
    return reordered_suite 
Example #3
Source File: runner.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
def reorder_suite(suite, classes, reverse=False):
    """
    Reorders a test suite by test type.

    `classes` is a sequence of types

    All tests of type classes[0] are placed first, then tests of type
    classes[1], etc. Tests with no match in classes are placed last.

    If `reverse` is True, tests within classes are sorted in opposite order,
    but test classes are not reversed.
    """
    class_count = len(classes)
    suite_class = type(suite)
    bins = [OrderedSet() for i in range(class_count + 1)]
    partition_suite(suite, classes, bins, reverse=reverse)
    reordered_suite = suite_class()
    for i in range(class_count + 1):
        reordered_suite.addTests(bins[i])
    return reordered_suite 
Example #4
Source File: lookups.py    From django-hashid-field with MIT License 6 votes vote down vote up
def process_rhs(self, compiler, connection):
        db_rhs = getattr(self.rhs, '_db', None)
        if db_rhs is not None and db_rhs != connection.alias:
            raise ValueError(
                "Subqueries aren't allowed across different databases. Force "
                "the inner query to be evaluated using `list(inner_query)`."
            )

        if self.rhs_is_direct_value():
            try:
                rhs = OrderedSet(self.rhs)
            except TypeError:  # Unhashable items in self.rhs
                rhs = self.rhs

            if not rhs:
                raise EmptyResultSet

            # rhs should be an iterable; use batch_process_rhs() to
            # prepare/transform those values.
            sqls, sqls_params = self.batch_process_rhs(compiler, connection, rhs)
            placeholder = '(' + ', '.join(sqls) + ')'
            return (placeholder, sqls_params)
        else:
            return super().process_rhs(compiler, connection) 
Example #5
Source File: graph.py    From python2017 with MIT License 6 votes vote down vote up
def iterative_dfs(self, start, forwards=True):
        """
        Iterative depth first search, for finding dependencies.
        """
        visited = deque()
        visited.append(start)
        if forwards:
            stack = deque(sorted(start.parents))
        else:
            stack = deque(sorted(start.children))
        while stack:
            node = stack.popleft()
            visited.appendleft(node)
            if forwards:
                children = sorted(node.parents, reverse=True)
            else:
                children = sorted(node.children, reverse=True)
            # reverse sorting is needed because prepending using deque.extendleft
            # also effectively reverses values
            stack.extendleft(children)

        return list(OrderedSet(visited)) 
Example #6
Source File: graph.py    From openhgsenti with Apache License 2.0 6 votes vote down vote up
def iterative_dfs(self, start, forwards=True):
        """
        Iterative depth first search, for finding dependencies.
        """
        visited = deque()
        visited.append(start)
        if forwards:
            stack = deque(sorted(start.parents))
        else:
            stack = deque(sorted(start.children))
        while stack:
            node = stack.popleft()
            visited.appendleft(node)
            if forwards:
                children = sorted(node.parents, reverse=True)
            else:
                children = sorted(node.children, reverse=True)
            # reverse sorting is needed because prepending using deque.extendleft
            # also effectively reverses values
            stack.extendleft(children)

        return list(OrderedSet(visited)) 
Example #7
Source File: graph.py    From python2017 with MIT License 5 votes vote down vote up
def descendants(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_descendants' not in self.__dict__:
            descendants = deque([self.key])
            for child in sorted(self.children):
                descendants.extendleft(reversed(child.descendants()))
            self.__dict__['_descendants'] = list(OrderedSet(descendants))
        return self.__dict__['_descendants'] 
Example #8
Source File: options.py    From python with Apache License 2.0 5 votes vote down vote up
def get_parent_list(self):
        """
        Returns all the ancestors of this model as a list ordered by MRO.
        Useful for determining if something is an ancestor, regardless of lineage.
        """
        result = OrderedSet(self.parents)
        for parent in self.parents:
            for ancestor in parent._meta.get_parent_list():
                result.add(ancestor)
        return list(result) 
Example #9
Source File: events.py    From GetTogether with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def manage_event_sponsors(request, event_id):
    event = get_object_or_404(Event, id=event_id)
    if not request.user.profile.can_edit_event(event):
        messages.add_message(
            request,
            messages.WARNING,
            message=_("You can not manage this event's sponsorss."),
        )
        return redirect(event.get_absolute_url())

    team_sponsors = list(event.team.sponsors.all())
    events_sponsors = list(Sponsor.objects.filter(events__team=event.team))

    if request.method == "POST":
        sponsor_form = SponsorForm(request.POST, request.FILES)
        if sponsor_form.is_valid():
            new_sponsor = sponsor_form.save()
            event.sponsors.add(new_sponsor)
            event.team.sponsors.add(new_sponsor)
            messages.add_message(
                request,
                messages.SUCCESS,
                message=_("Your sponsor has been added to this event."),
            )
            return redirect("manage-event-sponsors", event.id)

    else:
        sponsor_form = SponsorForm()
    context = {
        "event": event,
        "sponsors": OrderedSet(events_sponsors + team_sponsors),
        "sponsor_form": sponsor_form,
        "can_edit_event": request.user.profile.can_edit_event(event),
    }
    return render(request, "get_together/events/manage_event_sponsors.html", context) 
Example #10
Source File: graph.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def ancestors(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_ancestors' not in self.__dict__:
            ancestors = deque([self.key])
            for parent in sorted(self.parents):
                ancestors.extendleft(reversed(parent.ancestors()))
            self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
        return self.__dict__['_ancestors']

    # Use manual caching, @cached_property effectively doubles the
    # recursion depth for each recursion. 
Example #11
Source File: graph.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def descendants(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_descendants' not in self.__dict__:
            descendants = deque([self.key])
            for child in sorted(self.children):
                descendants.extendleft(reversed(child.descendants()))
            self.__dict__['_descendants'] = list(OrderedSet(descendants))
        return self.__dict__['_descendants'] 
Example #12
Source File: options.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def get_parent_list(self):
        """
        Returns all the ancestors of this model as a list ordered by MRO.
        Useful for determining if something is an ancestor, regardless of lineage.
        """
        result = OrderedSet(self.parents)
        for parent in self.parents:
            for ancestor in parent._meta.get_parent_list():
                result.add(ancestor)
        return list(result) 
Example #13
Source File: graph.py    From python2017 with MIT License 5 votes vote down vote up
def ancestors(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_ancestors' not in self.__dict__:
            ancestors = deque([self.key])
            for parent in sorted(self.parents):
                ancestors.extendleft(reversed(parent.ancestors()))
            self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
        return self.__dict__['_ancestors']

    # Use manual caching, @cached_property effectively doubles the
    # recursion depth for each recursion. 
Example #14
Source File: graph.py    From python with Apache License 2.0 5 votes vote down vote up
def descendants(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_descendants' not in self.__dict__:
            descendants = deque([self.key])
            for child in sorted(self.children):
                descendants.extendleft(reversed(child.descendants()))
            self.__dict__['_descendants'] = list(OrderedSet(descendants))
        return self.__dict__['_descendants'] 
Example #15
Source File: options.py    From python2017 with MIT License 5 votes vote down vote up
def get_parent_list(self):
        """
        Returns all the ancestors of this model as a list ordered by MRO.
        Useful for determining if something is an ancestor, regardless of lineage.
        """
        result = OrderedSet(self.parents)
        for parent in self.parents:
            for ancestor in parent._meta.get_parent_list():
                result.add(ancestor)
        return list(result) 
Example #16
Source File: views.py    From jararaca with GNU General Public License v3.0 5 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['future_events'] = OrderedSet(Event.objects.filter(eventday__date__gte=date.today()).order_by(
            'eventday__date'))
        context['past_events'] = OrderedSet(Event.objects.filter(eventday__date__lte=date.today()).order_by(
            '-eventday__date'))
        return context 
Example #17
Source File: views.py    From jararaca with GNU General Public License v3.0 5 votes vote down vote up
def get(self, request, *args, **kwargs):
        event = OrderedSet(Event.objects.filter(slug=kwargs['event'], eventday__date__gte=date.today(),
                                                closed_registration=False))
        if not len(event):
            raise Http404

        event = list(event)[0]

        return self.render_to_response(self.get_context_data(event=event)) 
Example #18
Source File: views.py    From jararaca with GNU General Public License v3.0 5 votes vote down vote up
def form_valid(self, form):
        event = OrderedSet(Event.objects.filter(slug=self.request.POST['event'], eventday__date__gte=date.today(),
                                                closed_registration=False))
        if not len(event):
            raise Http404

        event = list(event)[0]

        attendee = form.instance
        attendee.event = event
        defaults = form.cleaned_data
        authorize = defaults.pop('authorize')
        
        if not authorize:
            messages.error(self.request, _('Registration failed.'))
            return self.form_invalid(form)

        user, created = Attendee.objects.get_or_create(
            event=event,
            email=attendee.email,
            defaults=form.cleaned_data
        )
        try:
            qr_data = senders.send_registration_mail(user, event)
            context = self.get_context_data(qr_code=base64.b64encode(qr_data).decode('ascii'))
            if created:
                return render(self.request, 'site/thanks.html', context)
            return render(self.request, 'site/duplicate.html', context)
        
        except Exception as e:
            print(e)
            messages.error(self.request, _('Registration failed.'))
            return self.form_invalid(form) 
Example #19
Source File: views.py    From jararaca with GNU General Public License v3.0 5 votes vote down vote up
def form_invalid(self, form):
        event = OrderedSet(Event.objects.filter(slug=self.request.POST['event'], eventday__date__gte=date.today(),
                                                closed_registration=False))
        if not len(event):
            raise Http404
        event = list(event)[0]
        return self.render_to_response(self.get_context_data(form=form, event=event)) 
Example #20
Source File: options.py    From bioforum with MIT License 5 votes vote down vote up
def get_parent_list(self):
        """
        Return all the ancestors of this model as a list ordered by MRO.
        Useful for determining if something is an ancestor, regardless of lineage.
        """
        result = OrderedSet(self.parents)
        for parent in self.parents:
            for ancestor in parent._meta.get_parent_list():
                result.add(ancestor)
        return list(result) 
Example #21
Source File: graph.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def descendants(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_descendants' not in self.__dict__:
            descendants = deque([self.key])
            for child in sorted(self.children):
                descendants.extendleft(reversed(child.descendants()))
            self.__dict__['_descendants'] = list(OrderedSet(descendants))
        return self.__dict__['_descendants'] 
Example #22
Source File: options.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def get_parent_list(self):
        """
        Returns all the ancestors of this model as a list ordered by MRO.
        Useful for determining if something is an ancestor, regardless of lineage.
        """
        result = OrderedSet(self.parents)
        for parent in self.parents:
            for ancestor in parent._meta.get_parent_list():
                result.add(ancestor)
        return list(result) 
Example #23
Source File: graph.py    From bioforum with MIT License 5 votes vote down vote up
def ancestors(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_ancestors' not in self.__dict__:
            ancestors = []
            for parent in sorted(self.parents, reverse=True):
                ancestors += parent.ancestors()
            ancestors.append(self.key)
            self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
        return self.__dict__['_ancestors']

    # Use manual caching, @cached_property effectively doubles the
    # recursion depth for each recursion. 
Example #24
Source File: graph.py    From bioforum with MIT License 5 votes vote down vote up
def descendants(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_descendants' not in self.__dict__:
            descendants = []
            for child in sorted(self.children, reverse=True):
                descendants += child.descendants()
            descendants.append(self.key)
            self.__dict__['_descendants'] = list(OrderedSet(descendants))
        return self.__dict__['_descendants'] 
Example #25
Source File: graph.py    From bioforum with MIT License 5 votes vote down vote up
def iterative_dfs(self, start, forwards=True):
        """Iterative depth-first search for finding dependencies."""
        visited = []
        stack = [start]
        while stack:
            node = stack.pop()
            visited.append(node)
            stack += sorted(node.parents if forwards else node.children)
        return list(OrderedSet(reversed(visited))) 
Example #26
Source File: graph.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def ancestors(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_ancestors' not in self.__dict__:
            ancestors = deque([self.key])
            for parent in sorted(self.parents):
                ancestors.extendleft(reversed(parent.ancestors()))
            self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
        return self.__dict__['_ancestors']

    # Use manual caching, @cached_property effectively doubles the
    # recursion depth for each recursion. 
Example #27
Source File: graph.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def ancestors(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_ancestors' not in self.__dict__:
            ancestors = []
            for parent in sorted(self.parents, reverse=True):
                ancestors += parent.ancestors()
            ancestors.append(self.key)
            self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
        return self.__dict__['_ancestors']

    # Use manual caching, @cached_property effectively doubles the
    # recursion depth for each recursion. 
Example #28
Source File: graph.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def descendants(self):
        # Use self.key instead of self to speed up the frequent hashing
        # when constructing an OrderedSet.
        if '_descendants' not in self.__dict__:
            descendants = []
            for child in sorted(self.children, reverse=True):
                descendants += child.descendants()
            descendants.append(self.key)
            self.__dict__['_descendants'] = list(OrderedSet(descendants))
        return self.__dict__['_descendants'] 
Example #29
Source File: graph.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def iterative_dfs(self, start, forwards=True):
        """Iterative depth-first search for finding dependencies."""
        visited = []
        stack = [start]
        while stack:
            node = stack.pop()
            visited.append(node)
            stack += sorted(node.parents if forwards else node.children)
        return list(OrderedSet(reversed(visited))) 
Example #30
Source File: lookups.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def process_rhs(self, compiler, connection):
        db_rhs = getattr(self.rhs, '_db', None)
        if db_rhs is not None and db_rhs != connection.alias:
            raise ValueError(
                "Subqueries aren't allowed across different databases. Force "
                "the inner query to be evaluated using `list(inner_query)`."
            )

        if self.rhs_is_direct_value():
            try:
                rhs = OrderedSet(self.rhs)
            except TypeError:  # Unhashable items in self.rhs
                rhs = self.rhs

            if not rhs:
                raise EmptyResultSet

            # rhs should be an iterable; use batch_process_rhs() to
            # prepare/transform those values.
            sqls, sqls_params = self.batch_process_rhs(compiler, connection, rhs)
            placeholder = '(' + ', '.join(sqls) + ')'
            return (placeholder, sqls_params)
        else:
            if not getattr(self.rhs, 'has_select_fields', True):
                self.rhs.clear_select_clause()
                self.rhs.add_fields(['pk'])
            return super().process_rhs(compiler, connection)