Configuration and utilities

Utility functions

pynsee.utils.init_conn(sirene_key: str | None = None, http_proxy: str | None = None, https_proxy: str | None = None, raise_if_not_ok: bool = True) None

Save your credentials to connect to INSEE APIs, subscribe to api.insee.fr

In case of failure of an API outside your current usecase (ie. failure of SIREN API server while you are actually interested in metadata) you can bypass this init_conn(raise_if_not_ok=False)

Parameters:
  • sirene_key (str, optional) – user’s key for sirene API

  • http_proxy (str, optional) – Proxy server address, e.g. ‘http://my_proxy_server:port’. Defaults to “”.

  • https_proxy (str, optional) – Proxy server address, e.g. ‘http://my_proxy_server:port’. Defaults to “”.

  • raise_if_not_ok (bool, optional) – If set to True, a RequestException will automatically be raised if the response is not ok (= status_code >= 400). The default is True.

Notes

Environment variables can be used instead of init_conn function

Examples

>>> from pynsee.utils import init_conn
>>> init_conn(sirene_key="my_sirene_key")
>>> #
>>> # if the user has to use a proxy server use http_proxy and https_proxy arguments as follows:
>>> from pynsee.utils import init_conn
>>> init_conn(sirene_key="my_sirene_key",
>>>           http_proxy="http://my_proxy_server:port",
>>>           https_proxy="http://my_proxy_server:port")
>>> #
>>> # Alternativety you can use directly environment variables as follows:
>>> # Beware not to commit your credentials!
>>> import os
>>> os.environ['sirene_key'] = 'my_sirene_key'
>>> os.environ['http_proxy'] = "http://my_proxy_server:port"
>>> os.environ['https_proxy'] = "http://my_proxy_server:port"
>>> init_conn()
pynsee.utils.clear_all_cache()

Clear the cache of all functions

Notes

If the credentials provided fail to get a token from api.insee.fr even after a double check, try to clear the cache as the output of the function retrieving the token is cached even it is an error.

Examples

>>> from pynsee.utils import clear_all_cache
>>> clear_all_cache()

Configuring the library’s behavior

pynsee can also be configured via the following environment variables:

  • SIRENE_KEY takes precedence over the one that may be save in the config file

  • HTTP_PROXY and HTTPS_PROXY also override config proxy

  • PYNSEE_SILENT_MODE silences all logging information about cached files