<commit_before>"""Cmdlr yaml access functions."""

import os

import yaml


yaml.Dumper.ignore_aliases = lambda *args: True


def from_yaml_file(filepath):
    """Get yaml data from file."""
    with open(filepath, 'r', encoding='utf8') as f:
        return (yaml.load(
            f.read(),
            Loader=getattr(yaml, 'CSafeLoader', yaml.SafeLoader),
        ) or {})


def to_yaml_file(filepath, data, comment_out=False):
    """Save data to yaml file."""
    dirpath = os.path.dirname(filepath)

    if dirpath:
        os.makedirs(dirpath, exist_ok=True)

    with open(filepath, 'w', encoding='utf8') as f:
        content = yaml.dump(data,
                            default_flow_style=False,
                            allow_unicode=True,
                            width=78,
                            indent=4)

        if comment_out:
            content = '\n'.join('# ' + line for line in content.split('\n'))

        f.write(content)
<commit_msg>tweak yamla comment-out gen logic<commit_after>"""Cmdlr yaml access functions."""

import os
from textwrap import indent

import yaml


yaml.Dumper.ignore_aliases = lambda *args: True


def from_yaml_file(filepath):
    """Get yaml data from file."""
    with open(filepath, 'r', encoding='utf8') as f:
        return (yaml.load(
            f.read(),
            Loader=getattr(yaml, 'CSafeLoader', yaml.SafeLoader),
        ) or {})


def to_yaml_file(filepath, data, comment_out=False):
    """Save data to yaml file."""
    dirpath = os.path.dirname(filepath)

    if dirpath:
        os.makedirs(dirpath, exist_ok=True)

    with open(filepath, 'w', encoding='utf8') as f:
        content = yaml.dump(data,
                            default_flow_style=False,
                            allow_unicode=True,
                            width=78,
                            indent=4)

        if comment_out:
            content = indent(content, '# ')

        f.write(content)

{'fraction_non_alphanumeric': 0.12020460358056266, 'fraction_numerical': 0.005115089514066497, 'mean_word_length': 1.995405819295559, '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': '7417409', 'n_tokens_mistral': 607, 'n_tokens_neox': 575, 'n_words': 141}