Skip to content

Commit

Permalink
Represent tuples as strings and overload ignore_aliases parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
RoaldL committed May 16, 2018
1 parent 2e88d1a commit 1952a18
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion energy_demand/read_write/write_data.py
Expand Up @@ -11,6 +11,16 @@
from energy_demand.basic import basic_functions, conversions
from energy_demand.geography import write_shp

class ExplicitDumper(yaml.Dumper):
"""
A dumper that will never emit aliases.
"""
def ignore_aliases(self, data):
return True

def tuple_representer(dumper, data):
return dumper.represent_scalar(tag=u'tag:yaml.org,2002:str', value='({}, {})'.format(data[0], data[1]))

def write_array_to_txt(path_result, array):
"""Write scenario population for a year to txt file
"""
Expand Down Expand Up @@ -127,8 +137,9 @@ def dump(data, file_path):
data
Data to write (should be lists, dicts and simple values)
"""
yaml.add_representer(tuple, tuple_representer)
with open(file_path, 'w') as file_handle:
return yaml.dump(data, file_handle, Dumper=Dumper, default_flow_style=False)
return yaml.dump(data, file_handle, Dumper=ExplicitDumper, default_flow_style=False)

def write_yaml_output_keynames(path_yaml, key_names):
"""Generate YAML file where the outputs
Expand Down

0 comments on commit 1952a18

Please sign in to comment.