def add_multi_representer(data_type, multi_representer, Dumper=None, representer=Representer): # type: (Any, Any, Any, Any) -> None """ Add a representer for the given type. multi_representer is a function accepting a Dumper instance and an instance of the given data type or subtype and producing the corresponding representation node. """ if Dumper is None: representer.add_multi_representer(data_type, multi_representer) else: if hasattr(Dumper, 'add_multi_representer'): Dumper.add_multi_representer(data_type, multi_representer) return if issubclass(Dumper, BaseDumper): BaseRepresenter.add_multi_representer(data_type, multi_representer) elif issubclass(Dumper, SafeDumper): SafeRepresenter.add_multi_representer(data_type, multi_representer) elif issubclass(Dumper, Dumper): Representer.add_multi_representer(data_type, multi_representer) elif issubclass(Dumper, RoundTripDumper): RoundTripRepresenter.add_multi_representer(data_type, multi_representer) else: raise NotImplementedError
def __init__( self, stream, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, block_seq_indent=None, top_level_colon_align=None, prefix_colon=None, ): # type: (Any, StreamType, Any, bool, Union[None, int], Union[None, int], bool, Any, Any, Union[None, bool], Union[None, bool], Any, Any, Any, Any, Any) -> None # NOQA Emitter.__init__( self, stream, canonical=canonical, indent=indent, width=width, allow_unicode=allow_unicode, line_break=line_break, block_seq_indent=block_seq_indent, top_level_colon_align=top_level_colon_align, prefix_colon=prefix_colon, dumper=self, ) Serializer.__init__( self, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, version=version, tags=tags, dumper=self, ) RoundTripRepresenter.__init__( self, default_style=default_style, default_flow_style=default_flow_style, dumper=self, ) StrictYAMLResolver.__init__(self, loader=self)
def __init__( self, stream, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, block_seq_indent=None, top_level_colon_align=None, prefix_colon=None, ): # type: (Any, StreamType, Any, bool, Union[None, int], Union[None, int], bool, Any, Any, Union[None, bool], Union[None, bool], Any, Any, Any, Any, Any) -> None # NOQA Emitter.__init__( self, stream, canonical=canonical, indent=indent, width=width, allow_unicode=allow_unicode, line_break=line_break, block_seq_indent=block_seq_indent, top_level_colon_align=top_level_colon_align, prefix_colon=prefix_colon, dumper=self, ) Serializer.__init__( self, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, version=version, tags=tags, dumper=self, ) RoundTripRepresenter.__init__( self, default_style=default_style, default_flow_style=default_flow_style, dumper=self, ) StrictYAMLResolver.__init__(self, loader=self)
def __init__(self, stream, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, block_seq_indent=None, top_level_colon_align=None, prefix_colon=None): Emitter.__init__(self, stream, canonical=canonical, indent=indent, width=width, allow_unicode=allow_unicode, line_break=line_break, block_seq_indent=block_seq_indent, top_level_colon_align=top_level_colon_align, prefix_colon=prefix_colon) Serializer.__init__(self, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, version=version, tags=tags) RoundTripRepresenter.__init__(self, default_style=default_style, default_flow_style=default_flow_style) VersionedResolver.__init__(self)
def __init__(self, stream, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, block_seq_indent=None, top_level_colon_align=None, prefix_colon=None): Emitter.__init__(self, stream, canonical=canonical, indent=indent, width=width, allow_unicode=allow_unicode, line_break=line_break, block_seq_indent=block_seq_indent, top_level_colon_align=top_level_colon_align, prefix_colon=prefix_colon) Serializer.__init__(self, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, version=version, tags=tags) RoundTripRepresenter.__init__(self, default_style=default_style, default_flow_style=default_flow_style) VersionedResolver.__init__(self)
out = endlist1 exceptions = [] errors = [] for end in endlist2: if end.name in out.keys(): try: out[end.name] = out[end.name].merge(end) except ValueError as e: if fail_immediate: raise e else: exceptions.append(e) errors.append((end.name, out[end.name], end)) else: out.append(copy.deepcopy(end)) if errors: errorstring = " ".join([e[0] for e in errors]) raise ValueError("Errors merging {}".format(errorstring), exceptions, errors, out) return out from ruamel.yaml.representer import RoundTripRepresenter RoundTripRepresenter.add_representer(EndList, RoundTripRepresenter.represent_list) RoundTripRepresenter.add_representer(End, RoundTripRepresenter.represent_dict)
def repr_str(dumper: RoundTripRepresenter, data: str): if '\n' in data: return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') return dumper.represent_scalar('tag:yaml.org,2002:str', data)
from subprocess import PIPE, Popen from collections import OrderedDict from ruamel.yaml.scalarstring import DoubleQuotedScalarString try: from queue import Queue except ImportError: from Queue import Queue from ruamel.yaml.representer import ( RoundTripRepresenter, CommentedOrderedMap, CommentedMap, ) for typ in [OrderedDict, dict, CommentedMap, CommentedOrderedMap]: RoundTripRepresenter.add_representer(typ, RoundTripRepresenter.represent_dict) J = os.path.join B = os.path.basename D = os.path.dirname A = os.path.abspath R = os.path.relpath OW = os.getcwd() W = A(R(D(__file__))) TOP = D(W) RE_F = re.U | re.M N = os.path.basename(__file__) BN = os.path.basename(N) _LOGGER = 'cops.{0}'.format(BN) _LOGGER_FMT = '%(asctime)s:%(levelname)s:%(name)s:%(message)s' _LOGGER_DFMT = '%m/%d/%Y %I:%M:%S %p'
CommentedSeq.__setitem__(self, i, v) def keys(self): return [x['name'] for x in self if 'name' in x.keys()] def __deepcopy__(self, memo): # FIXME: this is here waiting on ruamel.yaml bugfix. res = self.__class__() memo[id(self)] = res for k in self: res.append(copy.deepcopy(k)) self.copy_attributes(res, deep=True) return res RoundTripRepresenter.add_representer(named_list, RoundTripRepresenter.represent_list) lton = { 'a': (0, ), 'b': (1, 2, 3), 'c': (1, ), 'd': (0, 2, 3), 'g': (2, ), 'h': (0, 1, 3), 'k': (2, 3), 'm': (0, 1), 'n': (0, 1, 2, 3), 's': (1, 2), 't': (3, ), 'v': (0, 1, 2), 'w': (0, 3)
if tun > 0: info['tns'] += " ({} unnamed)".format(tun) eun = sum(1 for x in self['ends'] if 'name' not in x.keys()) if eun > 0: info['ens'] += " ({} unnamed)".format(eun) return "TileSet{name}: {ntiles} tiles, {nends} ends, {ntends} ends in tiles.\nTiles: {tns}\nEnds: {ens}".format( **info) def __str__(self): return self.summary() def __deepcopy__(self, memo): # type: (Any) -> Any res = self.__class__() memo[id(self)] = res for k in self: res[k] = copy.deepcopy(self[k]) self.copy_attributes(res, deep=True) return res def dump(self, stream): return yaml.round_trip_dump(self, stream) RoundTripRepresenter.add_representer(tileset_dict, RoundTripRepresenter.represent_dict) def load_tileset_dict(*args, **kwargs): return tileset_dict(yaml.round_trip_load(*args, **kwargs))
# coding: utf-8 from __future__ import absolute_import from ruamel.yaml.representer import RoundTripRepresenter from ruamel.yaml.scalarstring import ScalarString from ruamel.yaml.emitter import Emitter from ruamel.yaml.serializer import Serializer from ruamel.yaml.resolver import BaseResolver import sys if sys.version_info[0] == 3: RoundTripRepresenter.add_representer(ScalarString, RoundTripRepresenter.represent_str) else: RoundTripRepresenter.add_representer( ScalarString, RoundTripRepresenter.represent_unicode) class StrictYAMLResolver(BaseResolver): def __init__(self, version=None, loader=None): BaseResolver.__init__(self, loader) class StrictYAMLDumper(Emitter, Serializer, RoundTripRepresenter, StrictYAMLResolver): def __init__( self, stream, default_style=None, default_flow_style=None,
# coding: utf-8 from __future__ import absolute_import from ruamel.yaml.representer import RoundTripRepresenter from ruamel.yaml.scalarstring import ScalarString from ruamel.yaml.emitter import Emitter from ruamel.yaml.serializer import Serializer from ruamel.yaml.resolver import BaseResolver import sys if sys.version_info[0] == 3: RoundTripRepresenter.add_representer( ScalarString, RoundTripRepresenter.represent_str ) else: RoundTripRepresenter.add_representer( ScalarString, RoundTripRepresenter.represent_unicode ) class StrictYAMLResolver(BaseResolver): def __init__(self, version=None, loader=None): BaseResolver.__init__(self, loader) class StrictYAMLDumper(Emitter, Serializer, RoundTripRepresenter, StrictYAMLResolver): def __init__( self, stream, default_style=None,
def represent_list(self, data): if isinstance(data, list): data = CommentedSeq(d for d in data) if isinstance(data, CommentedSeq) and len(data) > 1: data.fa.set_block_style() return RoundTripRepresenter.represent_list(self, data)
fail_immediate : bool (default True) if True, immediately fail on a failure, with ValueError( tilename, exception ) from exception If False, collect exceptions, then raise ValueError( "message", [(tilename, exception)] , output ). """ endlist = EndList() errors = [] for tile in self: try: endlist = endlist.merge(tile.endlist, in_place=True) except BaseException as e: if fail_immediate: raise ValueError(tile.name) from e else: errors.append((tile.name, e)) if errors: raise ValueError("End list generation failed on:", errors, endlist) return endlist from ruamel.yaml.representer import RoundTripRepresenter RoundTripRepresenter.add_representer(TileList, RoundTripRepresenter.represent_list) RoundTripRepresenter.add_representer(Tile, RoundTripRepresenter.represent_dict)
if len(r) > 1: raise KeyError( "There are {} elements named {}.".format(len(r), i)) elif len(r) == 0: raise KeyError("No element named {} found.".format(i)) else: CommentedSeq.__delitem__(self, r[0]) else: return CommentedSeq.__delitem__(self, i) def keys(self): return [x['name'] for x in self if 'name' in x.keys()] RoundTripRepresenter.add_representer(NamedList, RoundTripRepresenter.represent_list) import numpy as np RoundTripRepresenter.add_representer(np.str_, RoundTripRepresenter.represent_str) class ProgressLogger(object): def __init__(self, logger, N, seconds_interval=60): self.logger = logger stime = time.perf_counter() self.stime = stime self.ltime = stime self.li = 0 self self.seconds_interval = seconds_interval