<commit_before>from datetime import timedelta

from django import forms


class DurationInHoursField(forms.fields.FloatField):
    """Field representing duration as float hours."""

    def prepare_value(self, value):
        if isinstance(value, timedelta):
            return value.total_seconds() / 3600
        return value

    def to_python(self, value):
        value = super().to_python(value)
        if value is None:
            return value

        return timedelta(seconds=value * 3600)<commit_msg>fix(admin): validate method for DurationInHoursField<commit_after>from datetime import timedelta

from django.core.exceptions import ValidationError
from django.forms.fields import FloatField
from django.utils.translation import ugettext_lazy as _


class DurationInHoursField(FloatField):
    """Field representing duration as float hours."""

    def _get_hours(self, value):
        return value.total_seconds() / 3600

    def prepare_value(self, value):
        if isinstance(value, timedelta):
            return self._get_hours(value)
        return value

    def to_python(self, value):
        value = super().to_python(value)
        if value is None:
            return value

        return timedelta(seconds=value * 3600)

    def validate(self, value):
        if value in self.empty_values:
            return

        if not isinstance(value, timedelta):
            raise ValidationError(_("Enter a datetime.timedelta"))

        super().validate(self._get_hours(value))
{'fraction_non_alphanumeric': 0.082, 'fraction_numerical': 0.010666666666666666, 'mean_word_length': 2.992021276595745, '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': '3341104', 'n_tokens_mistral': 423, 'n_tokens_neox': 382, 'n_words': 116}