def assert_compare(a, b, print_objs = True): path = get_compound_diff(a, b) if path: contents = ['Value at path %s does not match.' % path] if print_objs: contents.append('a: %s' % _pformat(a)) contents.append('b: %s' % _pformat(b)) raise AssertionError('\n'.join(contents))
def shell_notify(msg, state=False, more=None, exitcode=None, verbose=True): ''' A pretty long wrapper for a :py:func:`print` function. But this :py:func:`print` is the only one in Photon. .. note:: |use_photon_m| :param msg: The message to show :param state: The message will be prefixed with [`state`] * If ``False`` (default): Prefixed with ~ * If ``None``: Prefixed with [WARNING] * If ``True``: Prefixed with [FATAL] and the exitcode \ will be set (see below) :param more: Something to add to the message (see below) * Anything you have. Just for further information. * Will be displayed after the message, \ pretty printed using :py:func:`pprint.pformat` :param exitcode: |appteardown| with given code :param verbose: Show message or not (see below) * If set to ``False``, you can use :func:`shell_notify` \ for the dictionary it returns. * Will be overruled if `exitcode` is set. :returns: A dictionary containing untouched `msg`, `more` and `verbose` ''' if state is True: state = '[FATAL]' exitcode = 23 elif state is None: state = '[WARNING]' elif state is False: state = '~' else: state = '[%s]' % (str(state)) m = ' %s %s' % (state, str(msg)) if more: m += '\n\t' + _pformat(more).replace('\n', '\n\t') if verbose or isinstance(exitcode, int): print(m) if isinstance(exitcode, int): _exit(exitcode) return dict(message=msg, more=more, verbose=verbose)
def text(self, text): ''' .. seealso:: :attr:`text` ''' if text: if not isinstance(text, str): text = _pformat(text) text += '\n\n' self.m('add text to mail', more=dict(len=len(text))) self.__message.attach(_MIMEText(text, 'plain', 'UTF-8'))
def text(self, text): ''' .. seealso:: :attr:`text` ''' if text: if not isinstance(text, str): text = _pformat(text) text += '\n\n' self.m( 'add text to mail', more=dict(len=len(text)) ) self.__message.attach(_MIMEText(text, 'plain', 'UTF-8'))
def print_list(x, key=''): if isinstance(x, list): y = _pformat(_arr(x)) y = y.replace(', dtype=object)', '') # y = y.replace('dtype=object,', '') y = y.replace('array(', '') y = y.replace(')', '') if '\n' in y: y = y.replace('[', '[\n ', 1) else: y = _re.sub(' +', ' ', y) y = y.replace('[', '{') y = y.replace(']', '}') print(' {}[{}] = '.format(key, len(x)), end='') print(y)
def format_dict(coll): if not coll: return if not isinstance(coll, dict) and hasattr(coll, "__iter__"): coll = dict(coll) out = list() key_len = max([len(str(x)) for x in coll]) key_len = min(48, key_len) key_len += 2 indent = " " * (key_len + 2) fmt = "%%-%ir %%s" % key_len for key in sorted(coll): value = _pformat(coll[key]) value = value.replace("\n", "\n{}".format(indent)) args = key, value out.append(fmt % args) return _os.linesep.join(out)
def format_list(coll): if not coll: return return ", ".join([_pformat(x) for x in coll])
def _debug(*pargs): formatted = (_pformat(x) for x in pargs) return _log.debug('\n\t'.join(formatted))
def pformat(obj): """pretty prints `obj` if possible""" try: return _pformat(obj) except Exception: return u'<could not parse>'