I am trying generate a YAML file from Python object in which I have some literal string attribute pattern = "^[0-9]+$"
to add to equally named node pattern
as folder block.
Code
So far I have progressed till below code
import yaml
class MyDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(MyDumper, self).increase_indent(flow, False)
source = {'row_filters':{'NONE':{'filter_sql_expr': True}},
'rule_dimensions': ['completeness','conformance'],
'rules': {'VALID_CUSTOMER_ID': {'rule_type': 'REGEX', 'dimension': 'accuracy', 'params': {'pattern': "^[0-9]+$"}}}}
print(yaml.dump(source, Dumper=MyDumper, default_flow_style=False,sort_keys=False,indent=2,allow_unicode=True))
Actual YAML Output
row_filters:
NONE:
filter_sql_expr: True
rule_dimensions:
- completeness
- conformance
rules:
VALID_CUSTOMER_ID:
rule_type: REGEX
dimension: accuracy
params:
pattern: ^[0-9]+$
Expected YAML output
row_filters:
NONE:
filter_sql_expr: |-
True
rule_dimensions:
- completeness
- conformance
rules:
VALID_CUSTOMER_ID:
rule_type: REGEX
dimension: accuracy
params:
pattern: |-
^[0-9]+$
Wanted
I need to add |-
for multi-line block with block chomping indicator - in order to add strings as block literals.
Questions
- How to force the adding the string in block-style with
|-
? - How to add the method to my my Dumper class for conversion ?