93 def __format__(self, fmt):
94 def get_width(fmt_spec):
95 """Parse fmt_spec to obtain width"""
96
97 def remove_alignment(fmt_spec):
98 if fmt_spec[1] in ["<","^",">"]:
99 fmt_spec=fmt_spec[2:len(fmt_spec)]
100 return fmt_spec
101
102 def remove_sign(fmt_spec):
103 if fmt_spec[0] in ["-","+"," "]:
104 fmt_spec=fmt_spec[1:len(fmt_spec)]
105 return fmt_spec
106
107 def remove_cross(fmt_spec):
108 if fmt_spec[0] in ["#"]:
109 fmt_spec=fmt_spec[1:len(fmt_spec)]
110 return fmt_spec
111
112 def do_width(fmt_spec):
113 n=""
114
115 while len(fmt_spec)>0:
116 if fmt_spec[0].isdigit():
117 n+=fmt_spec[0]
118 fmt_spec=fmt_spec[1:len(fmt_spec)]
119 else:
120 break
121 if n:
122 return int(n)
123 else:
124 return None
125
126 if len(fmt_spec)>=2:
127 fmt_spec=remove_alignment(fmt_spec)
128 if len(fmt_spec)>=1:
129 fmt_spec=remove_sign(fmt_spec)
130 if len(fmt_spec)>=1:
131 fmt_spec=remove_cross(fmt_spec)
132 width=do_width(fmt_spec)
133
134
135 return width
136
137 def cut(s, width, replace_string="..."):
138 """Cuts s to width and puts replace_string at it's end."""
139
140
141
142 if len(s)>width:
143 if len(replace_string)>width:
144 replace_string=replace_string[0:width]
145 s=s[0:width-len(replace_string)]
146 s=s+replace_string
147
148 return s
149
150 value=self.value
151
152
153
154 if isinstance(value, str):
155 value = value.replace("\t","|")
156 value = value.replace("\n","|")
157
158
159 value = value.__format__(fmt)
160
161
162 width = get_width(fmt)
163 if width:
164 value = cut(value, width, "...")
165
166 return value
167