Experiment w/ dataclasses (including Py36) (#3423)

* [ci] Also run test_examples in py37

(will revert at the end of the experiment)

* InputExample: use immutable dataclass

* [deps] Install dataclasses for Py<3.7

* [skip ci] Revert "[ci] Also run test_examples in py37"

This reverts commit d29afd9959786b77759b0b8fa4e6b4335b952015.
This commit is contained in:
Julien Chaumond 2020-03-25 11:10:20 -04:00 коммит произвёл GitHub
Родитель ccbe839ee0
Коммит 83272a3853
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 12 добавлений и 15 удалений

Просмотреть файл

@ -97,6 +97,8 @@ setup(
install_requires=[ install_requires=[
"numpy", "numpy",
"tokenizers == 0.5.2", "tokenizers == 0.5.2",
# dataclasses for Python versions that don't have it
"dataclasses;python_version<'3.7'",
# accessing files from S3 directly # accessing files from S3 directly
"boto3", "boto3",
# filesystem locks e.g. to prevent parallel downloads # filesystem locks e.g. to prevent parallel downloads

Просмотреть файл

@ -16,8 +16,11 @@
import copy import copy
import csv import csv
import dataclasses
import json import json
import logging import logging
from dataclasses import dataclass
from typing import Optional
from ...file_utils import is_tf_available, is_torch_available from ...file_utils import is_tf_available, is_torch_available
@ -25,7 +28,8 @@ from ...file_utils import is_tf_available, is_torch_available
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class InputExample(object): @dataclass(frozen=True)
class InputExample:
""" """
A single training/test example for simple sequence classification. A single training/test example for simple sequence classification.
@ -39,23 +43,14 @@ class InputExample(object):
specified for train and dev examples, but not for test examples. specified for train and dev examples, but not for test examples.
""" """
def __init__(self, guid, text_a, text_b=None, label=None): guid: str
self.guid = guid text_a: str
self.text_a = text_a text_b: Optional[str] = None
self.text_b = text_b label: Optional[str] = None
self.label = label
def __repr__(self):
return str(self.to_json_string())
def to_dict(self):
"""Serializes this instance to a Python dictionary."""
output = copy.deepcopy(self.__dict__)
return output
def to_json_string(self): def to_json_string(self):
"""Serializes this instance to a JSON string.""" """Serializes this instance to a JSON string."""
return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n" return json.dumps(dataclasses.asdict(self), indent=2, sort_keys=True) + "\n"
class InputFeatures(object): class InputFeatures(object):