Skip to content

Commit

Permalink
refactor: clean code
Browse files Browse the repository at this point in the history
Signed-off-by: slowy07 <[email protected]>
  • Loading branch information
slowy07 committed Jan 30, 2022
1 parent 557a684 commit f0af0c4
Show file tree
Hide file tree
Showing 388 changed files with 8,353 additions and 6,251 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import pickle


def Bdelete():
# Opening a file & loading it
F= open("studrec.dat","rb")
F = open("studrec.dat", "rb")
stud = pickle.load(F)
F.close()

print(stud)

# Deleting the Roll no. entered by user
rno= int(input("Enter the Roll no. to be deleted: "))
F= open("studrec.dat","wb")
rec= []
rno = int(input("Enter the Roll no. to be deleted: "))
F = open("studrec.dat", "wb")
rec = []
for i in stud:
if i[0] == rno:
continue
rec.append(i)
pickle.dump(rec,F)

This comment has been minimized.

Copy link
@YUSUPOVTUT

YUSUPOVTUT Jan 29, 2023

thanks

pickle.dump(rec, F)
F.close()



Bdelete()
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import pickle


def binaryread():
B=open("studrec.dat","rb")
stud=pickle.load(B)
B = open("studrec.dat", "rb")
stud = pickle.load(B)
print(stud)

# prints the whole record in nested list format
print("contents of binary file")

for ch in stud:
print(ch) #prints one of the chosen rec in list
Rno=ch[0]
Rname=ch[1] #due to unpacking the val not printed in list format
Rmark=ch[2]

print(Rno, Rname,Rmark, end="\t")

print(ch) # prints one of the chosen rec in list

Rno = ch[0]
Rname = ch[1] # due to unpacking the val not printed in list format
Rmark = ch[2]

print(Rno, Rname, Rmark, end="\t")

B.close


binaryread()
32 changes: 17 additions & 15 deletions 1 File handle/File handle binary/Update a binary file.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
#Updating records in a binary file
# Updating records in a binary file

import pickle


def update():
F=open("class.dat",'rb+')
S=pickle.load(F)
found=0
rno=int(input("enter the roll number you want to update"))
F = open("class.dat", "rb+")
S = pickle.load(F)
found = 0
rno = int(input("enter the roll number you want to update"))
for i in S:
if rno==i[0]:
print("the currrent name is",i[1])
i[1]=input("enter the new name")
found=1
if rno == i[0]:
print("the currrent name is", i[1])
i[1] = input("enter the new name")
found = 1
break

if found==0:
if found == 0:
print("Record not found")

else:
F.seek(0)
pickle.dump(S,F)
F.seek(0)
pickle.dump(S, F)

F.close()

F.close()

update()

F=open("class.dat","rb")
val=pickle.load(F)
F = open("class.dat", "rb")
val = pickle.load(F)
print(val)
F.close()
31 changes: 16 additions & 15 deletions 1 File handle/File handle binary/Update a binary file2.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
#updating records in a bnary file
# updating records in a bnary file

import pickle


def update():
F=open("studrec.dat","rb+")
value=pickle.load(F)
found=0
roll=int(input("Enter the roll number of the record"))
F = open("studrec.dat", "rb+")
value = pickle.load(F)
found = 0
roll = int(input("Enter the roll number of the record"))
for i in value:
if roll==i[0]:
if roll == i[0]:
print("current name", i[1])
print("current marks", i[2])
i[1]=input("Enter the new name")
i[2]=int(input("Enter the new marks"))
found=1
i[1] = input("Enter the new name")
i[2] = int(input("Enter the new marks"))
found = 1

if found==0:
if found == 0:
print("Record not found")

else:
pickle.dump(value,F)
pickle.dump(value, F)
F.seek(0)
newval=pickle.load(F)
newval = pickle.load(F)
print(newval)

F.close()
update()



update()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''Amit is a monitor of class XII-A and he stored the record of all
"""Amit is a monitor of class XII-A and he stored the record of all
the students of his class in a file named “class.dat”.
Structure of record is [roll number, name, percentage]. His computer
teacher has assigned the following duty to Amit
Expand All @@ -7,60 +7,71 @@
remedial class (student who scored less than 40 percent)
'''
#also find no. of children who got top marks
"""
# also find no. of children who got top marks

import pickle
F=open("class.dat",'ab')
list=[[1,"Ramya",30],[2,"vaishnavi",60],[3,"anuya",40],[4,"kamala",30],[5,"anuraag",10],[6,"Reshi",77],[7,"Biancaa.R",100],[8,"sandhya",65]]

F = open("class.dat", "ab")
list = [
[1, "Ramya", 30],
[2, "vaishnavi", 60],
[3, "anuya", 40],
[4, "kamala", 30],
[5, "anuraag", 10],
[6, "Reshi", 77],
[7, "Biancaa.R", 100],
[8, "sandhya", 65],
]

pickle.dump(list,F)

pickle.dump(list, F)
F.close()


def remcount():
F=open("class.dat","rb")
val=pickle.load(F)
count=0
F = open("class.dat", "rb")
val = pickle.load(F)
count = 0

for i in val:
if i[2]<=40:
print(i,"eligible for remedial")
count+=1
print("the total number of students are",count)
if i[2] <= 40:
print(i, "eligible for remedial")
count += 1
print("the total number of students are", count)
F.close()


remcount()


def firstmark():
F=open("class.dat",'rb')
val=pickle.load(F)
main=[]
count=0
F = open("class.dat", "rb")
val = pickle.load(F)
main = []
count = 0

for i in val:
data=i[2]
data = i[2]
main.append(data)
top=max(main)
print(top,"is the first mark")

top = max(main)
print(top, "is the first mark")

F.seek(0)
for i in val:
if top==i[2]:
if top == i[2]:
print(i)
print("congrats")
count+=1


print("the total number of students who secured top marks are",count)
count += 1

print("the total number of students who secured top marks are", count)
F.close()


firstmark()

F=open("class.dat","rb")
val=pickle.load(F)
F = open("class.dat", "rb")
val = pickle.load(F)
print(val)
F.close()



20 changes: 11 additions & 9 deletions 1 File handle/File handle binary/search record in binary file.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#binary file to search a given record
# binary file to search a given record

import pickle


def binary_search():
F=open("studrec.dat",'rb')
F = open("studrec.dat", "rb")
# your file path will be different
value=pickle.load(F)
search=0
rno=int(input("Enter the roll number of the student"))
value = pickle.load(F)
search = 0
rno = int(input("Enter the roll number of the student"))

for i in value:
if i[0]== rno:
if i[0] == rno:
print("Record found successfully")
print(i)
search=1
search = 1

if search==0:
if search == 0:
print("Sorry! record not found")
F.close()

binary_search()

binary_search()
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
def longlines():
F=open("story.txt","r")
line=F.readlines()
F = open("story.txt", "r")
line = F.readlines()

for i in line:
if len(i)<50:
print(i,end=" ")
if len(i) < 50:
print(i, end=" ")

F.close()
longlines()


longlines()
21 changes: 13 additions & 8 deletions 1 File handle/File handle text/input,output and error streams.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#practicing with streams
# practicing with streams
import sys

sys.stdout.write("Enter the name of the file")
file=sys.stdin.readline()
file = sys.stdin.readline()

F = open(file.strip(), "r")

F=open(file.strip(),"r")

while True:
ch=F.readlines()
for i in ch(): #ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
print(i,end="")
ch = F.readlines()
for (
i
) in (
ch()
): # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
print(i, end="")
else:
sys.stderr.write("End of file reached")
break
F.close()
F.close()
14 changes: 7 additions & 7 deletions 1 File handle/File handle text/question 2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
using read function
and display those words, which are less than 4 characters. """

F=open("story.txt","r")
value=F.read()
lines=value.split()
count=0
F = open("story.txt", "r")
value = F.read()
lines = value.split()
count = 0

for i in lines:
if len(i)<4:
if len(i) < 4:
print(i)
count+=1
count += 1
else:
pass

print("The total number of words with length less than 4 are",count)
print("The total number of words with length less than 4 are", count)
Loading

0 comments on commit f0af0c4

Please sign in to comment.