1BASEOBJ(3) baseobj 1.2 BASEOBJ(3)
2
3
4
6 baseobj - Base object
7
9 Base class so objects will inherit the methods providing the string
10 representation of the object and methods to change the verbosity of
11 such string representation. It also includes a simple debug printing
12 and logging mechanism including methods to change the debug verbosity
13 level and methods to add debug levels.
14
16 class BaseObj(__builtin__.object)
17 Base class so objects will inherit the methods providing the string
18 representation of the object and a simple debug printing and logging
19 mechanism.
20
21 Usage:
22 from baseobj import BaseObj
23
24 # Named arguments
25 x = BaseObj(a=1, b=2)
26
27 # Dictionary argument
28 x = BaseObj({'a':1, 'b':2})
29
30 # Tuple arguments: first for keys and second for the values
31 x = BaseObj(['a', 'b'], [1, 2])
32
33 # All of the above will create an object having two attributes:
34 x.a = 1 and x.b = 2
35
36 # Add attribute name, this will be the only attribute to be displayed
37 x.set_attrlist("a")
38
39 # Add list of attribute names to be displayed in that order
40 x.set_attrlist(["a", "b"])
41
42 # Set attribute with ordered display rights
43 x.set_attr("a", 1)
44 # This is the same as
45 setattr(x, "a", 1) or x.a = 1
46 x.set_attrlist("a")
47
48 # Set attribute with switch duplicate
49 # The following creates an extra attribute "switch" with
50 # the same value as attribute "a":
51 # x.a == x.switch
52 # x.a is x.switch
53 x.set_attr("a", 1, switch=True)
54
55 # Make the current object flat by allowing all the attributes
56 # for the new attribute to be accessed directly by the current
57 # object so the following is True:
58 # x.d == x.c.d
59 x.set_attr("c", BaseObj(d=11, e=22), switch=True)
60
61 # Set the comparison attribute so x == x.a is True
62 x.set_eqattr("a")
63
64 # Set verbose level of object's string representation
65 x.debug_repr(level)
66
67 # Set string format for verbose level 1
68 x.set_strfmt(1, "arg1:{0}")
69 # In the above example the first positional argument is "a"
70 # so the str(x) gives "arg1:1"
71
72 # Set attribute shared by all instances
73 # If a global or shared attribute is set on one instance,
74 # all other instances will have access to it:
75 # y = BaseObj(d=2, e=3)
76 # then the following is true
77 # x.g == y.g
78 # x.g is y.g
79 x.set_global("g", 5)
80
81 # Set level mask to display all debug messages matching mask
82 x.debug_level(0xFF)
83
84 # Add a debug mapping for mask 0x100
85 x.debug_map(0x100, 'opts', "OPTS: ")
86
87 # Set global indentation to 4 spaces for dprint
88 x.dindent(4)
89
90 # Set global indentation to 4 spaces for displaying objects
91 x.sindent(4)
92
93 # Set global truncation to 64 for displaying string objects
94 x.strsize(64)
95
96 # Do not display timestamp for dprint messages
97 x.tstamp(enable=False)
98
99 # Change timestamp format to include the date
100 x.tstamp(fmt="{0:date:%Y-%m-%d %H:%M:%S.%q} ")
101
102 # Get timestamp if enabled, else return an empty string
103 out = x.timestamp()
104
105 # Open log file
106 x.open_log(logfile)
107
108 # Close log file
109 x.close_log()
110
111 # Write data to log file
112 x.write_log(data)
113
114 # Format the given arguments
115 out = x.format("{0:x} - {1}", 1, "hello")
116
117 # Format the object attributes set by set_attrlist()
118 out = x.format("{0:x} - {1}")
119
120 # Print debug message only if OPTS bitmap matches the current
121 # debug level mask
122 x.dprint("OPTS", "This is an OPTS debug message")
123
124
125 Methods defined here:
126 ---------------------
127
128 __eq__(self, other)
129 Comparison method: this object is treated like the attribute
130 defined by set_eqattr()
131
132 __getattr__(self, attr)
133 Return the attribute value for which the lookup has not found
134 the attribute in the usual places. It checks the internal
135 dictionary for any attribute references, it checks if this
136 is a flat object and returns the appropriate attribute.
137 And finally, if any of the attributes listed in _attrlist
138 does not exist it returns None as if they exist but not
139 defined
140
141 __init__(self, *kwts, **kwds)
142 Constructor
143
144 Initialize object's private data according to the arguments given.
145 Arguments can be given as positional, named arguments or a
146 combination of both.
147
148 __ne__(self, other)
149 Comparison method: this object is treated like the attribute
150 defined by set_eqattr()
151
152 __repr__(self)
153 String representation of object
154
155 The representation depends on the verbose level set by debug_repr().
156 If set to 0 the generic object representation is returned, else
157 the representation of the object includes all object attributes
158 and their values with proper indentation.
159
160 __str__(self)
161 Informal string representation of object
162
163 The representation depends on the verbose level set by debug_repr().
164 If set to 0 the generic object representation is returned, else
165 the representation of the object includes all object attributes
166 and their values.
167
168 close_log(self)
169 Close log file.
170
171 debug_level(self, level=0)
172 Set debug level mask.
173
174
175 level: Level to set. This could be a number or a string expression
176 of names defined by debug_map()
177
178 Examples:
179 # Set level
180 x.debug_level(0xFF)
181
182 # Set level using expression
183 x.debug_level('all')
184 x.debug_level('debug ^ 1')
185
186 dprint(self, level, msg, indent=0)
187 Print debug message if level is allowed by the verbose level
188 given in debug_level().
189
190 format(self, fmt, *kwts, **kwds)
191 Format the arguments and return the string using the format given.
192 If no arguments are given either positional or named then object
193 attributes set by set_attrlist() are used as positional arguments
194 and all object attributes are used as named arguments
195
196
197 fmt: String format to use for the arguments, where {0}, {1}, etc.
198 are used for positional arguments and {name1}, {name2}, etc.
199 are used for named arguments given after fmt.
200
201 open_log(self, logfile)
202 Open log file.
203
204 set_attr(self, name, value, switch=False)
205 Add name/value as an object attribute and add the name to the
206 list of attributes to display
207
208
209 name: Attribute name
210
211 value: Attribute value
212
213 set_attrlist(self, attr)
214 Add list of attribute names in object to display by str() or repr()
215
216
217 attr: Name or list of names to add to the list of attribute names
218 to display
219
220 set_eqattr(self, attr)
221 Set the comparison attribute
222
223
224 attr: Attribute to use for object comparison
225
226 Examples:
227 x = BaseObj(a=1, b=2)
228 x.set_eqattr("a")
229 x == 1 will return True, the same as x.a == 1
230
231 set_global(self, name, value)
232 Set global variable.
233
234 set_strfmt(self, level, format)
235 Save format for given display level
236
237
238 level: Display level given as a first argument
239
240 format:
241 String format for given display level, given as a second argument
242
243 Static methods defined here:
244 ----------------------------
245
246 debug_map(bitmap, name='', disp='')
247 Add a debug mapping.
248
249 Generic debug levels map
250 <bitmap> <name> <disp prefix>
251 0x000 'none'
252 0x001 'info' 'INFO: ' # Display info messages only
253 0x0FF 'debug' 'DBG: ' # Display info and all debug messages (0x02-0x80)
254 >0x100 user defined verbose levels
255
256 debug_repr(level=None)
257 Return or set verbose level of object's string representation.
258 When setting the verbose level, return the verbose level before
259 setting it.
260
261
262 level: Level of verbosity to set
263
264 Examples:
265 # Set verbose level to its minimal object representation
266 x.debug_repr(0)
267
268 # Object representation is a bit more verbose
269 x.debug_repr(1)
270
271 # Object representation is a lot more verbose
272 x.debug_repr(2)
273
274 dindent(indent=None)
275 Set global dprint indentation.
276
277 dprint_count()
278 Return the number of dprint messages actually displayed.
279
280 flush_log()
281 Flush data to log file.
282
283 sindent(indent=None)
284 Set global object indentation.
285
286 strsize(size)
287 Set global string truncation.
288
289 timestamp(fmt=None)
290 Return the timestamp if it is enabled.
291
292
293 fmt: Timestamp format, default is given by the format
294 set by tstamp()
295
296 tstamp(enable=None, fmt=None)
297 Enable/disable timestamps on dprint messages and/or
298 set the default format for timestamps
299
300
301 enable:
302 Boolean to enable/disable timestamps
303
304 fmt: Set timestamp format
305
306 write_log(data)
307 Write data to log file.
308
310 formatstr(3)
311
312
314 No known bugs.
315
317 Jorge Mora (mora@netapp.com)
318
319
320
321NFStest 2.1.5 14 February 2017 BASEOBJ(3)