Coretex
utils.py
1 # Copyright (C) 2023 Coretex LLC
2 
3 # This file is part of Coretex.ai
4 
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 
18 from typing import Type, TypeVar, Optional, Iterator, Dict, Any
19 from contextlib import contextmanager
20 
21 from .network_dataset import NetworkDataset
22 from ..utils import isEntityNameValid
23 
24 
25 T = TypeVar("T", bound = NetworkDataset)
26 
27 
28 @contextmanager
29 def createDataset(
30  type_: Type[T],
31  name: str,
32  projectId: int,
33  meta: Optional[Dict[str, Any]] = None
34 ) -> Iterator[T]:
35 
36  """
37  Creates a new dataset with the provided name and type
38  and finalizes its state in data base
39 
40  Parameters
41  ----------
42  type_ : Type[T]
43  type of dataset which will be created
44  name : str
45  dataset name
46  projectId : int
47  project for which the dataset will be created
48  metadataPath : Union[Path, str]
49  path the zipped metadata file
50 
51  Returns
52  -------
53  The created sequence dataset object or None if creation failed
54 
55  Raises
56  -------
57  NetworkRequestError -> If dataset creation failed
58 
59  Example
60  -------
61  >>> from coretex import createDataset
62  \b
63  >>> with createDataset("dummyDataset", 123, pathToMetadata) as dataset:
64  >>> print(f"Dataset with id \"{dataset.id}\" and name \"{dataset.name}\" created")
65  """
66 
67  if not isEntityNameValid(name):
68  raise ValueError(">> [Coretex] Dataset name is invalid. Requirements: alphanumeric characters (\"a-z\", and \"0-9\") and dash (\"-\") with length between 3 to 50")
69 
70  dataset = type_.create(
71  name = name,
72  project_id = projectId,
73  meta = meta
74  )
75 
76  yield dataset
77  if not dataset.finalize():
78  raise ValueError(">> [Coretex] Failed to finalize dataset")