BASEOBJ(3)
π NAME π
baseobj β Base object. That's the whole vibe, no notes. π
Oh and hey bestie, welcome to BASEOBJ(3) β that's section 3 of the manual, from baseobj version 1.2. Grab a snack, she's a long one, but she eats. π
πΈ DESCRIPTION πΈ
Okay so picture this: it's a base class. Which means your objects inherit a whole lil' glow-up of methods β specifically the ones that give you the string representation of the object, plus the methods to change the verbosity of that string representation. So like, you can make your object whisper or scream its own attributes, your call. π¦
And as if that wasn't enough, she ALSO ships a simple debug printing and logging mechanism, which includes methods to change the debug verbosity level and methods to add debug levels. It's giving Swiss Army knife. It's giving main character. π₯
π CLASSES π
class BaseObj(builtins.object)
Base class so objects will inherit the methods providing the string representation of the object and a simple debug printing and logging mechanism. π
β¨ Usage, aka the whole tutorial in one code block β¨
Read it like a group chat, bestie β it's all in there:
from baseobj import BaseObj
# Named arguments
x = BaseObj(a=1, b=2)
# Dictionary argument
x = BaseObj({'a':1, 'b':2})
# Tuple arguments: first for keys and second for the values
x = BaseObj(['a', 'b'], [1, 2])
# All of the above will create an object having two attributes:
x.a = 1 and x.b = 2
# Add attribute name, this will be the only attribute to be displayed
x.set_attrlist("a")
# Add list of attribute names to be displayed in that order
x.set_attrlist(["a", "b"])
# Set attribute with ordered display rights
x.set_attr("a", 1)
# This is the same as
setattr(x, "a", 1) or x.a = 1
x.set_attrlist("a")
# Set attribute with switch duplicate
# The following creates an extra attribute "switch" with
# the same value as attribute "a":
# x.a == x.switch
# x.a is x.switch
x.set_attr("a", 1, switch=True)
# Make the current object flat by allowing all the attributes
# for the new attribute to be accessed directly by the current
# object so the following is True:
# x.d == x.c.d
x.set_attr("c", BaseObj(d=11, e=22), switch=True)
# Set the comparison attribute so x == x.a is True
x.set_eqattr("a")
# Set verbose level of object's string representation
x.debug_repr(level)
# Set string format for verbose level 1
x.set_strfmt(1, "arg1:{0}")
# In the above example the first positional argument is "a"
# so the str(x) gives "arg1:1"
# Set attribute shared by all instances
# If a global or shared attribute is set on one instance,
# all other instances will have access to it:
# y = BaseObj(d=2, e=3)
# then the following is true
# x.g == y.g
# x.g is y.g
x.set_global("g", 5)
# Set level mask to display all debug messages matching mask
x.debug_level(0xFF)
# Add a debug mapping for mask 0x100
x.debug_map(0x100, 'opts', "OPTS: ")
# Set global indentation to 4 spaces for dprint
x.dindent(4)
# Set global indentation to 4 spaces for displaying objects
x.sindent(4)
# Set global truncation to 64 for displaying string objects
x.strsize(64)
# Do not display timestamp for dprint messages
x.tstamp(enable=False)
# Change timestamp format to include the date
x.tstamp(fmt="{0:date:%Y-%m-%d %H:%M:%S.%q} ")
# Get timestamp if enabled, else return an empty string
out = x.timestamp()
# Open log file
x.open_log(logfile)
# Close log file
x.close_log()
# Write data to log file
x.write_log(data)
# Format the given arguments
out = x.format("{0:x} - {1}", 1, "hello")
# Format the object attributes set by set_attrlist()
out = x.format("{0:x} - {1}")
# Print debug message only if OPTS bitmap matches the current
# debug level mask
x.dprint("OPTS", "This is an OPTS debug message")
π Methods defined here π
__eq__(self, other)β Comparison method: this object is treated like the attribute defined byset_eqattr(). π__getattr__(self, attr)β Return the attribute value for which the lookup has not found the attribute in the usual places. It checks the internal dictionary for any attribute references, it checks if this is a flat object and returns the appropriate attribute. And finally, if any of the attributes listed in_attrlistdoes not exist it returnsNoneβ as if they exist but are not defined. π__init__(self, *kwts, **kwds)β Constructor. β¨ Initialize the object's private data according to the arguments given. Arguments can be given as positional, named arguments or a combination of both.__ne__(self, other)β Comparison method: this object is treated like the attribute defined byset_eqattr(). π__repr__(self)β String representation of object. The representation depends on the verbose level set bydebug_repr(). If set to0the generic object representation is returned, else the representation of the object includes all object attributes and their values with proper indentation. π__str__(self)β Informal string representation of object. The representation depends on the verbose level set bydebug_repr(). If set to0the generic object representation is returned, else the representation of the object includes all object attributes and their values. (No fancy indentation this time, she's being informal.) π«Άclose_log(self)β Close log file. That's it, that's the method. πdebug_level(self, level=0)β Set debug level mask.level: Level to set. This could be a number or a string expression of names defined bydebug_map().
Examples, bestie:
# Set level
x.debug_level(0xFF)
# Set level using expression
x.debug_level('all')
x.debug_level('debug ^ 1')
dprint(self, level, msg, indent=0)β Print debug message iflevelis allowed by the verbose level given indebug_level(). Not allowed? Then it just doesn't print, no drama. πformat(self, fmt, *kwts, **kwds)β Format the arguments and return the string using the format given. If no arguments are given either positional or named, then object attributes set byset_attrlist()are used as positional arguments and all object attributes are used as named arguments.fmt: String format to use for the arguments, where{0},{1}, etc. are used for positional arguments and{name1},{name2}, etc. are used for named arguments given afterfmt.
open_log(self, logfile)β Open log file. πset_attr(self, name, value, switch=False)β Addname/valueas an object attribute and add the name to the list of attributes to display. πname: Attribute namevalue: Attribute value
set_attrlist(self, attr)β Add list of attribute names in object to display bystr()orrepr(). β¨attr: Name or list of names to add to the list of attribute names to display
set_eqattr(self, attr)β Set the comparison attribute. πattr: Attribute to use for object comparison
Examples:
x = BaseObj(a=1, b=2)
x.set_eqattr("a")
x == 1 will return True, the same as x.a == 1
set_global(self, name, value)β Set global variable. πset_strfmt(self, level, format)β Save format for given display level. πlevel: Display level given as a first argumentformat: String format for given display level, given as a second argument
β Static methods defined here β
debug_map(bitmap, name='', disp='')β Add a debug mapping. π¦
Generic debug levels map:
Generic debug levels map
<bitmap> <name> <disp prefix>
0x000 'none'
0x001 'info' 'INFO: ' # Display info messages only
0x0FF 'debug' 'DBG: ' # Display info and all debug messages (0x02-0x80)
>0x100 user defined verbose levels
debug_repr(level=None)β Return or set verbose level of object's string representation. When setting the verbose level, return the verbose level before setting it. Not me getting receipts on the old value. πlevel: Level of verbosity to set
Examples:
# Set verbose level to its minimal object representation
x.debug_repr(0)
# Object representation is a bit more verbose
x.debug_repr(1)
# Object representation is a lot more verbose
x.debug_repr(2)
dindent(indent=None)β Set globaldprintindentation. πdprint_count()β Return the number ofdprintmessages actually displayed. Bestie is counting. π₯flush_log()β Flush data to log file. πΈsindent(indent=None)β Set global object indentation. πstrsize(size)β Set global string truncation. βοΈ (okay, technically the emoji is mine, but the truncation is real)timestamp(fmt=None)β Return the timestamp if it is enabled.fmt: Timestamp format, default is given by the format set bytstamp()
tstamp(enable=None, fmt=None)β Enable/disable timestamps ondprintmessages and/or set the default format for timestamps. Two jobs, one method, iconic. πenable: Boolean to enable/disable timestampsfmt: Set timestamp format
write_log(data)β Write data to log file. π
π SEE ALSO π
formatstr(3) β go read her too, she's a whole vibe. π«Ά
π BUGS π
No known bugs. Literally zero. She's unbothered, she's thriving, she's flawless fr fr. β¨
πΈ AUTHOR πΈ
Jorge Mora (mora@netapp.com) β the icon, the legend, the moment. π
And that's the page, bestie! This whole thing comes to you from NFStest 3.2, dated 21 March 2023. Screenshot it, bookmark it, send it to the group chat β you're gonna want it later. Byeeee! πβ¨ππ¦
π¦ where this page comes from
- debian trixie: nfstest 3.2-3
- fedora 43: NFStest 3.2-14.fc43 Β· GPL-2.0-or-later
- fedora 44: NFStest 3.2-16.fc44 Β· GPL-2.0-or-later
- ubuntu noble: nfstest 3.2-2
- ubuntu questing: nfstest 3.2-3
- ubuntu resolute: nfstest 3.2-3build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms