Skip to content

Commit 55dcc6c

Browse files
Merge pull request #1 from rahulgurujala/sourcery/master
refactored master branch
2 parents a2fc6fe + 24d5068 commit 55dcc6c

File tree

307 files changed

+1892
-2898
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

307 files changed

+1892
-2898
lines changed

1 File handle/File handle binary/Deleting record in a binary file.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ def bdelete():
1010
# Deleting the Roll no. entered by user
1111
rno = int(input("Enter the Roll no. to be deleted: "))
1212
with open("studrec.dat") as F:
13-
rec = []
14-
for i in stud:
15-
if i[0] == rno:
16-
continue
17-
rec.append(i)
13+
rec = [i for i in stud if i[0] != rno]
1814
pickle.dump(rec, F)
1915

2016

1 File handle/File handle binary/Update a binary file.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,27 @@
44

55

66
def update():
7-
F = open("class.dat", "rb+")
8-
S = pickle.load(F)
9-
found = 0
10-
rno = int(input("enter the roll number you want to update"))
11-
for i in S:
12-
if rno == i[0]:
13-
print("the currrent name is", i[1])
14-
i[1] = input("enter the new name")
15-
found = 1
16-
break
7+
with open("class.dat", "rb+") as F:
8+
S = pickle.load(F)
9+
found = 0
10+
rno = int(input("enter the roll number you want to update"))
11+
for i in S:
12+
if rno == i[0]:
13+
print("the currrent name is", i[1])
14+
i[1] = input("enter the new name")
15+
found = 1
16+
break
1717

18-
if found == 0:
19-
print("Record not found")
18+
if found == 0:
19+
print("Record not found")
2020

21-
else:
22-
F.seek(0)
23-
pickle.dump(S, F)
24-
25-
F.close()
21+
else:
22+
F.seek(0)
23+
pickle.dump(S, F)
2624

2725

2826
update()
2927

30-
F = open("class.dat", "rb")
31-
val = pickle.load(F)
32-
print(val)
33-
F.close()
28+
with open("class.dat", "rb") as F:
29+
val = pickle.load(F)
30+
print(val)

1 File handle/File handle binary/Update a binary file2.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@
44

55

66
def update():
7-
F = open("studrec.dat", "rb+")
8-
value = pickle.load(F)
9-
found = 0
10-
roll = int(input("Enter the roll number of the record"))
11-
for i in value:
12-
if roll == i[0]:
13-
print("current name", i[1])
14-
print("current marks", i[2])
15-
i[1] = input("Enter the new name")
16-
i[2] = int(input("Enter the new marks"))
17-
found = 1
18-
19-
if found == 0:
20-
print("Record not found")
21-
22-
else:
23-
pickle.dump(value, F)
24-
F.seek(0)
25-
newval = pickle.load(F)
26-
print(newval)
27-
28-
F.close()
7+
with open("studrec.dat", "rb+") as F:
8+
value = pickle.load(F)
9+
found = 0
10+
roll = int(input("Enter the roll number of the record"))
11+
for i in value:
12+
if roll == i[0]:
13+
print("current name", i[1])
14+
print("current marks", i[2])
15+
i[1] = input("Enter the new name")
16+
i[2] = int(input("Enter the new marks"))
17+
found = 1
18+
19+
if found == 0:
20+
print("Record not found")
21+
22+
else:
23+
pickle.dump(value, F)
24+
F.seek(0)
25+
newval = pickle.load(F)
26+
print(newval)
2927

3028

3129
update()

1 File handle/File handle binary/question 1 (elegible for remedial, top marks).py

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,67 @@
88
99
1010
"""
11+
1112
# also find no. of children who got top marks
1213

1314
import pickle
1415

15-
F = open("class.dat", "ab")
16-
list = [
17-
[1, "Ramya", 30],
18-
[2, "vaishnavi", 60],
19-
[3, "anuya", 40],
20-
[4, "kamala", 30],
21-
[5, "anuraag", 10],
22-
[6, "Reshi", 77],
23-
[7, "Biancaa.R", 100],
24-
[8, "sandhya", 65],
25-
]
16+
with open("class.dat", "ab") as F:
17+
list = [
18+
[1, "Ramya", 30],
19+
[2, "vaishnavi", 60],
20+
[3, "anuya", 40],
21+
[4, "kamala", 30],
22+
[5, "anuraag", 10],
23+
[6, "Reshi", 77],
24+
[7, "Biancaa.R", 100],
25+
[8, "sandhya", 65],
26+
]
2627

2728

28-
pickle.dump(list, F)
29-
F.close()
29+
pickle.dump(list, F)
3030

3131

3232
def remcount():
33-
F = open("class.dat", "rb")
34-
val = pickle.load(F)
35-
count = 0
33+
with open("class.dat", "rb") as F:
34+
val = pickle.load(F)
35+
count = 0
3636

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

4443

4544
remcount()
4645

4746

4847
def firstmark():
49-
F = open("class.dat", "rb")
50-
val = pickle.load(F)
51-
main = []
52-
count = 0
48+
with open("class.dat", "rb") as F:
49+
val = pickle.load(F)
50+
main = []
51+
count = 0
5352

54-
for i in val:
55-
data = i[2]
56-
main.append(data)
53+
for i in val:
54+
data = i[2]
55+
main.append(data)
5756

58-
top = max(main)
59-
print(top, "is the first mark")
57+
top = max(main)
58+
print(top, "is the first mark")
6059

61-
F.seek(0)
62-
for i in val:
63-
if top == i[2]:
64-
print(i)
65-
print("congrats")
66-
count += 1
60+
F.seek(0)
61+
for i in val:
62+
if top == i[2]:
63+
print(i)
64+
print("congrats")
65+
count += 1
6766

68-
print("the total number of students who secured top marks are", count)
69-
F.close()
67+
print("the total number of students who secured top marks are", count)
7068

7169

7270
firstmark()
7371

74-
F = open("class.dat", "rb")
75-
val = pickle.load(F)
76-
print(val)
77-
F.close()
72+
with open("class.dat", "rb") as F:
73+
val = pickle.load(F)
74+
print(val)

1 File handle/File handle binary/search record in binary file.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44

55

66
def binary_search():
7-
F = open("studrec.dat", "rb")
8-
# your file path will be different
9-
value = pickle.load(F)
10-
search = 0
11-
rno = int(input("Enter the roll number of the student"))
7+
with open("studrec.dat", "rb") as F:
8+
# your file path will be different
9+
value = pickle.load(F)
10+
search = 0
11+
rno = int(input("Enter the roll number of the student"))
1212

13-
for i in value:
14-
if i[0] == rno:
15-
print("Record found successfully")
16-
print(i)
17-
search = 1
13+
for i in value:
14+
if i[0] == rno:
15+
print("Record found successfully")
16+
print(i)
17+
search = 1
1818

19-
if search == 0:
20-
print("Sorry! record not found")
21-
F.close()
19+
if search == 0:
20+
print("Sorry! record not found")
2221

2322

2423
binary_search()

1 File handle/File handle text/input,output and error streams.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
while True:
1010
ch = F.readlines()
11-
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
11+
for i in ch:
1212
print(i, end="")
13-
else:
14-
sys.stderr.write("End of file reached")
15-
break
13+
sys.stderr.write("End of file reached")
14+
break
1615

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
F = open("happy.txt", "r")
2-
# method 1
3-
val = F.read()
4-
val = val.split()
5-
for i in val:
6-
print(i, "*", end="")
7-
print("\n")
1+
with open("happy.txt", "r") as F:
2+
# method 1
3+
val = F.read()
4+
val = val.split()
5+
for i in val:
6+
print(i, "*", end="")
7+
print("\n")
88

99

10-
# method 2
11-
F.seek(0)
12-
value = F.readlines()
13-
for line in value:
14-
for word in line.split():
15-
print(word, "*", end="")
16-
F.close()
10+
# method 2
11+
F.seek(0)
12+
value = F.readlines()
13+
for line in value:
14+
for word in line.split():
15+
print(word, "*", end="")

A solution to project euler problem 3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def solution(n: int = 600851475143) -> int:
3636
TypeError: Parameter n must be int or passive of cast to int.
3737
"""
3838
try:
39-
n = int(n)
39+
n = n
4040
except (TypeError, ValueError):
4141
raise TypeError("Parameter n must be int or passive of cast to int.")
4242
if n <= 0:
@@ -55,7 +55,7 @@ def solution(n: int = 600851475143) -> int:
5555
ans = i
5656

5757
while n % i == 0:
58-
n = n / i
58+
n /= i
5959

6060
i += 1
6161

AreaOfTriangle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
# calculate the area
1616
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
17-
print('The area of the triangle is: ' + area)
17+
print(f'The area of the triangle is: {area}')

Assembler/assembler.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ def loadFile(fileName):
5353
loadFile: This function loads the file and reads its lines.
5454
"""
5555
global lines
56-
fo = open(fileName)
57-
for line in fo:
58-
lines.append(line)
59-
fo.close()
56+
with open(fileName) as fo:
57+
for line in fo:
58+
lines.append(line)
6059

6160

6261
def scanner(string):

Automated Scheduled Call Reminders/caller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def search():
4242
timestamp = datetime.now().strftime("%H:%M")
4343
five_minutes_prior = (timestamp + timedelta(minutes=5)).strftime("%H:%M")
4444
for doc in list_of_docs:
45-
if doc["from"][0:5] == five_minutes_prior:
45+
if doc["from"][:5] == five_minutes_prior:
4646
phone_number = doc["phone"]
4747
call = client.calls.create(
4848
to=phone_number,

Base Converter Number system.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
def base_check(xnumber, xbase):
2-
for char in xnumber[len(xnumber ) -1]:
3-
if int(char) >= int(xbase):
4-
return False
5-
return True
2+
return all(int(char) < int(xbase) for char in xnumber[len(xnumber ) -1])
63

74
def convert_from_10(xnumber, xbase, arr, ybase):
8-
if int(xbase) == 2 or int(xbase) == 4 or int(xbase) == 6 or int(xbase) == 8:
5+
if int(xbase) in {2, 4, 6, 8}:
96

107
if xnumber == 0:
118
return arr
12-
else:
13-
quotient = int(xnumber) // int(xbase)
14-
remainder = int(xnumber) % int(xbase)
15-
arr.append(remainder)
16-
dividend = quotient
17-
convert_from_10(dividend, xbase, arr, base)
9+
quotient, remainder = divmod(int(xnumber), int(xbase))
10+
arr.append(remainder)
11+
dividend = quotient
12+
convert_from_10(dividend, xbase, arr, base)
1813
elif int(xbase) == 16:
1914
if int(xnumber) == 0:
2015
return arr
21-
else:
22-
quotient = int(xnumber) // int(xbase)
23-
remainder = int(xnumber) % int(xbase)
24-
if remainder > 9:
25-
if remainder == 10: remainder = 'A'
26-
if remainder == 11: remainder = 'B'
27-
if remainder == 12: remainder = 'C'
28-
if remainder == 13: remainder = 'D'
29-
if remainder == 14: remainder = 'E'
30-
if remainder == 15: remainder = 'F'
31-
arr.append(remainder)
32-
dividend = quotient
33-
convert_from_10(dividend, xbase, arr, ybase)
16+
quotient, remainder = divmod(int(xnumber), int(xbase))
17+
if remainder > 9:
18+
if remainder == 10: remainder = 'A'
19+
if remainder == 11: remainder = 'B'
20+
if remainder == 12: remainder = 'C'
21+
if remainder == 13: remainder = 'D'
22+
if remainder == 14: remainder = 'E'
23+
if remainder == 15: remainder = 'F'
24+
arr.append(remainder)
25+
dividend = quotient
26+
convert_from_10(dividend, xbase, arr, ybase)
3427
def convert_to_10(xnumber, xbase, arr, ybase):
3528
if int(xbase) == 10:
3629
for char in xnumber:

0 commit comments

Comments
 (0)