Dec 19-21, 2019 Vikrant Patil, Anand Chitipothu
These notes are available online at http://notes.pipal.in/2019/grofers_basic_dec/day3.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 3-part2
We will be using python 3 (>= 3.0) from anaconda for this training. You can download it from
!pip install requests
import requests
resp = requests.get("https://grofers.com/")
print(resp.text[:500])
resp.status_code
!python3 -m pip install requests
resp = requests.get("http://httpbin.org/get", params={"param1":"python",
"param2":"xyz"})
print(resp.text)
resp = requests.post("http://httpbin.org/post",
data={"name":"python",
"email":"xyz@xyz.com"})
print(resp.text)
resp.json()
Find repositories of grofers on git hub
url = "https://api.github.com/orgs/grofers/repos"
repos = requests.get(url).json()
type(repos)
repos[0]
def get_repo_size(r):
return r['size']
for repo in sorted(repos, key=get_repo_size)[:5]:
print(repo['full_name'], repo['size'], repo['forks'])
for repo in sorted(repos, key=get_repo_size, reverse=True)[:5]:
print(repo['full_name'], repo['size'], repo['forks'])
resp = requests.get("https://grofers.com/")
from bs4 import BeautifulSoup
!python3 -m pip install beautifulsoup4
resp = requests.get("https://grofers.com/s/?q=daliya&suggestion_type=0&t=0")
soup = BeautifulSoup(resp.content)
for d in soup.find_all("div", attrs={"class":"plp-product"}):
name = d.find_all("div", attrs={"class":"plp-product__name"})[0]
print(name.text)
soup.find_all("div", attrs={"class":"plp-product"})[0].find_all("div", attrs={"class":"plp-product__name"})[0]
for d in soup.find_all("div", attrs={"class":"plp-product"}):
name = d.find_all("div", attrs={"class":"plp-product__name"})[0]
price = d.find_all("div", attrs={"class":"plp-product__price"})[0]
print(name.text, price.text)
import time
time.sleep(0.5)
help(time.sleep)
def find_product(name):
url = "https://grofers.com/s/?q={product}&suggestion_type=0&t=1".format(product=name)
resp = requests.get(url)
soup = BeautifulSoup(resp.content)
products = []
for d in soup.find_all("div", attrs={"class":"plp-product"}):
name = d.find_all("div", attrs={"class":"plp-product__name"})[0]
price = d.find_all("div", attrs={"class":"plp-product__price"})[0]
products.append((name.text, price.text))
return products
find_product("daliya")
find_product("onion")
resp = requests.post("http://httpbin.org/post", files={"filename":open("poem.txt")})
print(resp.text)
resp.json()
%%file bank0.py
balance = 0
def get_balance():
return balance
def deposit(amount):
global balance
balance += amount
def withdraw(amount):
global balance
balance -= amount
import bank0
bank0.get_balance()
bank0.deposit(100)
%%file bank1.py
def make_account():
return {"balance":0}
def get_balance(account):
return account['balance']
def deposit(account , amount):
account['balance'] += amount
def withdraw(account, amount):
account['balance'] -= amount
def test_account():
a1 = make_account()
assert get_balance(a1) == 0
deposit(a1, 100)
assert get_balance(a1) == 100
withdraw(a1, 50)
assert get_balance(a1) == 50
a2 = make_account()
assert get_balance(a1) == 50
assert get_balance(a2) == 0
!pytest bank1.py
a1 = bank1.make_account()
class BankAccount:
def __init__(self, name, balance):# this constructor
self.name = name
self.balance = balance
def get_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
a1 = BankAccount("Vikrant", 1000)
a1.get_balance()
a1.deposit(1000)
a1.withdraw(1000)
a1.get_balance()
-problems
p1 = Point(2, 4)
p2 = Point(3, 4)
p3 = p1.add(p2)
print(p3.x, p3.y)
5 8
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self, point):
x = self.x + point.x
y = self.y + point.y
return Point(x ,y)
p1 = Point(1, 2)
p2 = Point(4, 5)
p3 = p1.add(p2)
p3.x, p3.y
p1
Point
l = [1,2,3]
l
p1
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self, point):
x = self.x + point.x
y = self.y + point.y
return Point(x ,y)
def __repr__(self):
return "Point({},{})".format(self.x, self.y)
def __str__(self):
return "({},{})".format(self.x, self.y)
p = Point(2,3)
p
print(p)
p
repr(p)
import time
time.time()
problem
import time
def wait():
time.sleep(5)
t =Timer()
t.start()
wait()
t.stop()
print(t.get_elapsed_time())
5.0212
class RedPoint(Point):
color = "red"
r = RedPoint(3, 5)
r
r.add(RedPoint(2,2))
r
print(r)
r.color
import unittest
class Test(unittest.TestCase):
def setUp(self):
print("Setup")
def tearDown(self):
print("cleaning up")
def test_case1(self):
print("case1")
def test_case2(self):
print("case2")
s
int("er")
def read_csv(filename):
with open(filename) as f:
return [[int(c) for c in line.strip().split(",")] for line in f]
%%file data.csv
1,2,3
2,3,4
1,2,3
read_csv("data.csv")
%%file data1.csv
1,,3
2,3,4
1,2,3
read_csv("data1.csv")
def parseint(strnum):
try:
return int(strnum)
except ValueError:
print("got missing/invalid value!")
return 0
def read_csv(filename):
with open(filename) as f:
return [[parseint(c) for c in line.strip().split(",")] for line in f]
read_csv("data1.csv")