From a03065f3841d4e80ecaf109bd3dedeb54a17a479 Mon Sep 17 00:00:00 2001 From: Antoine Goutenoir Date: Wed, 12 Feb 2020 06:32:43 +0100 Subject: [PATCH] Squeeze in another feature : shared model configuration. --- content.yml | 10 +++++++++- flaskr/core.py | 9 ++++----- flaskr/laws/__init__.py | 4 +++- flaskr/laws/travel_emission_linear_fit.py | 4 ++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/content.yml b/content.yml index 50bbd0e..5e667ae 100644 --- a/content.yml +++ b/content.yml @@ -19,6 +19,14 @@ meta: role: Benevolent Wizard +# Global configuration for all models +shared_config: + # In kg/km. + train_emission: 0.023 + # Multiplier to approximate conversion of Great Circle Distances to Road + gcd_to_road_scale: 1.3 + + # Aka. Laws models: - name: Atmosfair (RFI=3 for altitude > 9 km) @@ -46,7 +54,7 @@ models: rfi: 1.0 # Flat scalar to add before scaling with laws offset_before: 0 - # Flat scalar to multiply before scaling with laws + # Scalar to multiply before scaling with laws scale_before: 1 # The travel_emission_lerp_fit uses points instead of intervals # List of (distance in km, footprint in kg) diff --git a/flaskr/core.py b/flaskr/core.py index 822ebe2..36fd637 100644 --- a/flaskr/core.py +++ b/flaskr/core.py @@ -25,7 +25,7 @@ def get_emission_models(): model_file = model_conf.file the_module = importlib.import_module("flaskr.laws.%s" % model_file) - model = the_module.EmissionModel(model_conf) + model = the_module.EmissionModel(model_conf, content.shared_config) # model.configure(extra_model_conf) emission_models.append(model) @@ -47,14 +47,13 @@ def get_hit_counter(): def increment_hit_counter(): if isfile(hit_count_path): - hit_count = int(open(hit_count_path).read()) + hit_count = get_hit_counter() hit_count += 1 else: hit_count = 1 - hit_counter_file = open(hit_count_path, 'w') - hit_counter_file.write(str(hit_count)) - hit_counter_file.close() + with open(hit_count_path, 'w') as hcf: + hcf.write(str(hit_count)) return hit_count diff --git a/flaskr/laws/__init__.py b/flaskr/laws/__init__.py index 380a21c..cebddac 100644 --- a/flaskr/laws/__init__.py +++ b/flaskr/laws/__init__.py @@ -1,12 +1,14 @@ # @abc class BaseEmissionModel(): - def __init__(self, config): # Constructor + def __init__(self, config, shared_config): # Constructor self.name = config.name self.slug = config.slug self.color = config.color self.selected = config.selected self.config = config.config + # Here you'll get what's under `model_shared_config:` in content.yml + self.shared_config = shared_config def __repr__(self): # Cast to String return "Emission model\n" + \ diff --git a/flaskr/laws/travel_emission_linear_fit.py b/flaskr/laws/travel_emission_linear_fit.py index de6945d..a492dee 100644 --- a/flaskr/laws/travel_emission_linear_fit.py +++ b/flaskr/laws/travel_emission_linear_fit.py @@ -77,8 +77,8 @@ class EmissionModel(BaseEmissionModel): } def compute_train_footprint(self, distance): - gcd_to_road_correction = 1.30 # See issue #32 - train_emission = 0.023 # kg/km + gcd_to_road_correction = self.shared_config.gcd_to_road_scale + train_emission = self.shared_config.train_emission # kg/km return gcd_to_road_correction * distance * train_emission def compute_airplane_footprint( -- libgit2 0.21.2