<commit_before># vim: set ts=2 et:
import dateutil.parser

from .fields_core import Field

__all__ = ['DateTime', 'Text']


class DateTime(Field):
  def from_wire(self, val, rtfield, cur_store, doc):
    if val is None:
      return None
    return dateutil.parser.parse(val)

  def to_wire(self, obj, rtfield, cur_store, doc):
    if obj is None:
      return None
    return obj.isoformat()


class Text(Field):
  __slots__ = ('encoding', )

  def __init__(self, encoding='utf-8', store_empty=False, **kwargs):
    super(Text, self).__init__(**kwargs)
    self.encoding = encoding
    if not store_empty:
        self.should_write = lambda val: val

  def from_wire(self, val, rtfield, cur_store, doc):
    if val is None:
      return None
    return val.decode(self.encoding)

  def to_wire(self, obj, rtfield, cur_store, doc):
    if obj is None:<commit_msg>Catching parse errors and returning None.<commit_after># vim: set ts=2 et:
import dateutil.parser

from .fields_core import Field

__all__ = ['DateTime', 'Text']


class DateTime(Field):
  def from_wire(self, val, rtfield, cur_store, doc):
    if val is None:
      return None
    try:
      return dateutil.parser.parse(val)
    # Some parse errors manifest as Nones.
    except TypeError:
      return None

  def to_wire(self, obj, rtfield, cur_store, doc):
    if obj is None:
      return None
    return obj.isoformat()


class Text(Field):
  __slots__ = ('encoding', )

  def __init__(self, encoding='utf-8', store_empty=False, **kwargs):
    super(Text, self).__init__(**kwargs)
    self.encoding = encoding
    if not store_empty:
        self.should_write = lambda val: val

  def from_wire(self, val, rtfield, cur_store, doc):
    if val is None:
      return None
    return val.decode(self.encoding)

  def to_wire(self, obj, rtfield, cur_store, doc):
    if obj is None:
{'fraction_non_alphanumeric': 0.10984848484848485, 'fraction_numerical': 0.0021645021645021645, 'mean_word_length': 3.381516587677725, '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': '3709766', 'n_tokens_mistral': 615, 'n_tokens_neox': 601, 'n_words': 191}