hydrotools._restclient.utilities module#

class hydrotools._restclient.utilities.Alias(value: int | float | str | bytes | bool | Callable | tuple | frozenset | None, keys: Iterable | Hashable)#

Bases: object

Create an immutable many to one relationship where a set of keys alias some value. This is often useful in a variety of applications when the API differs from the backend value. This is also useful when writing factories patterns.

A value can be any scalar, callable, tuple, or frozenset type. At construction, value is deepcopied, meaning a value cannot be mutated by reference. Valid alias keys are scalar types and collections of scalar types. Keys are stored in a frozenset, so in the case of passing a dictionary as the keys arg, only the dictionary keys from the passed dictionary will be considered as keys.

Examples

cms = Alias(“cms”, [“CMS”, “m^3/s”])

cms[“CMS”] # returns “cms” cms.get(“m^3/s”) # returns “cms” “CMS” in cms # returns True

# foo is callable foo_alias = Alias(foo, [“bar”, “baz”])

result = foo_alias[“bar”]()

get(key: Hashable) int | float | str | bytes | bool | Callable | tuple | frozenset | None#

Get value given a valid key. Providing the value or an Alias instance is also supported. If a valid value is not provided, return None.

Parameters:

value (Hashable) – Valid alias value

Returns:

alias value if valid key, value, or Alias instance, else None

Return type:

Union[int, float, str, bytes, bool, Callable, tuple, frozenset, None]

keys: Iterable | Hashable#
value: int | float | str | bytes | bool | Callable | tuple | frozenset | None#
class hydrotools._restclient.utilities.AliasGroup(alias: List[Alias | AliasGroup])#

Bases: object

Create a group of Alias objects that are treated like a single Alias. This comes in handy to cut down on conditional statements when developing for example, rest api libraries. This is clearest shown through an example.

Example

base_url = “www.api.org”

# In the below, both path-1 and path-2 are different entities. # api.org/path-1/cool-feature # api.org/path-2/cool-feature

path_1 = Alias(“path-1”, [1, “1”]) path_2 = Alias(“path-2”, [2, “2”]) path_group = path_1 | path_2

# or equivalently (using logical or | opporator is recommended): # path_group = GroupAlias([path_1, path_2])

def get_cool_feature(path: Union[int, str]):

path = path_group[path] # ValueError thrown if invalid path

url = f”{base_url}/{path}/cool-feature”

response = requests.get(url) return response.json()

get(key: Hashable) int | float | str | bytes | bool | Callable | tuple | frozenset | None#

Get value of Alias in group from a corresponding key, alias value, or alias instance. If corresponding Alias value is not present, return None.

Parameters:

value (Hashable) – Valid alias value

Returns:

alias value if valid key, value, or Alias instance, else None

Return type:

Union[int, float, str, bytes, bool, Callable, tuple, frozenset, None]

property keys: set#

Alias keys

property option_groups#

Mapping of Alias keys’s to Alias value’s

property values: frozenset#

Frozenset of present Alias value’s