pycharmers.utils.environ_utils module

Utility programs for environment variables.

pycharmers.utils.environ_utils.name2envname(name, service='', prefix='')[source]

Convert name to environment varname. :param name: name. :type name: str :param service: service name. (default= "") :type service: str :param prefix: prefix of the name. (default= "") :type prefix: str

Examples

>>> from pycharmers.utils import name2envname
>>> name2envname(name="name", service="",        prefix="")
'PYTHON_CHARMERS_NAME'
>>> name2envname(name="name", service="service", prefix="")
'PYTHON_CHARMERS_SERVICE_NAME'
>>> name2envname(name="name", service="",        prefix="prefix")
'PYTHON_CHARMERS_PREFIX_NAME'
>>> name2envname(name="name", service="service", prefix="prefix")
'PYTHON_CHARMERS_SERVICE_PREFIX_NAME'
pycharmers.utils.environ_utils.where_is_envfile()[source]

Get Where the envfile is. .. rubric:: Examples

>>> from pycharmers.utils import where_is_envfile
>>> where_is_envfile()
'/Users/iwasakishuto/.pycharmers/.env'
pycharmers.utils.environ_utils.read_environ(dotenv_path='/Users/iwasakishuto/.pycharmers/.env')[source]

Read the environment variables from dotenv_path

Parameters

dotenv_path (str) – path/to/.env (default= DOTENV_PATH)

Returns

the dictionary containing dotenv_path’s environment variables.

Return type

dict

Examples

>>> from pycharmers.utils import read_environ
>>> read_environ()
{'PYTHON_CHARMERS_API_TRELLO_ID': 'id',
'PYTHON_CHARMERS_API_TRELLO_APIKEY': 'apikey',
'PYTHON_CHARMERS_API_TRELLO_TOKEN': 'token'}
pycharmers.utils.environ_utils.write_environ(dotenv_path='/Users/iwasakishuto/.pycharmers/.env', **kwargs)[source]

Overwrite or Write the environment variables in dotenv_path

Parameters
  • dotenv_path (str) – path/to/.env (default= DOTENV_PATH)

  • kwargs (dict) – new environment variables

Examples

>>> from pycharmers.utils import read_environ, write_environ
>>> read_environ()
{'PYTHON_CHARMERS_API_TRELLO_ID': 'id',
'PYTHON_CHARMERS_API_TRELLO_APIKEY': 'apikey',
'PYTHON_CHARMERS_API_TRELLO_TOKEN': 'token'}
>>> write_environ(PYTHON_CHARMERS_SAMPLE="SAMPLE")
>>> read_environ()
{'PYTHON_CHARMERS_API_TRELLO_ID': 'id',
'PYTHON_CHARMERS_API_TRELLO_APIKEY': 'apikey',
'PYTHON_CHARMERS_API_TRELLO_TOKEN': 'token',
'PYTHON_CHARMERS_SAMPLE': 'SAMPLE'}
>>> write_environ(PYTHON_CHARMERS_SAMPLE="SAMPLE_AMENDED")
>>> read_environ()
{'PYTHON_CHARMERS_API_TRELLO_ID': 'id',
'PYTHON_CHARMERS_API_TRELLO_APIKEY': 'apikey',
'PYTHON_CHARMERS_API_TRELLO_TOKEN': 'token',
'PYTHON_CHARMERS_SAMPLE': 'SAMPLE_AMENDED'}
pycharmers.utils.environ_utils.show_environ(dotenv_path='/Users/iwasakishuto/.pycharmers/.env')[source]

Show environment variables written in dotenv_path.

Parameters

dotenv_path (str) – path/to/.env (default= DOTENV_PATH)

Examples

>>> from pycharmers.utils import show_environ
>>> show_environ()
* PYTHON_CHARMERS_API_TRELLO_ID : "id"
* PYTHON_CHARMERS_API_TRELLO_APIKEY : "api_key"
* PYTHON_CHARMERS_API_TRELLO_TOKEN : "token
pycharmers.utils.environ_utils.load_environ(dotenv_path='/Users/iwasakishuto/.pycharmers/.env', env_varnames=[], verbose=False)[source]

Load environment variables from path file, and return whether every necessary VARNAMES (env_varnames) are set.

Parameters
  • dotenv_path (str) – path/to/.env (default= DOTENV_PATH)

  • env_varnames (list) – new environment variables

  • verbose (bool) – Whether to print message or not. (default= False)

Returns

Whether there are environment variables not defined in dotenv_path but in env_varnames

Return type

bool

Examples

>>> from pycharmers.utils import load_environ, show_environ
>>> is_ok = load_environ()
>>> is_ok
True
>>> show_environ()
* PYTHON_CHARMERS_API_TRELLO_ID : "id"
* PYTHON_CHARMERS_API_TRELLO_APIKEY : "api_key"
* PYTHON_CHARMERS_API_TRELLO_TOKEN : "token
>>> is_ok = load_environ(env_varnames=["PYTHON_CHARMERS_NOT_DEFINED"])
PYTHON_CHARMERS_NOT_DEFINED is not set.
EnvVariableNotDefinedWarning: Please set environment variable in /Users/iwasakishuto/.pycharmers/.env
>>> is_ok
False
pycharmers.utils.environ_utils.check_environ(required_keynames, required_env_varnames=None, verbose=True, **kwargs)[source]

Check whether meet the requirements.

Parameters
  • required_keynames (list) – Required keynames

  • required_env_varnames (list) – Required environment variables.

  • verbose (bool) – Whether to print message or not. (default= True)

Examples

>>> check_environ(
...     required_keynames=["hoge"],
...     required_env_varnames=["PYTHON_CHARMERS_HOGE"],
...     hoge="hoge"
>>> )
(True, [])
>>> check_environ(
...     required_keynames=["hoge"],
...     required_env_varnames=["PYTHON_CHARMERS_HOGE"],
...     hoge1="hoge"
>>> )
Please set PYTHON_CHARMERS_HOGE or give hoge as kwargs.
(False, ['hoge'])
Returns

  • is_ok : Whether meet the requirements.

  • not_meet_keynames : contains keynames you have to supply.

Return type

tuple (bool, list)