<commit_before>"""
Identifiers are used by products to represent their persistent
representations, for example product, for example, File uses it to represent
the path to a file. Identifiers are lazy-loaded, they can be initialized
with a jinja2.Template and rendered before task execution, which makes
passing metadata between products and its upstream tasks possible.
"""
from pathlib import Path
import inspect
import warnings

from jinja2 import Template


class Identifier:
    """Identifier abstract class
    """

    def __init__(self, s):
        self.needs_render = isinstance(s, Template)
        self.rendered = False
        self.s = s

        self.after_init_hook()

    def after_init_hook(self):
        raise NotImplementedError('Identifier subclasses must implement this '
                                  'method')

    def after_render_hook(self):
        raise NotImplementedError('Identifier subclasses must implement this '
                                  'method')

    def __str__(self):
        return self()

    def __repr__(self):
        return f'{type(self)}({self.s})'

    def render(self, params):
        if self.needs_render:
            if not self.rendered:
                self.s = self.s.render(params)
                self.rendered = True
                self.after_render_hook()
            else:
                warnings.warn(f'Trying to render {repr(self)}, with was'
                              ' already rendered, skipping render...')

        return self

    def __call__(self):
        """Identifiers must be called from here
        """
        if self.needs_render:
            if not self.rendered:
                raise RuntimeError('Attempted to read Identifier '
                                   f'{repr(self)} '
                                   '(which was initialized with '
                                   'a jinja2.Template object) wihout '
                                   'rendering the task first, call '
                                   'task.render() before reading '
                                   'the identifier or initialize with a str '
                                   'object, if this is part of a DAG, '
                                   'the task should render automatically')
            return self.s
        else:
            return self.s


class StringIdentifier(Identifier):
    """Identifier that represents a str object
    """

    def after_init_hook(self):
        if not self.needs_render and not isinstance(self.s, str):
            # if no Template passed but parameter is not str, cast...
            warnings.warn('Initialized StringIdentifier with non-string '<commit_msg>not showing warning when string identifier is initialized with a pathlib.Path object since that is also ok<commit_after>"""
Identifiers are used by products to represent their persistent
representations, for example product, for example, File uses it to represent
the path to a file. Identifiers are lazy-loaded, they can be initialized
with a jinja2.Template and rendered before task execution, which makes
passing metadata between products and its upstream tasks possible.
"""
from pathlib import Path
import inspect
import warnings

from jinja2 import Template


class Identifier:
    """Identifier abstract class
    """

    def __init__(self, s):
        self.needs_render = isinstance(s, Template)
        self.rendered = False
        self.s = s

        self.after_init_hook()

    def after_init_hook(self):
        raise NotImplementedError('Identifier subclasses must implement this '
                                  'method')

    def after_render_hook(self):
        raise NotImplementedError('Identifier subclasses must implement this '
                                  'method')

    def __str__(self):
        return self()

    def __repr__(self):
        return f'{type(self)}({self.s})'

    def render(self, params):
        if self.needs_render:
            if not self.rendered:
                self.s = self.s.render(params)
                self.rendered = True
                self.after_render_hook()
            else:
                warnings.warn(f'Trying to render {repr(self)}, with was'
                              ' already rendered, skipping render...')

        return self

    def __call__(self):
        """Identifiers must be called from here
        """
        if self.needs_render:
            if not self.rendered:
                raise RuntimeError('Attempted to read Identifier '
                                   f'{repr(self)} '
                                   '(which was initialized with '
                                   'a jinja2.Template object) wihout '
                                   'rendering the task first, call '
                                   'task.render() before reading '
                                   'the identifier or initialize with a str '
                                   'object, if this is part of a DAG, '
                                   'the task should render automatically')
            return self.s
        else:
            return self.s


class StringIdentifier(Identifier):
    """Identifier that represents a str object
    """

    def after_init_hook(self):
        # warn if object is not string, Path is ok too...
        if not self.needs_render and not (isinstance(self.s, str)
                                          or isinstance(self.s, Path)):
            # if no Template passed but parameter is not str, cast...
            warnings.warn('Initialized StringIdentifier with non-string '
{'fraction_non_alphanumeric': 0.07042001787310098, 'fraction_numerical': 0.0010723860589812334, 'mean_word_length': 1.677511961722488, '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': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '652740', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1273, 'n_words': 475}