Source code for abcd.table

from __future__ import print_function

import collections
import numpy as np
import time
from prettytable import PrettyTable
from .util import atoms2dict, filter_keys

__author__ = 'Patrick Szmucer'


[docs]def trim(val, length): '''Trim the string if it's longer than "length" (and add dots at the end)''' s = str(val) if length == -1 or (len(s) <= length+1): return s else: return (s[:length] + '..')
[docs]def atoms_list2dict(atoms_it): '''Converts an Atoms iterator into a plain, one-level-deep list of dicts''' dicts = [] for atoms in atoms_it: dct = atoms2dict(atoms, plain_arrays=True) if 'info' in dct and dct['info']: for key, value in dct['info'].items(): dct[key] = dct['info'][key] if 'arrays' in dct and dct['arrays']: for key, value in dct['arrays'].items(): dct[key] = dct['arrays'][key] dct.pop('info', None) dct.pop('arrays', None) dicts.append(dct) return dicts
[docs]def format_value(value, key): '''Applies special formatting for some key-value pairs''' v = value if key == 'c_time' or key == 'm_time': v = time.strftime('%d%b%y %H:%M', time.localtime(value)) elif key == 'pbc': if isinstance(v, collections.Container): v = '' for a in value: v += 'T' if a else 'F' else: v += 'T' if value else 'F' elif key == 'original_files' or key == 'original_file_contents': v = '<file>' return v