<commit_before># Copyright (c) 2015, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

import stix
import stix.utils as utils
import stix.bindings.stix_common as common_binding


DATE_PRECISION_VALUES = ("year", "month", "day")
TIME_PRECISION_VALUES = ("hour", "minute", "second")
DATETIME_PRECISION_VALUES = DATE_PRECISION_VALUES + TIME_PRECISION_VALUES


class DateTimeWithPrecision(stix.Entity):
    _binding = common_binding
    _binding_class = _binding.DateTimeWithPrecisionType
    _namespace = 'http://stix.mitre.org/common-1'

    def __init__(self, value=None, precision='second'):
        super(DateTimeWithPrecision, self).__init__()
        self.value = value
        self.precision = precision

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, value):
        self._value = utils.dates.parse_value(value)

    @property
    def precision(self):
        return self._precision

    @precision.setter
    def precision(self, value):
        if value not in DATETIME_PRECISION_VALUES:
            error = "The precision must be one of {0}. Received '{1}'"
            error = error.format(DATE_PRECISION_VALUES, value)
            raise ValueError(error)

        self._precision = value

    def to_obj(self, return_obj=None, ns_info=None):
        super(DateTimeWithPrecision, self).to_obj(return_obj=return_obj, ns_info=ns_info)

        obj = self._binding_class()
        obj.valueOf_ = utils.dates.serialize_value(self.value)
        obj.precision = self.precision
        return obj

    @classmethod
    def from_obj(cls, obj, return_obj=None):
        if not obj:
            return None

        if not return_obj:
            return_obj = cls()

        return_obj.value = obj.valueOf_
        return_obj.precision = obj.precision
        return return_obj

    def to_dict(self):
        if self.precision == 'second':
            return utils.dates.serialize_value(self.value)
       
        d  = {
            'value': utils.dates.serialize_value(self.value),
            'precision':self.precision
        }

        return d

    @classmethod
    def from_dict(cls, dict_, return_obj=None):
        if not dict_:
            return None

        if not return_obj:
            return_obj = cls()

        if not isinstance(dict_, dict):
            return_obj.value = dict_
        else:
            return_obj.precision = dict_.get('precision')
            return_obj.value = dict_.get('value')

        return return_obj<commit_msg>Added typedfield support to DateTimeWithPrecision<commit_after># Copyright (c) 2015, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

from mixbox.fields import DateTimeField

import stix
import stix.utils as utils
import stix.bindings.stix_common as common_binding

from stix.base import ContentField, AttributeField




DATE_PRECISION_VALUES = ("year", "month", "day")
TIME_PRECISION_VALUES = ("hour", "minute", "second")
DATETIME_PRECISION_VALUES = DATE_PRECISION_VALUES + TIME_PRECISION_VALUES


def _validate_precision(instance, value):
    if value is None:
        return
    elif value in DATETIME_PRECISION_VALUES:
        return
    else:
        error = "The precision must be one of {0}. Received '{1}'"
        error = error.format(DATE_PRECISION_VALUES, value)
        raise ValueError(error)


class DateTimeWithPrecision(stix.Entity):
    _binding = common_binding
    _binding_class = _binding.DateTimeWithPrecisionType
    _namespace = 'http://stix.mitre.org/common-1'

    value = DateTimeField("valueOf_", key_name="value")
    precision = AttributeField("precision", preset_hook=_validate_precision)

    def __init__(self, value=None, precision='second'):
        super(DateTimeWithPrecision, self).__init__()

        self.value = value
        self.precision = precision


    def to_dict(self):
        if self.precision == 'second':
            return utils.dates.serialize_value(self.value)

        d  = {
            'value': utils.dates.serialize_value(self.value),
            'precision':self.precision
        }

        return d

    @classmethod
    def from_dict(cls, cls_dict=None, return_obj=None):
        if not cls_dict:
            return None

        if not return_obj:
            return_obj = cls()

        if not isinstance(cls_dict, dict):
            return_obj.value = cls_dict
        else:
            return_obj.precision = cls_dict.get('precision')
            return_obj.value = cls_dict.get('value')

        return return_obj
{'fraction_non_alphanumeric': 0.09116809116809117, 'fraction_numerical': 0.0030681569143107606, 'mean_word_length': 2.7075548334687247, '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': '5984644', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1333, 'n_words': 315}