Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
- remove redundant backend methods from platform
- use unwrap to handle BigDecimals
- fix tests
- replace ixmp.jar (freshly built from appropriate branch)
  • Loading branch information
zikolach committed Jul 28, 2020
1 parent e5711c6 commit 121a876
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 45 deletions.
20 changes: 11 additions & 9 deletions ixmp/backend/jdbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ def _domain_enum(domain):
f'existing domains: {domains}')


def _unwrap(v):
"""Unwrap meta numeric value (BigDecimal -> Double)"""
return v.doubleValue() if isinstance(v, java.BigDecimal) else v


class JDBCBackend(CachingBackend):
"""Backend using JPype/JDBC to connect to Oracle and HyperSQL databases.
Expand Down Expand Up @@ -876,10 +881,12 @@ def item_delete_elements(self, s, type, name, keys):
args = (s,) if type == 'set' else (s, type, name)
self.cache_invalidate(*args)

def get_meta(self, model: str = None, scenario: str = None, version=None
) -> str:
def get_meta(self, model: str = None, scenario: str = None,
version: int = None) -> dict:

meta = self.jobj.getMeta(model, scenario, version)
return {entry.getKey(): entry.getValue() for entry in meta.entrySet()}
return {entry.getKey(): _unwrap(entry.getValue())
for entry in meta.entrySet()}

def set_meta(self, meta: dict, model: str = None, scenario: str = None,
version = None):
Expand All @@ -896,12 +903,7 @@ def set_meta(self, meta: dict, model: str = None, scenario: str = None,
return self.jobj.setMeta(model, scenario, version, jmeta)

def get_scenario_meta(self, s):
def unwrap(v):
"""Unwrap meta numeric value (BigDecimal -> Double)"""
return v.doubleValue() if isinstance(v, java.BigDecimal) else v
# TODO: unwrapping isn't always working?

return {entry.getKey(): unwrap(entry.getValue())
return {entry.getKey(): _unwrap(entry.getValue())
for entry in self.jindex[s].getMeta().entrySet()}

def set_scenario_meta(self, s, name_or_dict, value=None):
Expand Down
35 changes: 2 additions & 33 deletions ixmp/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class Platform:
'close_db',
'get_doc',
'set_doc',
'get_meta',
'set_meta',
]

def __init__(self, name=None, backend=None, **backend_args):
Expand Down Expand Up @@ -393,39 +395,6 @@ def check_access(self, user, models, access='view'):
else:
return {model: result.get(model) == 1 for model in models_list}

def get_meta(self, *args, **kwargs):
"""Retrieve meta entries.
Parameters
----------
model : str, optional
filter meta meta by a model
scenario : str, optional
filter meta by a scenario
version : int or str, optional
retrieve meta of a specific model/scenario run version
Returns
-------
dict (str -> any)
Mapping from meta keys to values.
"""
return self._backend.get_meta(*args, **kwargs)

def set_meta(self, *args, **kwargs):
"""Set new or overwrite existing meta entries.
Parameters
----------
metadata : dict
model : str, optional
filter meta by model name
scenario : str, optional
filter meta by scenario name
version : str, optional
filter meta by run version
"""
return self._backend.set_meta(*args, **kwargs)

class TimeSeries:
"""Collection of data in time series format.
Expand Down
Binary file modified ixmp/ixmp.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions ixmp/tests/backend/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class BE2(Backend):
set_node = noop
set_timeslice = noop
set_unit = noop
get_scenario_meta = noop
set_scenario_meta = noop
delete_scenario_meta = noop

# Complete subclass can be instantiated
be = BE2()
Expand Down
7 changes: 4 additions & 3 deletions ixmp/tests/core/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def test_set_meta(mp):
meta = {'sample_string': 3}
model = models['dantzig']['model']
mp.set_meta(meta, model=model)
assert(mp.get_meta(model=model) == meta)
obs = mp.get_meta(model=model)
assert obs == meta


def test_unique_meta(mp):
Expand All @@ -31,7 +32,7 @@ def test_unique_meta(mp):
scenario.commit('save dummy scenario')
mp.set_meta(sample_meta, model=model['model'])
with pytest.raises(Exception) as err:
# TODO: look for more exact exception -
# TODO: look for more exact exception -
# jpype._jclass.at.ac.iiasa.ixmp.exceptions.IxException: at.ac.iiasa.ixmp.exceptions.IxException: Metadata already contains category another_string
mp.set_meta(sample_meta, **model, version=scenario.version)
scen = ixmp.Scenario(mp, **model)
Expand All @@ -45,7 +46,7 @@ def test_unique_meta_reversed(mp):
level should fail too.
"""
model = models['dantzig']
scen = mp.Scenario(mp, **model)
scen = ixmp.Scenario(mp, **model)
scen.set_meta(sample_meta)

with pytest.raises(Exception):
Expand Down

0 comments on commit 121a876

Please sign in to comment.