How to: Connect your python script

To use cobra_db in a project:

import cobra_db
print(cobra_db.__version__)
from cobra_db import Connector, StudyDao

connector_kwargs = dict(
    host="my_host.com",
    port=27017,
    db_name="my_db_name",
    username="my_username",
)
0.4.4

Passwords must never be stored in plain text. This means never hardcoding the password in your script.

Option 1: Prompt the user for the password at execution time

This is useful in jupyter notebooks. An interactive prompt will appear when running the following cell:

connector = Connector.get_pass(**connector_kwargs)

Option 2: Get the password from the env variables

This option should be used for scripts.

We will need to run the following command only once per session (with a trailing space to avoid leaking your password to the command history).

 export MONGOPASS='my_passw'

Then you will be able to create the connector.

# in this case we set the password at runtime to be able to showcase the functionality
import os
os.environ['MONGOPASS'] = 'my_passw'
connector = Connector.get_env_pass(**connector_kwargs)
print(f"Successfully created connector to {connector}")
Successfully created connector to mongodb://my_username:%2A%2A%2A%2A%2A%2A%2A%2A@my_host.com:27017/my_db_name