Skip to content
Question

Q.Considering the following definition of class STOCK, write a method/function COSTLY() in python to search and display Name and Price from a pickled file STOCK.DAT, where Price of the items are more than 1000. class Stock : def init(self,N,P): self.Name=N self.Price=P def Show(self): print self.Name,"@",self.Price

(OR)
Considering the following definition of class DOCTORS, write a method/function SPLDOCS() in python to search and display all the content from a pickled file DOCS.DAT, where Specialisation of DOCTORS is "CARDIOLOGY". class DOCTORS : def init(self,N,S): self.Name=N self.Specialisation=S def Disp(self): print self.Name,"#",self.Specialisation
CBSECBSE Class XII Board 2019Subjective· 3mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): COSTLY() unpickles STOCK.DAT and displays Name and Price of items with Price > 1000.

Part (b): SPLDOCS() unpickles DOCS.DAT and displays every record whose Specialisation is "CARDIOLOGY".

Both parts read a pickled binary file of class objects, filter on one attribute, and display the matches. The reading loop is identical — pickle.load() raises EOFError at end of file, which we catch to stop.

Part (a)

The Stock class stores Name and Price. Open the file with "rb" (binary read), load objects one by one, and print those whose Price exceeds 1000.

import pickle

def COSTLY():
    with open("STOCK.DAT", "rb") as f:
        try:
            while True:
                obj = pickle.load(f)
                if obj.Price > 1000:
                    print(obj.Name, "@", obj.Price)
        except EOFError:
            pass
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.