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

scrap.Scrap as a dataclass #89

Open
philippefutureboy opened this issue Aug 19, 2021 · 0 comments
Open

scrap.Scrap as a dataclass #89

philippefutureboy opened this issue Aug 19, 2021 · 0 comments

Comments

@philippefutureboy
Copy link

philippefutureboy commented Aug 19, 2021

Hey there!

While I was fiddling around with scrapbook, I ended up writing a dataclass for scrap.Scrap items.
I noticed in the source that you have a comment that says:

# dataclasses would be nice here...
Scrap = namedtuple("Scrap", ["name", "data", "encoder", "display"])
Scrap.__new__.__defaults__ = (None,)

So I figured you could be interested by the implementation I made! :)
See below:

@dataclass
class Scrap(collections.abc.Mapping):
    name: str
    data: Any
    encoder: str
    display: str = None

    def keys(self):
        return self.__dataclass_fields__.keys() # pylint: disable=no-member

    def __getitem__(self, key: str) -> Any:
        if isinstance(key, str):
            if not hasattr(self, key):
                raise KeyError(key)
            return getattr(self, key)
        else:
            raise TypeError(f"Unsupported key type: {type(key)}")

    def __len__(self):
        return len(self.__dataclass_fields__.keys()) # pylint: disable=no-member

    def __iter__(self):
        return (attr for attr in dataclasses.astuple(self))

    def asdict(self) -> dict:
        return dataclasses.asdict(self)

    def astuple(self) -> tuple:
        return dataclasses.astuple(self)
  • keys, __getitem__, __len__ are for the Mapping protocol, to allow the Scrap to be converted to a dict using the ** operator ({**scrap})
  • __iter__ is to support unpacking the scrap
  • asdict, astuple are for convenience
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant