ksuit.utils.logging.formatting ============================== .. py:module:: ksuit.utils.logging.formatting Classes ------- .. autoapisummary:: ksuit.utils.logging.formatting.CustomFormatter Functions --------- .. autoapisummary:: ksuit.utils.logging.formatting.short_number_str ksuit.utils.logging.formatting.summarize_indices_list ksuit.utils.logging.formatting.tensor_like_to_string ksuit.utils.logging.formatting.dict_to_string ksuit.utils.logging.formatting.float_to_scientific_notation ksuit.utils.logging.formatting.seconds_to_duration_str Module Contents --------------- .. py:class:: CustomFormatter(fmt, datefmt) Bases: :py:obj:`logging.Formatter` Formatter instances are used to convert a LogRecord to text. Formatters need to know how a LogRecord is constructed. They are responsible for converting a LogRecord to (usually) a string which can be interpreted by either a human or an external system. The base Formatter allows a formatting string to be specified. If none is supplied, the style-dependent default value, "%(message)s", "{message}", or "${message}", is used. The Formatter can be initialized with a format string which makes use of knowledge of the LogRecord attributes - e.g. the default value mentioned above makes use of the fact that the user's message and arguments are pre- formatted into a LogRecord's message attribute. Currently, the useful attributes in a LogRecord are described by: %(name)s Name of the logger (logging channel) %(levelno)s Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL) %(levelname)s Text logging level for the message ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL") %(pathname)s Full pathname of the source file where the logging call was issued (if available) %(filename)s Filename portion of pathname %(module)s Module (name portion of filename) %(lineno)d Source line number where the logging call was issued (if available) %(funcName)s Function name %(created)f Time when the LogRecord was created (time.time() return value) %(asctime)s Textual time when the LogRecord was created %(msecs)d Millisecond portion of the creation time %(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time) %(thread)d Thread ID (if available) %(threadName)s Thread name (if available) %(taskName)s Task name (if available) %(process)d Process ID (if available) %(message)s The result of record.getMessage(), computed just as the record is emitted Initialize the formatter with specified format strings. Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format. Use a style parameter of '%', '{' or '$' to specify that you want to use one of %-formatting, :meth:`str.format` (``{}``) formatting or :class:`string.Template` formatting in your format string. .. versionchanged:: 3.2 Added the ``style`` parameter. .. py:attribute:: GREY :value: '\x1b[38;20m' .. py:attribute:: YELLOW :value: '\x1b[33;20m' .. py:attribute:: RED :value: '\x1b[31;20m' .. py:attribute:: BOLD_RED :value: '\x1b[31;1m' .. py:attribute:: RESET :value: '\x1b[0m' .. py:attribute:: fmt .. py:attribute:: FORMATS .. py:attribute:: datefmt .. py:method:: format(record) Format the specified record as text. The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message. .. py:function:: short_number_str(number, precision = 1) Convert a number to a short string with SI prefix, using rounding. Example: 1234567 -> 1.2M Example: 1234 -> 1.2K Example: 0.1234 -> 123.4m Example: 0.000123 -> 123.0ยต :param number: The number to convert. :param precision: The number of decimal places to include. :returns: The short string representation of the number. .. py:function:: summarize_indices_list(indices) Summarize a list of indices into ranges. Example: [0, 1, 2, 3, 6, 7, 8] -> ["0-3", "6-8"] :param indices: The list of indices to summarize. :returns: A list of strings representing the summarized indices. .. py:function:: tensor_like_to_string(tensor_or_list) Convert a list or a tensor to a string representation. Example: [1, 2, 3] -> "1, 2, 3" Example: [1.1, 2.2, 3.3] -> "1.10, 2.20, 3.30" :param tensor: Tensor or list to be converted to a string. :returns: The string representation of the tensor or list. .. py:function:: dict_to_string(obj, item_seperator = '-') Convert a dictionary to a string representation. Example: {epoch: 5, batch_size: 64} -> epoch=5-batchsize=64 :param obj: The dictionary to convert. :param item_seperator: The separator to use between items. :returns: The string representation of the dictionary. .. py:function:: float_to_scientific_notation(value, max_precision, remove_plus = True) Convert a float to scientific notation with a specified precision. Example: value = 0.0000032, max_precision = 10 -> "3.2e-6" :param value: The float to convert. :param max_precision: The maximum number of decimal places to include. :param remove_plus: Whether to remove the '+' sign from the exponent. :returns: The scientific notation string representation of the float. .. py:function:: seconds_to_duration_str(total_seconds) Convert a number of seconds to a duration string. Example: 60 * 60 * 24 * 14 + 1234 -> "14-10:20:34.00" :param total_seconds: The number of seconds to convert. :returns: The string representation of the duration.