<commit_before>'''
 This is a collection of filters that deals with lists of lists
'''
from __future__ import absolute_import

from ansible import errors
import itertools

class FilterModule(object):
    ''' Ansible list-of-dicts jinja2 filters '''

    def filters(self):
        return {
            'collapse': collapse,
            'expand_ranges': expand_ranges,
        }

def collapse(stuff):
    '''
    collapse will take a list of list and return a single list
    This really should perform the same function as with_flatten,
    excpet that you can use it in a jinja2 template.
    '''
    return list(itertools.chain.from_iterable(stuff))

def expand_ranges(stuff):
    '''
    Expands lists with embedded ranges to a single list
    e.g. 
    ---
    vars:
      ints: 
        - name: range
          prefix: "ge-0/1/"
          range: [0,4] 
        - name: ge-1/0/0
        - name: ge-2/0/0

    tasks:
      - name: expand_ranges
        debug: var={{ item.name }}
        with_items:
          ints|expand_ranges
    '''
    ret = []
    for s in stuff:
        if s['name'] != 'range':
            ret.append(s)
            continue
        for num in range(*s['range']):
            thing = {}
            thing.update(s)
            thing['name'] = "%s%s"%(s['prefix'],num)
            ret.append(thing)
    return ret
<commit_msg>Added option to specify field name to expand_ranges.<commit_after>'''
 This is a collection of filters that deals with lists of lists
'''
from __future__ import absolute_import

from ansible import errors
import itertools

class FilterModule(object):
    ''' Ansible list-of-dicts jinja2 filters '''

    def filters(self):
        return {
            'collapse': collapse,
            'expand_ranges': expand_ranges,
        }

def collapse(stuff):
    '''
    collapse will take a list of list and return a single list
    This really should perform the same function as with_flatten,
    excpet that you can use it in a jinja2 template.
    '''
    return list(itertools.chain.from_iterable(stuff))

def expand_ranges(stuff,field='name'):
    '''
    Expands lists with embedded ranges to a single list
    e.g. 
    ---
    vars:
      ints: 
        - name: range
          prefix: "ge-0/1/"
          range: [0,4] 
        - name: ge-1/0/0
        - name: ge-2/0/0

    tasks:
      - name: expand_ranges
        debug: var={{ item.name }}
        with_items:
          ints|expand_ranges('name')
    '''
    ret = []
    for s in stuff:
        if field not in s:
            ret.append(s)
            continue
        if s[field] != 'range':
            ret.append(s)
            continue
        prefix = s.get('prefix','')
        suffix = s.get('suffix','')
        for num in range(*s['range']):
            thing = {}
            thing.update(s)
            thing[field] = "%s%s%s"%(prefix,num,suffix)
            ret.append(thing)
    return ret

{'fraction_non_alphanumeric': 0.1115305422100206, 'fraction_numerical': 0.008236101578586136, 'mean_word_length': 1.9120879120879122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6248365', 'n_tokens_mistral': 889, 'n_tokens_neox': 868, 'n_words': 266}