Wednesday, 6 April 2022

P#23 Jokes Apart

 Jokes Chokes

You are bored. 

pip install jokes

import pyjokes
joke=pyjokes.get_joke(language='en', category= 'neutral')
print(joke)

Output: Enjoy!!! 

I've been using Vim for a long time now, mainly because I can't figure out how to exit.
Hey this is the code will play the joke, so you can listen@@@ 
pip install gTTS playsound
import os
import pyjokes
from gtts import gTTS
from playsound import playsound

joke=pyjokes.get_joke(language='en', category= 'neutral')

print(joke)

myobj = gTTS(text=joke, lang='en', slow=False)

myobj.save("joke.mp3")

#os.system('mpg321 joke.mp3')
playsound('joke.mp3')

Be a Columbus in discovering Python abilities!!!!  Happy learning @ AMET ODL...👧👧👧👧👧

Best Python Blogs

 


https://blog.feedspot.com/python_blogs/


P#22 Python for finding wifi password

Wifi Passwords can be found using the CLI command in windows:

 CLI: netsh wlan show profile to konw the wifi name
# netsh wlan show profile campuswifi key=clear > out
# notepad out ... to find out the password from key content in security settings: section.



import subprocess

data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8', errors="backslashreplace").split('\n')

profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]

for i in profiles:
try:

results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8', errors="backslashreplace").split('\n')

results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]

try:
print ("{:<30}| {:<}".format(i, results[0]))

except IndexError:
print ("{:<30}| {:<}".format(i, ""))

except subprocess.CalledProcessError:

print ("{:<30}| {:<}".format(i, "ENCODING ERROR"))

input("i am also.. waiting") # waiting to read

# wifiname | wifipassword

Eureka... I found ðŸ‘€ðŸ‘€ðŸ‘€ out the password with AMET ODL Happy Learning!!!!

Monday, 4 April 2022

P#21 3D Plots

 3d Plots are easy to plot using axes3d.  Let us see some examples to understand the 3d plots.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.plot([2,4,6],[4,8,12],color='Red')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.savefig('3dline41.png')
# plt.show()
The above one is 3d Line. we have set projection by ax = fig.add_subplot(111, projection='3d')
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
plt.savefig('3dline42.png')
plt.show()

Now let us plot 3d scatter plot.

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

def randrange(n, vmin, vmax):
return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

n = 100

# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# plt.savefig('3dscatter43.png')
plt.show()
Please note all the labels are printed corresponding label() methods.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)

# Plot a basic wireframe.
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.savefig('3dwireframe44.png')
plt.show()


Please note the wireframe model diagram and corresponding code.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np


fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.savefig('3dsurface45.png')
plt.show()

                           


from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np


fig = plt.figure()
ax = fig.gca(projection='3d')


def cc(arg):
return mcolors.to_rgba(arg, alpha=0.6)

xs = np.arange(0, 10, 0.4)
verts = []
zs = [0.0, 1.0, 2.0, 3.0]
for z in zs:
ys = np.random.rand(len(xs))
ys[0], ys[-1] = 0, 0
verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts, facecolors=[cc('r'), cc('g'), cc('b'),
cc('y')])
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_xlabel('X')
ax.set_xlim3d(0, 10)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 4)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
plt.savefig('3dploy46.png')
plt.show()


See the beauty of color. Check how it is nicely handled for better visualization. happy Visualizing 3d with python, matplotlib and AMET.
 

P#20 Slicing

SLICING

Slicing is the extraction of a part of a string, list, or tuple. It enables users to access the specific range of elements by mentioning their indices. 

We should understand index. Then only slicing will be easier.

 An index is a position of an individual character or element in a list, tuple, or string. The index value always starts at zero and ends at one less than the number of items.

#slice list

aList = [10,20,30, 40, 50]
slc = slice(1,4) # note slice method
print(aList[slc]) #[20, 30, 40]

# slice Tuple

Slicing on Tuple.

aTuple = (10,20,30, 40, 50)
slc = slice(1,4) # note slice method
print(aTuple[slc]) # (20, 30, 40)
# stepping
aList = [10,20,30, 40, 50,60, 70, 80, 90]
stepList = slice(1,8,2)
print(aList[stepList]) # [20, 40, 60, 80]

#Insertion @ Start
aList = [10,20,30, 40, 50,60, 70, 80, 90]
iList = ['a','b','c','d']
aList[:0] = iList
print(aList) # ['a', 'b', 'c', 'd', 10, 20, 30, 40, 50, 60, 70, 80, 90]

#Insertion @ End
aList = [10,20,30, 40, 50,60, 70, 80, 90]
iList = ['a','b','c','d']
aList[len(aList):] = iList
print(aList) # [10, 20, 30, 40, 50, 60, 70, 80, 90, 'a', 'b', 'c', 'd']
alist = "AMET ODL LEARNING"
print(alist[::-1]) # Reverse as string #GNINRAEL LDO TEMA

alist = "AMET ODL LEARNING"

print(alist[4:8]) # returns 4 to 7th characters in the string # ODL
print(alist[0:4]) # from 1 to 4 {4 exclusive} # AMET
print(alist[9::]) # From 10 to last character #LEARNING
print(alist[:8]) # From begining to 8th position #AMET ODL
print(alist[-1]) # last character G
See the above uses of indexing in slicing. Interestingly, it works even for negative indexing.  
Deleting an element

aList.remove(10)

print(aList) # [20, 30, 40, 50, 60, 70, 80, 90, 'a', 'b', 'c', 'd']

Happy Learning at AMET ODL!👌👪



Sunday, 3 April 2022

P#19 Duplicates Handling

DUPLICATE REMOVAL

In any Data set,  Duplicates are perennial problem in data cleaning. Let us brief how we can handle duplicates in this article.

Method 1: (Traditional ..loop way)

# Create a list with duplicates

dlist = [10,20,30,40,50,60,10,20,30]
print(dlist)
# remove duplicates
dupFreeList = []
for element in dlist:
print(element)
if element not in dupFreeList:
dupFreeList.append(element)
#
print(dupFreeList) # [10, 20, 30, 40, 50, 60]

Method 2 : (Comprhensive Way)


res = []
[res.append(x) for x in dlist if x not in res]

# printing list after removal
print ("The list after removing duplicates : " + str(res))
# The list after removing duplicates : [10, 20, 30, 40, 50]

Method 3:

You can convert to set and then convert to list to remove duplicates.



dlistset = set(dlist)
print(dlistset)
# {40, 10, 50, 20, 60, 30}
dupFreeList = list(dlistset)
print(dupFreeList) # [40, 10, 50, 20, 60, 30] # Order is not Maintained


Method 4:


from collections import OrderedDict

dupFreeList = list(OrderedDict.fromkeys(dlist))

print(dupFreeList) # [10, 20, 30, 40, 50, 60] # order is maintained

Here, we have imported package OrderedDict from collections and used the method  list(OrderedDict.fromkeys(dlist))

Method 5: list(dict.fromkeys(df)) usage 


dlist = ["10","20", "30","40","20","30"] # String
dflist = list(dict.fromkeys(dlist))
print(dlist, dflist)
#['10', '20', '30', '40', '20', '30'] ## ['10', '20', '30', '40']


dlist = [10,20,30,40,50,10,20] # integer
dflist = list(dict.fromkeys(dlist))
print(dlist, dflist) #[10, 20, 30, 40, 50, 10, 20] [10, 20, 30, 40, 50]

Happy Open Learning at AMET ODL!

index.html

Mobile Tea/Coffee POS Chai POS Mobile Tea/Coffee & Snacks Billing ...