cgnal.core.data.layer.mongo package

Submodules

cgnal.core.data.layer.mongo.archivers module

Module with abstraction for accessing to MongoDB persistent layers.

class cgnal.core.data.layer.mongo.archivers.MongoArchiver(collection: Union[pymongo.collection.Collection, MockCollection], dao: cgnal.core.data.layer.mongo.dao.MongoDAO)

Bases: cgnal.core.data.layer.Archiver[cgnal.core.typing.T]

Archiver based on MongoDB persistent layers.

Return an instance of the archiver to access and modify Mongodb collections via a DAO object.

Parameters
  • collection – A Mongodb collection

  • dao – An instance of cgnal.data.layer.mongo.dao.DocumentDao or cgnal.data.layer.mongo.dao.SeriesDAO that helps to retrieve/archive a document.

aggregate(pipeline: List[Dict[str, Dict[str, Any]]], allowDiskUse: bool = True) Iterator[cgnal.core.typing.T]

Aggregate collection’s documents using given aggregation steps.

Parameters
  • pipeline – a list of aggregation pipeline stages

  • allowDiskUse – Enables writing to temporary files. When set to True, aggregation stages can write data to the _tmp subdirectory of the –dbpath directory. The default is False.

Yield

iterator with parsed aggregated documents

archive(objs: Union[cgnal.core.typing.T, Iterable[cgnal.core.typing.T]]) Union[pymongo.results.UpdateResult, List[pymongo.results.UpdateResult]]

Archive one or more documents in collection.

Parameters

objs – documents to archive

Returns

list of instances of pymongo.results.UpdateResult with update operations’ results

archiveMany(objs: Iterable[cgnal.core.typing.T]) List[pymongo.results.UpdateResult]

Insert many documents in collection.

Parameters

objs – documents to archive

Returns

list of instances of pymongo.results.UpdateResult with update operations’ results

archiveOne(obj: cgnal.core.typing.T) pymongo.results.UpdateResult

Archive one document in collection.

Parameters

obj – document to archive

Returns

an instance of pymongo.results.UpdateResult with update operation’s results

first() cgnal.core.typing.T

Retrieve first element in collection.

Returns

parsed document

retrieve(condition: Dict[str, Dict[str, Any]] = {}, sort_by: Optional[Union[str, List[str]]] = None) Iterator[cgnal.core.typing.T]

Retrieve documents satisfying condition, sorted according to given ordering.

Parameters
  • condition – condition to satisfy. If {}, return all documents.

  • sort_by – ordering to respect. If None, no ordering is given.

Yield

iterator of (ordered) documents satisfying given condition

retrieveById(uuid: str) cgnal.core.typing.T

Retrive document from collection by id.

Parameters

uuid – document id

Returns

retrieved document parsed according to self.dao

cgnal.core.data.layer.mongo.dao module

Module with the implementation and abstraction for serializing/deserializing objects to/from MongoDB.

class cgnal.core.data.layer.mongo.dao.DocumentDAO(uuid: str = '_id')

Bases: cgnal.core.data.layer.mongo.dao.MongoDAO[cgnal.core.data.model.text.Document]

Base class for serializing/deserializing documents from/to MongoDB.

Serialize a Domain object into an instance of cgnal.data.model.text.Document.

Parameters

uuid – sets the name of the uuid field as str

computeKey(obj: cgnal.core.data.model.text.Document) Dict[str, bson.objectid.ObjectId]

Get document id as dictionary.

Parameters

obj – document whose id is to retrieve

Returns

dictionary with ‘_id’ key and ObjectId as value

conversion(d: Dict[str, Any]) Dict[str, Any]

Map dictionary keys according to the inverse of self.mapping.

Parameters

d – dict to map

Returns

dictionary with mapped keys

get(obj: cgnal.core.data.model.text.Document) Dict[str, Any]

Get a document as a dictionary.

Parameters

obj – a Document object to be transformed into a dictionary

Returns

document object as a dictionary

property inverse_mapping: Dict[str, Any]

Apply inverse of self.mapping.

Returns

dictionary with self.mapping.keys as values and self.mapping.values as keys

mapping: Dict[str, str] = {}
parse(json: Dict[str, Any]) cgnal.core.data.model.text.Document

Get a dictionary as a Document object.

Parameters

json – a json/dictionary to be parsed

Returns

an object of cgnal.data.model.text.Document

translate(d: Dict[str, Any]) Dict[str, Any]

Map dictionary keys according to self.mapping.

Parameters

d – dict to map

Returns

dictionary with mapped keys

class cgnal.core.data.layer.mongo.dao.MongoDAO(*args, **kwds)

Bases: cgnal.core.data.layer.DAO[cgnal.core.typing.T, dict]

Base Serialized/Deserializer for objects to/from Mongo.

class cgnal.core.data.layer.mongo.dao.SeriesDAO(key_field: str = '_id')

Bases: cgnal.core.data.layer.DAO, Generic[cgnal.core.data.layer.mongo.dao.K, cgnal.core.typing.T]

Base class for serializing/deserializing pandas Series from/to MongoDB.

Serialize a Domain object into an instance of :class: pd.Series.

Parameters

key_field – sets the name of the key_field field as str

computeKey(serie: pandas.core.series.Series) Dict[str, bson.objectid.ObjectId]

Get series id as a dictionary. The id is key_field.

Parameters

serie – the series whose id is to be retrieved

Returns

dictionary with self.key_field as key and name of the series as value

get(serie: pandas.core.series.Series) Dict[cgnal.core.data.layer.mongo.dao.K, cgnal.core.typing.T]

Get a series as a dictionary.

Parameters

serie – pd.Series

Returns

dictionary

parse(json: Dict[cgnal.core.data.layer.mongo.dao.K, cgnal.core.typing.T]) pandas.core.series.Series

Get a json as a pd.Series.

Parameters

json – dictionary

Returns

pd.Series

Module contents

Module for Mongo persistance layer.

class cgnal.core.data.layer.mongo.MongoConfig(config: cfg_load.Configuration)

Bases: cgnal.core.config.BaseConfig

Configuration for a Mongo DB.

Class instance initializer.

Parameters

config – configuration

property admin: cgnal.core.config.AuthConfig

Return administrator authentication config.

Returns

administrator authentication config

property auth: cgnal.core.config.AuthConfig

Return authetication config.

Returns

authetication config

property authSource: Any

Return the authentication source.

Returns

authentication source

property db_name: str

Return database name.

Returns

database name

getCollection(name) str

Return collection name at a given configuration node.

Parameters

name – configuration node name

Returns

collection name

property host: str

Return ost name.

Returns

host name

property port: int

Return port.

Returns

port