This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from urllib.request import Request, urlopen, URLError | |
| from urllib.parse import urlparse | |
| import time | |
| import threading | |
| import queue | |
| from bs4 import BeautifulSoup | |
| import ssl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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:') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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)) | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |