Skip to content

Utils module

get_api_key(api_key=None, env_variable_name='GEOAPIFY_KEY')

Simply passes the first argument if not None else returns value of environment variable.

Parameters:

Name Type Description Default
api_key str

API key or None.

None
env_variable_name str

If api_key is None, returns instead the value of the environment variable.

'GEOAPIFY_KEY'

Returns:

Type Description
str

API key as a string.

Source code in geobatchpy/utils.py
def get_api_key(api_key: str = None, env_variable_name: str = 'GEOAPIFY_KEY') -> str:
    """Simply passes the first argument if not None else returns value of environment variable.

    Args:
        api_key: API key or None.
        env_variable_name: If api_key is None, returns instead the value of the environment variable.

    Returns:
        API key as a string.
    """
    if api_key is None:
        try:
            api_key = os.environ[env_variable_name]
        except KeyError:
            logging.error(f'Set the --key option or set the key in the \'{env_variable_name}\' environment variable.')
            raise
    return api_key

read_data_from_json_file(file_path)

Reads data from a JSON file.

Json = Union[Dict[str, Any], List[Any]] is a superset of the JSON specification, excluding scalar objects.

Parameters:

Name Type Description Default
file_path Union[str, Path]

path to the JSON file.

required

Returns:

Type Description
Json

The Python equivalent of the JSON object.

Source code in geobatchpy/utils.py
def read_data_from_json_file(file_path: Union[str, Path]) -> Json:
    """Reads data from a JSON file.

    Json = Union[Dict[str, Any], List[Any]] is a superset of the JSON specification, excluding scalar objects.

    Arguments:
        file_path: path to the JSON file.

    Returns:
        The Python equivalent of the JSON object.
    """
    with open(Path(file_path), 'r') as f:
        data = json.load(fp=f)
    logging.info(f'File \'{file_path}\' read from disk.')
    return data

write_data_to_json_file(data, file_path)

Writes data to a JSON file.

Json = Union[Dict[str, Any], List[Any]] is a superset of the JSON specification, excluding scalar objects.

Parameters:

Name Type Description Default
data Json

an object of Json type.

required
file_path Union[str, Path]

destination path of the JSON file.

required
Source code in geobatchpy/utils.py
def write_data_to_json_file(data: Json, file_path: Union[str, Path]) -> None:
    """Writes data to a JSON file.

    Json = Union[Dict[str, Any], List[Any]] is a superset of the JSON specification, excluding scalar objects.

    Arguments:
        data: an object of Json type.
        file_path: destination path of the JSON file.
    """
    with open(Path(file_path), 'w') as f:
        json.dump(data, fp=f, indent=4)
    logging.info(f'File \'{file_path}\' written to disk.')