Private
Public Access
2
0

dic2str: doc

This commit is contained in:
2026-02-02 22:08:27 +03:00
parent ccdabff0be
commit 7e4857f762

View File

@@ -1,41 +1,36 @@
#!/usr/bin/python
class FilterModule(object):
def filters(self):
return {
'dict2str': self.dict2str
}
def dict2str(self, d, sep1='=', sep2=';', sep3=''):
return sep2.join(
[ str(k) + (sep1+str(v) if v!=None and v!='' else '') for k, v in d.items() ]
) + sep3
DOCUMENTATION = """
DOCUMENTATION = r"""
name: dict2str
author: Sébastien Namèche (@seb4itik)
short_description: This filter transforms a flat dict into a string using 4 separators.
version_added: "0.1.1"
short_description: This filter transforms a flat dict into a string using 3 separators.
author: Sébastien Namèche (@seb4itik)
description:
- If my_dict is {one: "un", two: "deux", three: "trois"}, then:
- {{ my_dict | dict2str(sep1=' is ', sep2=', ', sep3='.') }}
- will produce:
- "one is un, two is deux, three is trois."
options:
_input:
description: A flat dict.
type: dict
elements: str, int, bool
required: true
sep0:
description: Unique initial separator at the beginning of string.
type: str
default: ""
sep1:
description:
- separator between each key and value pairs of the dict
description: Separator between each key and value pairs of the dict.
type: str
default: "="
sep2:
description:
- separator between key/value pairs of the dict that has been combined with sep1
description: Separator between key/value pairs of the dict that has been combined with sep1.
type: str
default: ";"
sep3:
description:
- unique final separator at the end of string
description: Unique final separator at the end of string.
type: str
default: ""
notes:
@@ -43,9 +38,22 @@ DOCUMENTATION = """
- sep2 will not be used.
"""
RETURN = """
data:
RETURN = r"""
_value:
type: str
description:
- String representating the dict using provided separators.
"""
class FilterModule(object):
def filters(self):
return {
'dict2str': self.dict2str
}
def dict2str(self, d, sep0='', sep1='=', sep2=';', sep3=''):
return sep0 + sep2.join(
[ str(k) + (sep1+str(v) if v!=None and v!='' else '') for k, v in d.items() ]
) + sep3