<commit_before># -*- coding: utf-8 -*-
# @Date    : 2016-01-26 18:26
# @Author  : Basask (<EMAIL>)

from __future__ import unicode_literals
from django.conf import settings
from django.utils.dateparse import parse_datetime
from ext_fields import constants


DETECT_DATE = getattr(settings, "EXTFIELDS_DETECT_DATE", False)


class Mapper(object):

    def __init__(self, model_class, *args, **kwargs):
        self.translated = kwargs.get('translate', True)
        self.model_class = model_class

    def get_field_related(self, *args):
        chunks = [self.model_class._meta.model_name]
        chunks += args
        return ('__'.join(chunks)).lower()

    def get_value_map(self, value):
        value_type = type(value)
        if DETECT_DATE and value_type in [str, unicode]:
            if parse_datetime(value):
                return 'date'
        for k, v in constants.TYPEMAP.items():
            if value_type in v:
                return k
        return None

    def get_value_field_name(self, value):
        return constants.VALUE_PREFIX + self.get_value_map(value)

    def get_row_value(self, row):
        def last_nonempty_field(a, b):
            val = getattr(row, constants.VALUE_PREFIX + b)
            if val is not None:
                return val
            return a
        return reduce(last_nonempty_field, constants.TYPEMAP.keys(), None)

    def get_dict_val(self, value):
        vm = self.get_value_map(value)
        return {constants.VALUE_PREFIX + k: value if vm == k else None for k in constants.TYPEMAP.keys()}
<commit_msg>Change instance methods to static and class methods<commit_after># -*- coding: utf-8 -*-
# @Date    : 2016-01-26 18:26
# @Author  : Basask (<EMAIL>)

from __future__ import unicode_literals
from django.conf import settings
from django.utils.dateparse import parse_datetime
from ext_fields import constants


DETECT_DATE = getattr(settings, "EXTFIELDS_DETECT_DATE", False)


class Mapper(object):

    def __init__(self, model_class, *args, **kwargs):
        self.translated = kwargs.get('translate', True)
        self.model_class = model_class

    def get_field_related(self, *args):
        chunks = [self.model_class._meta.model_name]
        chunks += args
        return ('__'.join(chunks)).lower()

    @staticmethod
    def get_value_map(value):
        value_type = type(value)
        if DETECT_DATE and value_type in [str, unicode]:
            if parse_datetime(value):
                return 'date'
        for k, v in constants.TYPEMAP.items():
            if value_type in v:
                return k
        return None

    @classmethod
    def get_value_field_name(cls, value):
        return constants.VALUE_PREFIX + cls.get_value_map(value)

    @staticmethod
    def get_row_value(row):
        def last_nonempty_field(a, b):
            val = getattr(row, constants.VALUE_PREFIX + b)
            if val is not None:
                return val
            return a
        return reduce(last_nonempty_field, constants.TYPEMAP.keys(), None)

    @classmethod
    def get_dict_val(cls, value):
        vm = cls.get_value_map(value)
        return {constants.VALUE_PREFIX + k: value if vm == k else None for k in constants.TYPEMAP.keys()}

{'fraction_non_alphanumeric': 0.09615384615384616, 'fraction_numerical': 0.008064516129032258, 'mean_word_length': 2.6235955056179776, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3785933', 'n_tokens_mistral': 1002, 'n_tokens_neox': 971, 'n_words': 261}