Search code examples
pythonlisttyping

Issues while parsing the typing List in python


I am facing some issues while debugging this scenario.

What I want is , I want to copy all the contents of that return_value to the MainInfo class and return it as a string.

While doing this I am getting the error as

    for (name, data1, data2, data3, data4, data5) in range(12):
TypeError: cannot unpack non-iterable int object

I will copy the entire code bytes here.

class DataEntry(object):

name: str = ''
data1: int = 0
data2: int = 0
data3: int = 0
data4: int = 0
data5: int = 0

def __init__(self,
             name: str,
             data1: int,
             data2: int,
             data3: int,
             data4:int,
             data5: int) -> None:
    super().__init__()
    self.name = name
    self.data1 = data1
    self.data2 = data2
    self.data3 = data3
    self.data4 = data4
    self.data5 = data5

def __str__(self) -> str:
    output_string = f'name: {self.name}\n'
    output_string += f'data1: {self.data1}\n'
    output_string += f'data2: {self.data2}\n'
    output_string += f'data3: {self.data3}\n'
    output_string += f'data4: {self.data4}\n'
    output_string += f'data5: {self.data5}\n'
    return output_string


class DataInfo(object):

    data_entries: List[DataEntry] = []

    def __init__(self, data_entries: List[DataEntry] = None) -> None:
        if data_entries is None:
            data_entries = []
        self.data_entries = data_entries

    def __str__(self) -> str:
        output_string = 'DataInfo:\n'
        output_string += '-'*80
        for entry in self.data_entries:
            output_string += entry
            output_string += '-'*80
        return output_string

class MainInfo(object):

    data_info: DataInfo = None

    def __init__(self,data_info: DataInfo) -> None:
        self.data_info = data_info

    def __str__(self) -> str:
        output_string = self.data_info
        return output_string

def in_a_fun() -> MainInfo:

    return_value= fun()
    print("**** return_values - ",return_value)
    print("**** return_values - ",return_value[0][0][0])
    print("**** return_values - ",return_value[0][0][1])
    print("**** return_values - ",return_value[0][0][2])
    print("**** return_values - ",return_value[0][0][3])
    print("**** return_values - ",return_value[0][0][4])
    print("**** return_values - ",return_value[0][0][5])
    print("**** return_values - ",return_value[0][1][0])
    data_info = DataInfo()
    for (name, data1, data2, data3, data4, data5) in range(12):
        data_entry = DataEntry(name, data1, data2, data3,data4 ,data5)
    data_info.data_entries.append(data_entry)
    return MainInfo{
        data_info=data_info)

The console output for these prints are

**** return_values - [[('ABCD', 1234, 5678, 1, 1, 1), ('EFGH', 9012, 1314, 1718, 0, 10)]]
**** return_values -   ABCD
**** return_values -   1234
**** return_values -   5678
**** return_values -   1
**** return_values -   1
**** return_values -   1
**** return_values -   EFGH

Here I am pasting only two lists. But in actual it may be 12. That's why in the for-loop I have mentioned it as range(12)


Solution

  • Your error is coming from trying to unpack the first int returned by range(12). What you really want to be doing is something like:

    for i in range(12):
        # Unpack the data you want for the ith DataEntry
        name, data1, data2, ... = return_value[0][i][0], return_value[0][i][1], return_value[0][i][2], ... 
    
        # Same as before
        data_entry = DataEntry(name, data1, data2, data3,data4 ,data5)
        data_info.data_entries.append(data_entry)