Commit 7028adaf by Peter Weidenbach

cleanup and license change

parent 09dd4b3e
......@@ -9,3 +9,4 @@ __pycache__/
*~
.project
.pydevproject
.cache/
......@@ -9,4 +9,4 @@ __all__ = [
'get_field_execute_operation',
'get_list_of_all_values',
'get_list_of_all_values_and_collect_information_of_additional_field'
]
]
......@@ -3,7 +3,7 @@ from bson.son import SON
def get_list_of_all_values(collection, object_path, unwind=False, match=None):
"""
'''
Get a list of unique values on a specific object path in a collection.
An Optional search string (match) can be added.
......@@ -16,17 +16,17 @@ def get_list_of_all_values(collection, object_path, unwind=False, match=None):
:param match: mongo search string
:type match: dict
:return: list
"""
'''
pipeline = []
if match is not None:
pipeline.append({"$match": match})
pipeline.append({'$match': match})
pipeline.extend([
{"$group": {"_id": object_path}},
{"$sort": SON([("_id", 1)])}
{'$group': {'_id': object_path}},
{'$sort': SON([('_id', 1)])}
])
if unwind:
old_pipe = pipeline
pipeline = [{"$unwind": object_path}]
pipeline = [{'$unwind': object_path}]
pipeline.extend(old_pipe)
result = _get_list_of_aggregate_list(list(collection.aggregate(pipeline)))
logging.debug(result)
......@@ -34,7 +34,7 @@ def get_list_of_all_values(collection, object_path, unwind=False, match=None):
def get_list_of_all_values_and_collect_information_of_additional_field(collection, object_path, additional_information_object_path, unwind=False, match=None):
"""
'''
Get a list of unique values and a collection of additional information on a specific object path in a collection.
An Optional search string (match) can be added.
......@@ -49,17 +49,17 @@ def get_list_of_all_values_and_collect_information_of_additional_field(collectio
:param match: mongo search string
:type match: dict
:return: {<VALUE>:[<ADDITIONAL_INFORMATION_1>, ...], ...}
"""
'''
pipeline = []
if match is not None:
pipeline.append({"$match": match})
pipeline.append({'$match': match})
pipeline.extend([
{"$group": {"_id": object_path, "additional_information": {"$addToSet": "$_id"}}},
{"$sort": SON([("_id", 1)])}
{'$group': {'_id': object_path, 'additional_information': {'$addToSet': '$_id'}}},
{'$sort': SON([('_id', 1)])}
])
if unwind:
old_pipe = pipeline
pipeline = [{"$unwind": object_path}]
pipeline = [{'$unwind': object_path}]
pipeline.extend(old_pipe)
result = list(collection.aggregate(pipeline))
result = _get_dict_from_aggregat_list(result)
......@@ -82,7 +82,7 @@ def _get_list_of_aggregate_list(ag_list):
def get_objects_and_count_of_occurrence(collection, object_path, unwind=False, match=None):
"""
'''
Get a list of unique values and their occurences on a specific object path in a collection.
An Optional search string (match) can be added.
......@@ -95,17 +95,17 @@ def get_objects_and_count_of_occurrence(collection, object_path, unwind=False, m
:param match: mongo search string
:type match: dict
:return: [{'_id': <VALUE>, 'count': <OCCURENCES>}, ...]
"""
'''
pipeline = []
if match is not None:
pipeline.append({"$match": match})
pipeline.append({'$match': match})
pipeline.extend([
{"$group": {"_id": object_path, "count": {"$sum": 1}}},
{"$sort": SON([("count", -1), ("_id", -1)])}
{'$group': {'_id': object_path, 'count': {'$sum': 1}}},
{'$sort': SON([('count', -1), ('_id', -1)])}
])
if unwind:
old_pipe = pipeline
pipeline = [{"$unwind": object_path}]
pipeline = [{'$unwind': object_path}]
pipeline.extend(old_pipe)
result = list(collection.aggregate(pipeline))
logging.debug(result)
......@@ -113,7 +113,7 @@ def get_objects_and_count_of_occurrence(collection, object_path, unwind=False, m
def get_field_sum(collection, object_path, match=None):
"""
'''
Get sum of all values in this field
An Optional search string (match) can be added.
......@@ -124,12 +124,12 @@ def get_field_sum(collection, object_path, match=None):
:param match: mongo search string
:type match: dict
:return: int
"""
return get_field_execute_operation("$sum", collection, object_path, match=match)
'''
return get_field_execute_operation('$sum', collection, object_path, match=match)
def get_field_average(collection, object_path, match=None):
"""
'''
Get average of all values in this field
An Optional search string (match) can be added.
......@@ -140,15 +140,15 @@ def get_field_average(collection, object_path, match=None):
:param match: mongo search string
:type match: dict
:return: float
"""
return get_field_execute_operation("$avg", collection, object_path, match=match)
'''
return get_field_execute_operation('$avg', collection, object_path, match=match)
def get_field_execute_operation(operation, collection, object_path, match=None):
pipeline = []
if match is not None:
pipeline.append({"$match": match})
pipeline.append({"$group": {"_id": "null", "total": {operation: object_path}}})
pipeline.append({'$match': match})
pipeline.append({'$group': {'_id': 'null', 'total': {operation: object_path}}})
tmp = collection.aggregate(pipeline)
result = 0
for item in tmp:
......
[pytest]
addopts = --pep8 -v
pep8ignore =
*.py E501
from setuptools import setup, find_packages
VERSION = "0.3.2"
VERSION = '0.3.3'
setup(
name="common_helper_mongo",
name='common_helper_mongo',
version=VERSION,
packages=find_packages(),
install_requires=[
'pymongo >= 3.2'
],
description="MongoDB helper functions",
author="Fraunhofer FKIE",
author_email="peter.weidenbach@fkie.fraunhofer.de",
url="http://www.fkie.fraunhofer.de",
license="MIT License"
description='MongoDB helper functions',
author='Fraunhofer FKIE',
author_email='peter.weidenbach@fkie.fraunhofer.de',
url='http://www.fkie.fraunhofer.de',
license='GPL-3.0'
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment