Sunday, 26 June 2022

P#32 Reg expression

To find a pattern in a statement

match = re.search('love', 'i love you all')

if match:
print('found love')
else:
print('love Not found')

# found love
match = re.search('love', 'i leave you all')

if match:
print('found love')
else:
print('love Not found')

# love Not found

 ## . = any char but \n
match = re.search(r'..ve', 'i love you all') # found,
print(match)

To match email

 to check up Google Account
str = 'hello I am so& so i have account you@google.com'
match = re.search(r'\w+@\w+', str)
if match:
print(match.group()) #you@google

match = re.search(r'[\w.-]+@[\w.-]+', str)
if match:
print(match.group()) ## 'you@google.com'

To Validate IFSC code in banks

import re
def isValidIFSCode(str):
# Regex to check valid IFSC Code.
regex = "^[A-Z]{4}0[A-Z0-9]{6}$" # 4 Alphabets,0,
# Compile the ReGex
p = re.compile(regex)
# If the string is empty
# return false
if (str == None):
return False
# Return if the string
# matched the ReGex
if (re.search(p, str)):
return True
else:
return False
# Driver code
# Test Case 1:
str1 = "SBIN0125620"
print(isValidIFSCode(str1))

# Test Case 2:
str2 = "SBIN0125"
print(isValidIFSCode(str2))

# Test Case 3:
str3 = "1234SBIN012"
print(isValidIFSCode(str3))

# Test Case 4:
str4 = "SBIN7125620"
print(isValidIFSCode(str4))

# This code is contributed by avanitrachhadiya2155

"""
True
False
False
False
"""

Please Note:


# regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
# Where:
# ^ represents the starting of the string.
# [A-Z]{4} represents the first four characters should be upper case alphabets.
# 0 represents the fifth character should be 0.
# [A-Z0-9]{6} represents the next six characters usually numeric, but can also be alphabetic.
# $ represents the ending of the string.

To test Pin Code

import re

def isValidPinCode(pinCode):
# Regex to check valid pin code
# of India.
regex = "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$";

# Compile the ReGex
p = re.compile(regex);

# If the pin code is empty
# return false
if (pinCode == ''):
return False;

# Pattern class contains matcher() method
# to find matching between given pin code
# and regular expression.
m = re.match(p, pinCode);

# Return True if the pin code
# matched the ReGex else False
if m is None:
return False
else:
return True



# check Driver code
if __name__ == "__main__":
# Test case 1
num1 = "625001";
print(num1, ": ", isValidPinCode(num1));

# Test case 2:
num2 = "625 001";
print(num2, ": ", isValidPinCode(num2));

# Test case 3:
num3 = "0625 001";
print(num3, ": ", isValidPinCode(num3));

# Test case 4:
num4 = "6250001";
print(num4, ": ", isValidPinCode(num4));

Result


"""
625001 : True
625 001 : True
0625 001 : False
6250001 : False
"""

To check PANCARD in India


# To Valiate pan Card

import re

def checkPanCard():
import re


def isValidPanCardNo(panCardNo):

# PAN Card number
regex = "[A-Z]{5}[0-9]{4}[A-Z]{1}"

# Compile the ReGex
p = re.compile(regex)

# If the PAN Card number
# is empty return false
if(panCardNo == None):
return False

# Return if the PAN Card number
# matched the ReGex
if(re.search(p, panCardNo) and
len(panCardNo) == 10):
return True
else:
return False

# Driver Code.


# Test Case 1:
str1 = "AFGHA2318J"
print(isValidPanCardNo(str1),' ',str1)

# Test Case 2:
str2 = "23AFGHN18J"
print(isValidPanCardNo(str2),str2)

# Test Case 3:
str3 = "AFGHA2318JM"
print(isValidPanCardNo(str3),str3)

# Test Case 4:
str4 = "AFGHA23184"
print(isValidPanCardNo(str4),str4)

# Test Case 5:
str5 = "AFGHA 23184"
print(isValidPanCardNo(str5),str5)

checkPanCard()
"""
True AFGHA2318J
False 23AFGHN18J
False AFGHA2318JM
False AFGHA23184
False AFGHA 23184
"""


P#31 Magic Methods

 Magic or Dunder Methods


Methods having two prefix and suffix underscores in the method name. 

Dunder here means “Double Under (Underscores)”

Sample Method:

# declare our test string class
class Str:

# magic method to initiate object
def __init__(self, string):
self.string = string


# Driver Code
if __name__ == '__main__':
# object creation
s1 = Str('Hello Python')

# print object location
print(s1)

To print __ methods of int

print(dir(int))


# Result

"""
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__',
'__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__',
'__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__',
'__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__',
'__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__',
'__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
'__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__',
'__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
'__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__',
'__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag',
'numerator', 'real', 'to_bytes']
"""

Important Magic Methods

The following tables list important magic methods in Python 3.

Initialization and ConstructionDescription
__new__(cls, other)To get called in an object's instantiation.
__init__(self, other)To get called by the __new__ method.
__del__(self)Destructor method.
Unary operators and functionsDescription
__pos__(self)To get called for unary positive e.g. +someobject.
__neg__(self)To get called for unary negative e.g. -someobject.
__abs__(self)To get called by built-in abs() function.
__invert__(self)To get called for inversion using the ~ operator.
__round__(self,n)To get called by built-in round() function.
__floor__(self)To get called by built-in math.floor() function.
__ceil__(self)To get called by built-in math.ceil() function.
__trunc__(self)To get called by built-in math.trunc() function.
Augmented AssignmentDescription
__iadd__(self, other)To get called on addition with assignment e.g. a +=b.
__isub__(self, other)To get called on subtraction with assignment e.g. a -=b.
__imul__(self, other)To get called on multiplication with assignment e.g. a *=b.
__ifloordiv__(self, other)To get called on integer division with assignment e.g. a //=b.
__idiv__(self, other)To get called on division with assignment e.g. a /=b.
__itruediv__(self, other)To get called on true division with assignment
__imod__(self, other)To get called on modulo with assignment e.g. a%=b.
__ipow__(self, other)To get called on exponentswith assignment e.g. a **=b.
__ilshift__(self, other)To get called on left bitwise shift with assignment e.g. a<<=b.
__irshift__(self, other)To get called on right bitwise shift with assignment e.g. a >>=b.
__iand__(self, other)To get called on bitwise AND with assignment e.g. a&=b.
__ior__(self, other)To get called on bitwise OR with assignment e.g. a|=b.
__ixor__(self, other)To get called on bitwise XOR with assignment e.g. a ^=b.
Type Conversion Magic MethodsDescription
__int__(self)To get called by built-int int() method to convert a type to an int.
__float__(self)To get called by built-int float() method to convert a type to float.
__complex__(self)To get called by built-int complex() method to convert a type to complex.
__oct__(self)To get called by built-int oct() method to convert a type to octal.
__hex__(self)To get called by built-int hex() method to convert a type to hexadecimal.
__index__(self)To get called on type conversion to an int when the object is used in a slice expression.
__trunc__(self)To get called from math.trunc() method.
String Magic MethodsDescription
__str__(self)To get called by built-int str() method to return a string representation of a type.
__repr__(self)To get called by built-int repr() method to return a machine readable representation of a type.
__unicode__(self)To get called by built-int unicode() method to return an unicode string of a type.
__format__(self, formatstr)To get called by built-int string.format() method to return a new style of string.
__hash__(self)To get called by built-int hash() method to return an integer.
__nonzero__(self)To get called by built-int bool() method to return True or False.
__dir__(self)To get called by built-int dir() method to return a list of attributes of a class.
__sizeof__(self)To get called by built-int sys.getsizeof() method to return the size of an object.
Attribute Magic MethodsDescription
__getattr__(self, name)Is called when the accessing attribute of a class that does not exist.
__setattr__(self, name, value)Is called when assigning a value to the attribute of a class.
__delattr__(self, name)Is called when deleting an attribute of a class.

Operator Magic MethodsDescription
__add__(self, other)To get called on add operation using + operator
__sub__(self, other)To get called on subtraction operation using - operator.
__mul__(self, other)To get called on multiplication operation using * operator.
__floordiv__(self, other)To get called on floor division operation using // operator.
__truediv__(self, other)To get called on division operation using / operator.
__mod__(self, other)To get called on modulo operation using % operator.
__pow__(self, other[, modulo])To get called on calculating the power using ** operator.
__lt__(self, other)To get called on comparison using < operator.
__le__(self, other)To get called on comparison using <= operator.
__eq__(self, other)To get called on comparison using == operator.
__ne__(self, other)To get called on comparison using != operator.
__ge__(self, other)To get called on comparison using >= operator.



P#30 H/W Internals and Profiling

To know System Information through Python

# Know system Information
# pip install wmi

import wmi

computer = wmi.WMI()
our_system = computer.Win32_ComputerSystem()[0]

print(f"Manufacturer: {our_system.Manufacturer}")
print(f"Model: {our_system. Model}")
print(f"Name: {our_system.Name}")
print(f"NumberOfProcessors: {our_system.NumberOfProcessors}")
print(f"SystemType: {our_system.SystemType}")
print(f"SystemFamily: {our_system.SystemFamily}")
""" Output from my system:
Manufacturer: Dell Inc.

Model: Vostro 3888

Name: MECH1

NumberOfProcessors: 1

SystemType: x64-based PC

SystemFamily: Vostro
"""

Simple System info Python program using platform

# importing module
import platform

# dictionary
info = {}

# platform details
platform_details = platform.platform()

# adding it to dictionary
info["platform details"] = platform_details

# system name
system_name = platform.system()

# adding it to dictionary
info["system name"] = system_name

# processor name
processor_name = platform.processor()

# adding it to dictionary
info["processor name"] = processor_name

# architectural detail
architecture_details = platform.architecture()

# adding it to dictionary
info["architectural detail"] = architecture_details

# printing the details
for i, j in info.items():
print(i, " - ", j)

"""
platform details - Windows-10-10.0.19043-SP0
system name - Windows
processor name - Intel64 Family 6 Model 165 Stepping 3, GenuineIntel
architectural detail - ('64bit', 'WindowsPE')

Process finished with exit code 0
"""

Hardware Information using PSUTIL

import psutil

#Physical cores
print(f"Number of physical cores: {psutil.cpu_count(logical=False)}")
#Logical cores
print(f"Number of logical cores: {psutil.cpu_count(logical=True)}")

#Current frequency
print(f"Current CPU frequency: {psutil.cpu_freq().current}")
#Min frequency
print(f"Min CPU frequency: {psutil.cpu_freq().min}")
#Max frequency
print(f"Max CPU frequency: {psutil.cpu_freq().max}")
# cpu util
#System-wide CPU utilization
print(f"Current CPU utilization: {psutil.cpu_percent(interval=1)}")
#System-wide per-CPU utilization
print(f"Current per-CPU utilization: {psutil.cpu_percent(interval=1, percpu=True)}")

#ram details
#Total RAM
print(f"Total RAM installed: {round(psutil.virtual_memory().total/1000000000, 2)} GB")
#Available RAM
print(f"Available RAM: {round(psutil.virtual_memory().available/1000000000, 2)} GB")
#Used RAM
print(f"Used RAM: {round(psutil.virtual_memory().used/1000000000, 2)} GB")
#RAM usage
print(f"RAM usage: {psutil.virtual_memory().percent}%")


Results


"""
Number of physical cores: 4
Number of logical cores: 8
Current CPU frequency: 3600.0
Min CPU frequency: 0.0
Max CPU frequency: 3600.0
Current CPU utilization: 4.2
Current per-CPU utilization: [15.7, 0.0, 25.0, 0.0, 4.5, 0.0, 1.5, 1.6]
Total RAM installed: 8.33 GB
Available RAM: 1.58 GB
Used RAM: 6.75 GB
RAM usage: 81.0%
"""

Disk and N/W Information using Python

# Disk Usage

import psutil
import platform
from datetime import datetime

def get_size(bytes, suffix="B"):
"""
Scale bytes to its proper format
e.g:
1253656 => '1.20MB'
1253656678 => '1.17GB'
"""
factor = 1024
for unit in ["", "K", "M", "G", "T", "P"]:
if bytes < factor:
return f"{bytes:.2f}{unit}{suffix}"
bytes /= factor

# Disk Information
print("="*40, "Disk Information", "="*40)
print("Partitions and Usage:")
# get all disk partitions
partitions = psutil.disk_partitions()
for partition in partitions:
print(f"=== Device: {partition.device} ===")
print(f" Mountpoint: {partition.mountpoint}")
print(f" File system type: {partition.fstype}")
try:
partition_usage = psutil.disk_usage(partition.mountpoint)
except PermissionError:
# this can be catched due to the disk that
# isn't ready
continue
print(f" Total Size: {get_size(partition_usage.total)}")
print(f" Used: {get_size(partition_usage.used)}")
print(f" Free: {get_size(partition_usage.free)}")
print(f" Percentage: {partition_usage.percent}%")
# get IO statistics since boot
disk_io = psutil.disk_io_counters()
print(f"Total read: {get_size(disk_io.read_bytes)}")
print(f"Total write: {get_size(disk_io.write_bytes)}")

# N/w
# Network information
print("="*40, "Network Information", "="*40)
# get all network interfaces (virtual and physical)
if_addrs = psutil.net_if_addrs()
for interface_name, interface_addresses in if_addrs.items():
for address in interface_addresses:
print(f"=== Interface: {interface_name} ===")
if str(address.family) == 'AddressFamily.AF_INET':
print(f" IP Address: {address.address}")
print(f" Netmask: {address.netmask}")
print(f" Broadcast IP: {address.broadcast}")
elif str(address.family) == 'AddressFamily.AF_PACKET':
print(f" MAC Address: {address.address}")
print(f" Netmask: {address.netmask}")
print(f" Broadcast MAC: {address.broadcast}")
# get IO statistics since boot
net_io = psutil.net_io_counters()
print(f"Total Bytes Sent: {get_size(net_io.bytes_sent)}")
print(f"Total Bytes Received: {get_size(net_io.bytes_recv)}")

Result:


"""
======================================== Disk Information ========================================
Partitions and Usage:
=== Device: C:\ ===
Mountpoint: C:\
File system type: NTFS
Total Size: 194.70GB
Used: 111.24GB
Free: 83.46GB
Percentage: 57.1%
=== Device: D:\ ===
Mountpoint: D:\
File system type: NTFS
Total Size: 736.20GB
Used: 4.03GB
Free: 732.16GB
Percentage: 0.5%
Total read: 61.63GB
Total write: 46.68GB
======================================== Network Information ========================================
=== Interface: Local Area Connection* 11 ===
=== Interface: Local Area Connection* 11 ===
IP Address: 169.254.62.94
Netmask: 255.255.0.0
Broadcast IP: None
=== Interface: Local Area Connection* 11 ===
=== Interface: Local Area Connection* 12 ===
=== Interface: Local Area Connection* 12 ===
IP Address: 169.254.72.255
Netmask: 255.255.0.0
Broadcast IP: None
=== Interface: Local Area Connection* 12 ===
=== Interface: Ethernet ===
=== Interface: Ethernet ===
IP Address: 172.20.24.131
Netmask: 255.255.0.0
Broadcast IP: None
=== Interface: Ethernet ===
=== Interface: Wi-Fi 2 ===
=== Interface: Wi-Fi 2 ===
IP Address: 172.20.29.139
Netmask: 255.255.0.0
Broadcast IP: None
=== Interface: Wi-Fi 2 ===
=== Interface: Loopback Pseudo-Interface 1 ===
IP Address: 127.0.0.1
Netmask: 255.0.0.0
Broadcast IP: None
=== Interface: Loopback Pseudo-Interface 1 ===
Total Bytes Sent: 366.06MB
Total Bytes Received: 4.38GB
"""

Measuring Running Time in Python  #1

import timeit
time_test = timeit.repeat('[x**0.5 for x in range(1000)]', number=10000)
print(time_test)

Result:

"""
[1.415492, 1.3847563, 1.3762589999999997, 1.3683869, 1.3789484000000005]
"""
# importing cProfile
import cProfile

cProfile.run("10 + 10")

"""
3 function calls in 0.000 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
"""

Function Profiling code and the corresponding output

# Function Profiling
import cProfile

def f():
print("Hello Python World Profiling")
cProfile.run(
'f()')
         5 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 ex1-scit.py:252(f)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


Tuesday, 21 June 2022

Tuesday, 14 June 2022

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...