mirror of https://github.com/doccano/doccano.git
Browse Source
Merge pull request #1845 from doccano/enhancement/refactorDataImport
Merge pull request #1845 from doccano/enhancement/refactorDataImport
[Enhancement] refactor data importrelease-1.8.0
Hiroki Nakayama
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 38 deletions
Split View
Diff Options
-
32backend/data_import/datasets.py
-
21backend/data_import/pipeline/examples.py
-
21backend/data_import/pipeline/labels.py
-
25backend/data_import/tests/test_examples.py
-
26backend/data_import/tests/test_labels.py
@ -0,0 +1,21 @@ |
|||
from typing import Dict, List |
|||
|
|||
from pydantic import UUID4 |
|||
|
|||
from examples.models import Example |
|||
|
|||
|
|||
class Examples: |
|||
def __init__(self, examples: List[Example]): |
|||
self.examples = examples |
|||
self.uuid_to_example: Dict[UUID4, Example] = {} |
|||
|
|||
def __getitem__(self, uuid: UUID4) -> Example: |
|||
return self.uuid_to_example[uuid] |
|||
|
|||
def __contains__(self, uuid: UUID4) -> bool: |
|||
return uuid in self.uuid_to_example |
|||
|
|||
def save(self): |
|||
examples = Example.objects.bulk_create(self.examples) |
|||
self.uuid_to_example = {example.uuid: example for example in examples} |
@ -0,0 +1,25 @@ |
|||
import uuid |
|||
|
|||
from django.test import TestCase |
|||
|
|||
from data_import.pipeline.examples import Examples |
|||
from examples.models import Example |
|||
from projects.models import DOCUMENT_CLASSIFICATION |
|||
from projects.tests.utils import prepare_project |
|||
|
|||
|
|||
class TestExamples(TestCase): |
|||
def setUp(self): |
|||
self.project = prepare_project(DOCUMENT_CLASSIFICATION) |
|||
self.example_uuid = uuid.uuid4() |
|||
example = Example(uuid=self.example_uuid, text="A", project=self.project.item) |
|||
self.examples = Examples([example]) |
|||
|
|||
def test_save(self): |
|||
self.examples.save() |
|||
self.assertEqual(Example.objects.count(), 1) |
|||
|
|||
def test_getitem(self): |
|||
self.examples.save() |
|||
example = self.examples[self.example_uuid] |
|||
self.assertEqual(example.uuid, self.example_uuid) |
Write
Preview
Loading…
Cancel
Save