Advance Python
Advance Python
Emphasis- जोर
Python Classes/Objects
Python is an object oriented programming language.
Class of “a”
Self: -Formal parameter
self-keyword is used to store object reference. We can use other word also but as PEP8
we use standard “self” keyword.
uctors in Python
Constructor :__init__(self).:
A constructor is optional, and if we do not provide any constructor, then Python
provides the default constructor. Every class in Python has a constructor, but it's not
required to define it.
2.
3.Constructor with 2 obj(objects) But only 1 called .
5.
TYPES OF VARIABLE:-
1.Instance variable
_ _ dict _ _ :- to see data available with that object.
Here Double is the local variable which is used within the class.
4.All Variables
2.1Class method Value Change Of class variable
CHECK ORDER OF FUNCTION TO
CALL IT
3.1 Static method
*** All Variables & all Methods
_ _Main_ _:-(Driver code)
For most practical purposes, you can think of the conditional block that you open with if __name__ ==
"__main__" as a way to store code that should only run when your file is executed as a script.
Result of E
Result of F
Result of G
Hybrid Inheritance
Super Method
Polymorphism
1.Overloading: -
–
Method & Constuctor Overriding
Abstraction:-
Access Specifier
1.Public
2.Protected
3.Private
ALL
**MODULE**
EX.1
WITH ALIASING
WITH “ * “
1.
2.
“dir” METHOD
data = fp.read()
print (fp.closed)
print (fp.closed)
C:\Users\prafu\venv\Scripts\python.exe
"C:/Users/prafu/PycharmProjects/prafull/ADVPY/PRACTICE/FILE HANDLING/close.py"
False
True
1.READ MODE
a)read()-file found it return
error-filenotfounderror
Error
Read Desktop file with pathmode-
\\-is compulsory here to get result
b)readlines
DOUBLE CALL-
#sinle time print for 1 line
#double implementation gives 2 lines in result.
d)readable
2.WRITE MODE
a)write( )
RUN AGAINE WITH NEW DATA -RUN & OVERRIDE PREVIOUS DATA
b)Writelines
2.
c)writable-by import
3.Append
Need already available file to process operation in append/else create new file.
a)write( )
Below result look different it has been run twise 1st without \n at ‘ \n asdfgg ‘
b)Writelines
c)Writable
4.Exclusive mode
a)Write mode
if file present return error
b)Writelines()
c)Writable not possible gives error need new file
Tell ( ) – will return the location of cursor( ) 1st it will at 0 position then after read( ) it goes
to last position
**after read() it will return no. of character + spaces available in the given data** so to
check no. of character
Before run
After run
read upto(20) then write n complete read
In result we seen nothing coz after w+ mode cursor goes to end position and after that
there is no data remaining to read out.
By using seek( )
7)a+ Mode-(write[append]+read)
*** To create 10 files a time
*@CSV(COMMA SEPARATED VALUES
1a.W MODE(writeline)
2)Read(r) mode
r mode -result in row format by function
Csv to csv
Csv to text
Text to Csv
Text to Text
OS MODULE: -
Os.makedirs
-To delete file
_create dir/structure
-change working dir
d)change dir
From
To
Folder created
at given
location that is
desktop
Before
After
After
single delete of multiple dir by using rmdir
f) Rename
2.
h)listdir
i)Access any file from anywhere
1.Desktop
date
Time
**EXCEPTION HANDLING-(Unavoidable
condition)
When our logic is currect but due to user input or by condition our logic failed
their we use exception handling
1.Try-contains risky code-The code which may occur error should be written inside the
try block.
When it shows error, it goes to except block.
2.Except-contains handling code
**_in real scenario do not use except like this give proper error names.
3)
If file is available
10/0
#gfdfghokjhg
a=int("45.3")
With error
Try(run+error)-Excepy-Finally
Try-Except-else-Finally
****For-else***
If for run perfectly then only else run (friendly nature)
1.Run fail
2.Run successfully
3.Run successfully
4.Run fail
Try inside try
1.
2.
3.
4.
5.
Valid-invalid
**Logger-
5 levels of loggers
Critical-50
Error-40
Warning-30
Info-20
Debug-10
This is the class whose objects will be used in the application code directly to call
the functions.
LogRecords: Loggers automatically create LogRecords objects that have all the
information related to the event being logged, like the name of the logger, the
function, the line no., the message & more.
##Default-
1.message: console
2.level-30&above or warning and above
3.default mode -to store data append mode (we can change the mode of storage
data [file mode])
CRITICAL: root: Critical Message
Level : logger : message
1.Base with Level
Access By Name-
Filename – to store log data
In ‘python32.txt’
But our app.log file is empty cause error is not added their so we add
“Logging.error(obj)”
Now the same error we got on a cmd file that we
are added in our app.log file by adding
“logging.error("Exception Details:", exc_info=True)”
Or
2.
Application with error-filename is not found so due to that it will return
Error reduced by passing Try & except
**To trigger log file, we add multiple module log result in an only one file
always.
1.add
2.File handling
3.log
2.message
%p for Am & Pm
1. ZeroDivisionError
2. TypeError
1.Filehandling.py
2.log.py
3.Add.py
Result-Logger.log
**Assertion:-
Assert-condition:-
If true run.
If false stop run.
1.Simple Assert:-
Without Message
With Assertion Error:-
2.Augumented Assert-
Assertion with Message is called Augumented Assert.
**Multithreading:-
Json to Python
Store python (dict) data into YAML file format:-
Yaml to python