Base class for LIT datasets.
| 77 | |
| 78 | |
| 79 | class Dataset(object): |
| 80 | """Base class for LIT datasets.""" |
| 81 | |
| 82 | _spec: Spec = {} |
| 83 | _examples: list[JsonDict] = [] |
| 84 | _description: Optional[str] = None |
| 85 | _base: Optional['Dataset'] = None |
| 86 | |
| 87 | def __init__(self, |
| 88 | spec: Optional[Spec] = None, |
| 89 | examples: Optional[list[JsonDict]] = None, |
| 90 | description: Optional[str] = None, |
| 91 | base: Optional['Dataset'] = None): |
| 92 | """Base class constructor. |
| 93 | |
| 94 | This can derive from another dataset by passing the 'base' argument; |
| 95 | if so it will pre-populate with those fields, and override only those |
| 96 | specified individually as arguments. |
| 97 | |
| 98 | Args: |
| 99 | spec: dataset spec |
| 100 | examples: data examples (datapoints) |
| 101 | description: optional human-readable description of this component |
| 102 | base: optional base dataset to derive from |
| 103 | """ |
| 104 | self._base = base |
| 105 | if self._base is not None: |
| 106 | self._examples = self._base.examples |
| 107 | self._spec = self._base.spec() |
| 108 | self._description = self._base.description() |
| 109 | # In case user child class requires the instance to convert examples |
| 110 | # this makes sure the user class is preserved. We cannot do this below |
| 111 | # as the default method is static and does not require instance. |
| 112 | self.bytes_from_lit_example = self._base.bytes_from_lit_example |
| 113 | self.lit_example_from_bytes = self._base.lit_example_from_bytes |
| 114 | |
| 115 | # Override from direct arguments. |
| 116 | self._examples = examples if examples is not None else self._examples |
| 117 | self._spec = spec or self._spec |
| 118 | self._description = description or self._description |
| 119 | |
| 120 | def description(self) -> str: |
| 121 | """Return a human-readable description of this component. |
| 122 | |
| 123 | Defaults to class docstring, but subclass may override this (or simply set |
| 124 | self._description) to be instance-dependent - for example, including the |
| 125 | path from which the data was loaded. |
| 126 | |
| 127 | Returns: |
| 128 | (string) A human-readable description for display in the UI. |
| 129 | """ |
| 130 | return self._description or inspect.getdoc(self) or '' # pytype: disable=bad-return-type |
| 131 | |
| 132 | @classmethod |
| 133 | def init_spec(cls) -> Optional[lit_types.Spec]: |
| 134 | """Attempts to infer a Spec describing a Dataset's constructor parameters. |
| 135 | |
| 136 | The Dataset base class attempts to infer a Spec for the constructor using |
no outgoing calls
no test coverage detected