Skip to content

Commit ca487a0

Browse files
snake case folder name
1 parent 64eef66 commit ca487a0

17 files changed

+480
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STUDENTS_RECORD_FILE= "student_records.pkl"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# updating records in a binary file
2+
3+
import pickle
4+
import os
5+
6+
base = os.path.dirname(__file__)
7+
from dotenv import load_dotenv
8+
9+
load_dotenv(os.path.join(base, ".env"))
10+
student_record = os.getenv("STUDENTS_RECORD_FILE")
11+
12+
## ! Understand how pandas works internally
13+
14+
15+
def update():
16+
with open(student_record, "rb") as File:
17+
value = pickle.load(File)
18+
found = False
19+
roll = int(input("Enter the roll number of the record"))
20+
21+
for i in value:
22+
if roll == i[0]:
23+
print(f"current name {i[1]}")
24+
print(f"current marks {i[2]}")
25+
i[1] = input("Enter the new name")
26+
i[2] = int(input("Enter the new marks"))
27+
found = True
28+
29+
if not found:
30+
print("Record not found")
31+
32+
with open(student_record, "wb") as File:
33+
pickle.dump(value, File)
34+
35+
36+
update()
37+
38+
# ! Instead of AB use WB?
39+
# ! It may have memory limits while updating large files but it would be good
40+
# ! Few lakhs records would be fine and wouldn't create any much of a significant issues
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import logging
2+
import os
3+
import pickle
4+
5+
from dotenv import load_dotenv
6+
7+
base = os.path.dirname(__file__)
8+
load_dotenv(os.path.join(base, ".env"))
9+
10+
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
11+
student_record = os.getenv("STUDENTS_RECORD_FILE")
12+
13+
14+
def b_read():
15+
# Opening a file & loading it
16+
if not os.path.exists(student_record):
17+
logging.warning("File not found")
18+
return
19+
20+
with open(student_record, "rb") as F:
21+
student = pickle.load(F)
22+
logging.info("File opened successfully")
23+
logging.info("Records in the file are:")
24+
for i in student:
25+
logging.info(i)
26+
27+
28+
def b_modify():
29+
# Deleting the Roll no. entered by user
30+
if not os.path.exists(student_record):
31+
logging.warning("File not found")
32+
return
33+
roll_no = int(input("Enter the Roll No. to be deleted: "))
34+
student = 0
35+
with open(student_record, "rb") as F:
36+
student = pickle.load(F)
37+
38+
with open(student_record, "wb") as F:
39+
rec = [i for i in student if i[0] != roll_no]
40+
pickle.dump(rec, F)
41+
42+
43+
b_read()
44+
b_modify()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Amit is a monitor of class XII-A and he stored the record of all
2+
the students of his class in a file named “student_records.pkl”.
3+
Structure of record is [roll number, name, percentage]. His computer
4+
teacher has assigned the following duty to Amit
5+
6+
Write a function remcount( ) to count the number of students who need
7+
remedial class (student who scored less than 40 percent)
8+
and find the top students of the class.
9+
10+
We have to find weak students and bright students.
11+
"""
12+
13+
## Find bright students and weak students
14+
15+
from dotenv import load_dotenv
16+
import os
17+
18+
base = os.path.dirname(__file__)
19+
load_dotenv(os.path.join(base, ".env"))
20+
student_record = os.getenv("STUDENTS_RECORD_FILE")
21+
22+
import pickle
23+
import logging
24+
25+
# Define logger with info
26+
# import polar
27+
28+
29+
## ! Unoptimised rehne de abhi ke liye
30+
31+
32+
def remcount():
33+
with open(student_record, "rb") as F:
34+
val = pickle.load(F)
35+
count = 0
36+
weak_students = []
37+
38+
for student in val:
39+
if student[2] <= 40:
40+
print(f"{student} eligible for remedial")
41+
weak_students.append(student)
42+
count += 1
43+
print(f"the total number of weak students are {count}")
44+
print(f"The weak students are {weak_students}")
45+
46+
# ! highest marks is the key here first marks
47+
48+
49+
def firstmark():
50+
with open(student_record, "rb") as F:
51+
val = pickle.load(F)
52+
count = 0
53+
main = [i[2] for i in val]
54+
55+
top = max(main)
56+
print(top, "is the first mark")
57+
58+
for i in val:
59+
if top == i[2]:
60+
print(f"{i}\ncongrats")
61+
count += 1
62+
print("The total number of students who secured top marks are", count)
63+
64+
65+
remcount()
66+
firstmark()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pickle
2+
3+
4+
def binary_read():
5+
with open("studrec.dat", "rb") as b:
6+
stud = pickle.load(b)
7+
print(stud)
8+
9+
# prints the whole record in nested list format
10+
print("contents of binary file")
11+
12+
for ch in stud:
13+
print(ch) # prints one of the chosen rec in list
14+
15+
rno = ch[0]
16+
rname = ch[1] # due to unpacking the val not printed in list format
17+
rmark = ch[2]
18+
19+
print(rno, rname, rmark, end="\t")
20+
21+
22+
binary_read()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# binary file to search a given record
2+
3+
import pickle
4+
from dotenv import load_dotenv
5+
6+
7+
def search():
8+
with open("student_records.pkl", "rb") as F:
9+
# your file path will be different
10+
search = True
11+
rno = int(input("Enter the roll number of the student"))
12+
13+
for i in pickle.load(F):
14+
if i[0] == rno:
15+
print(f"Record found successfully\n{i}")
16+
search = False
17+
18+
if search:
19+
print("Sorry! record not found")
20+
21+
22+
search()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pickle
2+
import os
3+
from dotenv import load_dotenv
4+
5+
base = os.path.dirname(__file__)
6+
load_dotenv(os.path.join(base, ".env"))
7+
student_record = os.getenv("STUDENTS_RECORD_FILE")
8+
9+
10+
def update():
11+
with open(student_record, "rb") as F:
12+
S = pickle.load(F)
13+
found = False
14+
rno = int(input("enter the roll number you want to update"))
15+
16+
for i in S:
17+
if rno == i[0]:
18+
print(f"the currrent name is {i[1]}")
19+
i[1] = input("enter the new name")
20+
found = True
21+
break
22+
23+
if found:
24+
print("Record not found")
25+
26+
with open(student_record, "wb") as F:
27+
pickle.dump(S, F)
28+
29+
30+
update()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Class resposible for counting words for different files:
3+
- Reduce redundant code
4+
- Easier code management/debugging
5+
- Code readability
6+
"""
7+
8+
9+
class Counter:
10+
def __init__(self, text: str) -> None:
11+
self.text = text
12+
13+
# Define the initial count of the lower and upper case.
14+
self.count_lower = 0
15+
self.count_upper = 0
16+
self.count()
17+
18+
def count(self) -> None:
19+
for char in self.text:
20+
if char.lower():
21+
self.count_lower += 1
22+
elif char.upper():
23+
self.count_upper += 1
24+
25+
return (self.count_lower, self.count_upper)
26+
27+
def get_total_lower(self) -> int:
28+
return self.count_lower
29+
30+
def get_total_upper(self) -> int:
31+
return self.count_upper
32+
33+
def get_total(self) -> int:
34+
return self.count_lower + self.count_upper
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import time
3+
4+
file_name = input("Enter the file name to create:- ")
5+
6+
print(file_name)
7+
8+
9+
def write_to_file(file_name):
10+
if os.path.exists(file_name):
11+
print(f"Error: {file_name} already exists.")
12+
return
13+
14+
with open(file_name, "a") as F:
15+
while True:
16+
text = input("enter any text to add in the file:- ")
17+
F.write(f"{text}\n")
18+
choice = input("Do you want to enter more, y/n").lower()
19+
if choice == "n":
20+
break
21+
22+
23+
def longlines():
24+
with open(file_name, encoding="utf-8") as F:
25+
lines = F.readlines()
26+
lines_less_than_50 = list(filter(lambda line: len(line) < 50, lines))
27+
28+
if not lines_less_than_50:
29+
print("There is no line which is less than 50")
30+
else:
31+
for i in lines_less_than_50:
32+
print(i, end="\t")
33+
34+
35+
if __name__ == "__main__":
36+
write_to_file(file_name)
37+
time.sleep(1)
38+
longlines()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
hello how are you
2+
what is your name
3+
do not worry everything is alright
4+
everything will be alright
5+
please don't lose hope
6+
Wonders are on the way
7+
Take a walk in the park
8+
At the end of the day you are more important than anything else.
9+
Many moments of happiness are waiting
10+
You are amazing!
11+
If you truly believe.

0 commit comments

Comments
 (0)