forked from prabhudev740/pythonbatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOopsNotes.txt
More file actions
936 lines (645 loc) · 22.8 KB
/
OopsNotes.txt
File metadata and controls
936 lines (645 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
var = 10 ---> properties / data/ states
functions ---> functionalities / method /behaviour
I am MSysian.
className --> camel case
ClassName --> pascal case
var_name
func_name
Object Oriented Programming
------------------------------
Opps is a structure where we can bunddle some set of similar properties and methods
-> To write reusable codes using classes and objects is called OOP.
class
-----
This is a user defined datatype, which specifies the properties and functionalities to be storesd.
Here we are specifing the base/ intial data/ properties and functionalities to storesd
we define a class using 'class' keywod.
Syntax:
class ClassName:
data / properties / states
functionalities / methods/ behaviours
inside a class first we have write all the properties, then only we can define our menthod
Ex:
class Car:
color = 'red'
wheels = 4
def start(self):
print('started')
def stop(self):
print('stopped')
Object
------
Object is a real world entity consists of some data and menthods
-> It is an instance of a class, it will have all the properties and functionalities of that class
we can intialize object by calling the class name.
Syntax:
object_name = ClassName()
when ever we intialize an object, will copy all the properties and methods from class.
Ex:
nano = Car()
Memory management of class
------------------
Whenever our controller sees that class keyword, it will create a dict block with key and val.
it will store all the properties, key will store the variable name and val will store tha val of that data
for functionalities, it will go to method area and create a methof block and
address oft that block will be stored in val part of that function name.
some address wil be assigned to that dict block.
the class name will point to that adress.
Memory management of objects
-------------------------------
When our controller get a new object,
it will create one dict block again
it will copy all the properties and func_name to key part
for value it will get address and store it.
some address will given to that block
object will pont to that addres
There is two parts in oops
---------------------------
properties / data / states
i) Generic data / class data
ii) Specific data / object data
menthods / behaviours / functionalities
i) object method
ii) class method
ii) static method
Generic data
---------------
Generic data is also known as class data.
the data which are common in both class and object is called Generic data
the data is written in class block.
The Generic data will be stored inside the class dict block, and refernece ot the data will be stored in object dict block
To access the Generic data we can take help of class name and objec name with dot (.) operator
--> ClassName.propertie_name
--> obj_name. propertie_name
if we want to modify value for class
--> ClassName.propertie_name = new-val
if we want to modify data for object
--> obj_name.propertie_name = new_val
Note -if we change the val for object, it will not point to the class data, it will become a Specific data
Specific / object data
-----------------------
The data which is only specific to the object is known specific or object data
Specific data is created in object dict block
object data are specific to that perticular object only
To create a bject data, we user
Syntax:
obj_name.new_property = new_val
Ex:
tesla.ads = True
Constructor on initalization
-------------
Constructor is a method which is used to create specific data for all the objects
Constructor is special method in python whose name is predefined.
i.e. __init__()
it will take one defalut arg, ie. "self"
"self" is an arg., it will store the adress of that object from where it is called
Constructor / __init__ method will be called automatically when we create one object.
Constructor method should be written in class after writing all the feneric data.
To give that specific data we have to give args in parenthesis, while creating object
Constructor is one of object method.
Syntax:
def __init__(self, arg1, arg2, ..., argn):
self.obj_data1 = new_val1
self.obj_data2 = new_val2
self.obj_data3 = new_val3
obj_name = ClassName(arg1, arg2, arg3...)
Ex:
class Car:
wheels = 4
def __init__(self, c):
self.color = c
def start(self):
print("started")
def stop(self):
print('stopped')
nano = Car("red")
tesla = Car("Green")
Methods
------------
There are 3 types of method
1) object method
2) class menthod
3) static method
1) object method
---------------
object method is a method which will point to the object dict vlock
object method will have one defalut arg i.e. 'self'
whatever we doing in object method it will reflect in obj dict block.
self will store the adress of that object from where it is called
Syntax:
class ClassName:
properties
Constructor
def obj_method(self, arg1, arg2, arg3....):
statement 1
statement 2
statement 3
obj_name = ClassName()
# calling object name
obj_name.obj_method(arg1, arg2, arg3....)
# calling using class name
ClassName.obj_method(obj_name, arg1, arg2, arg3...)
Class method
---------------
Class method will point to class dict block
it will also have one default arg, "cls"
to define a class method we have to use one bulit in annotation, "@classmethod"
"@classmethod" will fetch the address of that calss and store in "cls"
we dont need to create a object to call a classmethod
Any changes we made to class data it will affect all the object
Synatx:
class ClassName:
class data
Constructor
@classmethod
def func_name(cls, arg1, arg2, arg3, ...):
statement1
statement2
statement3
To call a class method we have to use
--> obj_name.classmethod()
--> className.classmethod()
Static method
-------------
static method will be stored the class dict block
but here we dont need to provide any defalut arg, 'self', 'cls'
it is bound to the class, i.e its not mandatory to declare object to call static mathod
to declare static method, there is one inbuilt annotation "@staticmethod"
"@staticmethod" will tell the compiler, there is no default arg.
we cant access and modify any class or object data
Synatx:
class ClassName:
@staticmethod
def func_name(arg1, arg2, ...):
statement1
statement2
statement3
to call staticmethod
--> obj_name.staticmethod()
--> ClassName.staticmethod()
Inheritance
-----------------
Inheriting properties and methods from one class to another class is called Inheritance.
The class from where we are Inheriting properties and methods is called parent or base class
The class to which we are Inheriting is called child or derived class.
class Parent:
properties
methods
class Child(Parent):
properties
methods
Memory management
-------------------
parent class
-----------
Whenever controller sees the class keyword it will create a dict block in Main space
it will store all the properties
for methods it store the statements of methods in F.A., store the method name in key and address in value
i will assingn some address to that dict block, className will point to that dict block.
Child class
-------------
Whenever controller sees the class keyword it will create a dict block in Main space
if there is something inherited, it will store all the key and refernece to values first.
After storing all the properties and methods from parent class, then it will store own properties and methods
in case we are Inheriting from muliple class properties of class written in right side will stored first.
some address will be assingned to thet dict block, ClassName will point to that address.
method or Constructor chaining
------------------------------
it is a method to invoke or class method of parent class being inside child.
By using method chaning we can utilize the method of parent class in child class
'__init__', object, class, static mehtods can be chained.
To call parent class method in child class we have to use one method, i.e 'super()'
Synatx:
super().method_name(args)
There are 5 types of Inheritance
----------------------------------
1) Single level Inheritance
2) Multilevel Inheritance
3) muliple Inheritance
4) hierarchy Inheritance
5) Hybrid Inheritance
1) Single level Inheritance
--------------------------------
when properties and methods of one parent class will be inherited to chile class,
single level Inheritance
parent class
|
child child
Ex:
class Car:
wheels = 4
class Tesla(Car):
color = 'blue'
model = 'X'
2) Multilevel Inheritance
----------------------------
here properties and methods will be inherited from parent to child class
then, again it properties and methods will be inherited to its child class and so on.
parent
|
Child 1
|
Child 2
|
|
Child n
class Vehicle:
fuel = 'oil'
class Car(Vehicle):
wheels = 4
class XUV(Car):
speed = 100
3) muliple Inheritance
-----------------------
in this type of inheritace we are Inheriting data from 2 or more classes
parent 1 parent 2 parent 3
\ | /
child
Note: The right most class will be inherit first.
Ex:
class Animal:
legs = 4
class Tiger:
tail = True
class Cat(Animal, Tiger):
color = 'white'
4) hierarchy Inheritance
-------------------------
in his type inheritace there will be one parent 2 or more child class
parent
/ | \
child 1 child 2 child n
Ex :
class Car:
wheels = 4
class Tesla(Car):
color = 'red'
class Tata(Car):
color = 'white'
tl = Tesla()
tt = Tata()
5) Hybrid Inheritance
-------------------------
it is a type of inheritace, which has mixture of two or more types of inheritace
A
/ \
B(A) C(A)
\ /
D(B, C)
Ex:
class Car:
wheels = 4
class Tesla(Car):
color = 'red'
class Tata(Car):
color = 'white'
class TataTelsa(Tata, Tesla):
ads = 'True'
Polymerphism
-------------
Perfoming diffrent task, using same function name is called Polymerphism
poly --> many
merphism --> forms
There are 2 types of Polymerphism
1) method overloding
2) method overriding
1) method overloding
-----------------------
executing n no. of methods with same function name and sam eof different no. of args. is called method overloding.
in python method overloding is not possible.
if we try to write n no of function with same function name, last function will override all the previous function.
if we want to use method overloding, we can use -
1) by using default arg.
-----------------------
here we can pass the defalut args to achive method overloding
def add(a=0, b=0, c=0, d=0):
return a + b + c + d
2) by using packing
---------------------
we can use *args & **kwargs
def add(*vals):
res = 0
for i in vals:
res += i
return res
2) method overriding
-----------------------
When we create 2 or more methods by same name, then previous methods will be override with new
method overriding mostly seen while inheriting data from one class to other class
to overcome this we can use Constructor or method chaining.
class A:
def a(self):
print("a in class A")
class B(A):
def a(self):
super().a()
print("a in class B")
Access Specifier
----------------
To specify the access ability of any variable or data for usage or fethcing perpose or
modification purose we use access specifier.
there are 3 types of access specifier
public
protected
private
public
-----------
In this kind of access specifier we are giving complete accesibilty
of varible methods, class name from any where from python file and package
Generally all the varibles and methods we use are public
Syntax:
class ClassName:
pub_data = val
x = 10
y = 20
def test_meth(self):
print("public method")
protected
-----------------
this kind of specifier we are giving access pf varible or method only in cureent package.
Syntax:
class ClassName:
_var = val
_x = 10
_y = 10
def _mthod(self):
print("this is protected")
private
-----------
in this kind of specifier we are giving accesibilty of variable and methods
inside the class
Synatx:
class ClassName:
__x = 10
__y = 20
def __test(self):
print("private")
Exception Handling
---------------------
Exception :-
Exception is an error or obstacle or problem occured in normal flow of execution.
Whenever the Exception occures, the execution will stop there only.
when the Exception occure, we will create one alternative path of Exception
Creating an alternative path is called Exception Handling.
Except Syntax error we can Handle every type of error.
There are 2 blocks in Exception Handling
1) try
2) except
1)try
------
It is a block of instructions, which may raise Exception or error
try is a keyword, it is created where we are expecting Exception Handling
All the instructions should be written below try block try block using 1 tab space.
For 1 try block we can write n no of except block.
Syntax:
try:
statement1
statement2
statement n
2) except
------------
it is a block of statements which act like alternative solution,
for the Exception occured in 'try' block.
it is also a keyword created for Exception Handling.
except block will executed only if we will get error in 'try' block
Syntax
-------
try:
statements
except:
statements
There are 3 types of Exceptions block
------------------------------
1) specific
2) Generic
3) default
Specific Exception block
-------------------------
It is a specific Exception block which will act as a solution to the specific error
here we have mention the name of Exception which may raise.
Here name of Exception is case sensitive.
Syntax:
try:
statements
except <Name of Specific error>:
alternative statement
Ex:
x = [1, 2, 3, 4]
try:
print(x[10])
except IndexError:
print("We will get index error")
except NameError:
print("var is not defined")
Generic Exception Block:
---------------------------
it will handle all the standard exception which are raised in try block.\
Generic Exception cant handle system related error and hardware related error
in combination of specific and Generic error , we have write the specific errors first then Generic error
Here the name of Exception will be 'Exception'
try:
statements
except Exception:
alternative statement
defalut Exception Block
-------------------------
This can handle all the standard, sofware, hardware, userdeined erros.
Here we will not mention any exception name.
we have first write all specific, then Generic then only we can write default exception block.
try:
statement
except:
solution
finally
------------
it is a keyword
this will contain some code block, that will be executed during exception Handling
finally block will excecute compulsory whether the exception raised or not.
try:
statement
except:
solution
finally:
message
raise
--------
it is a keyword, which help use to raise standard and custom error
Syntax:
raise <name of the error>("message as str")
User Defined Exception
-------------------------
To define an exception for our own we have an user defined Exception.
To create user defined exception we have to inherit one inbuilt class "Exception" to one or more classes
The new class name will act as Exception or Error
inside this class we cant write any instructions.
we can only pass statement there
Note: The class name should end with "Error", ex: TestError, NumberError
Syntax:
class NameOfError(Exception):
pass
File Handling
------------
File:
------
This a place or document where we can store some data.
Ex: .txt, .c, .py, .java, .html, .yaml
The basic operations we can perform on a files
1) Reading data from a file
2) Writing data to a file
To perform any operation we have to take the permision from the file
twe have to use an inbuilt Fuction "open()" to get accesibilty from the file.
Syntax:
varName = open(arg1,arg2)
arg1 --> this will be a str, with fileName.extension, or the location of the file
arg2 --> the mode of the file
Note: The opened file must be closed manually.
varName.close()
File Handling
-------------------
the phenomenon of interacting with one independent file by being present in .py file
Modes of a file
-----------------
-> write
-> Read
-> append
1) write mode
------------
varname = open("testdir/temp.txt", 'w')
This mode is used to write data in a file
if the file is not present, controller will create a new file with the name give in that dir/ location
if the file is present, preveously stored data will be removed/ override with new data.
To write data in a file
varname.write(data)
we can write only str data to a file
The write method will take only one arg.
After performing all operation we have to close it.
varname.close()
Ex :
fp = open("temp.txt", "w")
fp.write("MSys")
fp.close()
read Mode
------------
varname = open("temp.txt", "r")
using this mode we can read data from a file
the must be present in the dir or location
if the file is not present we will get error
if the is present we can read the using 3 methods
a) read()
b) readline()
c) readlines()
a) read
----------
it will read entire file or data present in a file
it will return one str
Syntax:
fp = open('file", "r")
res = fp.read()
fp.close()
b) readline()
-------------
it will read the data line by line or one line at a line
for one readline(), the controller will read only one line, the cursor will go to next.
it will return str data
Syntax:
fp = open('file", "r")
res1 = fp.readline()
res2 = fp.readline()
res3 = fp.readline()
fp.close()
readlines()
------------
it will read the data from a file and store them in a list, each line will be one element in that list
Syntax:
fp = open('file", "r")
res = fp.readlines()
fp.close()
Append mode
----------
var = open("file path", "a")
-> append mode will store the data after previous data in a file.
-> if the file is not present then it will create a new file and write all data
-> in case of wwrite mode if the file present preveously, it will override with new data,
but in append mode it will add data after the previous data
-> to append new data we have to use write() method.
Syntax:
var.write(data)
-> after appending we have close the file
Ex:
fp = open("file path", "a")
fp.write(new data)
fp.close()
Note:
To check the cursor position we can use one inbuilt function, tell()
var.tell()
To place the cursor any given position, we have call seek()
var.seek(pos)
with
---------
traditionally we are manually operning and closing. that leads to error sometimes
To overcome this we can use 'with' keyword to open a file.
we only open the file and it gets the closed automatically.
Syntax:
with open("file_path", mode) as alias:
statement1
statement2
statement3
Ex:
with open("file_path", "a") as fp:
fp.write(new_data)
when our controller sees that with keyword it open the file and store the refernece in file object.
after completion of excecution of all statements it will close that file automatically.
print()
---------
print is the inbuilt function to display data in console
to display we can pass one or muliple objects
Syntax:
print(*objects, sep=" ", end="\n", file=sys.stdout, flush=False)
objects -> these are one or more values we want to display
sep -> it specifies the 'str' that should be used to separate the objects
defalut value is space (" ")
end -> it specifies the 'str' that should be printed after all of the objects have been printed.
The defalut value is new line cahe ("\n")
file -> It specifies the file like object where the output should be written.
The default value is 'sys.stdout', it means the output wil be written in termina or console
flush -> if the flush is True, it will store the output to the file object instantly.
defalut value is False
Iterator
--------------
It is a fuction to perform iteration.
when we traverse a sequence to fetch value one by one is called iteration
Two functions are present in Iterator
i) iter()
ii) next()
i) iter()
-----------
this function will intialize cursor at the first element at the sequence
Syntax:
variable = iter(args)
Note - This varibale will store the adrees of Iterator
ii) next()
-------------
it is afunction which traverse through the a sequence by returning one element at a time.
var_name = next(variable)
Note: var_name will store the elements in the sequence
lambda function
----------------
this are small annonymos function without any name
we use it when we dont want to call this function more than once
Syntax:
lambda arg1, arg2, ... : statement
Note: it will directly return the value, we dont need to write the return statement
map(arg1, arg2)
--------
it is an inbuilt function
it will take 2 args
--> arg1 : refernece to function
--> arg2 : one collection dt / Iterable
it will take one element each time from the collection, it will apply the func in arg1 to each element.
filter(arg1, arg2)
--------------------
it is an inbuilt function
it will take 2 args
--> arg1 : refernece to function
--> arg2 : one collection dt / Iterable
it will ony