<commit_before>from pprint import pprint
import yaml

from django.contrib.auth.models import User


class YamlUser(yaml.YAMLObject):
    yaml_tag = u'!username'

    def __init__(self, user):
        self.user = user

    def __str__(self):
        return f"!YAML-{self.id}-{str(self.user)}"

    @classmethod
    def from_yaml(cls, loader, node):
        user_scalar = loader.construct_scalar(node)
        user_list = user_scalar.split('-')
        id = user_list[0]
        user = User.objects.get(id=id)
        return YamlUser(user)

    @classmethod
    def to_yaml(cls, dumper, data):
        scalar = f"{data.user.id}-{data.user.username}"
        node = dumper.represent_scalar(cls.yaml_tag, scalar)
        return node<commit_msg>separando YamlBaseObject para utilizar com outras classes de modelos<commit_after>from pprint import pprint
import yaml

from django.contrib.auth.models import User


class YamlBaseObject(yaml.YAMLObject):

    def __init__(self, object_instance):
        self.object_instance = object_instance

    def __str__(self):
        return f"!YAML-{self.object_instance.id}-{str(self.object_instance)}"

    @classmethod
    def from_yaml(cls, loader, node):
        user_scalar = loader.construct_scalar(node)
        user_list = user_scalar.split('-')
        id = user_list[0]
        object_instance = cls.object_class.objects.get(id=id)
        return YamlUser(object_instance)

    @classmethod
    def to_yaml(cls, dumper, data):
        scalar = f"{data.object_instance.id}-{str(data.object_instance)}"
        node = dumper.represent_scalar(cls.yaml_tag, scalar)
        return node


class YamlUser(YamlBaseObject):
    yaml_tag = u'!username'
    object_class = User
{'fraction_non_alphanumeric': 0.10695499707773232, 'fraction_numerical': 0.0011689070718877848, 'mean_word_length': 3.2063882063882065, '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': '7208466', 'n_tokens_mistral': 564, 'n_tokens_neox': 541, 'n_words': 111}