Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print list item + integer/string using logging in Python

I'd like to print list item with the index of item such as

0: [('idx', 10), ('degree', 0)]
1: [('idx', 20), ('degree', 0)]

Based on the code below, how can I append '0:' as integer + string + list item?

import logging

class Node(object):
    __slots__= "idx", "degree"

    def __init__(self, idx, degree):
        self.idx = idx
        self.degree = 0


    def items(self):
        "dict style items"
        return [
            (field_name, getattr(self, field_name))
            for field_name in self.__slots__]

def funcA():

    a = []
    a.append(Node(10, 0))
    a.append(Node(20, 0))

    for i in range(0, len(a)):
        logging.debug(a[i].items())

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)  
    funcA()

Currently, result is

DEBUG:root:[('idx', 10), ('degree', 0)]
DEBUG:root:[('idx', 20), ('degree', 0)]

Expecting

DEBUG:root:0:[('idx', 10), ('degree', 0)]
DEBUG:root:1:[('idx', 20), ('degree', 0)]
like image 814
twfx Avatar asked Jul 17 '13 02:07

twfx


People also ask

How do I print logging INFO in Python?

Python - Print Logs in a File. If you want to print python logs in a file rather than on the console then we can do so using the basicConfig() method by providing filename and filemode as parameter. The format of the message can be specified by using format parameter in basicConfig() method.

How do I print a specific item in a list Python?

Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.

How do I log a list in Python?

You can only use log() with a single number. So you'll need to write a loop to iterate over your list and apply log() to each number. If you have a list of pressures you want the log of already produced, and given log() is the complete operation, I'm partial towards pressures_log = map(log, pressures) .

What is logging getLogger (__ Name __)?

logger = logging.getLogger(__name__) This means that logger names track the package/module hierarchy, and it's intuitively obvious where events are logged just from the logger name. Sounds like good advice.


2 Answers

with Python > 3.6 you can use fstring

logging.debug(f"{i}:{a[i].items()}")
like image 64
Vlad Bezden Avatar answered Nov 15 '22 01:11

Vlad Bezden


I would do it like this.

def funcA():
    a = []
    a.append(Node(10, 0))
    a.append(Node(20, 0))

    for i in range(0, len(a)):
        message = '%s:%s' % (i, a[i].items())
        logging.debug(message)

Which produces this as output:

DEBUG:root:0:[('idx', 10), ('degree', 0)]
DEBUG:root:1:[('idx', 20), ('degree', 0)]

You could also use join:

message = ':'.join([str(i), str(a[i].items())])

Or format:

message = '{0}:{1}'.format(str(i), a[i].items())

Whatever is the most clear to you.

like image 20
mr2ert Avatar answered Nov 15 '22 00:11

mr2ert