-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from NickSebClark/feature/mongo-db
Mongo DB Support
- Loading branch information
Showing
19 changed files
with
435 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,3 +160,5 @@ cython_debug/ | |
#.idea/ | ||
|
||
.vscode/ | ||
|
||
credentials.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class Datastore: | ||
def get_parameter_set(self, id: str) -> dict: | ||
raise NotImplementedError | ||
|
||
def write_process_data(self, data: dict) -> str: | ||
raise NotImplementedError | ||
|
||
|
||
class InMemoryDataStore(Datastore): | ||
"""Datastore which stores parameter sets and process data in memory. Parameter sets are stored as a dict of dicts. | ||
The top-level dict key is the id of the parameter set to allow easy access. | ||
The process data is simply a list of dicts. | ||
""" | ||
|
||
def __init__(self, parameter_sets: list[dict], key: str = "id", re_index: bool = False): | ||
"""Initialises datastore with a list of parametersets. | ||
Args: | ||
parameter_sets (list[dict]): Parameter sets to initialise the data store with | ||
key (str, optional): key string. Defaults to "id". | ||
re_index (bool, optional): Reinitialises the key in the param sets to numbers from 0. Defaults to False. | ||
""" | ||
if re_index: | ||
self.parameter_sets = {str(index): parameter_set for (index, parameter_set) in enumerate(parameter_sets)} | ||
for id in self.parameter_sets: | ||
self.parameter_sets[id]["id"] = id | ||
else: | ||
self.parameter_sets = {parameter_set[key]: parameter_set for parameter_set in parameter_sets} | ||
|
||
self.process_data: list[dict] = [] | ||
self.key = key | ||
|
||
def get_parameter_set(self, id: str) -> dict: | ||
"""Get the parameter set from the dictionary of parameter sets | ||
Args: | ||
id (str): Id of parameter set to return. | ||
Returns: | ||
dict: Returned parameter set. | ||
""" | ||
return self.parameter_sets[id] | ||
|
||
def write_process_data(self, process_data: dict): | ||
"""Adds a process data dict to the store. | ||
Args: | ||
process_data (dict): Process data to add. | ||
""" | ||
self.process_data.append(process_data) | ||
|
||
def read_all_process_data(self) -> list[dict]: | ||
"""Gets all stored process data dicts. | ||
Returns: | ||
list[dict]: The process data in the datastore. | ||
""" | ||
return self.process_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from pyprexor_datastore.datastore import Datastore | ||
import pymongo | ||
|
||
|
||
class MongoDataStore(Datastore): | ||
"""Basic wrapper around pymongo calls to MongoDB for pyprexor.""" | ||
|
||
def __init__(self, uri: str, database: str = "pyprexor"): | ||
client: pymongo.MongoClient = pymongo.MongoClient(uri) | ||
|
||
# check we have a good connection | ||
with pymongo.timeout(0.5): | ||
client.admin.command("ping") | ||
|
||
self.db = client[database] | ||
|
||
def get_parameter_set(self, id: str) -> dict: | ||
"""Get a parameter set from the parameter_sets collection | ||
Args: | ||
id (str): Id of parameter set to get. | ||
Raises: | ||
KeyError: pymongo find returns none when no document is found. We want a KeyError in this case. | ||
Returns: | ||
dict: Parameter set | ||
""" | ||
ps = self.db.parameter_sets.find_one({"_id": id}) | ||
if ps: | ||
return ps | ||
else: | ||
raise KeyError(f"Parameter set id {id} not present") | ||
|
||
def write_process_data(self, data: dict) -> str: | ||
"""Write process data object to process_data collection. | ||
Args: | ||
data (dict): Data to write | ||
Returns: | ||
str: Inserted Id | ||
""" | ||
return self.db.process_data.insert_one(data).inserted_id | ||
|
||
def write_parameter_set(self, parameter_set: dict) -> str: | ||
"""Write parameter set to parameter_sets collection. | ||
Args: | ||
parameter_set (dict): Data to write | ||
Returns: | ||
str: Inserted id | ||
""" | ||
return self.db.parameter_sets.insert_one(parameter_set).inserted_id | ||
|
||
def read_all_process_data(self) -> list[dict]: | ||
"""Get all the documents present in the process_data collection. | ||
Returns: | ||
list[dict]: List of documents | ||
""" | ||
return [data for data in self.db.process_data.find()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ version = "0.0.0" | |
description = "" | ||
authors = ["_MircoWave_ <[email protected]>"] | ||
readme = "README.md" | ||
packages = [{include = "pyprexor"}] | ||
packages = [{include = "pyprexor"}, {include = "pyprexor_datastore"}] | ||
repository = "https://github.com/NickSebClark/pyprexor" | ||
|
||
[tool.poetry.dependencies] | ||
|
@@ -16,6 +16,11 @@ pytest = "^7.4.0" | |
ruff = "^0.0.285" | ||
mypy = "^1.5.1" | ||
coverage = "^7.3.0" | ||
mongomock = "^4.1.2" | ||
|
||
|
||
[tool.poetry.group.mongo.dependencies] | ||
pymongo = "^4.5.0" | ||
|
||
[tool.poetry-dynamic-versioning] | ||
enable = true | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import pyprexor.datastore as datastore | ||
import pyprexor_datastore.datastore as datastore | ||
import pytest | ||
|
||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pyprexor_datastore.datastore as datastore | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def default_data(): | ||
return [{"id": 2, "param": 8}, {"id": 3, "param": 9}, {"id": 4, "param": 10}] | ||
|
||
|
||
def test_init_re_index(default_data): | ||
"""Test a new InMemoryDatastore with the re_index option. Check the id of the first element is set to 0.""" | ||
store = datastore.InMemoryDataStore(default_data, re_index=True) | ||
|
||
default_data[0]["id"] = "0" | ||
|
||
assert default_data[0] == store.get_parameter_set("0") | ||
|
||
|
||
def test_init(default_data): | ||
"""Test a new InMemoryDatastore with default parameters""" | ||
store = datastore.InMemoryDataStore(default_data) | ||
|
||
assert default_data[0] == store.get_parameter_set(2) | ||
|
||
|
||
def test_read_write_process_data(default_data): | ||
"""Round trip of process data.""" | ||
|
||
store = datastore.InMemoryDataStore({}) | ||
|
||
store.write_process_data(default_data[0]) | ||
store.write_process_data(default_data[1]) | ||
|
||
returned_process_data = store.read_all_process_data() | ||
|
||
assert returned_process_data == [default_data[0], default_data[1]] |
Oops, something went wrong.