Skip to main content

some Python (General Things )

#comprehension #dictionary data = {i : np.random.randn() for i in range(7)}#this will create a dictionary #other ways to create dictionary some_dict = {} words = ['apple', 'bat', 'bar', 'atom', 'book'] for word in words: some_dict.setdefault(word[0],[]).append(word) #or from collections import defaultdict some_dict = defaultdict(list) print(some_dict) for word in words: some_dict[word[0]].append(word) #print statement we can use seperator for i in squares(15): print(i, end=' ')#for printting in a single line, in place of space we can use anything we can use \r to print clear print #sting replace a = 'i have small car' b = a.replace('small', 'big') #decoding decode = b.encode('UTF-8') #None a=None a==None, a is None#both operation will return True #datetime from datetime import datetime, date, time dt = datetime(2018, 7, 30, 14, 58, 10)#defining a date dt.day, dt.month, dt.year, dt.hour, dt.date(), dt.time()#extracting info from date #strftime, strptime dt.strftime('%m/%d/%Y %H:%M')#this will format time in the give format dt.strftime('%m')#returns month #strptime is like reading date in a particular format and strftime is to change format datetime.strptime(date_string,date_string_format).strftime(convert_to_date_string_format) datetime.datetime.strptime("01/27/2012","%m/%d/%Y").strftime('%m-%d-%Y')#example #itertools import itertools first_letter = lambda x : x[0]#a function which returns first letter of a word names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven'] for letter, names in itertools.groupby(names, first_letter):#this code will group names based on return value of function first_letter print(letter, list(names)) #permutation for combis in itertools.permutations(['a','b','c'], 3):#all three permutation on a, b and c, here 3 means all 3 letter permutations print(combis) #counter from collections import Counter a = ['a', 'b', 'a'] print(Counter(a).most_common(1))#output is [('a', 2)] #sorted sorted(arr)#it gives sorted array also you can write some rule on which the sorting should be done using keys arr = ['abhi', 'zzzzzzz','yy','mmll'] sorted(arr)#output ['abhi', 'mmll', 'yy', 'zzzzzzz'] sorted(arr, key=lambda x : len(x))#sort the data based on length of the words, output ['yy', 'abhi', 'mmll', 'zzzzzzz'] #underscore in loops for _ in range(10):#it means do something 10 times, we need not to specify a variable there do something #to take user input abc = input('write something ') abc = int(input('write something'))#only takes integer input #evaluate abc = eval(input('write something'))#will evaluate and return the value, here we can input a String, that will be evaluated as Python code 1 == 1.0#this will return true in python 'a' > 'b'#will return false #code to find the cube root of a number cube = 0.5 epsilon = 0.01 num_guess = 0 low = min(0, cube) high = max(0, cube) if cube < 1: low = cube high = 1 guess = (high + low)/2.0 while abs(guess ** 3 - cube) >= epsilon: if guess ** 3 < cube: low = guess else: high = guess guess = (high + low)/2.0 num_guess += 1 print('number of guesses',num_guess) print('number closest to cube root is',guess) #global variable def some_global(num): global a a = num print(some_global(3), a)#prints out None, 3 #append vs extend x = [1,2,3] x.append([2,3]) print(x)#[1, 2, 3, [2, 3]] x = [1,2,3] x.extend([2,3]) print(x)#[1, 2, 3, 2, 3] #decorator from time import time def time_it(func): t1 = time() func() t2 = time() print('the time taken was ',t2-t1) return func @time_it def abc(): sum=0 for i in range(1000000): sum+=i #print(sum) #abc = time_it(abc)#either you can write this or you can write @time_it above function abc() #try except try: print(1/0) except: raise ValueError('some error')#raise explicitly value error #assert def someFunc(): a = 1 b = 0 assert not b == 0, 'denominator is zero'#will give assertion error if b==0 return a/b print(someFunc()) #appending zeros, adding leading Zeros str(3).zfill(3) #beautiful soup, web page scraping import requests from bs4 import BeautifulSoup var = 'https://thenewboston.com/forum/recent_activity.php?page=' def trade_spider(max_pages): page = 1 while page <= max_pages: url=var + str(page) source_code = requests.get(url) plain_text = source_code.text soup = BeautifulSoup(plain_text,"html.parser") for link in soup.findAll('a',{'class':'title text-semibold'}): href = link.string.strip() print(href) page += 1 trade_spider(1) #threading import threading class someMessenger(threading.Thread): def run(self): for _ in range(10): print(threading.current_thread().getName()) x = someMessenger(name='send message') y = someMessenger(name='recieve message') z = someMessenger(name='message failed') x.start() y.start() z.start() #word counter from a web page import requests from bs4 import BeautifulSoup import string import operator def getCode(url): words = [] response = requests.get(url) url_text = response.text soup = BeautifulSoup(url_text,"html.parser") for link in soup.findAll('a',{'class':'title text-semibold'}): word = link.string.lower().split() for wor in word: wo = wor.strip("'!@#$%^&*()_+-=:',./:\"<>?") if len(wo)>0: words.append(wor) # print(words) createDict(words) def createDict(words): dict = {} for word in words: if word in dict: dict[word]+=1 else: dict[word] = 1 for key,value in sorted(dict.items(),key=operator.itemgetter(0)): print(key,value) getCode('https://thenewboston.com/forum/recent_activity.php?page=1') #Pillow , image manipulation from PIL import Image from PIL import ImageFilter img = Image.open('qwe.jpg') img1 = Image.open('qwer.jpg') print(img.size) print(img.mode) resize_img = img.resize((250,250)) flip_img = img.transpose(Image.FLIP_TOP_BOTTOM) spin_img = img.transpose(Image.ROTATE_90) r, g, b = img.split() r1, g1, b1 = img1.split() new_img = Image.merge('RGB',(b, r1, g1)) print(img1.size) area = (300, 300, 636, 636) img.paste(img1, area) b_w=img.convert('L') b_w.show() new_img.show() img.show() img2 = img.crop(area) img2.show() blur_img = img.filter(ImageFilter.BLUR) blur_img.show() detail_img = img.filter(ImageFilter.DETAIL) detail_img.show() edge_img = img.filter(ImageFilter.FIND_EDGES) edge_img.show() #struct, interpret as packed binary data from struct import * # store as byte packed_data = pack('iif',6,21,3.34) print(packed_data) #print(calcsize('i')) #print(calcsize('f')) #print(calcsize('iif')) # unpack original_data = unpack('iif',packed_data) print(original_data) #map income = [20,30,40] def doubleMoney(dollar): return dollar*2 double = income*2 #iterate thorough function doubleIncome = list(map(doubleMoney,income)) print(doubleIncome) #another map example a = [10,22,13,41,52] b = [12,1,23,3,4] mini = list(map(min,a,b))#maps min function and iterate throug both lists to give min value print(mini) #output = [10,1,13,3,52] #bitwise operations #bitwise and a = 20 #10100 b = 30 #11110 c = 40 #101000 d = a & b #10100 print(d) #right shift y = c >> 2 print(y) #heapq sorting import heapq grades = [23,12,43,67,33,11] print(heapq.nlargest(2,grades))#print 2 largest values from grade stocks = [ {'ticker':'AAPL','price':200}, {'ticker':'GOOG','price':100}, {'ticker':'FB','price':300}, {'ticker':'AMZN','price':440}, {'ticker':'YHOO','price':150}, ] print(heapq.nsmallest(2,stocks,key=lambda stock:stock['price']))#print 2 smallest stock by stock price #sorting print(heapq.nsmallest(len(stocks),stocks,key=lambda stock:stock['price']))#sort the data based on stock price, len(stocks) means we want to return all values #sorting dict based on min or max of dict values or keys zip them and use min(zip(dict.values(),dict.keys()) #counter from collections import Counter text = "asdas asd asd sadas dsa dsad sad sa sa fsa f saf a fas f sf af as sg ag ag a ga gag ga s a s aas as a ag ag ag" words = text.split() counter = Counter(words) top_three = counter.most_common(3) print(top_three) #multile key dictionary sorter from operator import itemgetter stocks = [ {'ticker':'AAPL','price':200}, {'ticker':'GOOG','price':100}, {'ticker':'FB','price':300}, {'ticker':'AMZN','price':440}, {'ticker':'YHOO','price':150}, ] for stock in sorted(stocks,key=itemgetter('ticker')): print(stock) # for multiple level sorting like for same first names sort the sirnames # for stock in sorted(stocks,key=itemgetter('fname','lname')): #sorting custom object from operator import attrgetter class User: def __init__(self,x,y): self.name = x self.user_id = y #string __repr__(represetation) of this class def __repr__(self): return self.name + " : " + str(self.user_id) #make sure all are capital or small words users = [ User('Abhi',123), User('Jiya',234), User('Rama',223), User('Mona',998), User('Lola',111) ] # __repr__ use #for u in user: # print(u) #print(user[0].name) for usar in sorted(users, key=attrgetter('name')): print(usar) #taking inputs #it will always consider input as string a = input("get a number\n") print(a*3) #only integer inputs it will take else error a = int(input("get a number\n")) print(a*3) # list input a = list(input("get a number\n")) print(a*3) #assigning function to variables def add_num(x,y): print(x+y) sum = add_num sum(2,4) #type type(some_name) #return type , eg, int ,string , NoneType , function #mutable vs immutable #tuples tuples - immutable eg , a = (1,'2',4,'c') #list list - mutable #string as list of chars ** as string is a list of chars .. so operation u can do on list u can do on string #unpacking d = ('abhi','sing','jiya','muru') mf, *ml, hl = d print(mf+' '+str(ml)+' '+hl) #.format sales_record = { 'price':3.24, 'num_item':4, 'person':'abhishek' } statement = '{} bought {} items for {} rs each' print(statement.format(sales_record['person'],sales_record['num_item'],sales_record['price'])) #getcontext from decimal import * getcontext().prec = 2 print(Decimal(2.4343543534)) #basic csv opetations import csv import urllib.request import operator with open('cars.csv') as csvFile: mpg = list(csv.DictReader(csvFile)) #print(mpg[:3]) #print(len(mpg)) print(mpg[0].keys()) #average HWYMG for all the data print(round(sum(float(d['HWYMG']) for d in mpg)/len(mpg),2)) print(round(sum(float(d['COMB']) for d in mpg)/len(mpg),2)) #group by Model - gives different type of groups (creats a list of distinct model) Model = set(d['Model'] for d in mpg) print(Model) HMPGbyMODL=[] for m in Model: sumpg = 0 count = 0 for d in mpg: if d['Model']==m: sumpg+=float(d['HWYMG']) count+=1 HMPGbyMODL.append((m,round(sumpg/count,2))) HMPGbyMODL.sort(key=lambda x:x[1],reverse=True) print(HMPGbyMODL) # another way of sorting #for s in sorted(HMPGbyMODL,key=operator.itemgetter(1),reverse=True): # print(s) #date and time epoch = january 1st 1970 import datetime as dt import time as tm print(tm.time()) print(dt.datetime.fromtimestamp(tm.time()).year) print(dt.datetime.fromtimestamp(tm.time()).month) print(dt.datetime.fromtimestamp(tm.time()).hour) #create delta for subtraction or addition etc delta = dt.timedelta(days = 100) print(delta) #example today = dt.date.today() print(today) print(today-delta) #list comprehension a=[] for n in range(50): if n%2==0: a.append(n) print(a) #list comprehension b=[n for n in range(50) if n%2==0] print(b) #files from a folder filepath = r'C:\Users\avishek\Downloads\Dataset' filenames = [file for file in os.listdir(filepath) if file.endswith(".csv")] filenames #list .insert(index,element) .remove(index,element) .remove(element) # removes first occurence #__name__ == '__main__' # if running from the same file else if running via import __name__ != '__main__' #multiline print print(''' ahbish asjasj sajljlsan ''') # getting help on any thing # here we want to know about time module import time help(time) #os os.getcwd() os.mkdir('abc) os.rename('abc','abc1') os.rmdir('abc1') #sys #sys module import sys #error text sys.stderr.write('stderr text\n') sys.stderr.flush() sys.stdout.write('stdout text\n') #print the arguments helpful from command prompt print(sys.argv) #dir reurns valid list of attributes of the object dir(something) #generator def range_gen(end): current = 0 while current < end: yield current current += 1 for i in range_gen(5): print(i) # same output as above example #generator def simple_gen(): yield 'oh' yield 'hello' yield 'there' for i in simple_gen(): print(i) #iterators , yield . class range_examp: def __init__(self, end, step=1): self.current = 0 self.end = end self.step = step def __iter__(self): return self def __next__(self): if self.current >= self.end: raise StopIteration() else: return_val = self.current self.current += self.step return return_val for i in range_examp(5): print(i) #import this, it will automatically output The Zen of Python #enumerate example = ['sona', 'mona', 'jona', 'lona'] for i, j in enumerate(example): print(i,j)#i will give u 1, 2 ... new_dict = dict(enumerate(example))#makes a dictionary print(new_dict) #ctrl + p -- parameter info, jupyter notebook # 'and' and logical and are different #example print ('bhai' and 'behen') #prints the second one print ('bhai' or 'behen') #prints the first one print ('bhai' | 'behen') #error print (1010 | 101) #converts both to binary and does 'or' or 'and'(here they appear binary but they are not) print(1010 and 123) #same the second one it will print print() #formatting print('the value of pi is : {:0.2f}'.format(np.pi)) print() #equivalent to ls command #ls = [f for f in os.listdir('.') if os.path.isfile(f)] ls = [os.listdir(os.curdir)] #or ls = [os.listdir(.)] print(ls) #pwd present print(os.getcwd()) #some from https://www.youtube.com/watch?v=WiQqqB9MlkA #magic methods, dunder metthods #for example you have buld a class and you wanna used + operator on instance of that class you can implement __add__ method class addd: def __init__(self, n):#constructor self.n=n def __add__(self, other): return self.n + other.n a=addd(1) b=addd(2) print(a+b)#this will output 6 #also we can alias method like concat = __add__ class addd: def __init__(self, n): self.n=n def __add__(self, other): return self.n + other.n concat = __add__ a=addd(1) b=addd(2) a.concat(b)#same output as above #making class iterable #we have to implement __iter__ and it must return an iterator #in order to be an iterator a class has to implement __next__ menthod and it must raise StopIteration when no more items are left #example of above below we create a class which acts similar to range() function class myRange: def __init__(self, n): self.n = n self.position=0 def __iter__(self): return self def __next__(self): while self.position<self.n: self.position+=1 return self.position-1 raise StopIteration myList = myRange(5) for i in myList:#we can loop through myList print(i) print(list(myRange(5)))#or we can use it same as we will use range(5) #creating same code using generator (yield), here we dont't need to use __next__ as generator has already iterator implemented class myRange: def __init__(self, n): self.n = n self.position=0 def __iter__(self): while self.position<self.n: self.position+=1 yield self.position-1#if you put yield inside __next__ it creates some kind of infinite loop as yield is a generator and has an __iter__ method implemented myList = myRange(5) for i in myList: print(i) print(list(myRange(5))) #getattr class dog: sound='bark' def speak(self): print(self.sound+'! '+self.sound+'!') my_dog = dog() my_dog.speak()#this will print bark! bark! getattr(my_dog, 'speak')#this will tell you that speak is a bound method and you can assign to something and then you can call it to any method bark_method = getattr(my_dog, 'speak')#also you can give one more argument for default in case the attribute is not found bark_method()#this will print bark! bark! #context manager, as we saw we use with, it basically closes a file or connection once it comes out #the context manager used two dunder methods for this __enter__ __exit__ #a better way to do this is using the contextmanager decorator from contextlib import contextmanager #convert binary to int using int function int('10010', base=2)#returns 18 #functools #we can use functools to create object which behave like functions with args and kwargs #we can use the previous example from functools import partial basetwo = partial(int, base=2)#here we are creating a function using int function and kwarg base=2 basetwo('10010')#this will return 18 #id id(object)#returns id of an object, for e.g. if two names are pointing to same number like a=100 b=100 id(a)==id(b)#returns True id(a) will be equal to id(b) #copying a=[1,2,3,4] b=a#it does not copy the data to b, it just creates one more reference to the data, #if we do something like b[1]=9, a also changes, to avoid this we can use b=a[:]#as slicing data copies the data or you can do b=a.copy() #every python variable has three things, a name, a type and a reference #get size of an object import sys gso = sys.getsizeof lst = [1,2,3,4] gso(lst)#returns 96, means 96 bytes gso(0)#returns 24, for single element, so from above 96=24*4 gso(100)#returns 28, depends on the number #numpy data store location arr = np.array([1,2,3,4]) arr.data#returns the memory location #arr.strides #returns Tuple of bytes to step in each dimension when traversing an array #strings are immutable #also keys in dictionaries have to be immutable #dictionary #if we know the key exist we can use dict[key] to retrieve data, but when we are not sure we can use .get dct = {1:'A',2:'B'} dct[1]#returns A works fine dct[3]#give keyError, do in this case you can use .get dct.get(3, 'Key Not Found')#here key not found is a default message in case of keyError #__getitem__ dic = {'A':1, 'B':2} print(dic.__getitem__('A'))#will return 1 #__len__ class SomeShape: def __len__(self): return 4#len method can return int as len can't be a string ss=SomeShape() print(len(ss)) #docstring #we create docstring for a function using triple quotes """ """ def func(): """ this is a test function """ pass help(func)#will give you the docstring func.__doc__ #will also give you docstring, also you can use func? in jupyter notebook #variable in a loop for i in range(10): pass print(i)#it will print 9, as 9 was the last value assigned to it in the loop #iterate through a file #once you have iterated over a file you can't iterate over the same opened file again testFile = open('..\..\..\\tita\\train.csv') for i in testOpen: print(i,end='\r')#prints the lines for i in testOpen:#iterating twice print(i,end='\r')#prints nothing testFile.close() #pretty print import pprint #read csv import csv ### Some Extra things - Courtsey Sentdex Tutorials https://www.youtube.com/user/sentdex #regular expressions Identifiers: \d = any number \D = anything but a number \s = space \S = anything but a space \w = any letter \W = anything but a letter . = any character, except for a new line \b = space around whole words \. = period. must use backslash, because . normally means any character. Modifiers: {1,3} = for digits, u expect 1-3 counts of digits, or "places" + = match 1 or more ? = match 0 or 1 repetitions. * = match 0 or MORE repetitions $ = matches at the end of string ^ = matches start of a string | = matches either/or. Example x|y = will match either x or y [] = range, or "variance" {x} = expect to see this amount of the preceding code. {x,y} = expect to see this x-y amounts of the precedng code White Space Charts: \n = new line \s = space \t = tab \e = escape \f = form feed \r = carriage return Characters to REMEMBER TO ESCAPE IF USED! . + * ? [ ] $ ^ ( ) { } | \ Brackets: [] = quant[ia]tative = will find either quantitative, or quantatative. [a-z] = return any lowercase letter a-z [1-5a-qA-Z] = return all numbers 1-5, lowercase letters a-q and uppercase A-Z #example of re (regular expression) import re exampleString = ''' Jessica is 15 years old, and Daniel is 27 years old. Edward is 97 years old, and his grandfather, Oscar, is 102. ''' ages = re.findall(r'\d{1,3}', exampleString) names = re.findall(r'[]A-Z][a-z]*', exampleString) data = list(zip(ages,names)) print(data) #multiline string exampleString = ''' Jessica is 15 years old, and Daniel is 27 years old. Edward is 97 years old, and his grandfather, Oscar, is 102. ''' #urlib cool things import urllib.request as urlr import urllib.parse as parser ''' url = 'https://pythonprogramming.net/' values = {'s':'basic', 'submit':'Search'} data = parser.urlencode(values) #print(data) data = data.encode('utf-8') req = urlr.Request(url, data) print(req) resp = urlr.urlopen(req) respData = resp.read() print(respData) ''' try: x = urlr.urlopen('https://www.google.com/searc?q=test') print(x.read()) except Exception as e: print(str(e)) print('=================================================') try: url = 'https://www.google.com/search?q=test' headers = {} headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17" req = urlr.Request(url, headers=headers) resp = urlr.urlopen(req) respData = resp.read() print(respData) except Exception as e: print(str(e)) #threading import threading from queue import Queue import time print_lock = threading.Lock() q = Queue() def exampleJob(worker): time.sleep(.5) #pretend to do some work. with print_lock: print(threading.current_thread().name,worker) #The threader thread pulls an worker from the queue and processes it def someThread(): while True: #gets an worker from the queue worker = q.get() #Run the example job with the avail worker in queue (thread) exampleJob(worker) #completed with the job q.task_done() #how many threads are we going to allow for for x in range(10): t = threading.Thread(target=someThread) #classifying as a daemon, so they will die when the main dies t.daemon = True #begins, must come after daemon definition t.start() start = time.time() #20 jobs assigned. for worker in range(20): q.put(worker) #wait until the thread terminates. q.join() #with 10 workers and 20 tasks, with each task being .5 seconds, then the completed job #is ~1 second using threading. Normally 20 tasks with .5 seconds each would take 10 seconds. print('Entire job took:',time.time() - start) #make folders import os Folder = 'My New Folder' if not os.path.exists(Folder): os.makedirs(Folder) #cx_Freeze from cx_Freeze import setup, Executable setup(name = 'MakeFolder', version = 0.1, description = 'Make Folder', executables = [Executable("test.py")] #run python setup.py build from cmd (setup.py is the name of the file containing the cxfreeze code) #pickle --- serialization and deserialization import pickle myDict = {1:'abc',2:'cde',3:'def'} pickle_out = open("test_dict.pickle",'wb') pickle.dump(myDict, pickle_out) pickle_out.close() #loading back the pickle pickle_in = open("test_dict.pickle",'rb') myDict = pickle.load(pickle_in) print(myDict) #sqlLite import sqlite3 conn = sqlite3.connect('myDemoDb.db') c = conn.cursor() def create(): c.execute('CREATE TABLE IF NOT EXISTS dummyTable(col1 REAL, col2 TEXT, col3 TEXT, col4 REAL)') def insert(): c.execute("INSERT INTO dummyTable VALUES(12345, 'jiya', '2017-22-01', 008)") conn.commit() c.close() conn.close() create() insert() #inserting with variables import sqlite3 import time import datetime import random conn = sqlite3.connect('myDemoDb.db') c = conn.cursor() def create(): c.execute('CREATE TABLE IF NOT EXISTS dummyTableVar(unix REAL, dateStamp TEXT, keyWord TEXT, value REAL)') def dynamicInsert(): unix = time.time() dateStamp = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S')) keyWord = 'Python' value = random.randrange(0,10) c.execute("INSERT INTO dummyTableVar(unix, dateStamp, keyWord, value) VALUES (?, ?, ?, ?)", (unix, dateStamp, keyWord, value)) conn.commit() c.close() conn.close() create() dynamicInsert() #Select quering conn = sqlite3.connect('myDemoDb.db') c = conn.cursor() def select(): global data c.execute('SELECT * FROM dummyTableVar') data = c.fetchall() select() print(data) #Update c.execute('UPDATE dummyTableVar SET value=88 WHERE value=8') conn.commit() #Delete c.execute('DELETE FROM dummyTableVar WHERE vaue=88') conn.commit() #join string names= ['abhi','jiya','mona','sona'] print(', '.join(names)) #using join with os path = os.path.join('C:\\abc\\dce','abc.txt')#output = C:\abc\dce\abc.txt print(path) #argparse CLI(command line interface) import argparse import sys def main(): parser = argparse.ArgumentParser() parser.add_argument('--x', type=float, default=1.0, help='What is the first number') parser.add_argument('--y', type=float, default=1.0, help='What is the second number') parser.add_argument('--operation', type=str, default='add', help='What operation? (add, sub, mul or div)') args = parser.parse_args() sys.stdout.write(str(calc(args))) def calc(args): if args.operation == 'add': return args.x + args.y elif args.operation == 'sub': return args.x - args.y elif args.operation == 'mul': return args.x * args.y elif args.operation == 'div': return args.x / args.y if __name__ == '__main__': main() #eval (cant put definition) a = eval("1 == 1") print(a) x = input("code ")#input print('hi') x = eval(input("code "))#input print('hi') #exec (can define also) x = input("code ") x = exec(input("code "))#input y = 5 print(y) #will print 5 exec("def test(): print('something')") #we can also define a function here #list comprehension xyz = [i for i in range(5)] print(xyz) #generator expression xyz = (i for i in range(5)) print(xyz) #timit import timit print(timeit.timeit(''' input_list = range(100) def div_by_five(num): if num%5 == 0: return True else: return False xyz = (i for i in input_list if div_by_five(1)) ''', number=5000)) #enumerate example = ['sona', 'mona', 'jona', 'lona'] for i, j in enumerate(example): print(i,j)#i will give u 1, 2 ... new_dict = dict(enumerate(example))#makes a dictionary print(new_dict) #generator def simple_gen(): yield 'oh' yield 'hello' yield 'there' for i in simple_gen(): print(i) #multiprocessing import multiprocessing def spawn(i): print('spawned!', i) if __name__ == '__main__': for i in range(5): p = multiprocessing.Process(target=spawn, args=(i,)) #if u dont was to pass args dont use args p.start()#starts the process p.join()#waiting for the process to be complete if u want to run process irespective of each other then dont use .join() #multiprocessing returning values from multiprocessing import Pool def job(num): return num * 2 if __name__ == '__main__': p = Pool(processes=10) data = p.map(job, range(20)) p.close() print(data) #beautifulSoup with multiprocessing - making spider from multiprocessing import Pool import bs4 as bs import random import requests import string def random_starting_url(): starting = ''.join(random.SystemRandom().choice(string.ascii_lowercase) for _ in range(3))#choice will chose a character from a-z url = ''.join(['https://', starting, '.com']) return url url = random_starting_url() print(url) #print(string.ascii_lowercase) def handle_local_links(url, link): if link.startswith('/'): return ''.join([url, link]) else: return link def get_links(url): try: resp = requests.get(url) soup = bs.BeautifulSoup(resp.text, "html.parser") body = soup.body links = [link.get('href') for link in body.find_all('a')] links = [handle_local_links(url, link) for link in links] links = [str(link.encode("ascii")) for link in links] return links except TypeError as e: print(e) print('got a typeError') except IndexError as e: print(e) print('index error did not find any useful links') except AttributeError as e: print(e) print('attribute error') except Exception as e: print(str(e)) print('rest of the exception') def main(): how_many = 50 p = Pool(processes=how_many) parse_us = [random_starting_url() for _ in range(how_many)] data = p.map(get_links, [link for link in parse_us]) data = [url for url_list in data for url in url_list] p.close() print(data) with open('urls.txt','w') as f: f.write(str(data)) if __name__ == '__main__': main() #OOPs and pygame import random import pygame STARTING_BLUE_BLOB = 10 STARTING_RED_BLOB = 3 WIDTH = 800 HEIGHT = 600 WHITE = (255,255,255) BLUE = (0,0,255) RED = (255,0,0) game_display = pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("BlobWorld") clock = pygame.time.Clock() class Blob: def __init__(self, color): self.x = random.randrange(0, WIDTH) self.y = random.randrange(0, HEIGHT) self.size = random.randrange(4, 8) self.color = color def move(self): self.move_x = random.randrange(-1, 2) self.move_y = random.randrange(-1, 2) self.x += self.move_x self.y += self.move_y if self.x < 0: self.x = 0 elif self.x > WIDTH: self.x = WIDTH if self.y < 0: self.y = 0 elif self.y > HEIGHT: self.y = HEIGHT def draw_environment(blob_list): game_display.fill(WHITE) for blob_dict in blob_list: for blob_id in blob_dict: blob = blob_dict[blob_id] pygame.draw.circle(game_display, blob.color, [blob.x, blob.y], blob.size) blob.move() pygame.display.update() def main(): red_blob = Blob(RED) blue_blob = dict(enumerate([Blob(BLUE) for i in range(STARTING_BLUE_BLOB)])) red_blob = dict(enumerate([Blob(RED) for i in range(STARTING_RED_BLOB)])) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() draw_environment([blue_blob,red_blob]) #draw_environment(red_blob) clock.tick(60) if __name__ == '__main__': main() #decorators def add_wrapping(item): def wrapped_item(): return 'a wrapped up box of {}'.format(str(item())) return wrapped_item @add_wrapping def new_gpu(): #this whole function will go into item thats y we use item() above not item return 'a new Tesla P100' print(new_gpu()) #iterators , yield . class range_examp: def __init__(self, end, step=1): self.current = 0 self.end = end self.step = step def __iter__(self): return self def __next__(self): if self.current >= self.end: raise StopIteration() else: return_val = self.current self.current += self.step return return_val for i in range_examp(5): print(i) #generator def range_gen(end): current = 0 while current < end: yield current current += 1 for i in range_gen(5): print(i) # same output as above example #blob import pygame import random import numpy as np STARTING_BLUE_BLOBS = 15 STARTING_RED_BLOBS = 15 STARTING_GREEN_BLOBS = 15 WIDTH = 800 HEIGHT = 600 WHITE = (255, 255, 255) BLUE = (0, 0, 255) RED = (255, 0, 0) class Blob: def __init__(self, color, x_boundary, y_boundary, size_range=(4, 8), movement_range=(-1, 2)): self.size = random.randrange(size_range[0], size_range[1]) self.color = color self.x_boundary = x_boundary self.y_boundary = y_boundary self.x = random.randrange(0, self.x_boundary) self.y = random.randrange(0, self.y_boundary) self.movement_range = movement_range def move(self): self.move_x = random.randrange(self.movement_range[0], self.movement_range[1]) self.move_y = random.randrange(self.movement_range[0], self.movement_range[1]) self.x += self.move_x self.y += self.move_y def check_bounds(self): if self.x < 0: self.x = 0 elif self.x > self.x_boundary: self.x = self.x_boundary if self.y < 0: self.y = 0 elif self.y > self.y_boundary: self.y = self.y_boundary game_display = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Blob World") clock = pygame.time.Clock() class BlueBlob(Blob): def __init__(self, x_boundary, y_boundary): Blob.__init__(self, (0, 0, 255), x_boundary, y_boundary) def __add__(self, other_blob): if other_blob.color == (255, 0, 0): self.size -= other_blob.size other_blob.size -= self.size elif other_blob.color == (0, 255, 0): self.size += other_blob.size other_blob.size = 0 elif other_blob.color == (0, 0, 255): pass else: raise Exception('Tried to combine one or multiple blobs of unsupported colors!') class RedBlob(Blob): def __init__(self, x_boundary, y_boundary): Blob.__init__(self, (255, 0, 0), x_boundary, y_boundary) class GreenBlob(Blob): def __init__(self, x_boundary, y_boundary): Blob.__init__(self, (0, 255, 0), x_boundary, y_boundary) def is_touching(b1, b2): return np.linalg.norm(np.array([b1.x, b1.y]) - np.array([b2.x, b2.y])) < (b1.size + b2.size) def handle_collisions(blob_list): blues, reds, greens = blob_list for blue_id, blue_blob in blues.copy().items(): for other_blobs in blues, reds, greens: for other_blob_id, other_blob in other_blobs.copy().items(): if blue_blob == other_blob: pass else: if is_touching(blue_blob, other_blob): blue_blob + other_blob if other_blob.size <= 0: del other_blobs[other_blob_id] if blue_blob.size <= 0: del blues[blue_id] return blues, reds, greens def draw_environment(blob_list): game_display.fill(WHITE) blues, reds, greens = handle_collisions(blob_list) for blob_dict in blob_list: for blob_id in blob_dict: blob = blob_dict[blob_id] pygame.draw.circle(game_display, blob.color, [blob.x, blob.y], blob.size) blob.move() blob.check_bounds() pygame.display.update() return blues, reds, greens def main(): blue_blob = dict(enumerate([BlueBlob(WIDTH,HEIGHT) for i in range(STARTING_BLUE_BLOBS)])) red_blob = dict(enumerate([RedBlob(WIDTH,HEIGHT) for i in range(STARTING_RED_BLOBS)])) green_blob = dict(enumerate([GreenBlob(WIDTH,HEIGHT) for i in range(STARTING_GREEN_BLOBS)])) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() blue_blob, red_blob, green_blob = draw_environment([blue_blob,red_blob,green_blob]) #draw_environment(red_blob) clock.tick(60) if __name__ == '__main__': main()


Comments