From 267e2edbd47e9d0bf28c78b361e11114fde14252 Mon Sep 17 00:00:00 2001 From: DarkCodeOrg Date: Fri, 20 Aug 2021 19:03:26 +0530 Subject: [PATCH] classes and objects edited --- classes_objects.py | 49 +++++++++++++++++++++++++++++++++------- file_handling_4.py | 56 +++++++++++++++++++++++++++++++++++++++++++++- file_handling_5.py | 39 ++++++++++++++++++++++++++++++++ list_adv.py | 3 +++ student.csv | 3 +++ student.dat | 4 +++- 6 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 file_handling_5.py create mode 100644 student.csv diff --git a/classes_objects.py b/classes_objects.py index f45377b..590accf 100644 --- a/classes_objects.py +++ b/classes_objects.py @@ -5,8 +5,12 @@ # the properies defined in the class are called attributes and the functions are called methods + + + class Human: def __init__(self,name,age,gender): ## the init function is executed whenever a class is initiated // we can use this function to pass parameters to the object when it is created + ## this func is also called a constructor self.name = name self.age = age self.gender = gender @@ -14,17 +18,46 @@ def __init__(self,name,age,gender): ## the init function is executed whenever a def greet(self): ## self refers to the name of the object that is being creatd using this class print("Hello I am " + self.name) - def say_hi(self): + print("HI EVERYBODY !!") +class Robot: + def introduce_self(self): + print("my name is ",self.name) ## + print("my weight is " ,self.weight) + print("my address is ", self.addr) + +class Animal: + def __init__(self): + self.name = input("enter the name of the animal :") + self.age = int(input("enter the age of the animal :")) + self.habitat = input("enter the habitat of this animal :") + self.owner = input("enter the name of owner :") + self.care_taker_robo = r1 + + def info(self): + print("I am a ",self.name) + print("my age is ",self.age) + print("my owner is :",self.owner) + +H1 = Human("David",32,"male") +print(H1.name) +print(H1.age) +print(H1.gender) +H1.greet() + + + +r1 = Robot() +r1.name = "ROBO1" +r1.weight = 30 +r1.addr = "USA" +r1.introduce_self() + - -Human1 = Human("David",32,"male") -print(Human1.name) -print(Human1.age) -print(Human1.gender) -Human1.greet() -Human1.say_hi() +a1 = Animal() +a1.info() +a1.care_taker_robo.introduce_self() diff --git a/file_handling_4.py b/file_handling_4.py index 32c6af8..232d4be 100644 --- a/file_handling_4.py +++ b/file_handling_4.py @@ -1 +1,55 @@ -## \ No newline at end of file +## updating in a binary file +# +# tell() and seek() +# +import pickle +from sys import flags + +infile = open("trial.txt",'r') + +print("initially the file_pointer is at = ",infile.tell()) +print("reading 4 bytes : ",infile.read(4)) +print("now the file pointer is art :",infile.tell()) + +## the tell function gives the current position of the file pointer +print("now printing results of the seek function") + +infile.seek(10) ## default mode , puts the file pointer to the + # 10th byte from the beginning + +infile.seek(5,1) ## mode 1 , puts the file pointer to the 5th byte(forward direction) from + # the current file pointer position + +infile.seek(-10,2) ## mode 2 , puts the file pointer to the 10th byte backward from the + # end of file + +####################################################################### +#### UPDATING THE RECORDS OF A FILE AT A PARTICULAR POSITION ### +####################################################################### + +## read the file student_new.dat and give 10 marks bonus to students who have scored more that 550 +## + +upfile = open("student_new.dat","rb+") +stu = {} ## crating a empty file to store the records in the file +found = False + +try: + while True: + rpos = upfile.tell() + stu = pickle.load(upfile) + + if stu['Marks'] > 550: + stu['Marks'] += 10 + upfile.seek(rpos) + pickle.dump(stu,upfile) + found = True + +except EOFError: + if found == False: + print("no such records found") + else: + print("record(s) have been updated") + + upfile.close() + diff --git a/file_handling_5.py b/file_handling_5.py new file mode 100644 index 0000000..a47f76d --- /dev/null +++ b/file_handling_5.py @@ -0,0 +1,39 @@ +## CSV file manipulation + +import csv + + +infile = open("student.csv","ra+",newline='\n') +infile_1 = open("student.csv","ra+",newline='') + +## writing to csv files +## delimeter refers to the separator used in a csv file to separate datas + +## we have to create a writer object at first ..... csv.writer(input-file) creates the writer object +## the writer object is required for coverting user entered data into +## delimited strings +## the writer object has many methods like writerow, writerows +## etc.... +data_writer = csv.writer(infile_1) +data_writer.writerow(['Rollno','Name','Marks']) ## the coloumn headings are written here + ## the above line writes a row with Rollno,Name,Marks written ....that serves as a heading + +no_of_students = int(input("Enter the no of students :")) + +for i in range(no_of_students): + print("Student record ",i+1) + rollno = int(input("Enter the roll number of the student :")) + name = input("Enter the name of the student :") + marks = int(input("Enter the marks obtained by the student :")) + student_record = [rollno,name,marks] + data_writer.writerow(student_record) + + +## + +infile.close() +infile_1.close() + + + + diff --git a/list_adv.py b/list_adv.py index 07cb167..51710a1 100755 --- a/list_adv.py +++ b/list_adv.py @@ -72,3 +72,6 @@ def menu(): menu() +### to use this as a module in other programs uncomment all the +### lines with menu() + diff --git a/student.csv b/student.csv new file mode 100644 index 0000000..9702831 --- /dev/null +++ b/student.csv @@ -0,0 +1,3 @@ +Rollno,Name,Marks +12,ookla,356 +56,hjlls,987 diff --git a/student.dat b/student.dat index 0543745..329a354 100644 --- a/student.dat +++ b/student.dat @@ -1 +1,3 @@ -3,sdsd,32 +34,david,989 +23,dino,455 +45,gour,567