<commit_before>from rancon.tools import tag_replace

from dotmap import DotMap


class Service(DotMap):

    def __init__(self, name, host, port, source, **kwargs):
        super().__init__()
        self.name = name
        self.host = host
        self.port = port
        self.source = source
        for k, v in kwargs.items():
            if k not in ('host', 'port', 'source', ''):
                self[k] = v
        # perform tag replacement on all values. do three rounds so we can be
        # sure that this is working somewhat recursively
        replaced = 1
        rounds = 0
        while replaced > 0 and rounds < 3:
            replaced = 0
            rounds += 1
            for k in list(self.keys()):
                v = self[k]
                if isinstance(v, str) and v.find("%") > -1:
                    self[k] = tag_replace(v, self)
                    replaced += 1

    def __str__(self):
        return "{} ({}:{})".format(
            self.name, self.host, self.port
        )<commit_msg>refactored: replaced loop with regex substitution<commit_after>""" definition of a service """

import re

from dotmap import DotMap

class Service(DotMap):
    """ class representing a service """

    def __init__(self, name, host, port, source, **kwargs):
        super().__init__()

        self.name = name
        self.host = host
        self.port = port
        self.source = source
        self.update(kwargs)

        # replace all tags in all values
        for key, value in self.items():
            if isinstance(value, str):
                self[key] = re.sub("%([^%]+)%", lambda x: self[x.group(1)], value)

    def __str__(self):
        return "{} ({}:{})".format(
            self.name, self.host, self.port
        )
{'fraction_non_alphanumeric': 0.12293682413204325, 'fraction_numerical': 0.005122367672168469, 'mean_word_length': 1.8083067092651757, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '3780239', 'n_tokens_mistral': 512, 'n_tokens_neox': 490, 'n_words': 164}