-1

I looked at this example and tried to write a class which holds the header information. From another class, I would call this class and get the dictionary to use.

# -*- coding: utf-8 -*-
import binascii
import codecs
from datetime import datetime
from collections import defaultdict

class HeaderInfo(dict, object):
    def __init__(self):
        header_dict = defaultdict(list) # This shall hold all the issues in the parsed file
        # super(HeaderInfo, self).__init__(file)

        self._object = "C:\Sample.log"
        file = open(self._object, "rb")

        self._dict = {}

        byte = file.read(4)
        logFileSize = int.from_bytes(byte, "little")
        header_dict = self.add('logFileSize', logFileSize)
        dict = self.add('logFileSize', logFileSize)

        # print((dict))

        byte = file.read(20)
        hexadecimal = binascii.hexlify(byte)
        header_dict = self.add('fileType', codecs.decode(hexadecimal, "hex").decode('ascii'))
        dict = self.add('fileType', codecs.decode(hexadecimal, "hex").decode('ascii'))

        # print((dict))

        byte = file.read(5)
        hexadecimal = binascii.hexlify(byte)
        header_dict = self.add('fileVersion', codecs.decode(hexadecimal, "hex").decode('ascii'))
        dict = self.add('fileVersion', codecs.decode(hexadecimal, "hex").decode('ascii'))

        # print((dict))

        byte = file.read(10)
        hexadecimal = binascii.hexlify(byte)
        header_dict = self.add('fileNumber', codecs.decode(hexadecimal, "hex").decode('ascii'))
        dict = self.add('fileNumber', codecs.decode(hexadecimal, "hex").decode('ascii'))



    def add(self, id, val):
        self._dict[id] = val
        return self._dict

# print the data    
hi = HeaderInfo()    

print(hi)

when I tried with print statements,the data is printed, but

hi = HeaderInfo()

, doesn't return anyvalue in "hi".

any idea to return the dict value if HeaderInfo() is called?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user301016
  • 2,207
  • 7
  • 36
  • 50
  • 2
    All you're doing is set a bunch of local variables and/or the arbitrary attribute `self._dict`. You'll need to modify `self` if you want the data to become part of the instance. E.g. `self['foo'] = 'bar'`. – deceze Feb 06 '20 at 08:12
  • 1
    __init__ function is a constructor function and it doesn't return anything if you want to return a dictionary from your class just use it like "hi._dict" – ikibir Feb 06 '20 at 08:12

1 Answers1

2

hi is a variable pointing to an instance of your class HeaderInfo - it is not the internal dictionary self._dict of your class.

You essentially have a class thats a dict that also has a member that is a dict - you fill the member - not the class itself.

Printing hi won't show whats in your self._dict, unless you overwrite the def __str__(self) and maybe def __repl__(self) methods of your class to customize how your class is printed (by itself/inside lists).

To print the member, add

def Get_Data(self):  # or some other name
    return self._dict

to your class and use

print(hi.Get_Data())

to view whats inside your members dictionary.

If you want to store stuff in you class itself, change your add method

def add(self, id, val):
    self[id] = val             # store it inside yourself, not your member
    return self                # this is questionalbe - as is the whole method
                               # if you do not want to make it part of your public api
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69