Skip to content

Commit

Permalink
Tmp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinugu committed Apr 16, 2024
1 parent 02e9c7a commit d67a82c
Show file tree
Hide file tree
Showing 20 changed files with 1,012 additions and 141 deletions.
296 changes: 279 additions & 17 deletions src/omnipy/api/protocols/public/data.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,234 @@
from typing import Any, Callable, IO, Iterator, Protocol, Type, TypeVar
from abc import abstractmethod
from collections.abc import Sized
from pathlib import Path
from typing import (AbstractSet,
Any,
BinaryIO,
Callable,
Hashable,
IO,
Iterable,
Iterator,
overload,
Protocol,
runtime_checkable,
Type,
TypeVar)

from pydantic.fields import Undefined, UndefinedType

from omnipy.api.protocols.private.log import CanLog

_ModelT = TypeVar('_ModelT')
_RootT = TypeVar('_RootT', covariant=True)
_ModelT = TypeVar('_ModelT', bound='IsModel')
_ModelTContra = TypeVar('_ModelTContra', bound='IsModel', contravariant=True)
_ModelTCov = TypeVar('_ModelTCov', bound='IsModel', covariant=True)
KeyT = TypeVar('KeyT')
KeyContraT = TypeVar('KeyContraT', bound=Hashable, contravariant=True)
ValT = TypeVar('ValT')
ValCoT = TypeVar('ValCoT', covariant=True)

RootT = TypeVar('RootT')

class IsDataset(Protocol[_ModelT]):

class SupportsKeysAndGetItem(Protocol[KeyT, ValCoT]):
def keys(self) -> Iterable[KeyT]:
...

def __getitem__(self, __key: KeyT) -> ValCoT:
...


class IsSet(Protocol):
"""
IsSet is a protocol with the same interface as the abstract class Set.
It is the protocol of a finite, iterable container.
"""
def __le__(self, other: AbstractSet) -> bool:
...

def __lt__(self, other: AbstractSet) -> bool:
...

def __gt__(self, other: AbstractSet) -> bool:
...

def __ge__(self, other: AbstractSet) -> bool:
...

def __eq__(self, other: object) -> bool:
...

def __and__(self, other: Iterable) -> 'IsSet':
...

def __rand__(self, other: Iterable) -> 'IsSet':
...

def isdisjoint(self, other: AbstractSet) -> bool:
...

def __or__(self, other: Iterable) -> 'IsSet':
...

def __ror__(self, other: Iterable) -> 'IsSet':
...

def __sub__(self, other: Iterable) -> 'IsSet':
...

def __rsub__(self, other: Iterable) -> 'IsSet':
...

def __xor__(self, other: Iterable) -> 'IsSet':
...

def __rxor__(self, other: Iterable) -> 'IsSet':
...


class IsMapping(Protocol[KeyT, ValT]):
"""
IsMapping is a protocol with the same interface as the abstract class Mapping.
It is the protocol of a generic container for associating key/value pairs.
"""
@abstractmethod
def __getitem__(self, key: KeyT) -> ValT:
raise KeyError

def get(self, key: KeyT, /) -> ValT | None:
"""
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
"""
...

def __contains__(self, key: KeyT) -> ValT:
...

def keys(self) -> 'IsKeysView[KeyT]':
"""
D.keys() -> a set-like object providing a view on D's keys
"""
...

def items(self) -> 'IsItemsView[KeyT, ValT]':
"""
D.items() -> a set-like object providing a view on D's items
"""
...

def values(self) -> 'IsValuesView[ValT]':
"""
D.values() -> an object providing a view on D's values
"""
...

def __eq__(self, other: object) -> bool:
...


class IsMappingView(Protocol, Sized):
def __len__(self) -> int:
...

def __repr__(self) -> str:
...


class IsKeysView(IsMappingView, Protocol[KeyContraT]):
def __contains__(self, key: KeyContraT) -> bool:
...

def __iter__(self) -> Iterator[ValT]:
...


class IsItemsView(IsMappingView, Protocol[KeyT, ValT]):
def __contains__(self, item: tuple[KeyT, ValT]) -> bool:
...

def __iter__(self) -> Iterator[tuple[KeyT, ValT]]:
...


class IsValuesView(IsMappingView, Protocol[ValT]):
def __contains__(self, value: ValT) -> bool:
...

def __iter__(self) -> Iterator[ValT]:
...


class IsMutableMapping(IsMapping[KeyT, ValT], Protocol[KeyT, ValT]):
"""
IsMutableMapping is a protocol with the same interface as the abstract class MutableMapping.
It is the protocol of a generic mutable container for associating key/value pairs.
"""
@abstractmethod
def __setitem__(self, key: KeyT, value: ValT) -> None:
raise KeyError

@abstractmethod
def __delitem__(self, key: KeyT) -> None:
raise KeyError

def pop(self, key: KeyT) -> ValT:
"""
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised.
"""
...

def popitem(self) -> tuple[KeyT, ValT]:
"""
D.popitem() -> (k, v), remove and return some (key, value) pair
as a 2-tuple; but raise KeyError if D is empty.
"""
...

def clear(self) -> None:
"""
D.clear() -> None. Remove all items from D.
"""
...

@overload
def update(self, other: SupportsKeysAndGetItem[KeyT, ValT], /, **kwargs: ValT) -> None:
...

@overload
def update(self, other: Iterable[tuple[KeyT, ValT]], /, **kwargs: ValT) -> None:
...

@overload
def update(self, /, **kwargs: ValT) -> None:
...

def update(self, other: Any = None, /, **kwargs: ValT) -> None:
"""
D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
If E present and has a .keys() method, does: for k in E: D[k] = E[k]
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
In either case, this is followed by: for k, v in F.items(): D[k] = v
"""
...

def setdefault(self, key: KeyT, default: ValT | None = None):
"""
D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
"""
...


@runtime_checkable
class IsModel(Protocol[_RootT]):
# @property
# def contents(self) -> _RootT:
# ...
...


class IsDataset(IsMutableMapping, Protocol[_ModelTCov]):
"""
Dict-based container of data files that follow a specific Model
"""
Expand All @@ -21,7 +242,7 @@ def __init__(
...

@classmethod
def get_model_class(cls) -> Type[_ModelT]:
def get_model_class(cls) -> type[_ModelTCov]:
"""
Returns the concrete Model class used for all data files in the dataset, e.g.:
`Model[list[int]]`
Expand Down Expand Up @@ -49,42 +270,83 @@ def from_json(self,
def to_json_schema(cls, pretty=True) -> str | dict[str, str]:
...

def as_multi_model_dataset(self) -> 'MultiModelDataset[_ModelT]':
...
# def as_multi_model_dataset(self) -> 'IsMultiModelDataset[_ModelT]':
# ...


class MultiModelDataset(Protocol[_ModelT]):
class IsMultiModelDataset(IsDataset[_ModelTCov], Protocol[_ModelTCov]):
"""
Variant of Dataset that allows custom models to be set on individual data files
"""
def set_model(self, data_file: str, model: _ModelT) -> None:
...

def set_model(self, data_file: str, model: type[IsModel]) -> None:
...

# def get_model(self, data_file: str) -> type[_ModelT]:
# ...


def a() -> None:
from omnipy import Dataset, Model
from omnipy.data.dataset import MultiModelDataset

t: IsModel[int] = Model[int]

d: IsDataset[IsModel[int]] = Dataset[Model[int]]()
#
e: IsMultiModelDataset[IsModel[int]] = MultiModelDataset[Model[int]]()


class CanSerialize(Protocol[RootT]):
@classmethod
def serialize_to_bytes(cls, dataset: IsDataset[IsModel[RootT]]) -> BinaryIO:
...

@classmethod
def deserialize_from_bytes(cls, data: BinaryIO) -> IsDataset[IsModel[RootT]]:
...

@classmethod
def serialize_to_directory(cls, dataset: IsDataset[IsModel[RootT]],
dir_path: Path | str) -> None:
...

@classmethod
def deserialize_from_directory(cls, dir_path: Path | str) -> IsDataset[IsModel[RootT]]:
...


class IsDataEncoder(Protocol[RootT]):
@classmethod
def encode_data(cls, dataset_key: str, data: RootT) -> bytes:
...

def get_model(self, data_file: str) -> _ModelT:
@classmethod
def decode_data(cls, dataset_key: str, encoded_data: bytes) -> RootT:
...


class IsSerializer(Protocol):
class SupportsGeneralSerializerQueries(Protocol):
""""""
@classmethod
def is_dataset_directly_supported(cls, dataset: IsDataset) -> bool:
pass

@classmethod
def get_dataset_cls_for_new(cls) -> Type[IsDataset]:
pass
...

@classmethod
def get_output_file_suffix(cls) -> str:
pass

@classmethod
def serialize(cls, dataset: IsDataset) -> bytes | memoryview:
pass

@classmethod
def deserialize(cls, serialized: bytes, any_file_suffix=False) -> IsDataset:
pass
class IsSerializer(IsDataEncoder[RootT],
CanSerialize[RootT],
SupportsGeneralSerializerQueries,
Protocol[RootT]):
...


class IsTarFileSerializer(IsSerializer):
Expand Down
13 changes: 12 additions & 1 deletion src/omnipy/api/typedefs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
from typing import Callable, TypeAlias, TypeVar
from types import UnionType
from typing import (_AnnotatedAlias,
_GenericAlias,
_LiteralGenericAlias,
_SpecialForm,
_UnionGenericAlias,
Callable,
TypeAlias,
TypeVar)

GeneralDecorator = Callable[[Callable], Callable]
LocaleType: TypeAlias = str | tuple[str | None, str | None]
Expand All @@ -10,3 +18,6 @@
TaskTemplateT = TypeVar('TaskTemplateT')
TaskTemplateContraT = TypeVar('TaskTemplateContraT', contravariant=True)
TaskTemplateCovT = TypeVar('TaskTemplateCovT', covariant=True)

# TODO: While waiting for https://github.com/python/mypy/issues/9773
TypeForm: TypeAlias = type | UnionType | _UnionGenericAlias | _AnnotatedAlias | _GenericAlias | _LiteralGenericAlias | _SpecialForm
2 changes: 1 addition & 1 deletion src/omnipy/config/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

@dataclass
class DataConfig:
interactive_mode: bool = True
interactive_mode: bool = False
terminal_size_columns: int = _terminal_size.columns
terminal_size_lines: int = _terminal_size.lines
Loading

0 comments on commit d67a82c

Please sign in to comment.