pygsti.baseobjs.mongoserializable.WriteOpsByCollection#

class WriteOpsByCollection(session=None, allowed_collection_names=None)#

Bases: dict

A dictionary of pymongo write operations stored on a per-collection basis.

A dictionary with collection-name (string) keys and where each value is a list of pymongo.InsertOne and pymongo.ReplaceOne operations to be performed on the named collection at some later (unpsecified) time.

The collection names (keys) can be restricted to a predefined set if desired.

Methods

__init__([session, allowed_collection_names])

add_gridfs_put_op(collection_name, doc_id, ...)

Add a GridFS put operation to this dictionary of write operations.

add_one_op(collection_name, uid, doc, ...[, ...])

Add a single write operation to this dictionary, if one is allowed and needed.

add_ops_by_collection(other_ops)

Merge the information from another WriteOpsByCollection into this one.

clear()

copy()

execute(mongodb)

Execute all of the "queued" operations within this dictionary on a MongoDB instance.

fromkeys([value])

Create a new dictionary with keys from iterable and values set to value.

get(key[, default])

Return the value for key if key is in the dictionary, else default.

items()

keys()

pop(k[,d])

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()

Remove and return a (key, value) pair as a 2-tuple.

setdefault(key[, default])

Insert key with a value of default if key is not in the dictionary.

update([E, ]**F)

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values()

add_gridfs_put_op(collection_name, doc_id, binary_data, overwrite_existing, mongodb)#

Add a GridFS put operation to this dictionary of write operations.

This is a special type of operation for placing large chunks of binary data into a MongoDB. Arguments are similar to add_one_op().

add_one_op(collection_name, uid, doc, overwrite_existing, mongodb, check_local_ops=True)#

Add a single write operation to this dictionary, if one is allowed and needed.

Adds a pymongo.InsertOne or pymongo.ReplaceOne object to self[collection_name] in order to write a document doc with unique identifier uid. If overwrite_existing is False then an error is raised if a document with uid already exists – either in the database or within the list of existing write operations – and the existing document doesn’t match the document being written (doc).

Parameters:
  • collection_name (str) – The collection name (key) to add an operation to.

  • uid (bson.object.ObjectId or dict) – Unique identifier for the document to write.

  • doc (dict) – The document to write.

  • overwrite_existing (bool, optional) – Whether existing documents should be overwritten. The default of False causes a ValueError to be raised if a document with the given uid already exists and is different from doc.

  • mongodb (pymongo.database.Database) – The MongoDB instance documents will eventually be written to. This database is not written to, and used solely to test for existing documents so that insert vs. replace operations are chosen correctly.

  • check_local_ops (bool, optional) – Whether the queued-up write operations contained within this WriteOpsByCollection object are considered when testing for the existence of documents. Leaving this set to True is the safe option. Set this to False to get a performance increase when you’re sure there’s no possibility a document with uid could have been “written” to this WriteOpsByCollection since its initialization.

Returns:

The identifier (_id value) of the document to be written or that already exists and matches the one being written.

Return type:

bson.objectid.ObjectId

add_ops_by_collection(other_ops)#

Merge the information from another WriteOpsByCollection into this one.

Parameters:

other_ops (WriteOpsByCollection) – The dictionary of write operations to merge in.

Return type:

None

clear() None.  Remove all items from D.#
copy() a shallow copy of D#
execute(mongodb)#

Execute all of the “queued” operations within this dictionary on a MongoDB instance.

Note that mongodb should be the same as the mongodb given to any add_one_op() and add_gridfs_put_op() method calls. The session given at the initialization of this object is used for these write operations. On exit, this dictionary is empty, indicating there are no more queued operations.

Parameters:

mongodb (pymongo.database.Database) – The MongoDB instance to execute write operations on.

Return type:

None

fromkeys(value=None, /)#

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)#

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items#
keys() a set-like object providing a view on D's keys#
pop(k[, d]) v, remove specified key and return the corresponding value.#

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()#

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

setdefault(key, default=None, /)#

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.#

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values#