def add_representer(data_type, object_representer, Dumper=None, representer=Representer): # type: (Any, Any, Any, Any) -> None """ Add a representer for the given type. object_representer is a function accepting a Dumper instance and an instance of the given data type and producing the corresponding representation node. """ if Dumper is None: representer.add_representer(data_type, object_representer) else: if hasattr(Dumper, 'add_representer'): Dumper.add_representer(data_type, object_representer) return if issubclass(Dumper, BaseDumper): BaseRepresenter.add_representer(data_type, object_representer) elif issubclass(Dumper, SafeDumper): SafeRepresenter.add_representer(data_type, object_representer) elif issubclass(Dumper, Dumper): Representer.add_representer(data_type, object_representer) elif issubclass(Dumper, RoundTripDumper): RoundTripRepresenter.add_representer(data_type, object_representer) else: raise NotImplementedError
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)
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)
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'
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,
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