0

Data Structure

myshelf = {}
myshelf["PY"] ={}
myshelf["PY"]["WWW"] = {"Name": "Book1", "Class": "5th"}
myshelf["PY"]["XXX"] = {"Name": "Book2", "Class": "6th"}
myshelf["EE"] ={}
myshelf["EE"]["YYY"] = {"Name": "Book3", "Class": "7th"}
myshelf["EE"]["ZZZ"] = {"Name": "Book4", "Class": "8th"}
print(myshelf) 
for shelfkey in myshelf.keys():
for subshelf in myshelf[shelfkey].keys():
    print(myshelf[shelfkey][subshelf])

here is output of above code , which means my data structure is ok.

{'PY': {'WWW': {'Name': 'Book1', 'Class': '5th'}, 'XXX': {'Name': 'Book2', 'Class': '6th'}}, 'EE': {'YYY': {'Name': 'Book3', 'Class': '7th'}, 'ZZZ': {'Name': 'Book4', 'Class': '8th'}}}
{'Name': 'Book1', 'Class': '5th'}
{'Name': 'Book2', 'Class': '6th'}
{'Name': 'Book3', 'Class': '7th'}
{'Name': 'Book4', 'Class': '8th'}

Now i have a csv file which contains books , I want to arrange all the books into my shelved . Note myShelf have SubShelf . ShelfName is 2 letter capital of book name and my Subshelf have ISBN number of book. (Consider that ISBN number for particular book will remain same and will never change)

CSV FILE FORMAT

1   A11B12C13D14    Python Expert   English India   Raj     500 2
2   A11B12C13D14    Python Expert   English India   Raj     500 2
3   A11B12C13D14    Python Expert   English India   Raj     500 2
4   A11B12C13D16    Python Advanced English USA Amit    40000   1
5   A11B12C13D17    Aws Arch    English USA Sumit   40000   1

Now here is my main code

import csv

class Book:

def __init__(self, isbn, name, language, origin, authors, price, version):
    self.isbn = isbn
    self.name = name
    self.language = language
    self.origin = origin
    self.authors = authors
    self.price = price
    self.version = version
    self.counter= 1


class Shelf:
    def __init__(self, mycsvfilepath):
        self.shelf = {}
        self.mycsvfilepath = mycsvfilepath


        def _add_books_(self):
    with open(self.mycsvfilepath, "rt") as my_csv_file:
        my_csv_reader = csv.reader(my_csv_file)
        for line in my_csv_reader:
            mykeyforshelf = line[2][0:2].upper()
            print("ShelfName ={}".format(mykeyforshelf))
            print("SubShelfName={}".format(line[1]))
            if mykeyforshelf not in self.shelf.keys():
                self.shelf[mykeyforshelf] = {}
                self.shelf[mykeyforshelf][line[1]] = Book(line[1],line[2],line[3],line[4],line[5],line[6],line[7])
                print("Added New Book to new shelf ={} , subshelf= {}, BookName= {}".format(mykeyforshelf,line[1],self.shelf[mykeyforshelf][line[1]].name))
                print(self.shelf)
                print("\n")
            elif line[1] in self.shelf[mykeyforshelf].keys():
                print("this is existing shelf = {} and subshelf={}".format(mykeyforshelf,line[1]))
                print("existing counter for this book = {}".format(self.shelf[mykeyforshelf][line[1]].counter))
                self.shelf[mykeyforshelf][line[1]].counter = self.shelf[mykeyforshelf][line[1]].counter + 1
                print("New counter for this book = {}".format(self.shelf[mykeyforshelf][line[1]].counter))
                print(self.shelf)
                print("\n")
            else:
                print("I am existing shelf = {} , But new Sub Shelf ={}".format(mykeyforshelf,line[1]))
                self.shelf[mykeyforshelf][line[1]] = Book(line[1],line[2],line[3],line[4],line[5],line[6],line[7])
                print(self.shelf)
                print("\n")


myShelfObj = Shelf("E:\\Ashish\MyCsvFiles\\myCurrentInventory.csv")
myShelfObj._add_books_()
print("_____________________________________________________________")
print(myShelfObj)
print(type(myShelfObj))

#for shelfkey in myShelfObj:
#    for subshelf in myShelfObj.shelfkey:
#        print(myShelfObj[shelfkey][subshelf])

myCode is working file if i keep above 3 lines hashed marked. Here is output of working code.

ShelfName =PY
SubShelfName=A11B12C13D14
Added New Book to new shelf =PY , subshelf= A11B12C13D14, BookName= Python 
Expert
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>}}


ShelfName =PY
SubShelfName=A11B12C13D14
this is existing shelf = PY and subshelf=A11B12C13D14
existing counter for this book = 1
New counter for this book = 2
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>}}


ShelfName =PY
SubShelfName=A11B12C13D14
this is existing shelf = PY and subshelf=A11B12C13D14
existing counter for this book = 2
New counter for this book = 3
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>}}


ShelfName =PY
SubShelfName=A11B12C13D16
I am existing shelf = PY , But new Sub Shelf =A11B12C13D16
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>, 
'A11B12C13D16': <__main__.Book object at 0x000000FC39A1CA58>}}


ShelfName =AW
SubShelfName=A11B12C13D17
Added New Book to new shelf =AW , subshelf= A11B12C13D17, BookName= Aws Arch
{'PY': {'A11B12C13D14': <__main__.Book object at 0x000000FC39A1C940>, 
'A11B12C13D16': <__main__.Book object at 0x000000FC39A1CA58>}, 'AW': 
{'A11B12C13D17': <__main__.Book object at 0x000000FC39A1CAC8>}}


_____________________________________________________________________________
<__main__.Shelf object at 0x000000FC399C9748>
<class '__main__.Shelf'>

But as i mentioned above if i unhash last 3 lines it gives me below mentioned error:

for shelfkey in myShelfObj:
TypeError: 'Shelf' object is not iterable

Please help me.

2
  • i have changed my code a bit and instead of initializing a shelf in the class , I created it as a Global variable and it is working now for me without any issue. Commented Jul 31, 2018 at 0:10
  • But my question is unanswered. what was wrong in my first approch Commented Jul 31, 2018 at 0:11

1 Answer 1

1

See as you can see for yourself when you are typing the type of myShelfObj it is coming to be

print(type(myShelfObj))

Here is the output

<class '__main__.Shelf'>

So you are trying to iterate through a object which is not possible. You will get such error. You can only use for loops with list tuple dictionary but not an object. Hope I am clear

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.