-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pre-commit fixes, /singleband stretch, tests
- Loading branch information
1 parent
1775496
commit 7c4c20e
Showing
9 changed files
with
185 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,37 @@ | ||
import re | ||
from marshmallow import ValidationError, fields | ||
|
||
from typing import Any | ||
|
||
|
||
class StringOrNumber(fields.Field): | ||
def _serialize( | ||
self, value: Any, attr: Any, obj: Any, **kwargs: Any | ||
) -> str | float | None: | ||
if isinstance(value, (str, bytes)): | ||
return fields.String()._serialize(value, attr, obj, **kwargs) | ||
elif isinstance(value, (int, float)): | ||
return fields.Float()._serialize(value, attr, obj, **kwargs) | ||
else: | ||
raise ValidationError("Must be a string or a number") | ||
|
||
def _deserialize( | ||
self, value: Any, attr: Any, data: Any, **kwargs: Any | ||
) -> str | float | None: | ||
if isinstance(value, (str, bytes)): | ||
return fields.String()._deserialize(value, attr, data, **kwargs) | ||
elif isinstance(value, (int, float)): | ||
return fields.Float()._deserialize(value, attr, data, **kwargs) | ||
else: | ||
raise ValidationError("Must be a string or a number") | ||
|
||
|
||
def validate_stretch_range(data: Any) -> None: | ||
if isinstance(data, str) and data.startswith("p"): | ||
if not re.match("^p\\d+$", data): | ||
raise ValidationError("Percentile format is `p<digits>`") | ||
else: | ||
try: | ||
float(data) | ||
except ValueError: | ||
raise ValidationError("Must be a number") |
This file contains 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 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 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 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
Oops, something went wrong.