Skip to content

Instantly share code, notes, and snippets.

View kshirsagarsiddharth's full-sized avatar
🎯
Focusing

kshirsagarsiddharth

🎯
Focusing
View GitHub Profile
class Crawler(threading.Thread):
def __init__(self,base_url, links_to_crawl,have_visited, error_links,url_lock):
threading.Thread.__init__(self)
print(f"Web Crawler worker {threading.current_thread()} has Started")
self.base_url = base_url
self.links_to_crawl = links_to_crawl
self.have_visited = have_visited
self.error_links = error_links
self.url_lock = url_lock
from urllib.request import Request, urlopen, URLError
from urllib.parse import urlparse
import time
import threading
import queue
from bs4 import BeautifulSoup
import ssl
@kshirsagarsiddharth
kshirsagarsiddharth / inserting_retriving.py
Created August 16, 2020 05:18
Inserting files into database and retrieving them
import sqlite3
def convert_to_binary(filename):
with open(filename,'rb') as f:
blob_data = f.read()
return blob_data
def insert_blob(id,name,photo,resume):
try:
sqlite_connection = sqlite3.connect('sqlite3_python_database.db')
cursor = sqlite_connection.cursor()
class Rectangle:
def __init__(self,length,width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def peremeter(self):
return (self.length + self.width) * 2
@kshirsagarsiddharth
kshirsagarsiddharth / newline.py
Created August 7, 2020 07:20
importance of newline = "" in writing the csv file
# opening a file without newline = ""
import csv
with open('something.csv','w') as f:
writer = csv.writer(f)
writer.writerow(["one", "two", "three"])
writer.writerow(["one", "two", "three"])
# as I am writing on windows system it uses "\r\n" line endings on write
# so an extra \r (carriage return) is added
with open('something.csv','r') as f:
@kshirsagarsiddharth
kshirsagarsiddharth / spool.py
Created August 6, 2020 06:36
spooled memory
# the _rolled attribute of SpooledTemporaryFile object is a boolean, it returns True
# if the file is rolled over(written from the memory buffer to the disk) else False
import tempfile
temp = tempfile.SpooledTemporaryFile(max_size=10,mode="w+b")
# the dize of input is 11(greater than max_size)
temp.write(b"aaaaaaaaaaa")
temp._rolled
Output: True
@kshirsagarsiddharth
kshirsagarsiddharth / temp1.py
Created August 6, 2020 05:33
creating temp files
# creating temp file using common pattern for making up names
import os
import tempfile
print('Building a filename with PID:')
filename = 'custon_temp_file.{}.txt'.format(os.getpid())
with open(filename, 'w+b') as temp:
print('temp:')
print(' {!r}'.format(temp))
print('temp.name:')
# for normal file
import os
import time
start_time = time.time()
with open("example_file.pdf","rb") as f:
buffer_size = 64
retract_size = -32
while True:
f.seek(buffer_size,os.SEEK_CUR)
pass
# Reading
import mmap
with open("hello.txt",'r') as f:
with mmap.mmap(f.fileno(),0,access=mmap.ACCESS_READ) as m:
print("First 10 bytes via read -->",m.read(10))
print("First 10 bytes via slice -->",m[:10])
print("2nd 10 bytes via read -->",m.read(10))
"""
import mmap
with open("hello.txt","wb") as f:
f.write(b"Writing")
f = open("hello.txt","r+b")
mm = mmap.mmap(f.fileno(),0)
print(mm.readline())
#Output: b'Writing'
print(mm[:5])
#Output: b'Writi'