<commit_before>from collections import OrderedDict

import peewee as pw
from marshmallow import ValidationError, compat, fields
from marshmallow import validate as ma_validate

from .fields import ForeignKey


class ModelConverter(object):

    """ Convert Peewee model to Marshmallow schema."""

    TYPE_MAPPING = [
        (pw.AutoField, fields.String),
        (pw.IntegerField, fields.Integer),
        (pw.CharField, fields.String),
        (pw.BooleanField, fields.Boolean),
        (pw.DateTimeField, fields.DateTime),
        (pw.DateField, fields.Date),
        (pw.TextField, fields.String),
        (pw.ForeignKeyField, ForeignKey),
        (pw.FloatField, fields.Float),
        (pw.DecimalField, fields.Decimal),
        (pw.TimeField, fields.Time),
        (pw.BigIntegerField, fields.Integer),
        (pw.SmallIntegerField, fields.Integer),
        (pw.DoubleField, fields.Float),
        (pw.FixedCharField, fields.String),
        (pw.UUIDField, fields.UUID),
    ]

    try:
        TYPE_MAPPING.append((pw.BigAutoField, fields.String))
    except AttributeError:
        pass

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

    def fields_for_model(self, model):
        result = OrderedDict()
        for field in model._meta.sorted_fields:
            ma_field = self.convert_field(field)
            if ma_field:
                result[field.name] = ma_field
        return result

    def convert_field(self, field):
        pw_fields = [x[0] for x in self.TYPE_MAPPING]
        for f in pw_fields:
            if isinstance(fields, f):
                validate = [convert_value_validate(field.python_value)]
                break
        else:
            validate = []
        params = {
            'allow_none': field.null,
            'attribute': field.name,
            'required': not field.null and field.default is None,
            'validate': validate,
        }<commit_msg>Use field.db_value instead of field.python_value.<commit_after>from collections import OrderedDict

import peewee as pw
from marshmallow import ValidationError, compat, fields
from marshmallow import validate as ma_validate

from .fields import ForeignKey


class ModelConverter(object):

    """ Convert Peewee model to Marshmallow schema."""

    TYPE_MAPPING = [
        (pw.AutoField, fields.String),
        (pw.IntegerField, fields.Integer),
        (pw.CharField, fields.String),
        (pw.BooleanField, fields.Boolean),
        (pw.DateTimeField, fields.DateTime),
        (pw.DateField, fields.Date),
        (pw.TextField, fields.String),
        (pw.ForeignKeyField, ForeignKey),
        (pw.FloatField, fields.Float),
        (pw.DecimalField, fields.Decimal),
        (pw.TimeField, fields.Time),
        (pw.BigIntegerField, fields.Integer),
        (pw.SmallIntegerField, fields.Integer),
        (pw.DoubleField, fields.Float),
        (pw.FixedCharField, fields.String),
        (pw.UUIDField, fields.UUID),
    ]

    try:
        TYPE_MAPPING.append((pw.BigAutoField, fields.String))
    except AttributeError:
        pass

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

    def fields_for_model(self, model):
        result = OrderedDict()
        for field in model._meta.sorted_fields:
            ma_field = self.convert_field(field)
            if ma_field:
                result[field.name] = ma_field
        return result

    def convert_field(self, field):
                validate = [convert_value_validate(field.python_value)]
        params = {
            'allow_none': field.null,
            'attribute': field.name,
            'required': not field.null and field.default is None,
            'validate': [convert_value_validate(field.db_value)],
        }
{'fraction_non_alphanumeric': 0.10386473429951691, 'fraction_numerical': 0.00026838432635534085, 'mean_word_length': 2.3667570009033425, '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': '3195420', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1044, 'n_words': 244}