Exemplo n.º 1
0
def pipe_sort(context, _INPUT, conf, **kwargs):
    """This operator sorts the input source according to the specified key. 

    Keyword arguments:
    context -- pipeline context        
    _INPUT -- source generator
    kwargs -- other inputs, e.g. to feed terminals for rule values
    conf:
        KEY -- list of fields to sort by
    
    Yields (_OUTPUT):
    source items sorted by key
    """
    order = []
       
    keys = conf['KEY']
    if not isinstance(keys, list):
        keys = [keys]
    for key in keys:
        field = util.get_value(key['field'], None, **kwargs)
        sort_dir = util.get_value(key['dir'], None, **kwargs)
        order.append('%s%s' % (sort_dir=='DESC' and '-' or '', field))

    #read all and sort
    sorted_input = []
    for item in _INPUT:
        sorted_input.append(item)
    sorted_input = util.multikeysort(sorted_input, order)
            
    for item in sorted_input:
        yield item
        
Exemplo n.º 2
0
def pipe_uniq(context, _INPUT, conf, **kwargs):
    """This operator filters out non unique items according to the specified field. 

    Keyword arguments:
    context -- pipeline context        
    _INPUT -- source generator
    kwargs -- other inputs, e.g. to feed terminals for rule values
    conf:
        field -- field to be unique
    
    Yields (_OUTPUT):
    source items, one per unique field value
    """
       
    field = util.get_value(conf['field'], None, **kwargs)
    order = ['%s%s' % ('', field)]

    #read all and sort
    sorted_input = []
    for item in _INPUT:
        sorted_input.append(item)
    sorted_input = util.multikeysort(sorted_input, order)
            
    seen = None
    for item in sorted_input:
        #todo: do we ever need get_value here instead of item[]?
        v = util.get_subkey(field, item)
        if seen != v:
            yield item
            seen = v
Exemplo n.º 3
0
def pipe_uniq(context, _INPUT, conf, **kwargs):
    """This operator filters out non unique items according to the specified field. 

    Keyword arguments:
    context -- pipeline context        
    _INPUT -- source generator
    kwargs -- other inputs, e.g. to feed terminals for rule values
    conf:
        field -- field to be unique
    
    Yields (_OUTPUT):
    source items, one per unique field value
    """

    field = util.get_value(conf['field'], None, **kwargs)
    order = ['%s%s' % ('', field)]

    #read all and sort
    sorted_input = []
    for item in _INPUT:
        sorted_input.append(item)
    sorted_input = util.multikeysort(sorted_input, order)

    seen = None
    for item in sorted_input:
        #todo: do we ever need get_value here instead of item[]?
        if seen != item[field]:
            yield item
            seen = item[field]
Exemplo n.º 4
0
def pipe_sort(context, _INPUT, conf, **kwargs):
    """This operator sorts the input source according to the specified key. 

    Keyword arguments:
    context -- pipeline context        
    _INPUT -- source generator
    kwargs -- other inputs, e.g. to feed terminals for rule values
    conf:
        KEY -- list of fields to sort by
    
    Yields (_OUTPUT):
    source items sorted by key
    """
    order = []

    keys = conf['KEY']
    if not isinstance(keys, list):
        keys = [keys]
    for key in keys:
        field = util.get_value(key['field'], None, **kwargs)
        sort_dir = util.get_value(key['dir'], None, **kwargs)
        order.append('%s%s' % (sort_dir == 'DESC' and '-' or '', field))

    #read all and sort
    sorted_input = []
    for item in _INPUT:
        sorted_input.append(item)
    sorted_input = util.multikeysort(sorted_input, order)

    for item in sorted_input:
        yield item