Module omnipy.modules.json.models
Overview
View Source
from typing import Generic, Type, TypeAlias, TypeVar
from omnipy.data.model import Model
from .typedefs import JsonScalar
#
# Private models
#
# Basic building block models
_JsonBaseT = TypeVar(
'_JsonBaseT', bound='JsonScalarM | JsonListM | JsonDictM | JsonAnyListM | JsonAnyDictM')
class JsonScalarM(Model[JsonScalar]):
...
class JsonListM(Model[list[_JsonBaseT]], Generic[_JsonBaseT]):
...
class JsonDictM(Model[dict[str, _JsonBaseT]], Generic[_JsonBaseT]):
...
# Note: This intermediate level of JSON models is needed for two reasons. 1) as targets for
# .updateForwardRefs(), as this does not seem to work properly directly on a generic model
# (e.g. `JsonListM['_JsonAnyUnion'].updateForwardRefs()`), at least in pydantic v1.
# But even if this is fixed in pydantic v2, or more probably in python 3.13 with PEP649, the
# intermediate level is still needed due to the other reason: 2) For consistency in the
# hierarchy of JSON models, as tested in e.g. `test_json_model_consistency_basic()`. The
# intermediate level of models seems a good solution to make sure the level of the model
# hierarchy stays the same for e.g. `JsonModel` and `JsonDictModel`.
class JsonAnyListM(JsonListM['_JsonAnyUnion']):
...
class JsonAnyDictM(JsonDictM['_JsonAnyUnion']):
...
class JsonOnlyListsM(JsonListM['_JsonOnlyListsUnion']):
...
class JsonOnlyDictsM(JsonDictM['_JsonOnlyDictsUnion']):
...
# TypeAliases
# TODO: Consider whether these TypeAliases are needed in pydantic v2. In v1 they are needed to for
# the hack for propagating None to work. Removing this level will simplify JSON models.
# If updated, also update frozen models
_JsonAnyUnion: TypeAlias = JsonScalarM | JsonAnyListM | JsonAnyDictM
_JsonOnlyListsUnion: TypeAlias = JsonScalarM | JsonOnlyListsM
_JsonOnlyDictsUnion: TypeAlias = JsonScalarM | JsonOnlyDictsM
_JsonListOfScalarsM: TypeAlias = JsonListM[JsonScalarM]
_JsonDictOfScalarsM: TypeAlias = JsonDictM[JsonScalarM]
# Basic models needs to update their forward_refs with type aliases declared above
JsonAnyListM.update_forward_refs()
JsonAnyDictM.update_forward_refs()
JsonOnlyListsM.update_forward_refs()
JsonOnlyDictsM.update_forward_refs()
#
# Exportable models
#
# General
class JsonModel(Model[_JsonAnyUnion]):
"""
JsonModel is a general JSON model supporting any JSON content, any levels deep.
Examples:
>>> my_int_json = JsonModel(123)
>>> my_int_json.to_data()
123
>>> my_int_json.to_json()
'123'
>>> my_json = JsonModel([True, {'a': None, 'b': [1, 12.5, 'abc']}])
>>> my_json.to_data()
[True, {'a': None, 'b': [1, 12.5, 'abc']}]
>>> my_json.to_json()
'[true, {"a": null, "b": [1, 12.5, "abc"]}]'
>>> print(my_json.to_json(pretty=True))
[
true,
{
"a": null,
"b": [
1,
12.5,
"abc"
]
}
]
>>> try:
... my_json = JsonModel([{'a': [1, 2, 1+2j]}]) # nested complex number
... except Exception as e:
... print(str(e).splitlines()[0])
34 validation errors for JsonModel
"""
# Scalars
JsonScalarModel: TypeAlias = JsonScalarM
"""
JsonScalarModel is a limited JSON model supporting only scalar JSON content, e.g. the basic
types: `None`, `int`, `float`, `str`, and `bool`. Lists and dicts (or "objects") are not
supported.
Examples:
>>> my_none = JsonScalarModel(None)
>>> my_none.to_data(), my_none.to_json()
(None, 'null')
>>> my_int = JsonScalarModel(123)
>>> my_int.to_data(), my_int.to_json()
(123, '123')
>>> my_float = JsonScalarModel(12.3)
>>> my_float.to_data(), my_float.to_json()
(12.3, '12.3')
>>> my_str = JsonScalarModel('abc')
>>> my_str.to_data(), my_str.to_json()
(abc, '"abc"')
>>> my_bool = JsonScalarModel(False)
>>> my_bool.to_data(), my_bool.to_json()
(False, '"false"')
>>> try:
... my_json = JsonScalarModel([123])
... except Exception as e:
... print(str(e).splitlines()[0])
6 validation errors for JsonScalarModel
"""
# List at the top level
class JsonListModel(Model[JsonAnyListM]):
"""
JsonListModel is a limited JSON model supporting only JSON content that has a list (or "array"
in JSON nomenclature) at the root. The contents of the top-level list can be any JSON content,
though, any levels deep.
Examples:
>>> my_json = JsonListModel([True, {'a': None, 'b': [1, 12.5, 'abc']}])
>>> my_json.to_data()
[True, {'a': None, 'b': [1, 12.5, 'abc']}]
>>> my_json.to_json()
'[true, {"a": null, "b": [1, 12.5, "abc"]}]'
>>> print(my_json.to_json(pretty=True))
[
true,
{
"a": null,
"b": [
1,
12.5,
"abc"
]
}
]
>>> try:
... my_json = JsonListModel({'a': None, 'b': [1, 12.5, {'abc': 123}]})
... except Exception as e:
... print(str(e).splitlines()[0])
3 validation errors for JsonListModel
"""
class JsonListOfScalarsModel(Model[_JsonListOfScalarsM]):
...
class JsonListOfListsModel(Model[JsonListM[JsonAnyListM]]):
...
class JsonListOfListsOfScalarsModel(Model[JsonListM[_JsonListOfScalarsM]]):
...
class JsonListOfDictsModel(Model[JsonListM[JsonAnyDictM]]):
...
class JsonListOfDictsOfScalarsModel(Model[JsonListM[_JsonDictOfScalarsM]]):
...
# Dict at the top level
class JsonDictModel(Model[JsonAnyDictM]):
"""
JsonDictModel is a limited JSON model supporting only JSON content that has a dict (or "object"
in JSON nomenclature) at the root. The values of the top-level dict can be any JSON content,
though, any levels deep.
Examples:
>>> my_json = JsonDictModel({'a': None, 'b': [1, 12.5, {'abc': 123}]})
>>> my_json.to_data()
{'a': None, 'b': [1, 12.5, {'abc': 123}]}
>>> my_json.to_json()
'{"a": null, "b": [1, 12.5, {"abc": 123}]}'
>>> print(my_json.to_json(pretty=True))
{
"a": null,
"b": [
1,
12.5,
{
"abc": 123
}
]
}
>>> try:
... my_json = JsonDictModel([True, {'a': None, 'b': [1, 12.5, 'abc']}])
... except Exception as e:
... print(str(e).splitlines()[0])
3 validation errors for JsonDictModel
"""
class JsonDictOfScalarsModel(Model[_JsonDictOfScalarsM]):
...
class JsonDictOfListsModel(Model[JsonDictM[JsonAnyListM]]):
...
class JsonDictOfListsOfScalarsModel(Model[JsonDictM[_JsonListOfScalarsM]]):
...
class JsonDictOfDictsModel(Model[JsonDictM[JsonAnyDictM]]):
...
class JsonDictOfDictsOfScalarsModel(Model[JsonDictM[_JsonDictOfScalarsM]]):
...
# Nested models
class JsonNoDictsModel(Model[_JsonOnlyListsUnion]):
...
class JsonNestedListsModel(Model[JsonOnlyListsM]):
...
class JsonNoListsModel(Model[_JsonOnlyDictsUnion]):
...
class JsonNestedDictsModel(Model[JsonOnlyDictsM]):
...
# More specific models
class JsonListOfNestedDictsModel(Model[JsonListM[JsonOnlyDictsM]]):
...
class JsonDictOfNestedListsModel(Model[JsonDictM[JsonOnlyListsM]]):
...
class JsonDictOfListsOfDictsModel(Model[JsonDictM[JsonListM[JsonAnyDictM]]]):
...
# Custom models
class JsonCustomModel(Model[JsonListM[_JsonBaseT]], Generic[_JsonBaseT]):
...
class JsonCustomListModel(Model[JsonListM[_JsonBaseT]], Generic[_JsonBaseT]):
...
class JsonCustomDictModel(Model[JsonDictM[_JsonBaseT]], Generic[_JsonBaseT]):
...
# Add note of dict keys to models containing dicts
_NOTE_ON_DICT_KEYS = """
Note:
JSON dicts (or "objects") only supports strings as keys. By default, however, omnipy
JSON models parse the basic types `float`, `int`, and `bool` as strings if used as keys
in JSON dicts/objects.
Example:
>>> my_dict = JsonModel({'a': None, 0.5: 12.3, 1: 123, False: True})
>>> my_dict.to_data()
{'a': None, '0.5': 12.3, '1': 123, 'False': True}
>>> my_dict.to_json()
'{"a": null, "0.5": 12.3, "1": 123, "False": true}'
"""
_dict_related_json_model_classes: list[Type[Model]] = [
JsonModel,
JsonDictModel,
JsonDictOfScalarsModel,
JsonDictOfListsModel,
JsonDictOfListsOfScalarsModel,
JsonDictOfDictsModel,
JsonDictOfDictsOfScalarsModel,
JsonNoListsModel,
JsonNestedListsModel,
JsonListOfNestedDictsModel,
JsonDictOfNestedListsModel,
JsonDictOfListsOfDictsModel,
JsonCustomDictModel,
]
for _dict_related_json_model_cls in _dict_related_json_model_classes:
if _dict_related_json_model_cls.__doc__:
_dict_related_json_model_cls.__doc__ += _NOTE_ON_DICT_KEYS
Classes
JsonAnyDictM
class JsonAnyDictM(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonAnyDictM(JsonDictM['_JsonAnyUnion']):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonAnyListM
class JsonAnyListM(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonAnyListM(JsonListM['_JsonAnyUnion']):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonCustomDictModel
class JsonCustomDictModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonCustomDictModel(Model[JsonDictM[_JsonBaseT]], Generic[_JsonBaseT]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonCustomListModel
class JsonCustomListModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonCustomListModel(Model[JsonListM[_JsonBaseT]], Generic[_JsonBaseT]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonCustomModel
class JsonCustomModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonCustomModel(Model[JsonListM[_JsonBaseT]], Generic[_JsonBaseT]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictM
class JsonDictM(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictModel
class JsonDictModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
JsonDictModel is a limited JSON model supporting only JSON content that has a dict (or "object"
in JSON nomenclature) at the root. The values of the top-level dict can be any JSON content, though, any levels deep.
View Source
class JsonDictModel(Model[JsonAnyDictM]):
"""
JsonDictModel is a limited JSON model supporting only JSON content that has a dict (or "object"
in JSON nomenclature) at the root. The values of the top-level dict can be any JSON content,
though, any levels deep.
Examples:
>>> my_json = JsonDictModel({'a': None, 'b': [1, 12.5, {'abc': 123}]})
>>> my_json.to_data()
{'a': None, 'b': [1, 12.5, {'abc': 123}]}
>>> my_json.to_json()
'{"a": null, "b": [1, 12.5, {"abc": 123}]}'
>>> print(my_json.to_json(pretty=True))
{
"a": null,
"b": [
1,
12.5,
{
"abc": 123
}
]
}
>>> try:
... my_json = JsonDictModel([True, {'a': None, 'b': [1, 12.5, 'abc']}])
... except Exception as e:
... print(str(e).splitlines()[0])
3 validation errors for JsonDictModel
"""
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictOfDictsModel
class JsonDictOfDictsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonDictOfDictsModel(Model[JsonDictM[JsonAnyDictM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictOfDictsOfScalarsModel
class JsonDictOfDictsOfScalarsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonDictOfDictsOfScalarsModel(Model[JsonDictM[_JsonDictOfScalarsM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictOfListsModel
class JsonDictOfListsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonDictOfListsModel(Model[JsonDictM[JsonAnyListM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictOfListsOfDictsModel
class JsonDictOfListsOfDictsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonDictOfListsOfDictsModel(Model[JsonDictM[JsonListM[JsonAnyDictM]]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictOfListsOfScalarsModel
class JsonDictOfListsOfScalarsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonDictOfListsOfScalarsModel(Model[JsonDictM[_JsonListOfScalarsM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictOfNestedListsModel
class JsonDictOfNestedListsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonDictOfNestedListsModel(Model[JsonDictM[JsonOnlyListsM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonDictOfScalarsModel
class JsonDictOfScalarsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonDictOfScalarsModel(Model[_JsonDictOfScalarsM]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonListM
class JsonListM(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonListModel
class JsonListModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
JsonListModel is a limited JSON model supporting only JSON content that has a list (or "array"
in JSON nomenclature) at the root. The contents of the top-level list can be any JSON content, though, any levels deep.
View Source
class JsonListModel(Model[JsonAnyListM]):
"""
JsonListModel is a limited JSON model supporting only JSON content that has a list (or "array"
in JSON nomenclature) at the root. The contents of the top-level list can be any JSON content,
though, any levels deep.
Examples:
>>> my_json = JsonListModel([True, {'a': None, 'b': [1, 12.5, 'abc']}])
>>> my_json.to_data()
[True, {'a': None, 'b': [1, 12.5, 'abc']}]
>>> my_json.to_json()
'[true, {"a": null, "b": [1, 12.5, "abc"]}]'
>>> print(my_json.to_json(pretty=True))
[
true,
{
"a": null,
"b": [
1,
12.5,
"abc"
]
}
]
>>> try:
... my_json = JsonListModel({'a': None, 'b': [1, 12.5, {'abc': 123}]})
... except Exception as e:
... print(str(e).splitlines()[0])
3 validation errors for JsonListModel
"""
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonListOfDictsModel
class JsonListOfDictsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonListOfDictsModel(Model[JsonListM[JsonAnyDictM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonListOfDictsOfScalarsModel
class JsonListOfDictsOfScalarsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonListOfDictsOfScalarsModel(Model[JsonListM[_JsonDictOfScalarsM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonListOfListsModel
class JsonListOfListsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonListOfListsModel(Model[JsonListM[JsonAnyListM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonListOfListsOfScalarsModel
class JsonListOfListsOfScalarsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonListOfListsOfScalarsModel(Model[JsonListM[_JsonListOfScalarsM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonListOfNestedDictsModel
class JsonListOfNestedDictsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonListOfNestedDictsModel(Model[JsonListM[JsonOnlyDictsM]]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonListOfScalarsModel
class JsonListOfScalarsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonListOfScalarsModel(Model[_JsonListOfScalarsM]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonModel
class JsonModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
JsonModel is a general JSON model supporting any JSON content, any levels deep.
View Source
class JsonModel(Model[_JsonAnyUnion]):
"""
JsonModel is a general JSON model supporting any JSON content, any levels deep.
Examples:
>>> my_int_json = JsonModel(123)
>>> my_int_json.to_data()
123
>>> my_int_json.to_json()
'123'
>>> my_json = JsonModel([True, {'a': None, 'b': [1, 12.5, 'abc']}])
>>> my_json.to_data()
[True, {'a': None, 'b': [1, 12.5, 'abc']}]
>>> my_json.to_json()
'[true, {"a": null, "b": [1, 12.5, "abc"]}]'
>>> print(my_json.to_json(pretty=True))
[
true,
{
"a": null,
"b": [
1,
12.5,
"abc"
]
}
]
>>> try:
... my_json = JsonModel([{'a': [1, 2, 1+2j]}]) # nested complex number
... except Exception as e:
... print(str(e).splitlines()[0])
34 validation errors for JsonModel
"""
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonNestedDictsModel
class JsonNestedDictsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonNestedDictsModel(Model[JsonOnlyDictsM]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonNestedListsModel
class JsonNestedListsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonNestedListsModel(Model[JsonOnlyListsM]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonNoDictsModel
class JsonNoDictsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonNoDictsModel(Model[_JsonOnlyListsUnion]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonNoListsModel
class JsonNoListsModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonNoListsModel(Model[_JsonOnlyDictsUnion]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonOnlyDictsM
class JsonOnlyDictsM(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonOnlyDictsM(JsonDictM['_JsonOnlyDictsUnion']):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonOnlyListsM
class JsonOnlyListsM(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonOnlyListsM(JsonListM['_JsonOnlyListsUnion']):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
delitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
setitem
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonScalarM
class JsonScalarM(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonScalarM(Model[JsonScalar]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents
JsonScalarModel
class JsonScalarModel(
value: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
*,
__root__: Union[Any, pydantic.fields.UndefinedType] = PydanticUndefined,
**data: Any
)
A data model containing a value parsed according to the model.
If no value is provided, the value is set to the default value of the data model, found by
calling the model class without parameters, e.g. int()
.
Model is a generic class that cannot be instantiated directly. Instead, a Model class needs to be specialized with a data type before Model objects can be instantiated. A data model functions as a data parser and guarantees that the parsed data follows the specified model.
Example data model specialized as a class alias::
MyNumberList = Model[list[int]]
... alternatively as a Model subclass::
class MyNumberList(Model[list[int]]):
pass
Once instantiated, a Model object functions as a parser, e.g.::
my_number_list = MyNumberList([2,3,4])
my_number_list.contents = ['3', 4, True]
assert my_number_list.contents == [3,4,1]
While the following should raise a ValidationError
::
my_number_list.contents = ['abc', 'def']
The Model class is a wrapper class around the powerful GenericModel
class from pydantic.
See also docs of the Dataset class for more usage examples.
View Source
class JsonScalarM(Model[JsonScalar]):
...
Class variables
Static methods
to_json_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
@classmethod
def to_json_schema(cls, pretty=False) -> str:
schema = cls.schema()
if pretty:
return cls._pretty_print_json(schema)
else:
return json.dumps(schema)
validate
Hack to allow overwriting of iter method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
Model |
View Source
@classmethod
def validate(cls: Type['Model'], value: Any) -> 'Model':
"""
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
if isinstance(value, Model):
with AttribHolder(value, '__iter__', GenericModel.__iter__, on_class=True):
return super().validate(value)
else:
return super().validate(value)
Instance variables
Methods
eq
Return self==value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
object |
Returns:
Type | Description |
---|---|
bool |
View Source
def __eq__(self, other: object) -> bool:
return isinstance(other, Model) \
and self.__class__ == other.__class__ \
and self.contents == other.contents \
and self.to_data() == other.to_data() # last is probably unnecessary, but just in case
getattr
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
Returns:
Type | Description |
---|---|
Any |
View Source
def __getattr__(self, attr: str) -> Any:
ret = self._getattr_from_contents(attr)
if callable(ret):
ret = add_callback_after_call(ret, self.validate_contents)
return ret
iter
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
object |
||
kwargs |
object |
Returns:
Type | Description |
---|---|
object |
View Source
def _method(cls_or_self, /, *args, **keywords):
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *self.args, *args, **keywords)
setattr
Implement setattr(self, name, value).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr |
str |
||
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def __setattr__(self, attr: str, value: Any) -> None:
if attr in self.__dict__ and attr not in [ROOT_KEY]:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
contents_prop = getattr(self.__class__, attr)
contents_prop.__set__(self, value)
else:
raise RuntimeError('Model does not allow setting of extra attributes')
from_data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_data(self, value: Any) -> None:
self.contents = value
from_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_contents |
str |
Returns:
Type | Description |
---|---|
NoneType |
View Source
def from_json(self, json_contents: str) -> None:
new_model = self.parse_raw(json_contents, proto=pydantic_protocol.json)
self._set_contents_without_validation(new_model)
inner_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def inner_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=False, with_args=with_args)
is_nested_type
Returns:
Type | Description |
---|---|
bool |
View Source
def is_nested_type(self) -> bool:
return not self.inner_type(with_args=True) == self.outer_type(with_args=True)
outer_type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_args |
bool |
Returns:
Type | Description |
---|---|
type |
None |
View Source
def outer_type(self, with_args: bool = False) -> type | None:
return self.__class__._get_root_type(outer=True, with_args=with_args)
to_data
Returns:
Type | Description |
---|---|
Any |
View Source
def to_data(self) -> Any:
return self.dict()[ROOT_KEY]
to_json
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty |
Returns:
Type | Description |
---|---|
str |
View Source
def to_json(self, pretty=False) -> str:
json_content = self.json()
if pretty:
return self._pretty_print_json(json.loads(json_content))
else:
return json_content
validate_contents
View Source
def validate_contents(self):
self.contents = self.contents