Important concept of
Python 2021
Understanding Script, module, package, library
·
Script: python file that run
directly to perform certain task.
·
Module: Python file that is
imported in script or another program. It defines member like classes, functions
and variable intended to be used in other files that import it.
·
Python.org definition: An
object that serves as an organizational unit of Python code. Modules have a
namespace containing arbitrary Python objects. Modules are loaded into Python by
the process of importing.
·
Package: collection of related modules that work
together to provide certain functionality. This are folder containing modules.
It has special file with name __init__.py
·
Python.org definition
: A Python module which can contain submodules or recursively, subpackages. Technically, a package is a Python module with
an __path__ attribute.
·
To create Python package ourself, we have to create directory and a file named
__init__.py inside it. This file cab be empty.
·
Note: Directories without an
__init__.py file are still treated as packages by
Python. However, these won’t be regular packages, but something called
namespace packages.
·
In general, submodules and subpackages aren’t imported when you import a package.
However, you can use __init__.py to include any or all submodules and subpackages if you want.
·
Library: a bundle of code /
collection of packages. E.g. Matplotlib (plotting libaray)
o
Standard library (available
by default)
o
3rd-Party Modules
Understanding import methods / option:
It is python keyword to
make code in one module available in another. They help to structure code.
Method 1: Basic import:
Here pi is variable in
math module.
Math acts as namespace (In computing, a namespace is a set of signs (names) that are used to identify and
refer to objects of various kinds. )
To see content of math module you can use dir(math)
Method 2: import specific
part (objects) from module
Method 3: import with
rename
Importing module gives
access to its objects using syntax: module.object
Method 4: Relative import.
From
.
import <sum-package-name>
The dot refers to the
current package, and the statement is an example of a relative import. You can
read it as “From the current package, import the subpackage
<sum-package-name>”
Method 5 absolute import:
From math import pi
The PEP 8 style guide
recommends using absolute imports in general. However, relative imports are an
alternative for organizing package hierarchies. For more information, see
Absolute vs Relative Imports in Python.
Don’t use from
<module> import *
Python import path:
You can inspect Python’s
import path by using sys.path.
List will contain three
different kinds of locations:
·
The directory of the current script /
current directory
·
The contents of the PYTHONPATH environment
variable.
·
Other, installation-dependent direcotires.
Note 1: clearing import
catch
Modules are cached, if we
update module then we need to re-import it in old session and delete old cache.
That can be done using
Reload(<module-name>)
For Python3 (its not available by default)
Import importlib
Importlib.reload(<module-name>)
Note 2: __main__
Sometimes we want code to
be executed when module is run directly , but not when
it is imported by another module.
If __name__ == ‘__main__’ allows us to check whether the module is
being run directly.
Understanding
function, class
Function is set of code
that can be called repeatedly
What is Class ?
Object oriented
programming (OOP):
OOP terms:
·
Class
·
object
Object has two parameters:
1. Attributes
(data / properties e.g. age, name etc)
2. Behaviors
(action walking, talking etc)
E.G. phone is object, but
its design is class and object is instance.
Object is instance of
class.
Function is OOP is called
method.
·
Class can be considered as template for
creating object with related data (attributes) and function (action) that do
interesting thing with that data.
·
Class help to create customized data type
·
Function inside the class is called method.
·
# Creating class Example 1
Class User:
Pass
Note: pass is a sort of
place holder.
Creating object
# User1
User1=User()
# Creating class Example 2
To use instance variable
inside class function we can use self.<instance-variable-name>
Creating class with variable Example #3
Class Router:
‘’’Router Class’’’
def __init__(self,model,swversion,ip_add):
‘’’initialize values’’’
self.mode=model
self.swversion=swversion
self.ip_add=ip_add
rtr1=Router(“ios”,”15.6.7”,”10.10.10.1”)
Function __init__ is called dunder or magic methods.
To store attributes we map the
name self and the value you pass to it become variables inside the object,
which is then store those value as attributes.
>>> rtr1.model
“ios”
We can define additional variable later.
>>> rtr1.desc =”virtual
router”
>>> rtr1.desc
“virtual router”
Inheritance in Python classes allow a child class to
take on attributes and methods of another class.
Class Switch(Router):
Here class Switch inherits the attribute of parent
class Router.
Killing window
application via python
Import os
os.system("taskkill /f /im notepad.exe")
os.system("taskkill /f /im winword.exe")
os.system("taskkill /f /im excel.exe")
os.system("taskkill /f /im powerpnt.exe")
os.system("taskkill /f /im outlook.exe")
os.system("taskkill /f /im chrome.exe")
os.system("taskkill /f /im calc.exe")
os.system("taskkill /f /im iexplore.exe")
os.system("taskkill /f /im powershell.exe")
os.system("taskkill /f /im wordpad.exe")
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.