Skip to content

Commit 3f42dfc

Browse files
Yusuf, MuhammedYusuf, Muhammed
Yusuf, Muhammed
authored and
Yusuf, Muhammed
committed
Added the Programs for files and exceptions
1 parent 56ad9d3 commit 3f42dfc

14 files changed

+221
-0
lines changed

classInheritance/CSE_Group.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from Student import Student as s
2+
3+
4+
class CSE_Group(s):
5+
def __init__(self):
6+
super().__init__("rock", 3)
7+
self.major = "Computer Science"
8+
9+
def printDetails(self):
10+
super().printDetails()
11+
print(str(self.name).capitalize(), "Major subject is", self.major)
12+
13+
14+
c = CSE_Group()
15+
c.printDetails()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Father:
2+
def skills(self):
3+
print("sleeping, programming")
4+
5+
6+
class Mother:
7+
def skills(self):
8+
print("scolding, cooking")
9+
10+
11+
class Son(Father, Mother):
12+
def skills(self):
13+
# in order to call Father and Mother skills we have to use class name
14+
Father.skills(self)
15+
Mother.skills(self)
16+
# By default it will call only son skills if the above line don't exist
17+
print("playing, eating")
18+
19+
20+
s = Son()
21+
s.skills()

classInheritance/Student.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Student:
2+
name = "null"
3+
roll_no = "0"
4+
5+
def __init__(self, name, roll_no):
6+
self.name = name
7+
self.roll_no = roll_no
8+
9+
def printDetails(self):
10+
print("Student Roll Number is", self.roll_no)
11+
print("Student Name is", str(self.name).capitalize())
12+
13+
14+
s1 = Student("yu", 1)
15+
s1.printDetails()
16+
s2 = Student("ay", 2)
17+
s2.printDetails()

dataStructure/CustomIterator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class TvRemote():
2+
def __init__(self):
3+
self.channels = ["HBO", "Mtv", "Sony", "Sports"]
4+
self.index = -1
5+
6+
def __iter__(self):
7+
return self
8+
9+
def __next__(self):
10+
self.index += 1
11+
if self.index == len(self.channels):
12+
raise StopIteration
13+
return self.channels[self.index]
14+
15+
16+
tv = TvRemote()
17+
itr=iter(tv)
18+
print(next(itr))
19+
print(next(itr))
20+
print(next(itr))
21+
print(next(itr))
22+
print(next(itr))
23+
24+

dataStructure/Generators.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def fib():
2+
a, b = 0, 1
3+
while True:
4+
yield a
5+
a, b = b, a + b
6+
7+
8+
for f in fib():
9+
if f > 1000:
10+
break
11+
print(f)

dataStructure/Iterators.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
s = "you are cool bro. thanks for this explanation"
2+
l = s.split()
3+
print(l)
4+
5+
itr = iter(l)
6+
print(next(itr))
7+
print(next(itr))
8+
print(next(itr))
9+
print(next(itr))
10+
print(next(itr))
11+
print(next(itr))
12+
print(next(itr))
13+
print(next(itr))
14+
try:
15+
# error in the below line since it printed all the value.
16+
print(next(itr))
17+
except StopIteration as si:
18+
print(type(si).__name__)
19+
20+
print(50*"*")
21+
print(dir(l))
22+
print(50*"*")
23+
24+
itr = reversed(l)
25+
print(next(itr))
26+
print(next(itr))
27+
print(next(itr))
28+
print(next(itr))
29+
print(next(itr))
30+
print(next(itr))
31+
print(next(itr))
32+
print(next(itr))

exceptions/CustomException.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Accident(Exception):
2+
def __init__(self, msg):
3+
self.msg = msg
4+
5+
def printException(self):
6+
print("My Custom Exception msg :", self.msg)
7+
8+
9+
try:
10+
raise Accident("I Raised Custom Exception Now")
11+
except Accident as a:
12+
a.printException()

exceptions/RaiseException.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
try:
2+
raise TypeError("I raised Type Error exception")
3+
except TypeError as te:
4+
print(te)
5+
raise ValueError("I raised Value Error exception")
6+
finally:
7+
print("I will execute. No Matter What")

exceptions/exception.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
x = input("Enter 1st Number : ")
2+
y = input("Enter 2nd Number : ")
3+
try:
4+
z = x / int(y)
5+
except TypeError as te:
6+
print("asdf", te)

exceptions/usageOfModule.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import utility
2+
3+
utility.pyramid(11)
4+
5+
6+

exceptions/utility.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def printStar():
2+
print(20 * "*")
3+
4+
5+
def printHash():
6+
print(20 * "#")
7+
8+
9+
import math as m
10+
11+
12+
def pyramid(r):
13+
for i in range(1, r):
14+
print(m.ceil(abs(r - i) // 2) * " ", i * "* ")
15+

files/ReadJson.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import json
2+
3+
s = ""
4+
with open("data.json","r") as f:
5+
s = f.read()
6+
7+
print(s)
8+
print(type(s))
9+
s = json.loads(s)
10+
print(type(s))
11+
print(s["tom"])

files/WriteFile.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# unsafe
2+
f = open("data.txt", "w")
3+
f.write("wow i can write now - overwrite now")
4+
f.close()
5+
6+
# by using "a" i can append
7+
f = open("data.txt", "a")
8+
f.write("\nwow i can append now ")
9+
f.close()
10+
11+
# safe option use withm so no need close
12+
with open("data.txt", "a") as f:
13+
f.write("\nI'm new line")
14+
15+
with open("data.txt", "r") as f:
16+
print(len(str(f.read()).split(" ")))
17+
f.seek(0)
18+
for line in f:
19+
print(line)

lesson2/argumentPassTuts.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import argparse
2+
3+
if __name__ == "__main__":
4+
parser = argparse.ArgumentParser()
5+
parser.add_argument("number1", help="first number")
6+
parser.add_argument("--number2", help="second number")
7+
parser.add_argument("--operation", help="operation", choices=["add", "sub", "mul"])
8+
9+
args = parser.parse_args()
10+
11+
print("number1", args.number1)
12+
print("number2", args.number2)
13+
print("operation", args.operation)
14+
15+
n1 = int(args.number1)
16+
n2 = int(args.number2)
17+
Result = None
18+
19+
if args.operation == "add":
20+
Result = n1 + n2
21+
elif args.operation == "sub":
22+
Result = n1 - n2
23+
elif args.operation == "mul":
24+
Result = n1 * n2
25+
print("Result is", Result)

0 commit comments

Comments
 (0)