Skip to content

Commit

Permalink
Merge pull request #67 from Pica4x6/load_dump
Browse files Browse the repository at this point in the history
__numina_dump__  changed --> _datatype_dump
  • Loading branch information
sergiopasra committed Dec 10, 2015
2 parents 706cd68 + 6492342 commit 5e88ff9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
21 changes: 9 additions & 12 deletions megaradrp/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

"""Products of the Megara Pipeline"""


'''
RAW_BIAS DataFrameType
RAW_DARK DataFrameType
Expand Down Expand Up @@ -64,23 +63,21 @@ class MasterSensitivity(MEGARAProductFrame):


class TraceMap(DataProductType):

def __init__(self, default=None):
super(TraceMap, self).__init__(
ptype=dict, default=default)

def __numina_dump__(self, obj, where):
super(TraceMap, self).__init__(ptype=dict, default=default)

def _datatype_dump(self, obj, where):
filename = where.destination + '.yaml'

with open(filename, 'w') as fd:
yaml.dump(obj, fd)

return filename

def __numina_load__(tag, obj):

with open(obj, 'r') as fd:
traces = yaml.load(fd)

return traces
def _datatype_load(self, obj):
try:
with open(obj, 'r') as fd:
traces = yaml.load(fd)
except IOError as e:
raise e
return traces
66 changes: 66 additions & 0 deletions megaradrp/tests/test_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# Copyright 2015 Universidad Complutense de Madrid
#
# This file is part of Megara DRP
#
# Megara DRP is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Megara DRP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Megara DRP. If not, see <http://www.gnu.org/licenses/>.
#

from megaradrp.products import TraceMap
from tempfile import NamedTemporaryFile
import pytest
import yaml

@pytest.mark.xfail
def test_fail_traceMap(benchmark=None):
my_obj = TraceMap()
my_obj._datatype_load('')

def test_load_traceMap(benchmark=None):
data = dict(A = 'a',
B = dict(C = 'c',
D = 'd',
E = 'e',)
)

my_obj = TraceMap()
my_file = NamedTemporaryFile()
with open(my_file.name, 'w') as fd:
yaml.dump(data, fd)
my_open_file = my_obj._datatype_load(my_file.name)
assert (my_open_file == {'A': 'a', 'B': {'C': 'c', 'E': 'e', 'D': 'd'}})


def test_dump_traceMap(benchmark=None):
class aux(object):
def __init__(self, destination):
self.destination = destination

data = dict(A = 'a',
B = dict(C = 'c',
D = 'd',
E = 'e',)
)

my_obj = TraceMap()
my_file = NamedTemporaryFile()
work_env = aux(my_file.name)
my_open_file = my_obj._datatype_dump(data, work_env)
with open(my_open_file, 'r') as fd:
traces = yaml.load(fd)
assert (traces == {'A': 'a', 'B': {'C': 'c', 'E': 'e', 'D': 'd'}})

if __name__ == "__main__":
test_load_traceMap()
test_dump_traceMap()

0 comments on commit 5e88ff9

Please sign in to comment.