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

Change prefs to pydantic #155

Open
sneakers-the-rat opened this issue Feb 21, 2022 · 4 comments
Open

Change prefs to pydantic #155

sneakers-the-rat opened this issue Feb 21, 2022 · 4 comments

Comments

@sneakers-the-rat
Copy link
Contributor

Trying pydantic right now and absolutely love it. Seems like a superset of dataclasses that makes typing very easy.

For example:

from pydantic import BaseModel, Field
from typing import Optional
from pathlib import Path

class Directories(BaseModel):
    user_dir: Path = Path.home() / 'autopilot'
    prefs_file: Path = user_dir / 'prefs.json'

class Audio_Prefs(BaseModel):
    fs:int = 192000
    device: str = Field(description="The sound device to use!")

class Pigpio_Prefs(BaseModel):
    mask: str = 'etc.'

class Prefs(BaseModel):
    audio_prefs: Audio_Prefs
    pigpio_prefs: Optional[Pigpio_Prefs] = None

    def save(self, file: Path = Directories.prefs_file):
        with open(file, 'w') as pfile:
            pfile.write(self.json())

    @classmethod
    def load(cls, file: Path = Directories.prefs_file) -> 'Prefs':
        with open(file, 'r') as pfile:
            prefs_raw = pfile.read()
        return Prefs.parse_raw(prefs_raw)

This would solve a lot of problems, like the ballooning complexity of the prefs file and its relatively undocumented state, the ability to have impossible values which causes a lot of coercion throughout the program, and the awkwardness of the current multiprocessing-safe prefs which could be solved by writing/reading to a file with a lock.

@sneakers-the-rat
Copy link
Contributor Author

We could also make a trivial mapping from types to graphical elements and solve this and make the prefs TUI a lot a lot a lot easier to manage, re: #153

@sneakers-the-rat
Copy link
Contributor Author

@cxrodgers
Copy link
Contributor

One gotcha I've run into before when hardcoding my own prefs-esque code is things like "True" vs True, or "1" vs 1, or "None" vs None vs np.nan vs "" vs, and you want something to handle that sanely when converting to/from plaintext. I imagine pydantic might be good for that

@sneakers-the-rat
Copy link
Contributor Author

One gotcha I've run into before when hardcoding my own prefs-esque code is things like "True" vs True, or "1" vs 1, or "None" vs None vs np.nan vs "" vs, and you want something to handle that sanely when converting to/from plaintext. I imagine pydantic might be good for that

Definitely. and the current prefs manager is a really huge mess for any circumstance where you're using autopilot stuff in a nonstandard context due to the use of the multiprocessing manager... Definitely need to move to a different model of setting prefs where it's not globally shared across the module but each module knows how to get its own. Prefs are basically never prefs.set anyway, and the times they are are just sort of a stopgap that should be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants