Skip to content

Commit

Permalink
Minor improvements to yaml dumping for ArkaneSpecies
Browse files Browse the repository at this point in the history
Provide file stream directly to yaml.dump
Remove optional Dumper argument and associated imports
Add str representer to dump multiline strings as block literals
  • Loading branch information
mliu49 authored and alongd committed Mar 14, 2019
1 parent 87e5b70 commit ca88d81
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions arkane/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
import string

import yaml
try:
from yaml import CDumper as Dumper, CLoader as Loader, CSafeLoader as SafeLoader
except ImportError:
from yaml import Dumper, Loader, SafeLoader

from rmgpy.rmgobject import RMGObject
from rmgpy import __version__
Expand All @@ -61,6 +57,16 @@
################################################################################


# Add a custom string representer to use block literals for multiline strings
def str_repr(dumper, data):
if len(data.splitlines()) > 1:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
return dumper.represent_scalar('tag:yaml.org,2002:str', data)


yaml.add_representer(str, str_repr)


class ArkaneSpecies(RMGObject):
"""
A class for archiving an Arkane species including its statmech data into .yml files
Expand Down Expand Up @@ -206,7 +212,7 @@ def save_yaml(self, path):
''.join(c for c in self.label if c in valid_chars) + '.yml')
full_path = os.path.join(path, filename)
with open(full_path, 'w') as f:
f.write(yaml.dump(data=self.as_dict(), Dumper=Dumper))
yaml.dump(data=self.as_dict(), stream=f)
logging.debug('Dumping species {0} data as {1}'.format(self.label, filename))

def load_yaml(self, path, species, pdep=False):
Expand Down

0 comments on commit ca88d81

Please sign in to comment.