-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add myself as a maintainer for it. - Some integration tests.
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright: Max Gautier <[email protected]> | ||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
DOCUMENTATION = ''' | ||
name: accumulate | ||
short_description: Produce a list of accumulated sums of the input list contents. | ||
version_added: 10.0.0 | ||
author: Max Gautier (@VannTen) | ||
description: | ||
- Passthrough to L(python itertools.accumulate function,https://docs.python.org/3/library/itertools.html#itertools.accumulate). | ||
- Transforms an input list into the cumulative list of results from applying addition to the elements of the input list. | ||
- Addition means the default python implementation of '+' for input list elements type. | ||
options: | ||
_input: | ||
description: A list | ||
type: list | ||
elements: any | ||
required: true | ||
''' | ||
|
||
RETURN = ''' | ||
_value: | ||
description: A list of cumulated sums of the elements of the input list | ||
type: list | ||
''' | ||
|
||
EXAMPLES = ''' | ||
- name: Enumerate parent directories of some path | ||
ansible.builtin.debug: | ||
var: > | ||
"/some/path/to/my/file" | ||
| split('/') | map('split', '/') | ||
| community.general.enumerate | map('join', '/') | ||
# Produces: ['', '/some', '/some/path', '/some/path/to', '/some/path/to/my', '/some/path/to/my/file'] | ||
''' | ||
|
||
from itertools import accumulate | ||
|
||
def list_accumulate(sequence): | ||
return accumulate(sequence) | ||
|
||
|
||
class FilterModule(object): | ||
|
||
def filters(self): | ||
return { | ||
'accumulate': list_accumulate, | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/integration/targets/filter_accumulate/tasks/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
# Copyright (c), Max Gautier <[email protected]> | ||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
- name: "Test accumulate filter" | ||
assert: | ||
that: | ||
- "['a', 'b'] | community.general.accumulate == ['a', 'ab']" | ||
- "[1, 2, 3] | community.general.accumulate == [1, 3, 6]" | ||
- "[['foo'],['bar'],['foobar']] | community.general.accumulate == [['foo'], ['foo', 'bar'], ['foo', 'bar', 'foobar']]" | ||
- "'path/to/file' | split('/') | map('split', '/') | community.general.accumulate | map('join', '/') == ['path', 'path/to', 'path/to/file']" | ||
- "[{'foo':1}, {'bar':2}] | map('dict2items') | community.general.accumulate | map('items2dict') == [{'foo':1}, {'foo':1, 'bar':2}]" |