From 83e8c92903a658c14e5b3f9e0fb326dd23bfb953 Mon Sep 17 00:00:00 2001 From: "guangrong.su" <13041025112@163.com> Date: Sun, 24 Dec 2017 22:07:10 +0800 Subject: [PATCH] format Vector: use ... to indicate long Vector(use '*' to showall) --- 10-seq-hacking/vector_v5.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/10-seq-hacking/vector_v5.py b/10-seq-hacking/vector_v5.py index cf327bc..ca00d44 100644 --- a/10-seq-hacking/vector_v5.py +++ b/10-seq-hacking/vector_v5.py @@ -268,7 +268,13 @@ def angle(self, n): # <2> def angles(self): # <3> return (self.angle(n) for n in range(1, len(self))) + max_format_size = 30 def __format__(self, fmt_spec=''): + showall = False + if fmt_spec.endswith('*'): + fmt_spec = fmt_spec[:-1] + showall = True + if fmt_spec.endswith('h'): # hyperspherical coordinates fmt_spec = fmt_spec[:-1] coords = itertools.chain([abs(self)], @@ -277,7 +283,14 @@ def __format__(self, fmt_spec=''): else: coords = self outer_fmt = '({})' # <6> - components = (format(c, fmt_spec) for c in coords) # <7> + + # if truncate is needed + if (not showall) and len(self) > self.max_format_size: + components = itertools.chain((format(x, fmt_spec) for index, x in enumerate(coords) + if index < self.max_format_size), ['...']) + else: + components = (format(x, fmt_spec) for x in coords) # <7> + return outer_fmt.format(', '.join(components)) # <8> @classmethod