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

Use hardcoded filenames. #815

Merged
merged 9 commits into from
Nov 24, 2023
Merged

Use hardcoded filenames. #815

merged 9 commits into from
Nov 24, 2023

Conversation

evetion
Copy link
Member

@evetion evetion commented Nov 21, 2023

Fixes #799

@evetion evetion requested a review from Hofer-Julian November 21, 2023 15:15
Comment on lines 112 to 115
basin::String = "basin.arrow"
flow::String = "flow.arrow"
control::String = "control.arrow"
allocation::String = "allocation.arrow"
Copy link
Contributor

Choose a reason for hiding this comment

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

To fit #799 (comment)

Those fields need to be removed as far as I can see

Copy link
Member Author

Choose a reason for hiding this comment

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

These fields need to be hardcodes somewhere for core to write them.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, but if you hardcode them here, it will be possible to override them with the TOML right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I now always override them.

Comment on lines 47 to 50
basin: Path = Field(default=Path("basin.arrow"), exclude=True, repr=False)
flow: Path = Field(default=Path("flow.arrow"), exclude=True, repr=False)
control: Path = Field(default=Path("control.arrow"), exclude=True, repr=False)
allocation: Path = Field(default=Path("allocation.arrow"), exclude=True, repr=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

These need to be removed too

Copy link
Member Author

Choose a reason for hiding this comment

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

I've excluded them from any exports. They are here for when we need to parse the output files.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just an FYI: I just found out that TOML hates these Path objects, and complains that it cannot serialize them.

However, due to the exclude_unset logic, they aren't included unless you set them yourself. For extra confusion, if you set the path (in current main) as a string, pydantic will coerce it to a Path. Then pydantic detects it has been set, and then TOML complains that it cannot serialize it.

To be honest, together with issue #811, I'm getting really radicalized against exclude_unset.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's why there's

@field_serializer("input_dir", "results_dir") 
  def serialize_path(self, path: Path) -> str: 
      return str(path)

Right? Which fields are you trying to set?

I don't like the unset behaviour either, but that should be a separate issue/discussion.

core/src/config.jl Outdated Show resolved Hide resolved
@evetion evetion requested a review from Hofer-Julian November 23, 2023 11:55
- Wrap `Config` around new struct `Toml`
- Remove result keys: `basin`, `flow`, `control` and `allocation`
- Create constants for these keys
- Remove default values of `input_dir` and `results_dir`
@Hofer-Julian Hofer-Julian force-pushed the feat/hardcoded-filenames branch from 1440df8 to ca869bb Compare November 23, 2023 16:06
@Hofer-Julian Hofer-Julian requested a review from visr November 23, 2023 16:06
core/src/consts.jl Outdated Show resolved Hide resolved
core/src/config.jl Outdated Show resolved Hide resolved
Comment on lines 147 to 150
struct Config
toml::Toml
relative_dir::String
end
Copy link
Member

@visr visr Nov 23, 2023

Choose a reason for hiding this comment

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

I like separating the TOML from other things. I don't like having to always type e.g. config.toml.starttime. Would it work for you if we forward Base.getproperty on Config to the toml field? To maintain the old API. To access the relative_dir field, which perhaps should just be dir or directory, we could add Base.dirname(config::Config) = getfield(config, :relative_dir).

The Base.show method in this file probably also need updating.

Copy link
Contributor

@Hofer-Julian Hofer-Julian Nov 24, 2023

Choose a reason for hiding this comment

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

forward Base.getproperty on Config to the toml field

Nice idea. It adds a bit complexity, but I think it's worth it.

relative_dir field, which perhaps should just be dir or directory

I think relative_dir is more descriptive

we could add Base.dirname(config::Config) = getfield(config, :relative_dir)

The docs of Base.dirname talk about paths.
Since config isn't a path, I don't think it is applicable here. I've created a function relative_dir instead.

The Base.show method in this file probably also need updating.

It seems to work fine for me. It's what is called if I evaluate a Config instance in the REPL, right?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Not sure what you think, but the name relative_dir has always been a bit confusing to me. It is not necessarily relative, it is just a directory, it's other paths in the TOML that are relative to it.

Regarding Base.dirname, good that you checked the docstring for it. The line "Get the directory part of a path." really seems to describe the particular ::String method though. If it is a true function docstring it should describe what is does ("get the directory"), not from where. And what it does seems to match fine. Wflow also has this method on their Config.

Indeed Base.show seems to work fine, I mistakenly assumed it would be broken by this PR.

Copy link
Member

@visr visr left a comment

Choose a reason for hiding this comment

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

Left some final minor comments.

core/src/config.jl Show resolved Hide resolved
Comment on lines 147 to 150
struct Config
toml::Toml
relative_dir::String
end
Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Not sure what you think, but the name relative_dir has always been a bit confusing to me. It is not necessarily relative, it is just a directory, it's other paths in the TOML that are relative to it.

Regarding Base.dirname, good that you checked the docstring for it. The line "Get the directory part of a path." really seems to describe the particular ::String method though. If it is a true function docstring it should describe what is does ("get the directory"), not from where. And what it does seems to match fine. Wflow also has this method on their Config.

Indeed Base.show seems to work fine, I mistakenly assumed it would be broken by this PR.

@Hofer-Julian
Copy link
Contributor

@visr I've renamed relative_dir to dir, used getproperty instead of getfield where applicable and started using Base.dirname

@visr visr merged commit ab1a0dc into main Nov 24, 2023
15 checks passed
@visr visr deleted the feat/hardcoded-filenames branch November 24, 2023 10:42
deltamarnix pushed a commit that referenced this pull request Nov 24, 2023
This code gives an error in the QGIS plugin:

```python
    def _get_database_path_from_model_file(self) -> str:
        with open(self.path, "rb") as f:
            model_filename = tomllib.load(f)["database"]
            return str(Path(self.path).parent.joinpath(model_filename))
```

Because the database entry is no longer present in the TOML because of
#815.
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

Successfully merging this pull request may close these issues.

Default results design
4 participants