cervmongo package

package contents

cervmongo.get_client()[source]

returns SyncIOClient class

Return type

SyncIOClient

cervmongo.get_doc()[source]

returns SyncIODoc class

Return type

SyncIODoc

cervmongo.get_async_client()[source]

returns AsyncIOClient class

Return type

AsyncIOClient

cervmongo.get_async_doc()[source]

returns AsyncIODoc class

Return type

AsyncIODoc

cervmongo.quick_load_client(host=None, port=None, database='test_db', collection='test_col', replica_set=None, async_=False)[source]

Returns CollectionClient instance

Parameters
  • host (Optional[str]) –

  • port (Optional[int]) –

  • database (str) –

  • collection (str) –

  • replica_set (Optional[str]) –

  • async_ (bool) –

cervmongo.get_config()[source]

returns the Config class to set MongoDB configuration

Return type

Config

cervmongo.config submodule

class cervmongo.config.Config[source]

Bases: object

MongoDB and cervmongo settings, loaded initially by environmental variables

MONGO_DB: Optional[str] = None

The MongoDB database to use

MONGO_HOST: str = '127.0.0.1'

The host server for MongoDB

MONGO_PORT: int = 27017

The port for connection to host server

MONGO_REPLICA_SET: Optional[str] = None

The name of the replica set, if any

MONGO_MAX_POOL_SIZE: int = 20
MONGO_MIN_POOL_SIZE: int = 10
MONGO_USER: Optional[str] = None

The username for MongoDB connection

MONGO_PASSWORD: Optional[str] = None

The password for MongoDB connection

MONGO_URI: Optional[str] = 'mongodb://127.0.0.1:27017/None'

The MongoDB URI that will be used when accessing clients

DEBUG_LEVEL: int = 30

The level at which to display information, defaults to logging.warning

JSON_SAMPLE_PATH: str = './'

For use with JSON sample records to simplify schema process of new documents

JSON_SCHEMA_PATH: str = './'

For use with JSON schema documents for validating new documents on creation

LIST_MODE: bool = False

Only implemented in SyncIOClient. Instructs results to convert any Cursor to list instantly, ensuring no partially-filled cursors remain open

classmethod reset()[source]

resets config values to the first values assigned when cervmongo was imported

Return type

None

classmethod set_debug_level(debug_level)[source]

sets debug level of application, default is logging.warning

Parameters

debug_level (int) –

Return type

~Config

classmethod enable_list_mode()[source]

sets cls.LIST_MODE to True; use corresponding function if SyncIOClient instantiated.

Return type

~Config

classmethod disable_list_mode()[source]

sets cls.LIST_MODE to False; use corresponding function if SyncIOClient instantiated.

Return type

~Config

classmethod get_list_mode()[source]

returns cls.LIST_MODE value

Return type

bool

classmethod set_mongo_db(database_name)[source]

assigns the MONGO_DB var in config and regenerates mongodb uri

returns Class

Parameters
  • cls (Type[~Config]) –

  • database_name (str) –

Return type

~Config

classmethod set_mongo_host(host)[source]

assigns the MONGO_HOST var in config and regenerates mongodb uri

returns Class

Parameters
  • cls (Type[~Config]) –

  • host (str) –

Return type

~Config

classmethod set_mongo_port(port)[source]

assigns the MONGO_PORT var in config and regenerates mongodb uri

returns Class

Parameters
  • cls (Type[~Config]) –

  • port (int) –

Return type

~Config

classmethod set_mongo_replica_set(replica_set=None)[source]

assigns the MONGO_REPLICA_SET var in config and regenerates mongodb uri

returns Class

Parameters

cls (Type[~Config]) –

Return type

~Config

classmethod generate_mongo_uri()[source]

uses config class values to generate mongodb uri and returns connection string

Return type

NewType()(MongoDB URI, str)

classmethod reload()[source]

Reloads config class values from environment, falls back to current values if none found

Return type

None

classmethod reload_from_file(env_path='.env', override=False)[source]

Re-assign class variables using .env config file

Parameters
  • env_path (str) –

  • override (bool) –

Return type

None

classmethod reload_from_stream(stream, override=False)[source]

Re-assign class variables using stream in .env config format

Parameters
  • stream (IO) –

  • override (bool) –

Return type

None

cervmongo.main submodule

cervmongo.main.SUPPORT_GRIDFS = True

True if gridfs functionality is available else False

cervmongo.main.get_client()[source]

returns SyncIOClient class

Return type

SyncIOClient

cervmongo.main.get_doc()[source]

returns SyncIODoc class

Return type

SyncIODoc

class cervmongo.main.SyncIOClient(mongo_uri=None, default_collection=None, **kwargs)[source]

Bases: pymongo.mongo_client.MongoClient

High-level MongoClient subclass with additional methods added for ease-of-use, having some automated conveniences and defaults.

Parameters
  • mongo_uri (Optional[str]) –

  • default_collection (Optional[str]) –

enable_list_mode()[source]

enables _LIST_MODE

Return type

None

disable_list_mode()[source]

disables _LIST_MODE

Return type

None

get_list_mode()[source]

returns value of _LIST_MODE

Return type

None

set_database(database)[source]

is used to change or set database of client instance, also changes db in Config class

Parameters

database (str) –

Return type

None

COLLECTION(collection)[source]

returns unique CollectionClient class instance

Be aware, CollectionClient is NOT a valid MongoClient. To access the original SyncIOClient instance, use method get_client of CollectionClient instance.

Parameters

collection (str) –

PAGINATED_QUERY(collection, limit=20, sort=<Pagination Sort Fields._id: '_id'>, after=None, before=None, page=None, endpoint='/', ordering=-1, query={}, **kwargs)[source]

Returns paginated results of cursor from the collection query.

Available pagination methods:
  • Cursor-based (default)
    • after

    • before

    • limit (results per page, default 20)

  • Time-based (a datetime field must be selected)
    • sort (set to datetime field)

    • after (records after this time)

    • before (records before this time)

    • limit (results per page, default 20)

  • Offset-based (not recommended)
    • limit (results per page, default 20)

    • page

Parameters
  • collection (Optional[str]) –

  • limit (int) –

  • sort (Pagination Sort Fields) –

  • after (Optional[str]) –

  • before (Optional[str]) –

  • page (Optional[int]) –

  • endpoint (str) –

  • ordering (int) –

  • query (dict) –

GENERATE_ID(_id=None)[source]

returns a unique ObjectID, simply for convenience

Parameters

_id (Optional[str]) –

Return type

NewType()(Document ID, ObjectId)

DELETE(collection, record_or_records, soft=False, one=False)[source]

deletes the requested document(s)

returns MongoDB response document

If soft=true, creates collection (‘deleted.{collection}’) and inserts the deleted document there. field ‘oid’ is guaranteed to equal the original document’s “_id”.

Parameters
  • collection (Optional[str]) –

  • soft (bool) –

  • one (bool) –

Return type

Union[MongoDictResponse, MongoListResponse]

INDEX(collection, key='_id', sort=1, unique=False, reindex=False)[source]

creates an index, however most useful in constraining certain fields as unique

Parameters
  • key (Union[List, str]) –

  • sort (Union[List, int]) –

  • unique (bool) –

  • reindex (bool) –

Return type

None

ADD_FIELD(collection, field, value=None, data=False, query={})[source]

adds field with value provided to all records in collection that match query

kwarg ‘data’, if provided, is an existing field in the record that can be used as the default value. useful for name changes of fields in schema.

Parameters
  • field (str) –

  • value (Union[Dict, List, str, int, float, None]) –

  • query (dict) –

Return type

None

REMOVE_FIELD(collection, field, query={})[source]

removes field of all records in collection that match query

useful for complete field removal, once field is no longer needed.

Parameters
  • field (str) –

  • query (dict) –

Return type

None

UPLOAD(fileobj, filename=None, content_type=None, extension=None, **kwargs)[source]

returns GridFS response document after successful upload

fileobj can be either valid filepath or file/file-like object, all other kwargs are stored as meta data

Parameters
  • fileobj (Union[IO, str]) –

  • filename (Optional[str]) –

  • content_type (Optional[str]) –

  • extension (Optional[str]) –

ERASE(filename_or_id, revision=- 1)[source]

deletes the GridFS file. if multiple revisions, deletes most recent by default.

filename_or_id can be the _id field value or the filename value.

Parameters
  • filename_or_id (Union[str, NewType()(Document ID, ObjectId)]) –

  • revision (int) –

Return type

None

DOWNLOAD(filename_or_id=None, revision=- 1, skip=None, limit=None, sort=- 1, **query)[source]

returns download stream of file if filename_or_id is provided, else returns a cursor of files matching query

Parameters
  • filename_or_id (Union[str, NewType()(Document ID, ObjectId), None]) –

  • revision (int) –

  • skip (Optional[int]) –

  • limit (Optional[int]) –

  • sort (int) –

GET(collection, id_or_query={}, sort=1, key='_id', count=None, search=None, fields=None, page=None, perpage=False, limit=None, after=None, before=None, empty=None, distinct=None, one=False, **kwargs)[source]

record can be either _id (accepts unicode form of ObjectId, as well as extended JSON bson format) or query

Unless certain kwargs are used (count, distinct, one), behaviour is as follows:

  • if _id is recognized, one is set to true and will return the exact matching document

  • if query is recognized, will return MongoList response of cursor

  • if count is provided and _id is not recognized, returns number of documents in cursor

  • if distinct is provided, returns a unique list of the field values (accepts dot notation)

  • if one is provided, returns the first matching document of cursor

kwargs count, distinct, one cannot be used together, priority is as follows if all are provided:

  1. count

  2. distinct

  3. one

Parameters
  • id_or_query (Union[NewType()(Document ID, ObjectId), Dict, str]) –

  • sort (int) –

  • key (str) –

  • count (Optional[bool]) –

  • search (Optional[str]) –

  • fields (Union[List, dict, None]) –

  • page (Optional[int]) –

  • perpage (int) –

  • limit (Optional[int]) –

  • after (Optional[str]) –

  • before (Optional[str]) –

  • distinct (Optional[str]) –

  • one (bool) –

SEARCH(collection, search, **kwargs)[source]

returns the results of querying textIndex of collection

Parameters

search (str) –

POST(collection, record_or_records)[source]

creates new record(s) and returns MongoDB response document

Parameters

record_or_records (Union[List, Dict]) –

PUT(collection, record_or_records)[source]

creates or replaces record(s) with exact _id provided, _id is required with record object(s)

returns original document, if replaced

Parameters

record_or_records (Union[List, Dict]) –

REPLACE(collection, original, replacement, upsert=False)[source]
Parameters

upsert (bool) –

PATCH(collection, id_or_query, updates, upsert=False, w=1)[source]
Parameters
  • id_or_query (Union[NewType()(Document ID, ObjectId), Dict, List, str]) –

  • updates (Union[Dict, List]) –

  • upsert (bool) –

  • w (int) –

class cervmongo.main.SyncIODoc(_id=None, doc_type=None, doc_sample=None, doc_schema=None, doc_id=None, mongo_uri=None, **kwargs)[source]

Bases: cervmongo.main.SyncIOClient

Custom MongoClient subclass with customizations for creating standardized documents and adding json schema validation.

Parameters
  • doc_type (Optional[str]) –

  • doc_sample (Union[Dict, str, None]) –

  • doc_schema (Union[Dict, str, None]) –

  • doc_id (Optional[str]) –

  • mongo_uri (Optional[str]) –

load(_id=None)[source]
view(_id=False)[source]
reload()[source]
id()[source]
create(save=False, trigger=None, template='{total}', query={}, **kwargs)[source]
Parameters
  • save (bool) –

  • template (str) –

  • query (dict) –

push(**kwargs)[source]
pull(**kwargs)[source]
increment(query={}, **kwargs)[source]
Parameters

query (dict) –

update(query={}, **kwargs)[source]
Parameters

query (dict) –

patch(save=False, trigger=None, **kwargs)[source]
save(trigger=None)[source]
close()[source]

Cleanup client resources and disconnect from MongoDB.

On MongoDB >= 3.6, end all server sessions created by this client by sending one or more endSessions commands.

Close all sockets in the connection pools and stop the monitor threads. If this instance is used again it will be automatically re-opened and the threads restarted unless auto encryption is enabled. A client enabled with auto encryption cannot be used again after being closed; any attempt will raise InvalidOperation.

Changed in version 3.6: End all server sessions created by this client.

add_relation(key, *args, **kwargs)[source]
add_timestamp(key, value=None, relation={})[source]
add_object(field, object_name=None, key=None, value=None, **kwargs)[source]
class cervmongo.main.SyncIOClient.COLLECTION.CollectionClient

Convenience class that is instantiated and supplied by the COLLECTION method. It auto-supplies collection to all upper-cased SyncIOClient methods, where required [source]

get_client()
GENERATE_ID(_id=None)
COLLECTION(collection)
UPLOAD(fileobj, filename=None, content_type=None, extension=None, **kwargs)
DOWNLOAD(filename_or_id=None, revision=- 1, skip=None, limit=None, sort=- 1, **query)
ERASE(filename_or_id, revision=- 1)
INDEX(key='_id', sort=1, unique=False, reindex=False)
ADD_FIELD(field, value='', data=False, query={})
REMOVE_FIELD(field, query={})
DELETE(record_or_records, soft=False)
GET(id_or_query={}, sort=1, key='_id', count=None, search=None, fields=None, page=None, perpage=False, limit=None, after=None, before=None, empty=None, distinct=None, one=False, **kwargs)
POST(record_or_records)
PUT(record_or_records)
PATCH(original, updates, w=1, upsert=False, multi=False, log=None)
REPLACE(original, replacement, upsert=False)
SEARCH(search, **kwargs)
PAGINATED_QUERY(limit=20, sort=<Pagination Sort Fields._id: "_id">, after=None, before=None, page=None, endpoint='/', ordering=-1, query={}, **kwargs)

cervmongo.aio submodule

cervmongo.aio.SUPPORT_ASYNCIO_CLIENT = True

True if motor package is installed else False

cervmongo.aio.SUPPORT_ASYNCIO_BUCKET = True

True if motor package is installed else False

cervmongo.aio.get_async_client()[source]

returns AsyncIOClient class

Return type

AsyncIOClient

cervmongo.aio.get_async_doc()[source]

returns AsyncIODoc class

Return type

AsyncIODoc

class cervmongo.aio.AsyncIOClient(mongo_uri=None, default_collection=None, **kwargs)[source]

Bases: motor.motor_asyncio.AsyncIOMotorClient

High-level AsyncIOMotorClient subclass with additional methods added for ease-of-use, having some automated conveniences and defaults.

set_database(database)[source]
COLLECTION(collection)[source]
Parameters

collection (str) –

async PAGINATED_QUERY(collection, limit=20, sort=<Pagination Sort Fields._id: '_id'>, after=None, before=None, page=None, endpoint='/', ordering=-1, query={}, **kwargs)[source]

Returns paginated results of collection w/ query.

Available pagination methods:
  • Cursor-based (default)
    • after

    • before

    • limit (results per page, default 20)

  • Time-based (a datetime field must be selected)
    • sort (set to datetime field)

    • after (records after this time)

    • before (records before this time)

    • limit (results per page, default 20)

  • Offset-based (not recommended)
    • limit (results per page, default 20)

    • page

Parameters
  • limit (int) –

  • sort (Pagination Sort Fields) –

  • after (Optional[str]) –

  • before (Optional[str]) –

  • page (Optional[int]) –

  • endpoint (str) –

  • ordering (int) –

  • query (dict) –

GENERATE_ID(_id=None)[source]
async UPLOAD(fileobj, filename=None, content_type=None, extension=None, **kwargs)[source]
Parameters
  • filename (Optional[str]) –

  • content_type (Optional[str]) –

  • extension (Optional[str]) –

async ERASE(filename_or_id, revision=- 1)[source]
Parameters

revision (int) –

async DOWNLOAD(filename_or_id=None, revision=- 1, skip=None, limit=None, sort=- 1, **query)[source]
Parameters
  • revision (int) –

  • skip (Optional[int]) –

  • limit (Optional[int]) –

  • sort (int) –

async DELETE(collection, record, soft=False, one=False)[source]
Parameters
  • soft (bool) –

  • one (bool) –

INDEX(collection, key='_id', sort=1, unique=False, reindex=False)[source]
Parameters
  • key (str) –

  • sort (int) –

  • unique (bool) –

  • reindex (bool) –

async ADD_FIELD(collection, field, value='', data=False, query={})[source]
Parameters
  • field (str) –

  • value (Union[Dict, List, str, int, float]) –

  • query (dict) –

async REMOVE_FIELD(collection, field, query={})[source]
Parameters
  • field (str) –

  • query (dict) –

Return type

None

async GET(collection, id_or_query={}, sort=1, key='_id', count=None, search=None, fields=None, page=None, perpage=False, limit=None, after=None, before=None, empty=None, distinct=None, one=False, **kwargs)[source]
Parameters
  • id_or_query (Union[NewType()(Document ID, ObjectId), str, Dict]) –

  • sort (int) –

  • key (str) –

  • count (Optional[bool]) –

  • search (Optional[str]) –

  • fields (Optional[dict]) –

  • page (Optional[int]) –

  • perpage (int) –

  • limit (Optional[int]) –

  • after (Optional[str]) –

  • before (Optional[str]) –

  • distinct (Optional[str]) –

  • one (bool) –

async SEARCH(collection, search, **kwargs)[source]
Parameters

search (str) –

async POST(collection, record_or_records)[source]
Parameters

record_or_records (Union[List, Dict]) –

async PUT(collection, record_or_records)[source]

creates or replaces record(s) with exact _id provided, _id is required with record object(s)

returns original document, if replaced

Parameters

record_or_records (Union[List, Dict]) –

async REPLACE(collection, original, replacement, upsert=False)[source]
Parameters

replacement (dict) –

async PATCH(collection, id_or_query, updates, upsert=False, w=1)[source]
Parameters
  • id_or_query (Union[NewType()(Document ID, ObjectId), Dict, List, str]) –

  • updates (Union[Dict, List]) –

  • upsert (bool) –

  • w (int) –

class cervmongo.aio.AsyncIODoc(_id=None, doc_type=None, doc_sample=None, doc_schema=None, doc_id=None, mongo_uri=None, **kwargs)[source]

Bases: cervmongo.aio.AsyncIOClient

Custom MongoClient subclass with customizations for creating standardized documents and adding json schema validation.

Parameters
  • doc_type (Optional[str]) –

  • doc_sample (Union[Dict, str, None]) –

  • doc_schema (Union[Dict, str, None]) –

  • doc_id (Optional[str]) –

  • mongo_uri (Optional[str]) –

async load(_id=None)[source]
async view(_id=False)[source]
async reload()[source]
id()[source]
async create(save=False, trigger=None, template='{total}', query={}, **kwargs)[source]
Parameters
  • save (bool) –

  • template (str) –

  • query (dict) –

async add_enum(value)[source]
Parameters

name (str) –

async push(**kwargs)[source]
async pull(**kwargs)[source]
async increment(query={}, **kwargs)[source]
Parameters

query (dict) –

async update(query={}, **kwargs)[source]
Parameters

query (dict) –

async patch(save=False, trigger=None, **kwargs)[source]
Parameters

save (bool) –

async save(trigger=None)[source]
async close()[source]

Cleanup client resources and disconnect from MongoDB.

On MongoDB >= 3.6, end all server sessions created by this client by sending one or more endSessions commands.

Close all sockets in the connection pools and stop the monitor threads. If this instance is used again it will be automatically re-opened and the threads restarted unless auto encryption is enabled. A client enabled with auto encryption cannot be used again after being closed; any attempt will raise InvalidOperation.

Changed in version 3.6: End all server sessions created by this client.

async add_relation(key, *args, **kwargs)[source]
Parameters

key (str) –

async add_timestamp(key, value=None, relation={})[source]
Parameters
  • key (str) –

  • relation (dict) –

async add_object(field, object_name=None, key=None, value=None, **kwargs)[source]
Parameters
  • field (str) –

  • object_name (Optional[str]) –

  • key (Optional[str]) –

class cervmongo.aio.AsyncIOClient.COLLECTION.CollectionClient

Convenience class that is instantiated and supplied by the COLLECTION method. It auto-supplies collection to all upper-cased AsyncIOClient methods, where required [source]

get_client()
GENERATE_ID(_id=None)
COLLECTION(collection)
async UPLOAD(fileobj, filename=None, content_type=None, extension=None, **kwargs)
async DOWNLOAD(filename_or_id=None, revision=- 1, skip=None, limit=None, sort=- 1, **query)
async ERASE(filename_or_id, revision=- 1)
INDEX(key='_id', sort=1, unique=False, reindex=False)
async ADD_FIELD(field, value='', data=False, query={})
async REMOVE_FIELD(field, query={})
async DELETE(record_or_records, soft=False)
async GET(id_or_query={}, sort=1, key='_id', count=None, search=None, fields=None, page=None, perpage=False, limit=None, after=None, before=None, empty=None, distinct=None, one=False, **kwargs)
async POST(record_or_records)
async PUT(record_or_records)
async PATCH(original, updates, w=1, upsert=False, multi=False, log=None)
async REPLACE(original, replacement, upsert=False)
async SEARCH(search, **kwargs)
async PAGINATED_QUERY(limit=20, sort=<Pagination Sort Fields._id: "_id">, after=None, before=None, page=None, endpoint='/', ordering=-1, query={}, **kwargs)

cervmongo.models submodule

class cervmongo.models.DefaultModel[source]

Bases: object

Intended for use as a base class for externally-facing models. Any models that inherit from this class will: * accept fields using snake_case or camelCase keys * use camelCase keys in the generated OpenAPI spec (when supported) * create an orm_mode Config attrib and have it on by default

  • from fastapi-utils

class Config[source]

Bases: object

orm_mode = True
allow_population_by_field_name = True
alias_generator(*, start_lower: bool = True) → str

Converts a snake_case string to camelCase. The start_lower argument determines whether the first letter in the generated camelcase should be lowercase (if start_lower is True), or capitalized (if start_lower is False).

class cervmongo.models.MetaConfig[source]

Bases: type

class cervmongo.models.GenericResponse(*args, **kwargs)[source]

Bases: cervmongo.models._GenericResponse

a premade web API friendly response object

class cervmongo.models.StandardResponse(*args, **kwargs)[source]

Bases: cervmongo.models._StandardResponse

a premade web API friendly response object

data: Union[Dict, List, str, int, float]
class cervmongo.models.YAMLStandardResponse(*args, **kwargs)[source]

Bases: cervmongo.models._StandardResponse

a premade web API friendly response object

data: Union[Dict, List, str, int, float]
class cervmongo.models.MongoDictResponse[source]

Bases: dict

the normal response for a single document when using cervmongo.main.SyncIOClient or cervmongo.aio.AsyncIOClient

class cervmongo.models.MongoListResponse(cursor=[], _original=None)[source]

Bases: list

the normal response for multiple documents when using cervmongo.main.SyncIOClient or cervmongo.aio.AsyncIOClient

get()[source]

returns cursor or list instance

Return type

Union[Cursor, List]

close()[source]

closes cursor or clears list

Return type

None

rewind()[source]

rewinds cursor, if any

Return type

None

flatten()[source]

returns a MongoListResponse containing an array of flattened records, saving record of original

Return type

List[Dict]

original()[source]

returns last (original) MongoListResponse if available, else self

distinct(field='_id')[source]

returns list of distinct values based on field, defaults to “_id”. supports dot notation for nested values.

Parameters

field (str) –

Return type

List[Any]

count()[source]

returns the count of the number of records in cursor or list results

Return type

int

sort(sort=1, key='_id')[source]

sorts cursor results or list results, default is cervmongo.ASC (int 1)

Options:
  • cervmongo.ASC or 1

  • cervmongo.DESC or -1

Parameters
  • sort (int) –

  • key (str) –

Return type

self

list()[source]

returns a new list representation of the current cursor

Return type

List[Dict]

cervmongo.utils submodule

cervmongo.utils.detect_mimetype(filename)[source]
Return type

str

cervmongo.utils.flatten_dict(dictionary)[source]
Parameters

dictionary (dict) –

Return type

dict

cervmongo.utils.file_and_fileobj(fileobj)[source]
cervmongo.utils.get_file_meta_information(fileobj, filename=None, content_type=None, extension=None, **kwargs)[source]
Parameters
  • filename (Optional[str]) –

  • content_type (Optional[str]) –

  • extension (Optional[str]) –

Return type

dict

cervmongo.utils.dict_to_query(dictionary)[source]
Parameters

dictionary (dict) –

Return type

str

cervmongo.utils.sort_list(item, field)[source]
Parameters

field (str) –

cervmongo.utils.getenv_boolean(var_name, default_value=False)[source]
cervmongo.utils.current_datetime(alt=False)[source]

Returns current datetime object by default. Accepts alternate format for string format result.

Parameters

alt (str) –

Return type

datetime

cervmongo.utils.current_date(alt=False)[source]

Returns current date object by default. Accepts alternate format for string format result.

Parameters

alt (str) –

Return type

date

cervmongo.utils.clean_kwargs(*, ONLY=[], kwargs={})[source]

Allows for sanitization of keyword args before passing to another function

Parameters
  • ONLY (list) –

  • kwargs (dict) –

Return type

dict

cervmongo.utils.parse_string_header(string)[source]

For use when parsing nested data from tabular data formats, such as spreadsheets

Parameters

string (str) –

Return type

str

cervmongo.utils.format_string_for_id(string)[source]

Cleans string to allow for functional, readable, and permissible MongoDB ID

Parameters

string (str) –

Return type

str

cervmongo.utils.return_value_from_dict(dictionary, key, if_not=' ')[source]
Parameters
  • dictionary (dict) –

  • key (str) –

  • if_not (str) –

cervmongo.utils.snake2camel(snake, start_lower=False)[source]

Converts a snake_case string to camelCase. The start_lower argument determines whether the first letter in the generated camelcase should be lowercase (if start_lower is True), or capitalized (if start_lower is False).

Parameters
  • snake (str) –

  • start_lower (bool) –

Return type

str

cervmongo.utils.camel2snake(camel)[source]

Converts a camelCase string to snake_case.

Parameters

camel (str) –

Return type

str

cervmongo.utils.objectid_representer(dumper, data)[source]
cervmongo.utils.objectid_constructor(loader, data)[source]
cervmongo.utils.generate_new_id()[source]
Return type

str

cervmongo.utils.ensure_enums_to_strs(items)[source]
Parameters

items (Union[Sequence[Union[Enum, str]], Type[Enum]]) –

cervmongo.utils.yaml_dump(data)[source]
Parameters

data (dict) –

Return type

str

cervmongo.utils.yaml_load(data, _file=False)[source]
Parameters

_file (bool) –

Return type

dict

cervmongo.utils.json_dump(data, pretty=False)[source]
Parameters
  • data (dict) –

  • pretty (bool) –

Return type

str

cervmongo.utils.json_load(data)[source]
Parameters

data (str) –

Return type

dict

cervmongo.utils.clean_traceback()[source]
Return type

str

cervmongo.utils.silent_drop_kwarg(kwargs, key, reason='')[source]
Parameters
  • kwargs (dict) –

  • key (str) –

  • reason (str) –

cervmongo.utils.type_from_schema(schema_type)[source]

retrieves type function based on JSON sample inferred data schema type

Parameters

schema_type (str) –

cervmongo.utils.schema_from_dict(dictionary, additional={})[source]

creates a simple JSON schema from JSON sample document

Parameters
  • dictionary (dict) –

  • additional (dict) –

cervmongo.vars submodule

class cervmongo.vars.StringEnum(value)[source]

Bases: str, enum.Enum

An enumeration.

class cervmongo.vars.IntEnum(value)[source]

Bases: int, enum.Enum

An enumeration.

cervmongo.vars.PAGINATION_SORT_FIELDS

alias of cervmongo.vars.Pagination Sort Fields

class cervmongo.vars.ObjectIdStr[source]

Bases: str

classmethod validate(v)[source]
cervmongo.vars.str2bool(v)[source]
cervmongo.vars.str2datetime(v)[source]