CSafeDumper is significantly faster and can be subclassed for custom extensions.

This commit is contained in:
Daniel Otto de Mentock 2022-12-06 20:17:23 +00:00 committed by Martin Diehl
parent 4717536f1a
commit ba2062e4ed
1 changed files with 7 additions and 5 deletions

View File

@ -8,8 +8,10 @@ import numpy as np
import yaml import yaml
try: try:
from yaml import CSafeLoader as SafeLoader from yaml import CSafeLoader as SafeLoader
from yaml import CSafeDumper as SafeDumper
except ImportError: except ImportError:
from yaml import SafeLoader # type: ignore from yaml import SafeLoader # type: ignore
from yaml import SafeDumper # type: ignore
from ._typehints import FileHandle from ._typehints import FileHandle
from . import Rotation from . import Rotation
@ -17,20 +19,20 @@ from . import util
MyType = TypeVar('MyType', bound='Config') MyType = TypeVar('MyType', bound='Config')
class NiceDumper(yaml.SafeDumper): class NiceDumper(SafeDumper):
"""Make YAML readable for humans.""" """Make YAML readable for humans."""
def write_line_break(self, def write_line_break(self,
data: Optional[str] = None): data: Optional[str] = None):
super().write_line_break(data) super().write_line_break(data) # type: ignore
if len(self.indents) == 1: if len(self.indents) == 1: # type: ignore
super().write_line_break() super().write_line_break() # type: ignore
def increase_indent(self, def increase_indent(self,
flow: bool = False, flow: bool = False,
indentless: bool = False): indentless: bool = False):
return super().increase_indent(flow, False) return super().increase_indent(flow, False) # type: ignore
def represent_data(self, def represent_data(self,
data: Any): data: Any):