Skip to content

Latest commit

 

History

History
102 lines (68 loc) · 3.6 KB

File metadata and controls

102 lines (68 loc) · 3.6 KB

A quick rundown of Pseudocodes for certain advanced Pythonic operations

Python_@class_@static_methods_WebCrawler_RESTfulAPI_TimeComplexity

  • ClassMethod vs Static Method pseudocode
  • Python Web crawling pseudocode
  • RESTful API creation pseudocode
  • Time Complexity order of variuos operation on Pythonic arrays data structure

ClassMethod vs Static Method -

We can use a class method without making an object of a class(Person) using @classmethod. In @classmethod a cls variable is passed as an argument instead of self variable like other methods of class. While in staticmethod, neither class variable is passed nor self variable.

Python program to demonstrate use of class method and static method.

from datetime import date 

class Person: 
	def __init__(self, name, age): 
		self.name = name 
		self.age = age 
	
	# a class method to create a Person object by birth year. 
	@classmethod
	def fromBirthYear(cls, name, year): 
		return cls(name, date.today().year - year) 
	
	# a static method to check if a Person is adult or not. 
	@staticmethod
	def isAdult(age): 
		return age > 18

person1 = Person('mayank', 21) 
person2 = Person.fromBirthYear('mayank', 1996) 

print person1.age 
print person2.age 
print Person.isAdult(22) 

Python Web crawling Psuedo code

  import urllib2
  from bs4 import BeautifulSoup

  quote_page =http://www.bloomberg.com/quote/SPX:IND'
  page = urllib2.urlopen(quote_page)   #query the website and return the html to the variable ‘page’

  soup = BeautifulSoup(page, ‘html.parser’) # parse the html using beautiful soup and store in variable `soup'
  name_box = soup.find(‘h1’, attrs={‘class’: ‘name’}) 
  name = name_box.text.strip() # strip() is used to remove starting and trailing spaces

  price_box = soup.find(‘div’, attrs={‘class’:’price’}) # get the index price
  price = price_box.text
  print price

Reference links for web crawling & scarpping

basic tutorial for web scrapping

advanced tutorial for web scrapping

Creating REST API with python FLASK micro web framework - pseudocode

	from flask import request
	from flask import jsonify
	from flask import Flask
	app = Flask(__name__) 
	@app.route('/hello',methods=['POST']) 
	
	def hello():
		message = request.get_json(force=True)  #always parse json even if it is unsure of data type
		name = message['name']
		response = {'greeting': 'Hello, ' + name + '!'}
		return jsonify(response)    #convert python dictionary into json
  
 #export FLASH_APP= hello_app.py
 #flask run --host=0.0.0.0
 

Time Complexity order of variuos operation on Pythonic arrays data structure

  • Arrays offer random access to their contents through a numeric index. Therefore array access is O(1).

  • Searching for a given value requires iterating through the array. So array search is O(n).

  • If you know that the array is sorted in a way that's amenable to binary search, then you can search the array that way and achieve in the best case, array search is O(log n).

  • Inserting into an array requires you to shift all elements to the right by 1. If we say that on average that means n/2 elements need to be shifted, that devolves to **array insert is O(n)*.

  • Deleting an element from an array requires you to shift all the elements to the left by 1. By similar logic to the above insertion case, array deletion is O(n).