Ejemplo n.º 1
0
def sort(data, fieldnames=None, already_normalized=False):
    """
    PASS A FIELD NAME, OR LIST OF FIELD NAMES, OR LIST OF STRUCTS WITH {"field":field_name, "sort":direction}
    """
    try:
        if data == None:
            return Null

        if not fieldnames:
            return wrap(sorted(data, value_compare))

        if already_normalized:
            formal = fieldnames
        else:
            formal = query._normalize_sort(fieldnames)

        funcs = [(jx_expression_to_function(f.value), f.sort) for f in formal]

        def comparer(left, right):
            for func, sort_ in funcs:
                try:
                    result = value_compare(func(left), func(right), sort_)
                    if result != 0:
                        return result
                except Exception as e:
                    Log.error("problem with compare", e)
            return 0

        if isinstance(data, list):
            output = FlatList([unwrap(d) for d in sorted(data, cmp=comparer)])
        elif hasattr(data, "__iter__"):
            output = FlatList(
                [unwrap(d) for d in sorted(list(data), cmp=comparer)])
        else:
            Log.error("Do not know how to handle")
            output = None

        return output
    except Exception as e:
        Log.error("Problem sorting\n{{data}}", data=data, cause=e)
Ejemplo n.º 2
0
def sort(data, fieldnames=None, already_normalized=False):
    """
    PASS A FIELD NAME, OR LIST OF FIELD NAMES, OR LIST OF STRUCTS WITH {"field":field_name, "sort":direction}
    """
    try:
        if data == None:
            return Null

        if not fieldnames:
            return wrap(sorted(data, value_compare))

        if already_normalized:
            formal = fieldnames
        else:
            formal = query._normalize_sort(fieldnames)

        funcs = [(jx_expression_to_function(f.value), f.sort) for f in formal]

        def comparer(left, right):
            for func, sort_ in funcs:
                try:
                    result = value_compare(func(left), func(right), sort_)
                    if result != 0:
                        return result
                except Exception as e:
                    Log.error("problem with compare", e)
            return 0

        if isinstance(data, list):
            output = FlatList([unwrap(d) for d in sorted(data, cmp=comparer)])
        elif hasattr(data, "__iter__"):
            output = FlatList([unwrap(d) for d in sorted(list(data), cmp=comparer)])
        else:
            Log.error("Do not know how to handle")
            output = None

        return output
    except Exception as e:
        Log.error("Problem sorting\n{{data}}",  data=data, cause=e)
Ejemplo n.º 3
0
def sort(data, fieldnames=None):
    """
    PASS A FIELD NAME, OR LIST OF FIELD NAMES, OR LIST OF STRUCTS WITH {"field":field_name, "sort":direction}
    """
    try:
        if data == None:
            return Null

        if not fieldnames:
            return wrap(sorted(data, value_compare))

        fieldnames = listwrap(fieldnames)
        if len(fieldnames) == 1:
            fieldnames = fieldnames[0]
            # SPECIAL CASE, ONLY ONE FIELD TO SORT BY
            if fieldnames == ".":
                return wrap(sorted(data))
            if isinstance(fieldnames, (basestring, int)):
                fieldnames = wrap({"value": fieldnames, "sort": 1})

            # EXPECTING {"field":f, "sort":i} FORMAT
            fieldnames.sort = sort_direction.get(fieldnames.sort, 1)
            fieldnames.value = coalesce(fieldnames.value, fieldnames.field)
            if fieldnames.value == None:
                Log.error("Expecting sort to have 'value' attribute")

            if fieldnames.value == ".":
                #VALUE COMPARE
                def _compare_v(l, r):
                    return value_compare(l, r, fieldnames.sort)
                return DictList([unwrap(d) for d in sorted(data, cmp=_compare_v)])
            elif isinstance(fieldnames.value, Mapping):
                func = qb_expression_to_function(fieldnames.value)
                def _compare_o(left, right):
                    return value_compare(func(coalesce(left)), func(coalesce(right)), fieldnames.sort)
                return DictList([unwrap(d) for d in sorted(data, cmp=_compare_o)])
            else:
                def _compare_o(left, right):
                    return value_compare(coalesce(left)[fieldnames.value], coalesce(right)[fieldnames.value], fieldnames.sort)
                return DictList([unwrap(d) for d in sorted(data, cmp=_compare_o)])

        formal = query._normalize_sort(fieldnames)
        for f in formal:
            f.func = qb_expression_to_function(f.value)

        def comparer(left, right):
            left = coalesce(left)
            right = coalesce(right)
            for f in formal:
                try:
                    result = value_compare(f.func(left), f.func(right), f.sort)
                    if result != 0:
                        return result
                except Exception, e:
                    Log.error("problem with compare", e)
            return 0

        if isinstance(data, list):
            output = DictList([unwrap(d) for d in sorted(data, cmp=comparer)])
        elif hasattr(data, "__iter__"):
            output = DictList([unwrap(d) for d in sorted(list(data), cmp=comparer)])
        else:
            Log.error("Do not know how to handle")
            output = None

        return output
Ejemplo n.º 4
0
def sort(data, fieldnames=None):
    """
    PASS A FIELD NAME, OR LIST OF FIELD NAMES, OR LIST OF STRUCTS WITH {"field":field_name, "sort":direction}
    """
    try:
        if data == None:
            return Null

        if fieldnames == None:
            return wrap(sorted(data))

        fieldnames = listwrap(fieldnames)
        if len(fieldnames) == 1:
            fieldnames = fieldnames[0]
            # SPECIAL CASE, ONLY ONE FIELD TO SORT BY
            if isinstance(fieldnames, (basestring, int)):
                fieldnames = wrap({"field": fieldnames, "sort": 1})

            # EXPECTING {"field":f, "sort":i} FORMAT
            fieldnames.sort = sort_direction.get(fieldnames.sort, 1)
            fieldnames.field = coalesce(fieldnames.field, fieldnames.value)
            if fieldnames.field == None:
                Log.error("Expecting sort to have 'field' attribute")

            if fieldnames.field == ".":
                #VALUE COMPARE
                def _compare_v(l, r):
                    return value_compare(l, r, fieldnames.sort)

                return DictList(
                    [unwrap(d) for d in sorted(data, cmp=_compare_v)])
            else:

                def _compare_o(left, right):
                    return value_compare(
                        coalesce(left)[fieldnames.field],
                        coalesce(right)[fieldnames.field], fieldnames.sort)

                return DictList(
                    [unwrap(d) for d in sorted(data, cmp=_compare_o)])

        formal = query._normalize_sort(fieldnames)

        def comparer(left, right):
            left = coalesce(left)
            right = coalesce(right)
            for f in formal:
                try:
                    result = value_compare(left[f.field], right[f.field],
                                           f.sort)
                    if result != 0:
                        return result
                except Exception, e:
                    Log.error("problem with compare", e)
            return 0

        if isinstance(data, list):
            output = DictList([unwrap(d) for d in sorted(data, cmp=comparer)])
        elif hasattr(data, "__iter__"):
            output = DictList(
                [unwrap(d) for d in sorted(list(data), cmp=comparer)])
        else:
            Log.error("Do not know how to handle")
            output = None

        return output