Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common_helper_mongo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-gitdep
common_helper_mongo
Commits
7028adaf
Commit
7028adaf
authored
7 years ago
by
Peter Weidenbach
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleanup and license change
parent
09dd4b3e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
49 additions
and
44 deletions
+49
-44
.gitignore
.gitignore
+1
-0
LICENSE
LICENSE
+0
-0
__init__.py
common_helper_mongo/__init__.py
+8
-8
aggregate.py
common_helper_mongo/aggregate.py
+29
-29
pytest.ini
pytest.ini
+4
-0
setup.py
setup.py
+7
-7
No files found.
.gitignore
View file @
7028adaf
...
...
@@ -9,3 +9,4 @@ __pycache__/
*~
.project
.pydevproject
.cache/
This diff is collapsed.
Click to expand it.
LICENSE
View file @
7028adaf
This diff is collapsed.
Click to expand it.
common_helper_mongo/__init__.py
View file @
7028adaf
...
...
@@ -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
__all__
=
[
'overwrite_file'
,
'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'
]
'overwrite_file'
,
'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'
]
This diff is collapsed.
Click to expand it.
common_helper_mongo/aggregate.py
View file @
7028adaf
...
...
@@ -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
:
...
...
This diff is collapsed.
Click to expand it.
pytest.ini
0 → 100644
View file @
7028adaf
[pytest]
addopts
=
--pep8 -v
pep8ignore
=
*.py E501
This diff is collapsed.
Click to expand it.
setup.py
View file @
7028adaf
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'
)
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment