Jun Jul 18-22, 2022 Vikrant Patil
All notes are available online at https://notes.pipal.in/2022/arcesium_finop_batch1/
Please accept the invitation that you have received in your email and login to
From there launch your jupyter lab. Create a notebook with name module2-day3
© Pipal Academy LLP
Suppose we want to model bank account
What all do you need to make a bank account
%%file bank0.py
balance = 0
def get_balance():
return balance
def deposit(amount):
global balance
balance = balance + amount
def withdraw(amount):
global balance
balance = balance - amount
if __name__ == "__main__":
pass
Overwriting bank0.py
import bank0
bank0.get_balance()
0
bank0.deposit(10000.0)
bank0.get_balance()
10000.0
bank0.withdraw(2500)
bank0.get_balance()
7500.0
%%file bank1.py
def make_account(KYC, initial_balance):
return {"KYC":KYC, "balance":initial_balance}
def get_balance(account):
return account['balance']
def deposit(account, amount):
account['balance'] += amount
def withdraw(account, amount):
account['balance'] -= amount
if __name__ == "__main__":
pass
Writing bank1.py
import bank1
account1 = bank1.make_account("Vikrant", 5000)
bank1.deposit(account1, 700)
bank1.get_balance(account1)
5700
bank1.withdraw(account1, 300)
bank1.get_balance(account1)
5400
print("Vikrant's account balance:", bank1.get_balance(account1))
account2 = bank1.make_account("Alice", 4000)
bank1.withdraw(account2, 400)
print("Alice's account balance:", bank1.get_balance(account2))
print("Vikrant's account balance:", bank1.get_balance(account1))
Vikrant's account balance: 5400 Alice's account balance: 3600 Vikrant's account balance: 5400
Instance of class
+---------------+
| |<---------methods to manipulate data
| data |
| |<---------methods
+---------------+
|
|
+------<------- methods to access data
nums = [1, 2, 3, 4, 5, 6]
nums.append(0) # this is just one method from list which allows us to manipulate the data
class BankAccount:
def __init__(self, KYC, initial_balance):
self.KYC = KYC
self.balance = initial_balance
# there is no return statement required in __init__
def get_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
acc1 = BankAccount("Solo", 4000) # you call class name like a function...and it creates instance of class
acc1 # this is instance object of BankAccount
<__main__.BankAccount at 0x7f9f28153f70>
acc1.get_balance()
4000
acc1.deposit(4500)
acc1.get_balance()
8500
acc1.withdraw(567)
acc1.get_balance()
7933
text = str("hello this is text!")
text.split()
['hello', 'this', 'is', 'text!']
import random
class StockData:
def __init__(self, ticker):
self.ticker = ticker
def get_current_price(self):
# fire complicated query on internet to any server that provides facility to give stock data
return 100
def get_last_week_daily_closing_prices(self):
# fire complicated query on internet to any server that provides facility to give stock data
return [random.random()*100 for i in range(7)]
def buy(self, quantity):
## go to demat account, and place order
return True
s = StockData("IBM")
s.get_current_price()
100
s.get_last_week_daily_closing_prices()
[95.80095646367658, 26.711162046757096, 71.21965347450825, 85.66381897175262, 76.501012700196, 94.76433299078772, 69.10920075057427]
s.buy(100)
True
class BankAccount:
def __init__(self, KYC, initial_balance):
self.KYC = KYC
self.balance = initial_balance
# there is no return statement required in __init__
def get_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
a = BankAccount("KYC", 5000)
a.get_balance() # it expects one argument , self! but we are not passing it while calling
5000
b = BankAccount("KYC", 5000)
a is b
False
class BankAccount:
def __init__(self, KYC, initial_balance):
self.KYC = KYC
self.balance = initial_balance
# there is no return statement required in __init__
def get_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def __eq__(self, acc):
return self.KYC == acc.KYC and self.balance == acc.balance
a1 = BankAccount("KYC", 5000)
a2 = BankAccount("KYC", 5000)
a1 == a2
True
a1 is a2
False
a1
<__main__.BankAccount at 0x7f9f13e2c7f0>
print(a1)
<__main__.BankAccount object at 0x7f9f13e2c7f0>
class BankAccount:
def __init__(self, KYC, initial_balance):
self.KYC = KYC
self.balance = initial_balance
# there is no return statement required in __init__
def get_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def __eq__(self, acc):
return self.KYC == acc.KYC and self.balance == acc.balance
def __repr__(self):
return "BankAccount<" + self.KYC + ">"
a =BankAccount("Alice", 5000)
a
BankAccount<Alice>
nums = [1, 2, 34]
nums
[1, 2, 34]
Have a look at modelling of Light as a class
Light
+---------------+
| |<---------switch_on
| on=True |
| |<---------switch_off
+---------------+
|
|
+------<------- is_on
class Light:
def __init__(self):
self.on = False
def switch_on(self):
self.on = True
def switch_off(self):
self.on = False
def is_on(self):
return self.on
light1 = Light()
lights_from_house = [Light(), Light(), Light(), Light()]
def check_lights(lights):
return [light.is_on() for light in lights]
def switch_off_all(lights):
for light in lights:
light.switch_off()
check_lights(lights_from_house)
[False, False, False, False]
lights_from_house[-1].switch_on()
check_lights(lights_from_house)
[False, False, False, True]
lights_from_house[1].switch_on()
check_lights(lights_from_house)
[False, True, False, True]
switch_off_all(lights_from_house)
check_lights(lights_from_house)
[False, False, False, False]
class Light:
def __init__(self):
self.on = False
def switch_on(self):
self.on = True
def switch_off(self):
self.on = False
def is_on(self):
return self.on
class ColoredLight(Light): # inheritence
def __init__(self, color):
self.color = color
self.on = False
def get_color(self):
return self.color
def change_color(self, color):
self.color = color
c1 = ColoredLight("white")
c1.is_on()
False
c1.switch_on()
c1.is_on()
True
c1.get_color()
'white'
class HDFCBankAccount(BankAccount):
bankname = "HDFC"
alex = HDFCBankAccount("Alex", 6506)
alex.get_balance()
6506
alex.bankname
'HDFC'
ray = HDFCBankAccount("Ray", 4000)
ray.bankname
'HDFC'
HDFCBankAccount.bankname = "NEWBANK"
ray.bankname
'NEWBANK'
class DummyBankAccount(BankAccount):
def set_bankname(self, name):
self.bankname = name
db = DummyBankAccount("KYCsdsd", 4545)
db.set_bankname("SBI")
db.bankname
'SBI'
db1 = DummyBankAccount("KYCsdsd", 4545)
db1.set_bankname("YES")
db1.bankname
'YES'
class DummyBankAccount(BankAccount):
bankname = "HDFC"
def set_bankname(self, name):
self.bankname = name # in real life programming we never use same names for class and instance variable
d1 = DummyBankAccount("safdsf", 565)
d1.bankname # this is class variable
'HDFC'
d1.bankname
'HDFC'
d2 = DummyBankAccount("dsds", 5676)
d2
BankAccount<dsds>
d2.bankname
'HDFC'
DummyBankAccount.bankname = "YES"
d1.bankname
'YES'
d2.bankname
'YES'
d1.set_bankname("SBI")
d1.bankname
'SBI'
d2.bankname
'YES'
d2.bankname
'YES'
DummyBankAccount.bankname = "HELLO"
d2.bankname
'HELLO'
d1.bankname # now it is referring to instance variable
'SBI'
class FixedColorLight(Light):
color = "Yellow"
class DefaultColoredLight(Light):
def __init__(self, color="Red"): # this is default colur instance wise
self.color = color
super() # this calls parent __init__
DefaultColoredLight()
<__main__.DefaultColoredLight at 0x7f9f284499f0>
Problem Write a class for Stock with fields name, value, high, low and mechanism to update value. Updating value will also update high and low automatically if required.
Stock
+---------------+
| |<---------update_value()
| fields |
| |
+---------------+
class Stock:
def __init__(self, name, value):
self.name = name
self.value = value
self.high = value
self.low = value
def update_value(self, value):
self.value = value
if value > self.high:
self.high = value
elif value < self.low:
self.low = value
def __repr__(self):
return f"Stock<{self.name}, {self.value}>" # f" jsagdj sajgd {localvar} hdjhgsad {var2} sadas" ..string formating
ibm = Stock("IBM", 500)
ibm
Stock<IBM, 500>
ibm.update_value(600)
ibm
Stock<IBM, 600>
ibm.update_value(300)
ibm
Stock<IBM, 300>
ibm.high
600
ibm.low
300