sc4py.env module

sc4py.env.env(name: str, default=None, wrapped: bool = False) str | None[source]

Reads an environment variable as a string.

Parameters:
  • name (str) – The environment variable name.

  • default (Any, optional) – Default value if variable is not set. Default is None.

  • wrapped (bool, optional) – If True, removes single quotes around the value. Default is False.

Returns:

The value of the environment variable or default.

Return type:

str | None

Example::
>>> env('HOME')
'/home/user'
sc4py.env.env_as_bool(name: str, default=None, wrapped: bool = False) bool | None[source]

Reads an environment variable and converts it to a boolean.

Parameters:
  • name (str) – The environment variable name.

  • default (Any, optional) – Default value if variable is not set. Default is None.

  • wrapped (bool, optional) – If True, removes single quotes around the value. Default is False.

Returns:

Boolean value or None if not set.

Return type:

bool | None

Example::
>>> env_as_bool('FEATURE_ENABLED', 'true')
True
sc4py.env.env_as_int(key: str, default=None, wrapped: bool = False) int | None[source]

Reads an environment variable and converts it to an integer.

Parameters:
  • key (str) – The environment variable name.

  • default (Any, optional) – Default value if variable is not set. Default is None.

  • wrapped (bool, optional) – If True, removes single quotes around the value. Default is False.

Returns:

Integer value or None if not set.

Return type:

int | None

Example::
>>> env_as_int('PORT', default='8080')
8080
sc4py.env.env_as_list(name: str, default: str | list[str] | tuple[str, ...] = '', delimiter: str = ',', wrapped: bool = False) list[str] | None[source]

Reads an environment variable and returns it as a list of strings.

Parameters:
  • name (str) – The environment variable name.

  • default (str | list[str] | tuple[str, ...], optional) – Default value if variable is not set. Default is “”.

  • delimiter (str, optional) – Delimiter to split the string. Default is “,”.

  • wrapped (bool, optional) – If True, removes single quotes around the value. Default is False.

Returns:

List of strings or None if not set.

Return type:

list[str] | None

Example::
>>> env_as_list('PATH', delimiter=':')
['/usr/bin', '/bin', '/usr/local/bin']
sc4py.env.env_as_list_of_maps(name: str, key: str, default: str | list[str] | tuple[str, ...] = '', delimiter: str = ',', wrapped: bool = False) list[dict] | None[source]

Reads an environment variable and returns a list of dictionaries with a given key.

Parameters:
  • name (str) – The environment variable name.

  • key (str) – The key for each dictionary.

  • default (str | list[str] | tuple[str, ...], optional) – Default value if variable is not set. Default is “”.

  • delimiter (str, optional) – Delimiter to split the string. Default is “,”.

  • wrapped (bool, optional) – If True, removes single quotes around the value. Default is False.

Returns:

List of dictionaries or None if not set.

Return type:

list[dict] | None

Example::
>>> env_as_list_of_maps('PATH', key='dir', delimiter=':')
[{'dir': '/usr/bin'}, {'dir': '/bin'}, {'dir': '/usr/local/bin'}]
sc4py.env.env_from_json(key: str, default: str | dict | list = '', wrapped: bool = False) dict | list | None[source]

Reads an environment variable and parses it as JSON.

Parameters:
  • key (str) – The environment variable name.

  • default (str | dict | list, optional) – Default value if variable is not set. Default is “”.

  • wrapped (bool, optional) – If True, removes single quotes around the value. Default is False.

Returns:

Parsed JSON object or None if not set.

Return type:

dict | list | None

Example::
>>> env_from_json('CONFIG', default='{"a":1}')
{'a': 1}