xtuml/meta.py
def sort_reflexive(set_of_instances, rel_id, phrase):
    '''
    Sort a *set of instances* in the order they appear across a conditional and
    reflexive association. The first instance in the resulting ordered set is
    **not** associated to an instance across the given *phrase*.
    '''
    if not isinstance(set_of_instances, QuerySet):
        raise MetaException('The collection to sort must be a QuerySet')
    
    if not set_of_instances.first:
        return QuerySet()
    
    if isinstance(rel_id, int):
        rel_id = 'R%d' % rel_id
    
    # Figure out the phrase in the other direction
    metaclass = get_metaclass(set_of_instances.first)
    for link in metaclass.links.values():
        if link.to_metaclass != metaclass:
            continue
        
        if link.rel_id != rel_id:
            continue

        if link.phrase == phrase:
            continue

        other_phrase = link.phrase
        break
    else:
        raise UnknownLinkException(metaclass.kind, metaclass.kind, rel_id, phrase)
    
    first_filt = lambda sel: not navigate_one(sel).nav(metaclass.kind, rel_id, phrase)()
    first_instances = list(filter(first_filt, set_of_instances))
    if not first_instances:
        #the instance sequence is recursive, start anywhere
        first_instances = [set_of_instances.first]
    
    def sequence_generator():
        for first in first_instances:
            inst = first
            while inst:
                if inst in set_of_instances:
                    yield inst
                inst = navigate_one(inst).nav(metaclass.kind, rel_id, other_phrase)()
                if inst is first:
                    break
                
    return QuerySet(sequence_generator())
def sort_reflexive(set_of_instances, rel_id, phrase):
    '''
    Sort a *set of instances* in the order they appear across a conditional and
    reflexive association. The first instance in the resulting ordered set is
    **not** associated to an instance across the given *phrase*.
    '''
    if not isinstance(set_of_instances, QuerySet):
        raise MetaException('The collection to sort must be a QuerySet')
    
    if not set_of_instances.first:
        return QuerySet()
    
    if isinstance(rel_id, int):
        rel_id = 'R%d' % rel_id
    
    # Figure out the phrase in the other direction
    metaclass = get_metaclass(set_of_instances.first)
    for link in metaclass.links.values():
        if link.to_metaclass != metaclass:
            continue
        
        if link.rel_id != rel_id:
            continue

        if link.phrase == phrase:
            continue

        other_phrase = link.phrase
        break
    else:
        raise UnknownLinkException(metaclass.kind, metaclass.kind, rel_id, phrase)
    
    first_filt = lambda sel: not navigate_one(sel).nav(metaclass.kind, rel_id, phrase)()
    first_instances = list(filter(first_filt, set_of_instances))
    if not first_instances:
        #the instance sequence is recursive, start anywhere
        first_instances = [set_of_instances.first]
    
    def sequence_generator():
        for first in first_instances:
            inst = first
            while inst:
                if inst in set_of_instances:
                    yield inst
                inst = navigate_one(inst).nav(metaclass.kind, rel_id, other_phrase)()
                if inst is first:
                    break
                
    return QuerySet(sequence_generator())
def sort_reflexive ( set_of_instances , rel_id , phrase ) : if not isinstance ( set_of_instances , QuerySet ) : raise MetaException ( 'The collection to sort must be a QuerySet' ) if not set_of_instances . first : return QuerySet ( ) if isinstance ( rel_id , int ) : rel_id = 'R%d' % rel_id # Figure out the phrase in the other direction metaclass = get_metaclass ( set_of_instances . first ) for link in metaclass . links . values ( ) : if link . to_metaclass != metaclass : continue if link . rel_id != rel_id : continue if link . phrase == phrase : continue other_phrase = link . phrase break else : raise UnknownLinkException ( metaclass . kind , metaclass . kind , rel_id , phrase ) first_filt = lambda sel : not navigate_one ( sel ) . nav ( metaclass . kind , rel_id , phrase ) ( ) first_instances = list ( filter ( first_filt , set_of_instances ) ) if not first_instances : #the instance sequence is recursive, start anywhere first_instances = [ set_of_instances . first ] def sequence_generator ( ) : for first in first_instances : inst = first while inst : if inst in set_of_instances : yield inst inst = navigate_one ( inst ) . nav ( metaclass . kind , rel_id , other_phrase ) ( ) if inst is first : break return QuerySet ( sequence_generator ( ) )
Sort a *set of instances* in the order they appear across a conditional and
    reflexive association. The first instance in the resulting ordered set is
    **not** associated to an instance across the given *phrase*.
Sort a * set of instances * in the order they appear across a conditional and reflexive association . The first instance in the resulting ordered set is ** not ** associated to an instance across the given * phrase * .