A package that provides tools for interacting with databases when working with alfred3 experiments.
Project description
alfred3_dbtools
This module provides additional tools for working with databases in the context of alfred experiments (see alfred on GitHub).
Installation
pip install alfred3_dbtools
Usage
To import the tools for working with mongodb, include this statement at the beginning of your script:
from alfred3_dbtools import mongotools
You can then access the classes provided in the module:
mongotools.MongoDBConnector
can be used to establish an independent connection to an instance ofpymongo.MongoClient
. Access to the client is provided viamongotools.MongoDBConnector.db
. Depending on how theMongoDBConnector
was initialised, this will return either a database instance or a specific collection inside a database.- Parameters:
host
,port
,username
,password
,database
,collection
(defaults toNone
),auth_source
(defaults to "admin"),ssl
(defaults toFalse
),ca_file
(defaults toNone
). Seehelp(mongotools.MongoDBConnector)
for details.
- Parameters:
mongotools.ExpMongoDBConnector
can be used to establish a connection to an experiments' MongoDBs.- The constructor takes one parameter:
experiment
, which needs to be an alfred experiment. Seehelp(mongotools.ExpMongoDBConnector)
for details. mongotools.ExpMongoDBConnector.db
will return the MongoDB collection of theMongoSavingAgent
with the lowest activation level (i.e. the primaryMongoSavingAgent
). It will raise aValueError
, if the lowest activation level is occupied by two or moreMongoSavingAgent
s.mongotools.ExpMongoDBConnector.list_agents
will return a list of allMongoSavingAgent
s added to the experiment.- Your experiment needs to have at least one MongoSavingAgent for this class to work.
- The constructor takes one parameter:
Refer to the pymongo documentation for further details on how to interact with the clients.
Example of using ExpMongoDBConnector
from alfred3_dbtools import mongotools
from alfred import Experiment
from alfred.page import Page
from alfred.element import TextElement
class Welcome(Page):
def on_showing(self):
db = self.experiment.db
doc = db.find_one({"exp_title": "dbtools Test"}) # query the first dataset for the experiment with title "dbtools Test"
el = TextElement(doc["exp_title"]) # include title of query result in TextElement
self.append(el)
def generate_experiment(self, config=None):
exp = Experiment(config=config)
db_connector = mongotools.ExpMongoDBConnector(exp) # initialise ExpMongoDBConnector
exp.db = db_connector.db # attaching the instance to the experiment instance facilitates availability from within pages (see line 9)
welcome = Welcome(title="Welcome")
exp.append(welcome)
return exp
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
alfred3_dbtools-0.1.2.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for alfred3_dbtools-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2ddb963a70f8a60ed23f769452cbe35abfc1d30ac74df623d5cfaba4b28a6ef |
|
MD5 | 512c48cc4044b69c858a2108b62e33e9 |
|
BLAKE2b-256 | 2080d2ddad5b74e716d1ab714efadcad6a2cdc1868b323dda4f503dde356b865 |