Added functionality to use CLoader / CDumper from yaml #199

Merged
merged 1 commit into from May 24, 2013
Jump to file or symbol
Failed to load files and symbols.
+11 −2
Split

Added functionality to use CLoader / CDumper from yaml to load and sa…

…ve yaml files. This takes advantage of a c-library (libyaml) if its available for faster loads and saves.
  • Loading branch information...
commit beb21bf10892dd566fccf567f907404957db1aed @schwancr schwancr committed May 23, 2013
@@ -19,6 +19,15 @@
import os
import numpy as np
import yaml
+# if CLoader/CDumper are available (i.e. user has libyaml installed)
+# then use them since they are much faster.
+try:
+ from yaml import CLoader as Loader
+ from yaml import CDumper as Dumper
+except ImportError:
+ from yaml import Loader
+ from yaml import Dumper
+
from msmbuilder import Trajectory
from msmbuilder import io
from msmbuilder import MSMLib
@@ -141,7 +150,7 @@ def load_from(cls, filename):
if filename.endswith('.yaml'):
with open(filename) as f:
- ondisk = yaml.load(f)
+ ondisk = yaml.load(f, Loader=Loader)
records = {'conf_filename': ondisk['conf_filename'],
'traj_lengths': [],
'traj_paths': [],
@@ -212,7 +221,7 @@ def save(self, filename_or_file):
'length': int(self._traj_lengths[i]),
'errors': self._traj_errors[i]})
- yaml.dump(records, handle)
+ yaml.dump(records, handle, Dumper=Dumper)
if own_fid:
handle.close()