GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions | Data Fields
str_methods.ClassWithCutting__format__ Class Reference

Public Member Functions

 __init__ (self, value)
 
 __format__ (self, fmt)
 

Data Fields

 value
 

Detailed Description

This class provides a __format__ method which cuts values to a certain width.

If width is too big '...' will be put at the end of the resulting string.

Definition at line 85 of file str_methods.py.

Constructor & Destructor Documentation

◆ __init__()

str_methods.ClassWithCutting__format__.__init__ (   self,
  value 
)

Definition at line 90 of file str_methods.py.

90 def __init__(self,value):
91 self.value = value
92

Member Function Documentation

◆ __format__()

str_methods.ClassWithCutting__format__.__format__ (   self,
  fmt 
)

Definition at line 93 of file str_methods.py.

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 # Stop parsing here for we only need width
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 #s=s.decode('UTF-8', "replace")
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 # Replace Tabs and linebreaks
153 #import types
154 if isinstance(value, str):
155 value = value.replace("\t","|")
156 value = value.replace("\n","|")
157
158 # Do regular formatting of object
159 value = value.__format__(fmt)
160
161 # Cut resulting value if longer than specified by width
162 width = get_width(fmt)
163 if width:
164 value = cut(value, width, "...")
165
166 return value
167

Field Documentation

◆ value

str_methods.ClassWithCutting__format__.value

Definition at line 91 of file str_methods.py.


The documentation for this class was generated from the following file: