Commit eed20b63 authored by Yannick Roehlly's avatar Yannick Roehlly

Implement context manager in database

parent 8cd6338e
......@@ -177,12 +177,11 @@ class Psum(AnalysisModule):
if not name.endswith('_err')]
transmission = {}
effective_wavelength = {}
base = Database()
for name in filter_list:
filt = base.get_filter(name)
transmission[name] = filt.trans_table
effective_wavelength[name] = filt.effective_wavelength
base.close()
with Database() as base:
for name in filter_list:
filt = base.get_filter(name)
transmission[name] = filt.trans_table
effective_wavelength[name] = filt.effective_wavelength
# We get the redshift module.
redshift_module = get_module(redshift_module_name,
......
......@@ -97,9 +97,8 @@ class SaveFluxes(AnalysisModule):
# Get the filters in the database
filter_names = [name for name in column_list
if not name.endswith('_err')]
base = Database()
filter_list = [base.get_filter(name) for name in filter_names]
base.close()
with Database() as base:
filter_list = [base.get_filter(name) for name in filter_names]
# Columns of the output table
out_columns = []
......
......@@ -66,9 +66,8 @@ class BC03(CreationModule):
"""Read the SSP from the database."""
imf = self.parameters["imf"]
metallicity = float(self.parameters["metallicity"])
database = Database()
self.ssp = database.get_ssp_bc03(imf, metallicity)
database.session.close_all()
with Database() as database:
self.ssp = database.get_ssp_bc03(imf, metallicity)
def process(self, sed):
"""Add the convolution of a Bruzual and Charlot SSP to the SED
......
......@@ -66,10 +66,9 @@ class Dale2014(CreationModule):
"""
alpha = self.parameters["alpha"]
database = Database()
self.model_sb = database.get_dale2014(0.00, alpha)
self.model_quasar = database.get_dale2014(1.00, alpha)
database.session.close_all()
with Database() as database:
self.model_sb = database.get_dale2014(0.00, alpha)
self.model_quasar = database.get_dale2014(1.00, alpha)
def process(self, sed):
"""Add the IR re-emission contributions
......
......@@ -48,9 +48,8 @@ class DH2002(CreationModule):
def _init_code(self):
"""Get the template set out of the database"""
database = Database()
self.dh2002 = database.get_dh2002_infrared_templates()
database.session.close_all()
with Database() as database:
self.dh2002 = database.get_dh2002_infrared_templates()
def process(self, sed):
"""Add the IR re-emission contributions
......
......@@ -76,10 +76,9 @@ class DL2007(CreationModule):
umax = self.parameters["umax"]
gamma = self.parameters["gamma"]
database = Database()
self.model_minmin = database.get_dl2007(qpah, umin, umin)
self.model_minmax = database.get_dl2007(qpah, umin, umax)
database.session.close_all()
with Database() as database:
self.model_minmin = database.get_dl2007(qpah, umin, umin)
self.model_minmax = database.get_dl2007(qpah, umin, umax)
# The models in memory are in W/nm for 1 kg of dust. At the same time
# we need to normalize them to 1 W here to easily scale them from the
......
......@@ -258,10 +258,9 @@ class CalzLeit(CreationModule):
filter_list = [item.strip() for item in
self.parameters["filters"].split("&")]
self.filters = {}
base = Database()
for filter_name in filter_list:
self.filters[filter_name] = base.get_filter(filter_name)
base.close()
with Database() as base:
for filter_name in filter_list:
self.filters[filter_name] = base.get_filter(filter_name)
def process(self, sed):
"""Add the CCM dust attenuation to the SED.
......
......@@ -182,10 +182,9 @@ class PowerLawAtt(CreationModule):
filter_list = [item.strip() for item in
self.parameters["filters"].split("&")]
self.filters = {}
base = Database()
for filter_name in filter_list:
self.filters[filter_name] = base.get_filter(filter_name)
base.close()
with Database() as base:
for filter_name in filter_list:
self.filters[filter_name] = base.get_filter(filter_name)
def process(self, sed):
"""Add the CCM dust attenuation to the SED.
......
......@@ -104,9 +104,8 @@ class M2005(CreationModule):
"""Read the SSP from the database."""
imf = self.parameters["imf"]
metallicity = float(self.parameters["metallicity"])
database = Database()
self.ssp = database.get_ssp_m2005(imf, metallicity)
database.session.close_all()
with Database() as database:
self.ssp = database.get_ssp_m2005(imf, metallicity)
def process(self, sed):
"""Add the convolution of a Maraston 2005 SSP to the SED
......
......@@ -217,6 +217,12 @@ class Database(object):
self.session = SESSION()
self.is_writable = writable
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def upgrade_base(self):
""" Upgrade the table schemas in the database
"""
......
......@@ -150,9 +150,8 @@ class Configuration(object):
"""
# Getting the list of the filters available in pcigale database
base = Database()
filter_list = base.get_filter_list()[0]
base.close()
with Database() as base:
filter_list = base.get_filter_list()[0]
# Finding the known filters in the data table
obs_table = atpy.Table(self.config['data_file'], verbose=False)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment