Пример #1
0
def capture_stream(stream_name='stdout'):
    r'''Capture stream (stdout/stderr) in a string

    >>> with capture_stream() as stdout:
    ...     print('Hi there')
    >>> stdout.getvalue()
    'Hi there\n'
    '''
    stream = getattr(sys, stream_name)
    data = cStringIO()
    setattr(sys, stream_name, data)
    yield data
    setattr(sys, stream_name, stream)
    data.flush()
Пример #2
0
def capture_stream(stream_name='stdout'):
    r'''Capture stream (stdout/stderr) in a string

    >>> with capture_stream() as stdout:
    ...     print('Hi there')
    >>> stdout.getvalue()
    'Hi there\n'
    '''
    stream = getattr(sys, stream_name)
    data = cStringIO()
    setattr(sys, stream_name, data)
    yield data
    setattr(sys, stream_name, stream)
    data.flush()
Пример #3
0
    def dump(self, default_flow_style=True):
        '''Serialize odict into yaml string'''
        class Dumper(yaml.SafeDumper):
            def __init__(self, *args, **kwargs):
                super(Dumper, self).__init__(*args, **kwargs)
                self.ignore_aliases = lambda self: True
                self.add_representer(Odict, lambda self, data:
                    self.represent_dict(data.items()))

            def increase_indent(self, flow=False, indentless=False):
                return super(Dumper, self).increase_indent(flow, False)

        stream = cStringIO()
        yaml.dump(self, stream, Dumper, default_flow_style=default_flow_style)
        return stream.getvalue()[:-1]
Пример #4
0
    def dump(self, default_flow_style=True):
        '''Serialize odict into yaml string'''
        class Dumper(yaml.SafeDumper):
            def __init__(self, *args, **kwargs):
                super(Dumper, self).__init__(*args, **kwargs)
                self.ignore_aliases = lambda self: True
                self.add_representer(
                    Odict,
                    lambda self, data: self.represent_dict(data.items()))

            def increase_indent(self, flow=False, indentless=False):
                return super(Dumper, self).increase_indent(flow, False)

        stream = cStringIO()
        yaml.dump(self, stream, Dumper, default_flow_style=default_flow_style)
        return stream.getvalue()[:-1]