Commit a03065f3841d4e80ecaf109bd3dedeb54a17a479

Authored by Antoine Goutenoir
1 parent ce850e3a
Exists in master

Squeeze in another feature : shared model configuration.

Solves #32
content.yml
... ... @@ -19,6 +19,14 @@ meta:
19 19 role: Benevolent Wizard
20 20  
21 21  
  22 +# Global configuration for all models
  23 +shared_config:
  24 + # In kg/km.
  25 + train_emission: 0.023
  26 + # Multiplier to approximate conversion of Great Circle Distances to Road
  27 + gcd_to_road_scale: 1.3
  28 +
  29 +
22 30 # Aka. Laws
23 31 models:
24 32 - name: Atmosfair (RFI=3 for altitude > 9 km)
... ... @@ -46,7 +54,7 @@ models:
46 54 rfi: 1.0
47 55 # Flat scalar to add before scaling with laws
48 56 offset_before: 0
49   - # Flat scalar to multiply before scaling with laws
  57 + # Scalar to multiply before scaling with laws
50 58 scale_before: 1
51 59 # The travel_emission_lerp_fit uses points instead of intervals
52 60 # List of (distance in km, footprint in kg)
... ...
flaskr/core.py
... ... @@ -25,7 +25,7 @@ def get_emission_models():
25 25 model_file = model_conf.file
26 26 the_module = importlib.import_module("flaskr.laws.%s" % model_file)
27 27  
28   - model = the_module.EmissionModel(model_conf)
  28 + model = the_module.EmissionModel(model_conf, content.shared_config)
29 29 # model.configure(extra_model_conf)
30 30  
31 31 emission_models.append(model)
... ... @@ -47,14 +47,13 @@ def get_hit_counter():
47 47  
48 48 def increment_hit_counter():
49 49 if isfile(hit_count_path):
50   - hit_count = int(open(hit_count_path).read())
  50 + hit_count = get_hit_counter()
51 51 hit_count += 1
52 52 else:
53 53 hit_count = 1
54 54  
55   - hit_counter_file = open(hit_count_path, 'w')
56   - hit_counter_file.write(str(hit_count))
57   - hit_counter_file.close()
  55 + with open(hit_count_path, 'w') as hcf:
  56 + hcf.write(str(hit_count))
58 57  
59 58 return hit_count
60 59  
... ...
flaskr/laws/__init__.py
1 1  
2 2 # @abc
3 3 class BaseEmissionModel():
4   - def __init__(self, config): # Constructor
  4 + def __init__(self, config, shared_config): # Constructor
5 5 self.name = config.name
6 6 self.slug = config.slug
7 7 self.color = config.color
8 8 self.selected = config.selected
9 9 self.config = config.config
  10 + # Here you'll get what's under `model_shared_config:` in content.yml
  11 + self.shared_config = shared_config
10 12  
11 13 def __repr__(self): # Cast to String
12 14 return "Emission model\n" + \
... ...
flaskr/laws/travel_emission_linear_fit.py
... ... @@ -77,8 +77,8 @@ class EmissionModel(BaseEmissionModel):
77 77 }
78 78  
79 79 def compute_train_footprint(self, distance):
80   - gcd_to_road_correction = 1.30 # See issue #32
81   - train_emission = 0.023 # kg/km
  80 + gcd_to_road_correction = self.shared_config.gcd_to_road_scale
  81 + train_emission = self.shared_config.train_emission # kg/km
82 82 return gcd_to_road_correction * distance * train_emission
83 83  
84 84 def compute_airplane_footprint(
... ...