-
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.
Add rgb_range percentile support (#322)
Co-authored-by: Dion Häfner <[email protected]> Co-authored-by: Dion Häfner <[email protected]>
- Loading branch information
1 parent
1647843
commit 83bd1d7
Showing
10 changed files
with
399 additions
and
19 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,47 @@ | ||
"""server/fields.py | ||
Custom marshmallow fields for the server API. | ||
""" | ||
|
||
import re | ||
from marshmallow import ValidationError, fields | ||
|
||
from typing import Any, Union | ||
|
||
|
||
class StringOrNumber(fields.Field): | ||
""" | ||
Marshmallow type that can be either a string or a number. | ||
Uses marshmallow's default serialization/deserialization | ||
for `String` or a `Float` depending on the value type. | ||
""" | ||
|
||
def _serialize( | ||
self, value: Union[str, bytes, int, float], attr: Any, obj: Any, **kwargs: Any | ||
) -> Union[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: Union[str, bytes, int, float], attr: Any, data: Any, **kwargs: Any | ||
) -> Union[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: | ||
""" | ||
Validates that the stretch range is in the format `p<digits>` | ||
when a string is used. | ||
""" | ||
if isinstance(data, str): | ||
if not re.match("^p\\d+$", data): | ||
raise ValidationError("Percentile format is `p<digits>`") |
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.