Commit 7028adaf by Peter Weidenbach

cleanup and license change

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