Skip to content

Commit 9a8f094

Browse files
authored
Added OOPs and Functions
1 parent ce5c2ef commit 9a8f094

21 files changed

Lines changed: 2155 additions & 0 deletions

DAY_19/Exception_Handling_15_06_23.py

Lines changed: 417 additions & 0 deletions
Large diffs are not rendered by default.

DAY_20/Decorator.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
# Decorators : It is nothing but one function
3+
4+
# stage 1: take agrument in form of function reference
5+
# stage 2: having inner function
6+
# stage 3: return inner function reference
7+
8+
# ===========================================================================================
9+
10+
print(40 * '=')
11+
12+
# Program 1 :
13+
14+
def deco_func(take_func_ref): # stage 1: take agrument in form of function reference
15+
def inner_func(): # stage 2: having inner function
16+
print("This is inner function of decorator")
17+
take_func_ref() # stage 3: calling function reference
18+
19+
return inner_func # stage 4: return inner function reference
20+
21+
def func():
22+
print("This is normal function")
23+
24+
func = deco_func(func)
25+
func()
26+
27+
28+
# ===========================================================================================
29+
30+
print(40 * '=')
31+
32+
# Program 2 :
33+
34+
def deco_func(take_func_ref):
35+
def inner_func():
36+
print("This is inner function of decorator")
37+
take_func_ref()
38+
39+
return inner_func
40+
41+
@deco_func # other way to call decorator
42+
def func():
43+
print("This is normal function")
44+
45+
46+
func()
47+
48+
49+
# ===========================================================================================
50+
51+
print(40 * '=')
52+
53+
# Program 3 :
54+
55+
def swap(divde_ref):
56+
def inner(*args, **kwargs):
57+
# print(args,kwargs)
58+
num1 = args[0]
59+
num2 = args[1]
60+
if num2 > num1:
61+
num1,num2 = num2,num1
62+
63+
divde_ref(num1, num2)
64+
65+
return inner
66+
67+
@swap
68+
def divide(num1,num2):
69+
print(num1 // num2)
70+
71+
divide(2,10)
72+
73+
74+
# ===========================================================================================
75+
76+
print(40 * '=')
77+
78+
# Program 4:
79+
80+
def repeat(n):
81+
def decorator(func):
82+
def wrapper(*args, **kwargs):
83+
for _ in range(n):
84+
result = func(*args, **kwargs)
85+
return result
86+
return wrapper
87+
return decorator
88+
89+
@repeat(3)
90+
def greet(name):
91+
print(f"Hello, {name}!")
92+
93+
greet("Bob")
94+
95+
96+
# ===============================================================================================

DAY_21/File_Handling.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
# File Handling :
3+
4+
# file --> data (.txt,.mp4,etc)
5+
6+
# handling --> operations (read, write and update)
7+
8+
# f1 = open("file1.txt","w")
9+
# f1.write("This is first program of file handling") # --> string
10+
11+
# This is continue from last write function output
12+
# f1.writelines(["This is another method of writing in file\n","Please check"]) # string inside list object
13+
14+
# f1 = open("file1.txt","a")
15+
# f1.writelines(["This is append operation\n","This will write on next line"])
16+
# f1.write("Ashish")
17+
18+
# Manual way to read file using loop :
19+
# f1 = open("file1.txt","r")
20+
21+
# for data in f1:
22+
# print(data)
23+
24+
# print(f1.read()) # This will not print unwanted extra lines
25+
26+
# print(f1.readline(1)) # this will return one character
27+
28+
# print(f1.readlines(0)) # this will return one line in form of list
29+
30+
31+
# tell() and seek() :
32+
33+
# print(f1.tell()) # this will return current location of file pointer
34+
# print(f1.readline())
35+
# print(f1.readline())
36+
# print(f1.readline())
37+
# print(f1.readline())
38+
# print(f1.readline())
39+
# print(f1.tell())
40+
# print(f1.seek(7)) # this will shift pointer location to given number
41+
# print(f1.readline())
42+
# print(f1.readline())
43+
# print(f1.tell())
44+
45+
# f1.close() # always close file to avoid differnt output and errors (this can be done in finally block of exception)
46+
47+
# with block : This will close file automatically
48+
49+
# with open("file1.txt","r") as f1:
50+
# print(f1.read())
51+
52+
# we can copy any file easily
53+
54+
# with open("file1.txt",'r') as f1, open("file2.txt","w") as f2:
55+
# f2.write(f1.read())
56+
57+
# Serialization and Deserialization :
58+
59+
# In file handling by default only string is allowed for read and write operation
60+
#
61+
# Serialization => object --> btye
62+
# Deserialization => byte --> object
63+
64+
import pickle
65+
66+
# load(read) , dump(write)
67+
68+
li1 = ["Ashish",23]
69+
70+
# "wb" and "rb" -> for byte mode
71+
72+
# f1 = open("file1.txt",'wb')
73+
# # f1.write(li1) # TypeError: a bytes-like object is required, not 'list'
74+
75+
# pickle.dump(li1,f1)
76+
# f1.close()
77+
78+
# f1 = open("file1.txt","rb")
79+
# print(pickle.load(f1))
80+
81+
class Person:
82+
def __init__(self,name,age) -> None:
83+
self.name = name
84+
self.age = age
85+
86+
def __str__(self) -> str:
87+
return f"{self.name} {self.age}"
88+
89+
p1 = Person("Ashish",23)
90+
91+
with open("file1.txt","wb") as f1:
92+
pickle.dump(p1,f1)
93+
94+
with open("file1.txt", "rb") as f2:
95+
print(pickle.load(f2))
96+
97+
98+
99+
100+

DAY_21/file1.txt

66 Bytes
Binary file not shown.

DAY_21/file2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is another method of writing in file
2+
Please check

0 commit comments

Comments
 (0)