-
Notifications
You must be signed in to change notification settings - Fork 1
Improve module structure #140
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9b7f299
Split converters and models into modules
jacob720 46f888a
Move tests
jacob720 822a173
Add models to public API
jacob720 43c757b
Improve coverage
jacob720 ecb1889
Update docs
jacob720 ec2409c
Clean up APIs and private modules
jacob720 b62051b
Merge branch 'main' into 134_improve_module_structure
jacob720 cc0f1c4
Small change
jacob720 378e8cd
PR comments
jacob720 144faa6
Fixes
jacob720 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| from .converters import ConfigModel | ||
| from .converters.display_config import ( | ||
| DisplayConfig, | ||
| DisplayConfigData, | ||
| ) | ||
| from .converters.lookup_tables import GenericLookupTable | ||
|
|
||
| __all__ = ["ConfigModel", "DisplayConfig", "DisplayConfigData", "GenericLookupTable"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| from ._base_model import ConfigModel | ||
| from ._converter_utils import parse_value, remove_comments | ||
|
|
||
| __all__ = [ | ||
| "ConfigModel", | ||
| "remove_comments", | ||
| "parse_value", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class ConfigModel(BaseModel): ... | ||
25 changes: 25 additions & 0 deletions
25
src/daq_config_server/models/converters/_converter_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import ast | ||
| from typing import Any | ||
|
|
||
| ALLOWED_BEAMLINE_PARAMETER_STRINGS = ["FB", "FULL", "deadtime"] | ||
|
|
||
|
|
||
| class ConverterParseError(Exception): ... | ||
|
|
||
|
|
||
| def remove_comments(lines: list[str]) -> list[str]: | ||
| return [ | ||
| line.strip().split("#", 1)[0].strip() | ||
| for line in lines | ||
| if line.strip().split("#", 1)[0] | ||
| ] | ||
|
|
||
|
|
||
| def parse_value(value: str, convert_to: type | None = None) -> Any: | ||
| """Convert a string value into an appropriate Python type. Optionally provide a type | ||
| to convert to. If not given, the type will be inferred. | ||
| """ | ||
| value = ast.literal_eval(value.replace("Yes", "True").replace("No", "False")) | ||
| if convert_to: | ||
| value = convert_to(value) | ||
| return value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/daq_config_server/models/converters/beamline_parameters/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from ._converters import beamline_parameters_to_dict | ||
|
|
||
| __all__ = ["beamline_parameters_to_dict"] |
28 changes: 28 additions & 0 deletions
28
src/daq_config_server/models/converters/beamline_parameters/_converters.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from typing import Any | ||
|
|
||
| from daq_config_server.models.converters import parse_value, remove_comments | ||
|
|
||
| ALLOWED_BEAMLINE_PARAMETER_STRINGS = ["FB", "FULL", "deadtime"] | ||
|
|
||
|
|
||
| def beamline_parameters_to_dict(contents: str) -> dict[str, Any]: | ||
olliesilvester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Extracts the key value pairs from a beamline parameters file. If the value | ||
| is in ALLOWED_BEAMLINE_PARAMETER_STRINGS, it leaves it as a string. Otherwise, it is | ||
| converted to a number or bool.""" | ||
| lines = contents.splitlines() | ||
| config_pairs: dict[str, Any] = {} | ||
|
|
||
| # Get dict of parameter keys and values | ||
| for line in remove_comments(lines): | ||
| splitline = line.split("=") | ||
| if len(splitline) >= 2: | ||
| param, value = line.split("=") | ||
| if param.strip() in config_pairs: | ||
| raise ValueError(f"Repeated key in parameters: {param}") | ||
| config_pairs[param.strip()] = value.strip() | ||
|
|
||
| # Parse each value | ||
| for param, value in config_pairs.items(): | ||
| if value not in ALLOWED_BEAMLINE_PARAMETER_STRINGS: | ||
| config_pairs[param] = parse_value(value) | ||
| return dict(config_pairs) | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/daq_config_server/models/converters/display_config/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from ._converters import display_config_to_model | ||
| from ._models import DisplayConfig, DisplayConfigData | ||
|
|
||
| __all__ = ["display_config_to_model", "DisplayConfig", "DisplayConfigData"] |
31 changes: 31 additions & 0 deletions
31
src/daq_config_server/models/converters/display_config/_converters.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from daq_config_server.models.converters import parse_value, remove_comments | ||
|
|
||
| from ._models import DisplayConfig, DisplayConfigData | ||
|
|
||
|
|
||
| def display_config_to_model(contents: str) -> DisplayConfig: | ||
olliesilvester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| lines = contents.splitlines() | ||
| config_dict: dict[float, dict[str, int | float]] = {} | ||
| zoom_level = None | ||
|
|
||
| for line in remove_comments(lines): | ||
| key, value = (item.strip() for item in line.split("=", 1)) | ||
| if key == "zoomLevel": | ||
| zoom_level = float(value) | ||
| assert zoom_level not in config_dict.keys(), ( | ||
| f"Multiple instances of zoomLevel {zoom_level}" | ||
| ) | ||
| config_dict[zoom_level] = {} | ||
| continue | ||
|
|
||
| assert zoom_level is not None, "File must start with a zoom level" | ||
| assert key not in config_dict[zoom_level].keys(), ( | ||
| "File can't have repeated keys for a given zoom level" | ||
| ) | ||
| config_dict[zoom_level][key] = parse_value(value) | ||
| zoom_levels = { | ||
| key: DisplayConfigData.model_validate(value) | ||
| for key, value in config_dict.items() | ||
| } | ||
|
|
||
| return DisplayConfig(zoom_levels=zoom_levels) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/daq_config_server/models/converters/lookup_tables/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from ._converters import ( | ||
| beamline_pitch_lut, | ||
| beamline_roll_lut, | ||
| detector_xy_lut, | ||
| undulator_energy_gap_lut, | ||
| ) | ||
| from ._models import GenericLookupTable | ||
|
|
||
| __all__ = [ | ||
| "GenericLookupTable", | ||
| "detector_xy_lut", | ||
| "beamline_pitch_lut", | ||
| "beamline_roll_lut", | ||
| "undulator_energy_gap_lut", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/daq_config_server/models/converters/lookup_tables/_models.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from pydantic import model_validator | ||
|
|
||
| from daq_config_server.models.converters import ConfigModel | ||
|
|
||
|
|
||
| class GenericLookupTable(ConfigModel): | ||
| column_names: list[str] | ||
| rows: list[list[int | float]] | ||
|
|
||
| @model_validator(mode="after") | ||
| def check_row_length_matches_n_columns(self): | ||
| n_columns = len(self.column_names) | ||
| for row in self.rows: | ||
| if len(row) != n_columns: | ||
| raise ValueError( | ||
| f"Length of row {row} does not match number " | ||
| f"of columns: {self.column_names}" | ||
| ) | ||
| return self |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not relevant to this PR really but can we add a comment just to say it should be the parent class to all other config models