<commit_before>"""
Command function to rename columns in a HXL dataset.
<NAME>
October 2014

License: Public Domain
Documentation: https://github.com/HXLStandard/libhxl-python/wiki
"""

import sys
import argparse
import copy
import re
import hxl
from hxl.model import DataProvider, TagPattern, Column
from hxl.filters import make_input, make_output
from hxl.io import StreamInput, HXLReader, write_hxl

class RenameFilter(DataProvider):
    """
    Composable filter class to rename columns in a HXL dataset.

    This is the class supporting the hxlrename command-line utility.

    Because this class is a {@link hxl.model.DataProvider}, you can use
    it as the source to an instance of another filter class to build a
    dynamic, single-threaded processing pipeline.

    Usage:

    <pre>
    source = HXLReader(sys.stdin)
    filter = RenameFilter(source, rename=[[TagPattern.parse('#foo'), Column.parse('#bar')]])
    write_hxl(sys.stdout, filter)
    </pre>
    """

    def __init__(self, source, rename=[]):
        """
        Constructor
        @param source the DataProvider for the data.
        @param rename_map map of tags to rename
        """
        self.source = source
        self.rename = rename
        self._saved_columns = None

    @property
    def columns(self):
        """
        Return the renamed columns.
        """

        if self._saved_columns is None:
            def rename_column(column):
                for spec in self.rename:
                    if spec[0].match(column):
                        new_column = copy.copy(spec[1])
                        if new_column.header is None:
                            new_column.header = column.header
                        return new_column
                return column
            self._saved_columns = [rename_column(column) for column in self.source.columns]
        return self._saved_columns

    def __next__(self):
        return next(self.source)

    next = __next__


#
# Command-line support.
#

RENAME_PATTERN = r'^\s*#?({token}):(?:([^#]*)#)?({token})\s*$'.format(token=hxl.TOKEN)

def parse_rename(s):<commit_msg>Fixed rename filter per #40.<commit_after>"""
Command function to rename columns in a HXL dataset.
<NAME>
October 2014

License: Public Domain
Documentation: https://github.com/HXLStandard/libhxl-python/wiki
"""

import sys
import argparse
import copy
import re
import hxl
from hxl.model import DataProvider, TagPattern, Column
from hxl.filters import make_input, make_output
from hxl.io import StreamInput, HXLReader, write_hxl

class RenameFilter(DataProvider):
    """
    Composable filter class to rename columns in a HXL dataset.

    This is the class supporting the hxlrename command-line utility.

    Because this class is a {@link hxl.model.DataProvider}, you can use
    it as the source to an instance of another filter class to build a
    dynamic, single-threaded processing pipeline.

    Usage:

    <pre>
    source = HXLReader(sys.stdin)
    filter = RenameFilter(source, rename=[[TagPattern.parse('#foo'), Column.parse('#bar')]])
    write_hxl(sys.stdout, filter)
    </pre>
    """

    def __init__(self, source, rename=[]):
        """
        Constructor
        @param source the DataProvider for the data.
        @param rename_map map of tags to rename
        """
        self.source = source
        self.rename = rename
        self._saved_columns = None

    @property
    def columns(self):
        """
        Return the renamed columns.
        """

        if self._saved_columns is None:
            def rename_column(column):
                for spec in self.rename:
                    if spec[0].match(column):
                        new_column = copy.copy(spec[1])
                        if new_column.header is None:
                            new_column.header = column.header
                        return new_column
                return column
            self._saved_columns = [rename_column(column) for column in self.source.columns]
        return self._saved_columns

    def __iter__(self):
        return iter(self.source)


#
# Command-line support.
#

RENAME_PATTERN = r'^\s*#?({token}):(?:([^#]*)#)?({token})\s*$'.format(token=hxl.TOKEN)

def parse_rename(s):
{'fraction_non_alphanumeric': 0.09783121169259783, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 2.4162640901771337, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '730299', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1178, 'n_words': 363}