1. Trang chủ
  2. » Kinh Doanh - Tiếp Thị

Making use of python phần 6 docx

42 276 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 42
Dung lượng 1,03 MB

Nội dung

#a subclass of the library class ‘books class’ def __init__(self): Author=Publisher=PageCount=ISBN=’’ #Initializes the #attributes of #the books #class def bks_method(self): #Takes input for book details ‘Enter book details’ BkFile=open(‘BookDetails’, ‘a’) #Creates and opens #a file in the #append mode libM=self.lib_method() #Calls the method of the #base class, which takes #input for three attributes, #LibCode, Title, and Price, #and returns their values BkFile.write(libM[0] + ‘,’) #Values in attributes #are written to the #file BkFile.write(libM[1] + ‘,’) Author=raw_input(‘Enter the name of the author: ‘) BkFile.write(Author + ‘,’) Publisher=raw_input(‘Enter the name of the publisher: ‘) BkFile.write(Publisher + ‘,’) ISBN=raw_input(‘Enter the ISBN: ‘) BkFile.write(ISBN + ‘,’) PageCount=raw_input(‘Enter the page count: ‘) BkFile.write(PageCount + ‘,’) BkFile.write(libM[2] + ‘\n’) BkFile.close() print ‘’’ You have entered the following details for a book: ================================================== Library code: %s Title: %s Author: %s Publisher: %s ISBN: %s Page count: %s Price: $%s’’’ % (libM[0], libM[1], Author, Publisher, ISBN, PageCount, libM[2]) #Prints the book details entered #recently def bks_display(self): #Display all the book records #available in the BookDetails #file ‘Display book details’ BkFile=open(‘BookDetails’, ‘a’) BkFile.seek(0,2) 184 Chapter 8 TEAM LinG - Live, Informative, Non-cost and Genuine! BkFileLen=BkFile.tell() if BkFileLen == 0L: #Check if the length of the #file is zero print print print ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ print ‘xxxxxx NO RECORDS AVAILABLE xxxxxx’ print ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ print print BkFile.close() else: BkFile=open(‘BookDetails’, ‘r’) #Opens the file #in read mode #to print all #its records print print print ‘##################################’ print ‘########## BOOK DETAILS ##########’ print ‘##################################’ print end=0 record=1 while not end: BkDet=BkFile.readline() if BkDet != ‘’: print print ‘Record number: %s’ % (record) print ‘================’ print BkDet record = record + 1 else: print print ‘*********************************’ print ‘********** END OF FILE **********’ print ‘*********************************’ print print end=1 BkFile.close() class software(library): #Defines the library class, #which is a subclass of the #library class ‘software class’ def __init__(self): ‘software class constructor’ ProductOf=Size=’’ #Initializes the attributes of #the library class def sws_method(self): #Takes input for software Object-Oriented Programming 185 TEAM LinG - Live, Informative, Non-cost and Genuine! #details ‘Enter software details’ SwFile=open(‘SoftwareDetails’, ‘a’) libM=self.lib_method() #Calls the method of the #base class, which takes #input for three #attributes, LibCode, #Title, and Price, and #returns their values SwFile.write(libM[0] + ‘,’) SwFile.write(libM[1] + ‘,’) ProductOf=raw_input(‘Enter the name of the software vendor: ‘) SwFile.write(ProductOf + ‘,’) Size=raw_input(‘Enter the size of the software (in MB): ‘) SwFile.write(Size + ‘,’) SwFile.write(libM[2] + ‘\n’) SwFile.close() print ‘’’ You have entered the following details for a book: ================================================== Library code: %s Title: %s Vendor: %s Size: %sMB Price: $%s’’’ % (libM[0],libM[1],ProductOf,Size,libM[2]) def sws_display(self): #Displays all software records #available in the #SoftwareDetails file ‘Display software details’ SwFile=open(‘SoftwareDetails’, ‘a’) SwFile.seek(0,2) SwFileLen=SwFile.tell() if SwFileLen == 0L: #Check if the length of the #file is zero print print print ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ print ‘xxxxxx NO RECORDS AVAILABLE xxxxxx’ print ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ print print SwFile.close() else: SwFile=open(‘SoftwareDetails’, ‘r’) print print print ‘##################################’ print ‘######## SOFTWARE DETAILS ########’ print ‘##################################’ print SwFile=open(‘SoftwareDetails’, ‘r’) 186 Chapter 8 TEAM LinG - Live, Informative, Non-cost and Genuine! end=0 record=1 while not end: SwDet=SwFile.readline() if SwDet != ‘’: print print ‘Record number: %s’ % (record) print ‘===============’ print SwDet record = record + 1 else: print print ‘*********************************’ print ‘********** END OF FILE **********’ print ‘*********************************’ print print end=1 SwFile.close() def MainMenu(): #Displays the main menu, takes input for #choice, and calls an appropriate method #based on the choice MenuItems=’’’ <#><#><#><#><#><#><#><#><#> <#><#><#><#><#><#><#><#><#> <#> <#> <#> TECHSITY UNIVERSITY <#> <#> LIBRARY <#> <#> <#> <#><#><#><#><#><#><#><#><#> <#><#><#><#><#><#><#><#><#> MAIN MENU ========= 1 Enter details for books 2 Enter details for software 3 View details of books 4 View details of software 5 Delete all book records 6 Delete all software records 7 Quit Enter choice (1-7): ‘’’ done=0 while not done: MenuChoice=raw_input(MenuItems) #Asks input for #choice ClearScreen = os.system(‘clear’) print ‘You entered: %s’ % MenuChoice if MenuChoice not in ‘1234567’: #Checks if the #choice is correct print print ‘Wrong choice. Enter 1, 2, 3, 4, 5, 6, or 7.’ Object-Oriented Programming 187 TEAM LinG - Live, Informative, Non-cost and Genuine! print else: if MenuChoice ==’7’: #Quits if the choice is #7 done=1 if MenuChoice ==’1’: print print print ‘ ENTER BOOK DETAILS’ print ‘ ==================’ print bk.bks_method() #Calls bks_method() of the #books class to accept book #details bk.clear_screen_method() #Calls the #clear_screen_method() #of the library class #to clear the screen if MenuChoice ==’2’: print print print ‘ ENTER SOFTWARE DETAILS’ print ‘ ======================’ print sw.sws_method() #Calls sws_method() of the #software class to accept #software details sw.clear_screen_method() #Calls the #clear_screen_method() #of the library class #to clear the screen if MenuChoice ==’3’: bk.bks_display() #Calls bks_display() of #the books class to #display all book #records bk.clear_screen_method() if MenuChoice ==’4’: sw.sws_display() #Calls sws_display() of #the software class to #display all software #records sw.clear_screen_method() if MenuChoice ==’5’: bk.empty_file_method(‘BookDetails’) #Calls empty_file_method() of the library #class and passes the name of the file to #delete all its records bk.clear_screen_method() 188 Chapter 8 TEAM LinG - Live, Informative, Non-cost and Genuine! if MenuChoice ==’6’: sw.empty_file_method(‘SoftwareDetails’) #Calls empty_file_method() of the library class #and passes the name of the file to delete all #its records sw.clear_screen_method() bk=books() #Creates instance of the books class sw=software() #Creates instance of the software class MainMenu() #Calls the MainMenu() function Execute the Code To be able to implement or view the output of the code to automate the books and soft- ware sections of the Techsity University library, you need to execute the following steps: 1. Write the preceding code in a text editor and save it with the .py extension. 2. At the shell prompt, type python followed by the name of the file if the file is in the current directory. 3. Use the Main menu (see Figure 8.2) to add, view, and delete details about books and software. Figure 8.2 The main menu. Object-Oriented Programming 189 TEAM LinG - Live, Informative, Non-cost and Genuine! Summary In this chapter, you learned the following: ■■ The object-oriented approach to programming has changed the way programs are written today. ■■ Object-oriented programming (OOP) has the following two major components: ■■ Objects ■■ Classes ■■ OOP has the following benefits: ■■ Models the real world ■■ Allows code reusability ■■ Is favorable to change ■■ In Python, all the data types are objects, and the word “object” need not mean an instance of a class. ■■ Python classes are data structures used to define objects. ■■ You can work with class objects by performing the following two types of operations: ■■ Creating attribute references ■■ Creating an instance of a class ■■ A class attribute is an element of a class. ■■ The class attributes belong to the class in which they are defined. ■■ The class attributes are of the following two types: ■■ Data attributes ■■ Functional attributes ■■ Data attributes are commonly known as static members or class variables and are set when the class is created. ■■ Functional attributes or method class attributes are the class methods. ■■ Methods can be invoked only by using an instance of the class to which they belong. ■■ A class instance is a variable that contains a reference to a class. ■■ The process of creating an instance of a class is known as instantiation. ■■ __init__() is a constructor or a special method that can be defined in a class to create objects in the initial state. ■■ The __init__() special method has self as the first argument like any other function or method defined in Python. 190 Chapter 8 TEAM LinG - Live, Informative, Non-cost and Genuine! ■■ Classes can be implemented in the following two ways: ■■ Composition ■■ Derivation ■■ In composition, classes are combined to create a code that provides better functionality. ■■ Derivation provides a powerful feature of OOP, which allows for the use of the features and behavior of a class by another class without disturbing the rest of the program. ■■ The term “subclass” describes a class that inherits or derives the attributes from another class. ■■ The term “base class” describes a class from which a subclass has been derived. ■■ Subclasses inherit most of the attributes of their base classes. ■■ Inheritance is the property by which a subclass derives the attributes of the base class. ■■ In inheritance, the base class is also termed as the parent and the subclass as the child. ■■ When a subclass is inherited from multiple base classes, it is known as multiple inheritance. ■■ There might be times when you use the same names for methods in the base class and the subclasses. In such a situation, the methods in the base classes override the methods of their subclasses. This is known as method overriding. ■■ Python has some of the following common built-in functions for OOP: ■■ dir() ■■ var() ■■ isinstance() ■■ issubclass() ■■ hasattr() ■■ getattr() ■■ setattr() ■■ delattr() ■■ Python allows you to modify, add, or remove some functionality to an existing object, such as a data type or some code by packaging the object. This is known as wrapping. ■■ Delegation is a characteristic of wrapping that uses the existing functionality of the type to enable code reusability. Object-Oriented Programming 191 TEAM LinG - Live, Informative, Non-cost and Genuine! TEAM LinG - Live, Informative, Non-cost and Genuine! 193 CHAPTER 9 OBJECTIVES: In this chapter, you will learn to do the following: ߜ Identify basics of exceptions ߜ Identify standard exceptions in Python ߜ Handle exceptions ߜ Raise exceptions ߜ Create user-defined exceptions Getting Started While shopping for fruits and vegetables from a grocery shop, will you buy them if they are rotten? Imagine that, while you are driving, your car stops after every little pebble that comes your way and you have to restart it every time. Similarly, errors in the program execution may cause your program to come to a fatal stop or may produce garbage output. In such a situation, you might have to re-execute the program to show the output or correct the error that caused the problem. It’s nice that your car is designed with features that handle little hurdles very well. In the same way, you can also construct your programs to handle possible errors. Exception Handling CHAPTER TEAM LinG - Live, Informative, Non-cost and Genuine! [...]... regardless of the browser used As a result, an important feature of applications is cross-browser support Cross-browser support ensures a uniform display of content independent of the browser or platform used This feature of Web applications enables you to view pages in the correct format because of Web applications’ compatibility with both browsers, Netscape Navigator and Microsoft Internet Explorer Because... as a client and a server Mode of Data Transmission Both of the requirements of assigning an address to the destination and providing a method of transmitting data can be taken care of by a set of rules that govern the sending and receiving of data over a network called protocols Some examples of network protocols are TCP/IP, UDP, Apple Talk, and Net BEUI The Internet uses the TCP/IP protocol to transfer... studobjects.append(Student(‘Tom’,’5552383745’,4000)) studobjects.append(Student(‘Mac’, 64 7 863 8323’,4500,22)) studobjects.append(Student(‘Leonard’,’8485242 263 ’ ,65 00,19,0)) ctr=0 r=os.system(“clear”) try: #start of the try block while ctr . ‘,’) SwFile.write(libM[1] + ‘,’) ProductOf=raw_input(‘Enter the name of the software vendor: ‘) SwFile.write(ProductOf + ‘,’) Size=raw_input(‘Enter the size of the software (in MB): ‘) SwFile.write(Size. MENU ========= 1 Enter details for books 2 Enter details for software 3 View details of books 4 View details of software 5 Delete all book records 6 Delete all software records 7 Quit Enter choice (1-7): ‘’’ done=0 while. (libM[0],libM[1],ProductOf,Size,libM[2]) def sws_display(self): #Displays all software records #available in the #SoftwareDetails file ‘Display software details’ SwFile=open(‘SoftwareDetails’, ‘a’) SwFile.seek(0,2) SwFileLen=SwFile.tell() if

Ngày đăng: 09/08/2014, 16:20

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN