Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Generic(De)Serializer stub classes for generic formats #178

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/ngen_init_config/src/ngen/init_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
YamlDeserializer,
TomlDeserializer,
JsonDeserializer,
GenericDeserializer,
)
from .serializer import (
IniSerializer,
NamelistSerializer,
YamlSerializer,
TomlSerializer,
JsonSerializer,
GenericSerializer,
)

from .serializer_deserializer import (
Expand All @@ -23,4 +25,5 @@
YamlSerializerDeserializer,
TomlSerializerDeserializer,
JsonSerializerDeserializer,
GenericSerializerDeserializer,
)
2 changes: 1 addition & 1 deletion python/ngen_init_config/src/ngen/init_config/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.6"
__version__ = "0.0.7"
20 changes: 20 additions & 0 deletions python/ngen_init_config/src/ngen/init_config/deserializer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod

from .core import Base
from .utils import merge_class_attr
from ._deserializers import (
Expand Down Expand Up @@ -105,3 +107,21 @@ def from_json(cls, p: Path) -> Self:
@classmethod
def from_json_str(cls, s: str) -> Self:
return cls.parse_raw(s)


class GenericDeserializer(Base, ABC):
"""
Stub for deserializing from a generic format.
Subclasses must implement the `from_file` and `from_str` abstract class methods.
See `ngen.init_config.core.Base` and `pydantic`'s documentation for configuration options.
"""

@classmethod
@abstractmethod
def from_file(cls, p: Path, *_) -> Self:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewer, *_ effectively means that subclasses that add arguments must make them keyword only / optional. The goal is improve uniformity of implementations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add that note in the extended section of the doc string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can do that if you are adamant about it. I didn't add a comment b.c. the syntax and meaning are self evident (which I kind of like). Its not a super esoteric pattern, but to be fair, it is a newish pattern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too worried. I probably would literally have written it as "Note to self:" because I would forget in the future haha.

...

@classmethod
@abstractmethod
def from_str(cls, s: str, *_) -> Self:
...
17 changes: 17 additions & 0 deletions python/ngen_init_config/src/ngen/init_config/serializer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
from abc import ABC, abstractmethod

from . import core, format_serializers, utils
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -146,3 +147,19 @@ def to_json_str(self, *, indent: int = 0) -> str:
options = {} if not indent else {"indent": indent}
# remove trailing eol
return self.json(by_alias=True, **options).rstrip()


class GenericSerializer(core.Base, ABC):
"""
Stub for serializing a generic format.
Subclasses must implement the `to_file` and `to_str` abstract class methods.
See `ngen.init_config.core.Base` and `pydantic`'s documentation for configuration options.
"""

@abstractmethod
def to_file(self, p: pathlib.Path, *_) -> None:
...

@abstractmethod
def to_str(self, *_) -> str:
...
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ class JsonSerializerDeserializer(ser.JsonSerializer, de.JsonDeserializer):
"""

...


class GenericSerializerDeserializer(ser.GenericSerializer, de.GenericDeserializer):
"""
aaraney marked this conversation as resolved.
Show resolved Hide resolved
Stub for deserializing from and serializing to a generic format.
Subclasses must implement the `to_file`, `to_str`, `from_file`, and `from_str` abstract methods.
See `ngen.init_config.core.Base` and `pydantic`'s documentation for configuration options.
"""

...
Loading