Skip to content

Commit

Permalink
Make pymongo optional again (#519)
Browse files Browse the repository at this point in the history
* Make pymongo optional again

* Add test for running without optional deps

* Add new tox env to travis

* Add cpuinfo back in
  • Loading branch information
JarnoRFB committed Jul 18, 2019
1 parent 29e14eb commit c59e9db
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ matrix:
env: TOX_ENV=tensorflow-1
- python: "3.6"
env: TOX_ENV=tensorflow-2
- python: "3.7"
env: TOX_ENV=setup
- python: "3.6"
env: TOX_ENV=flake8
- python: "3.6"
Expand Down
6 changes: 4 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ numpy==1.16.2
packaging>=18.0
pandas==0.24.2
pbr==1.10.0
py-cpuinfo==4.0
pymongo==3.4.0
# tests/test_utils.py depends on that pytest version is exactly 4.3.0
pytest==4.3.0
python-dateutil==2.6.0
pytz==2016.10
Expand All @@ -27,3 +26,6 @@ tinydb==3.2.1
tinydb-serialization==1.0.3
wrapt==1.10.8
scikit-learn==0.20.3
pymongo==3.8.0
py-cpuinfo==4.0

9 changes: 1 addition & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,5 @@ jsonpickle>=0.9.3
mock>=2.0.0
munch>=2.0.4
pbr>=1.10.0
py>=1.4.32
py-cpuinfo>=4.0
six>=1.10.0
wrapt>=1.10.8
packaging>=18.0
pymongo>=3.8.0

# tests/test_utils.py depends on that pytest version is exactly 4.3.0
pytest==4.3.0
packaging>=18.0
13 changes: 7 additions & 6 deletions sacred/observers/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import time
from tempfile import NamedTemporaryFile

import pymongo
import pymongo.errors
import gridfs

import sacred.optional as opt
from sacred.commandline_options import CommandLineOption
from sacred.dependencies import get_digest
Expand Down Expand Up @@ -81,6 +77,8 @@ def create(cls, url=None, db_name='sacred', collection='runs',
-------
An instantiated MongoObserver.
"""
import pymongo
import gridfs

if client is not None:
if not isinstance(client, pymongo.MongoClient):
Expand Down Expand Up @@ -266,6 +264,8 @@ def log_metrics(self, metrics_by_name, info):
.append({"name": key, "id": str(result.upserted_id)})

def insert(self):
import pymongo.errors

if self.overwrite:
return self.save()

Expand Down Expand Up @@ -318,8 +318,7 @@ def final_save(self, attempts):
"Most likely it is either the 'info' or the 'result'.",
file=sys.stderr)

if not os.path.exists(self.failure_dir):
os.makedirs(self.failure_dir)
os.makedirs(self.failure_dir, exist_ok=True)
with NamedTemporaryFile(suffix='.pickle', delete=False,
prefix='sacred_mongo_fail_{}_'.format(
self.run_entry["_id"]
Expand Down Expand Up @@ -448,6 +447,7 @@ def log_metrics(self, metric_name, metrics_values, info):
.append({"name": metric_name, "id": str(result.upserted_id)})

def save(self):
import pymongo
try:
self.runs.update_one({'_id': self.run_entry['_id']},
{'$set': self.run_entry})
Expand All @@ -456,6 +456,7 @@ def save(self):
'(most likely in the info)')

def final_save(self, attempts):
import pymongo
try:
self.runs.update_one({'_id': self.run_entry['_id']},
{'$set': self.run_entry}, upsert=True)
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
'py-cpuinfo>=4.0',
'colorama>=0.4',
'packaging>=18.0',
'pymongo>=3.8.0'
],
tests_require=[
'mock>=0.8, <3.0',
Expand Down
12 changes: 10 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = py35, py36, py37, flake8, tensorflow-1, tensorflow-2
envlist = py35, py36, py37, setup, flake8, tensorflow-1, tensorflow-2
# py32 does not work because of the 'wrapt' dependency
# py33 is not supported by newer versions of numpy
# py34 is not supported by newer versions of pandas
Expand Down Expand Up @@ -34,10 +34,18 @@ commands =
pytest tests/test_stflow \
{posargs}

[testenv:setup]
basepython = python
deps =
pytest==4.3.0
mock==2.0.0
commands =
pytest --ignore=tests/test_observers \
{posargs}

[testenv:flake8]
basepython = python
deps =
-rrequirements.txt
flake8
pep8-naming
mccabe
Expand Down

0 comments on commit c59e9db

Please sign in to comment.