Posts

Showing posts from August, 2022

Saving Web dataset into a file, Pickling that file, then Unpickling it

import pickle import requests # getting iris dataset from a web address resp = requests.get("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") # saving the web data into a text file with open("iris.txt", "w") as f: f.write(resp.text) # reading the created text file containing the web data with open("iris.txt", "r") as f: dataset = f.read() # splitting the web data in text file listof = dataset.split("\n") # converting list into list of lists lol = [[item] for item in listof] # pickling the data pickleed = open("irislists.pkl", "wb") pickle.dump(file=pickleed, obj=lol) pickleed.close() # unpickling the data unpickleed = open("irislists.pkl", "rb") pythonobj = pickle.load(file=unpickleed) print(pythonobj) unpickleed.close()

Pickle Module in Python

import pickle # pickling a python object mylist = ["waqas", "sheeraz",{"sheeraz": "haroon", "sheer": "veer"}, "haroon", "farrukh"] file_object = open("mylist.pkl", "wb") # write as binary file pickle.dump(file=file_object, obj=mylist) file_object.close() # unpickling into python object fileb = open("mylist.pkl", "rb") # read binary file python_obj = pickle.load(file=fileb) print(python_obj) fileb.close()

News API Client in Python

from newsapi import NewsApiClient news_api = NewsApiClient(api_key="YOURAPIKEY") # YOURAPIKEY get from https://newsapi.org/ by making an account first top_headlines = news_api.get_top_headlines(q="amazon") # q represents keywords or phrases that NEWS must contain print(top_headlines) all_articles = news_api.get_everything(q="amazon") print(all_articles) sources = news_api.get_sources() print(sources)

Get and Read top NEWS Headlines in Python

import json import requests def newsreader(string): from win32com.client import Dispatch speak_it = Dispatch("SAPI.SpVoice") speak_it.Speak(string) response = requests.get("https://newsapi.org/v2/top-headlines?country=in&category=general&apiKey=YOURAPIKEY") # YOURAPIKEY get from https://newsapi.org/ by making an account first news = json.loads(response.text) # print(news) for i in range(5): breaking_news = news["articles"][i]["description"] print(breaking_news) if i==0: newsreader("Welcome to Breaking news for this hour.") newsreader("the first breaking news for this hour is, ") newsreader(breaking_news) elif i==4: newsreader("the last breaking news for this hour is, ") newsreader(breaking_news) newsreader("Thanks for listening. Plz come again for next round of updates.") else: newsreader("the ne...

Text to Speech conversion in Python

from win32com.client import Dispatch speak_it = Dispatch("SAPI.SpVoice") string = input("Enter the text and we will speak it: ") speak_it.Speak(string)

JSON module in Python

import json # Converting python object and writing into json file data = { "name": "Satyam kumar", "place": "patna", "skills": ["Raspberry pi", "Machine Learning", "Web Development"], "email": "xyz@gmail.com", "projects": ["Python Data Mining", "Python Data Science"], "player": True} with open("data2.json", "w") as f: json.dump(data, f) # Converting python object into json string. res = json.dumps(data) print(res) # loads the created json file with open("data.json", "r") as f: print(json.load(f)) # json string, after parsing becomes a python dict data = """{ "name" : "abdur", "natur" : "space", "favterm" : "infinity", "lvoe" : true }""" pasa = js...

Coroutines in Python

Note: Get your free API key at https://newsapi.org/ by first making your account. from urllib.request import urlopen from bs4 import BeautifulSoup def searcher(): url = "https://en.wikipedia.org/wiki/Kabul#Toponymy_and_etymology" page = urlopen(url) html = page.read().decode("utf-8") soup = BeautifulSoup(html, "html.parser") article1 = soup.get_text() url = "https://en.wikipedia.org/wiki/Python_(programming_language)" page = urlopen(url) html = page.read().decode("utf-8") soup = BeautifulSoup(html, "html.parser") article2 = soup.get_text() url = "https://en.wikipedia.org/wiki/Amoeba_(operating_system)" page = urlopen(url) html = page.read().decode("utf-8") soup = BeautifulSoup(html, "html.parser") article3 = soup.get_text() article = article1 + article2 + article3 while True: word = (yield) ...

Extract text only from a webpage through "html.parser"

from bs4 import BeautifulSoup from urllib.request import urlopen url = "https://en.wikipedia.org/wiki/Kabul#Toponymy_and_etymology" page = urlopen(url) html = page.read().decode("utf-8") soup = BeautifulSoup(html, "html.parser") print(soup.get_text())

Extracting Text From HTML (Web Pages) in Python

  from urllib . request import urlopen # scenario 1: html with proper <title> tags url = "http://olympus.realpython.org/profiles/aphrodite" page = urlopen ( url ) html_bytes = page .read() html = html_bytes .decode( "utf-8" ) # OR replace the above 4 lines with the following 1 line html = urlopen ( "http://olympus.realpython.org/profiles/aphrodite" ).read().decode( "utf-8" ) # Extract Title Text From HTML With String Methods title_index = html .find( "<title>" ) start_index = title_index + len ( "<title>" ) end_index = html .find( "</title>" ) title = html [ start_index : end_index ] print ( title ) # scenario 2: html with improper <title > tags url = "http://olympus.realpython.org/profiles/poseidon" page = urlopen ( url ) html = page .read().decode( "utf-8" ) start_index = html .find( "<title>" ) + len ( "<title>" ) en...

Function Caching in Python

import time from functools import lru_cache @lru_cache(maxsize=1) def add_work(n, m): time.sleep(3) return n + m print(add_work(3,4)) # takes 3 secs to print print(add_work(3,5)) # takes 3 secs to print print(add_work(3,4)) # takes 3 secs to print print(add_work(3,5)) # takes 3 secs to print print(add_work(3,5)) # this and rest takes no time to print print(add_work(3,5)) print(add_work(3,5)) print(add_work(3,5)) ###############################################3 # Following takes user input for defining maxsize import time from functools import lru_cache @lru_cache(maxsize=int(input("Enter maxsize for lru_cache: "))) def some_work(n, m): time.sleep(3) return n + m print(some_work(3,4)) print(some_work(3,5)) print(some_work(3,4)) print(some_work(3,5)) print(some_work(3,4)) print(some_work(3,5))

List, Dictionary, Set and Generators Comprehensions in Python

# List comprehension [] listc = [i for i in range(20)] print(listc) print([i for i in range(1, 21) if i%2!=0]) # Dictionary comprehension {} mydict = {i:f"item{i}" for i in range(1, 6)} print(mydict) # Dictionary comprehension can also be used to reverse key, value pairs newdict = {value:key for key, value in mydict.items()} print(newdict) # Set comprehension {} setc = {i for i in [3,4,5,6,3,4,5,6]} print(setc) # Generator comprehension () genc = (i for i in range(5)) print(genc.__next__()) print(genc.__next__()) print(genc.__next__()) print(genc.__next__()) print(genc.__next__()) # or for item in genc: print(item)

Generators and Yield keyword in Python

def generator(n): for i in range(n): yield i g = (generator(4)) print(iter(g)) print(g) # code from line 6-8 is equivalent print(g.__iter__()) print(g.__next__()) # code from line 10-12 is equivalent print(iter(g).__next__()) print(g.__iter__().__next__()) ################################ name = "abdur" print(iter(name)) i = iter(name) print(i.__next__()) print(i.__next__()) print(i.__next__()) print(i.__next__()) print(i.__next__())

Random Password Generator in Python - Source Code

import random import string print("---Welcome to Random Password Generator Tool---") characters = string.ascii_letters + string.digits + string.punctuation lengthOfPass = int(input("Enter the length of your required password (max length allowed is 94): ")) temp_password = random.sample(characters, lengthOfPass) password = "".join(temp_password) print("Your password is", password) # or the following code can be used, both do the same job import random import string upper_chars = string.ascii_uppercase lower_chars = string.ascii_lowercase nums_chars = string.digits symbol_chars = string.punctuation all_chars = upper_chars + lower_chars + nums_chars + symbol_chars length_pass = int(input("Enter the required password length(Max=94): ")) temp_password = random.sample(all_chars, length_pass) password = "".join(temp_password) print(password)

Library Management System in Python - Source Code

books_in_library = ["In Search of Lost Time by Marcel Proust", "Ulysses by James Joyce", "One Hundred Years of Solitude by Gabriel Garcia Marquez", "War and Peace by Leo Tolstoy", "The Odyssey by Homer"] logbooks = {} class Library: def __init__(self, library_name, library_books_list): self.libraryname = library_name self.bookslist = library_books_list def books_available(self): if books_in_library != []: print(f"The book(s) available for lending in {self.libraryname} are:") for index, book in enumerate(books_in_library): print(index+1,"-", book) if books_in_library == []: print(f"All the books in {self.libraryname} has benn lended. Plz wait till users return the books.") def lend_books(self): lender_name = input("Plz enter your kind name: ") print...

Function in Python that repeats each character in a string twice - Source Code

def repeat(string): for item in string: print(item * 2, end="") repeat("rehman")

Function in Python that filters integers or strings from a given list

# returns integers only from a list def onlyints(string): x = list(filter(lambda e: isinstance(e, int), string)) return x mylist = [3,6,"abdur", "space", 767, 555, 678] print(onlyints(mylist)) # returns strings only from a list def onlyints(string): x = list(filter(lambda e: isinstance(e, str), string)) return x mylist = [3,6,"abdur", "space", 767, 555, 678] print(onlyints(mylist)) # returns integers only from a list def onlyints(string): return [e for e in string if isinstance(e, int)] mylist = [3,6,"abdur", "space", 767, 555, 678] print(onlyints(mylist)) # returns strings only from a list def onlyints(string): return [e for e in string if isinstance(e, str)] mylist = [3,6,"abdur", "space", 767, 555, 678] print(onlyints(mylist))

Function in Python that calculates discount on a price

def give_discount(fullprice, discountpc): discount = discountpc / 100 * fullprice askprice = fullprice - discount print(f"The actual price is {fullprice}, discount is {discountpc} percent, you give only {askprice}. Thanks.") fullprice = int(input("Enter the actual price of the product plz: ")) discount = float(input("What percentage of discount you want to give?: ")) give_discount(fullprice, discount)