<commit_before>import itertools
from pathlib import Path
from typing import List


class WorkDir:
    """a simple model for a"""

    commit_command: str
    signed_commit_command: str
    add_command: str

    def __repr__(self):
        return f"<WD {self.cwd}>"

    def __init__(self, cwd: Path) -> None:
        self.cwd = cwd
        self.__counter = itertools.count()

    def __call__(self, cmd: "List[str] | str", **kw):
        if kw:
            assert isinstance(cmd, str), "formatting the command requires text input"
            cmd = cmd.format(**kw)
        from setuptools_scm.utils import do

        return do(cmd, self.cwd)

    def write(self, name: str, value: "str | bytes", **kw: object) -> Path:
        filename = self.cwd / name
        if kw:
            assert isinstance(value, str)
            value = value.format(**kw)
        if isinstance(value, bytes):
            filename.write_bytes(value)
        else:
            filename.write_text(value)
        return filename

    def _reason(self, given_reason: "str | None") -> str:
        if given_reason is None:
            return f"number-{next(self.__counter)}"
        else:
            return given_reason

    def add_and_commit(self, reason: "str | None" = None, **kwargs):
        self(self.add_command)
        self.commit(reason, **kwargs)

    def commit(self, reason: "str | None" = None, signed: bool = False) -> None:
        reason = self._reason(reason)
        self(
            self.commit_command if not signed else self.signed_commit_command,
            reason=reason,
        )

    def commit_testfile(self, reason: "str | None" = None, signed: bool = False):
        reason = self._reason(reason)
        self.write("test.txt", "test {reason}", reason=reason)
        self(self.add_command)
        self.commit(reason=reason, signed=signed)

    def get_version(self, **kw):
        __tracebackhide__ = True
        from setuptools_scm import get_version

        version = get_version(root=str(self.cwd), fallback_root=str(self.cwd), **kw)
        print(version)<commit_msg>refactor test util Workdir.write flor clarity<commit_after>import itertools
from pathlib import Path
from typing import List


class WorkDir:
    """a simple model for a"""

    commit_command: str
    signed_commit_command: str
    add_command: str

    def __repr__(self):
        return f"<WD {self.cwd}>"

    def __init__(self, cwd: Path) -> None:
        self.cwd = cwd
        self.__counter = itertools.count()

    def __call__(self, cmd: "List[str] | str", **kw):
        if kw:
            assert isinstance(cmd, str), "formatting the command requires text input"
            cmd = cmd.format(**kw)
        from setuptools_scm.utils import do

        return do(cmd, self.cwd)

    def write(self, name: str, content: "str | bytes", /, **kw: object) -> Path:
        path = self.cwd / name
        if kw:
            assert isinstance(content, str)
            content = content.format(**kw)
        if isinstance(content, bytes):
            path.write_bytes(content)
        else:
            path.write_text(content)
        return path

    def _reason(self, given_reason: "str | None") -> str:
        if given_reason is None:
            return f"number-{next(self.__counter)}"
        else:
            return given_reason

    def add_and_commit(self, reason: "str | None" = None, **kwargs):
        self(self.add_command)
        self.commit(reason, **kwargs)

    def commit(self, reason: "str | None" = None, signed: bool = False) -> None:
        reason = self._reason(reason)
        self(
            self.commit_command if not signed else self.signed_commit_command,
            reason=reason,
        )

    def commit_testfile(self, reason: "str | None" = None, signed: bool = False):
        reason = self._reason(reason)
        self.write("test.txt", "test {reason}", reason=reason)
        self(self.add_command)
        self.commit(reason=reason, signed=signed)

    def get_version(self, **kw):
        __tracebackhide__ = True
        from setuptools_scm import get_version

        version = get_version(root=str(self.cwd), fallback_root=str(self.cwd), **kw)
        print(version)
{'fraction_non_alphanumeric': 0.1130683170673649, 'fraction_numerical': 0.0, 'mean_word_length': 2.3805309734513274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '5072218', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1238, 'n_words': 340}