Saturday, May 29, 2021
Sending text / chat messages
Chatting application in python
https://www.youtube.com/watch?v=zHUUTGQWjrM
# Server.py
import socket
ser = socket.socket()
ser.bind(("localhost", 999))
ser.listen(3)
while True:
c, add = ser.accept()
print(c.recv(1024).decode())
while True:
mes = "Server: " + input("Me: ")
c.send(bytes(mes, "utf-8"))
print(" " + c.recv(1024).decode())
c.close()
Create audiobooks in Python
import pyttsx3, PyPDF2
with open("Hello! Python ~ Briggs.pdf", "rb") as book:
full_text = ""
reader = PyPDF2.PdfFileReader(book)
audio_reader = pyttsx3.init()
audio_reader.setProperty("rate", 300)
for page in range(reader.numPages):
next_page = reader.getPage(page)
content = next_page.extractText()
full_text += content
audio_reader.save_to_file(content, "Hello Python.mp3")
audio_reader.runAndWait()
Animating matplotlib
# https://www.youtube.com/watch?v=7RgoHTMbp4A
# Draw Scatter Diagram & Regression Line
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
import random
reg = LinearRegression()
x_values, y_values = [], []
plt.xlim(0, 100)
plt.ylim(0, 100)
for i in range(95):
plt.clf()
x_values.append(random.randint(0, 100))
y_values.append(random.randint(0, 100))
x = np.array(x_values)
x = x.reshape(-1, 1)
y = np.array(y_values)
y = y.reshape(-1, 1)
if i % 5 == 0 or i == 94:
reg.fit(x, y)
plt.scatter(x_values, y_values, color='brown')
plt.plot(list(range(100)), reg.predict(np.array([x for x in range(100)]).reshape(-1, 1)))
plt.pause(0.0001)
plt.show()
# Draw histogram
import matplotlib.pyplot as plt
import random
values = [0] * 50
plt.xlim(0, 50)
plt.ylim(0, 100)
for i in range(50):
values[i] = random.randint(0, 100)
plt.bar(list(range(50)), values)
plt.pause(0.0001)
plt.show()
Thursday, May 27, 2021
Timing Python script using timeit
from timeit import timeit
def whileLoop(n = 100_000):
i, s = 0, 0
while i < n:
s += i
i += 1
return s
def forLoop(n = 100_000):
s = 0
for i in range(n):
s += i
return s
print("While loop", timeit(whileLoop, number=1))
print("For loop", timeit(forLoop, number=1))
Sunday, May 23, 2021
Ticker in Python tkinter
Screenshot.png
from tkinter import *
from time import sleep
win = Tk()
win.geometry("1355x800-10+0")
pic2 = PhotoImage(file="BCRS.png")
lblSchoolPhoto = Label(win, image=pic2)
lblSchoolPhoto.place(x=0, y=0)
dummyStr = "*" * 200 # To provide background
lblBackground4Ticker = Label(win, text=dummyStr, bg="yellow", fg="yellow", font=("Helvetica", 40, "bold"))
lblBackground4Ticker.place(x=0, y=630)
s = "WELCOME TO THE VIRTUAL OPENING OF BCRS CLASS XI, 2021-22"
lblTicker = Label(win, bg="yellow", text=s, font=("Helvetica", 40, "bold"))
while True:
X = 1400
while X > -1900:
lblTicker.place(x=X, y=630)
sleep(.05)
win.update()
X -= 5
win.mainloop()
Monday, May 10, 2021
Chatbot
########## Intents.json ##############
{"intents": [
{"tag": "greetings",
"patterns":["hello", "hey", "hi", "good day", "greetings", "what's u?", "howdy"],
"responses": ["Hello!", "Hey!", "What can I do for you?"]
},
{"tag": "goodbye",
"patterns":["cya", "see you later", "goodbye", "i am leaving",
"have a good day", "bye", "ciao", "tata", "farewell"],
"responses": ["Sad to see you go :(", "Talk to you later", "Goodbye", "So long ..." ]
},
{"tag": "age",
"patterns":["how old", "how old is binoy", "what is your age", "how old are you", "age?"],
"responses": ["My owner Binoy is 54 years young!", "54 years", "He has seen 54 summers"]
},
{"tag": "name",
"patterns":["what is your name?", "what should I call you?", "whats your name?", "who are you?", "can you tell me your name?", "please tell me your name"],
"responses": ["You can call me Neural", "I'm Neural", "I'm Neural, the assistant to Binoy"]
},
{"tag": "shop",
"patterns":["I'd like to buy something", "what do you sell?", "what are your products?", "what do you recommend?", "what are you selling", "what's for sale", "what can I buy?"],
"responses": ["You can buy a snazzy car", "We have cars of all makes and models", "Wanna see our range of cars?"]
},
{"tag": "hours",
"patterns":["when are you open", "what are your hours", "hours of operation", "timing", "opening time", "closing time", "working hours"],
"responses": ["24/7"]
},
{"tag": "stocks",
"patterns":["what stocks do I own?", "how are my shares?", "what companies am I investing in?", "what am I doing with my savings"],
"responses": ["You own the following shares: RIL & Tata Motors on the BSE and FB, MS & Intel on the NYSE"]
},
{"tag": "shutdown",
"patterns":["", "", "", "", "", "", ""],
"responses": ["", "", ""]
}
]}
Colour using Colorama
from colorama import Back, Fore, Style, init
init(autoreset=True)
print(Fore.RED + "Hello World")
print(Back.GREEN + "Hello World")
print(f"{Fore.RED}H{Fore.YELLOW}E{Fore.GREEN}L{Fore.CYAN}L{Fore.MAGENTA}O")
print(f"{Fore.RED}W{Fore.YELLOW}O{Fore.GREEN}R{Fore.CYAN}L{Fore.MAGENTA}D")
print(f"{Fore.BLACK}{Back.RED}H{Back.YELLOW}E{Back.GREEN}L{Back.CYAN}L{Back.MAGENTA}O")
print(f"{Fore.BLACK}{Back.RED}W{Back.YELLOW}O{Back.GREEN}R{Back.CYAN}L{Back.MAGENTA}D")
print(f"{Fore.RED}{Back.BLACK}{Style.BRIGHT}Hello World")
print(f"{Fore.RED}{Back.BLACK}{Style.NORMAL}Hello World")
print(f"{Fore.RED}{Back.BLACK}{Style.DIM}Hello World")


