<jupyter_start><jupyter_text>Python course day 9  Revision of concepts * Generator function: function in which you use the keyword yield to control the flow so that each time you call a function, you can only generate one value.* Object Oriented programming (OOP) : obj.func() In OOP, you have a concept of class that describes attributes of the object of that class.* procedure oriented programming: func()<jupyter_code>class Student:
    def __init__(self, n, a, c):
        self.name    = n
        self.age     = a
        self.country = c
    def __str__(self):
        return self.name + "-" + str(self.age) + "-" + self.country
class University:
    def __init__(self, students):
        self.students = students
        
    def print_students(self):
        for s in self.students:
            print(s)
# create 3 student objects
student1 = Student("Ling", 25, "China")
student2 = Student("Simon", 23, "Belgium")
student3 = Student("Sarah", 24, "USA")
# a list of student objects
student_list = []
student_list.append(student1)
student_list.append(student2)
student_list.append(student3)
vu = University(student_list)
vu.print_students() # obj.func() => func(obj)
class animal:
    def __init__(self, n):
        self.name = n
        
    def print_animal(self):
        print(self.name)
#reptile is a sub-class, animal is a super-class
class reptile(animal):
    def __init__(self, n, f):
        super().__init__(n)
        self.feature = f
    
    def print_reptile(self):
        super().print_animal()
        print(self.feature)
crocodile = reptile("Crocodile", "lay eggs")
crocodile.print_reptile()
!pip install pytube
from pytube import Playlist
pwd
import os
os.mkdir('Dutch-songs')
os.chdir('Dutch-songs')
pwd
myp = Playlist('https://www.youtube.com/playlist?list=PLp4IruhRtnPP4qPNpjGyUL6kEIF70kpWU')
myp.title()
videos = myp.populate_video_urls()
myp.playlist_url
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=7ECB5AigzzY&list=PLp4IruhRtnPP4qPNpjGyUL6kEIF70kpWU&index=2&t=0s')
yt.views
video = yt.streams.first()
type(video)
pwd
os.chdir('/Users/unmesh/Unicorn')
pwd
import pandas as pd
# df is the object of Dataframe class returned by read_excel
df = pd.read_excel("SampleData.xlsx", sheet_name="SalesOrders")
df.head()
df.describe()
# all columns of this data frame
df.columns
df.head()
# Locations
# iloc : access with row index and column index
df.iloc[1][4]
df.loc[1]['Units']
df
df2 = df.set_index('Rep')
df2.head()
df2.loc['Jones']['Item']
df2.iloc[4:6]['Item']
result = df2.loc['Jones']['Total']
result.shape
sum(result)
my_list = [1,2,4,5,6,7,3,3]
sum(my_list)
type(result)
sum(df2.loc['Jones']['Total'])
class SalesOrders:
    def __init__(self, od, rg, rp, i, u, uc, t):
        self.order_date = od
        self.region     = rg
        self.rep        = rp
        self.item       = i
        self.units      = u
        self.unit_cost  = uc
        self.total      = t
df = pd.read_excel("SampleData.xlsx", sheet_name="SalesOrders")
pwd
len(df)
df.columns
# populate the list of columns
cols = df.columns
# run the loop 43 times (0 to 42)
sales_orders = []
for i in range(len(df)):
    attributes = []
    temp_order_date = None
    temp_region = None
    for c in cols:
        attributes.append(df.iloc[i][c])
    # Expand the list of attributes that contain
    # all column values and call constructor of class SalesOrders
    sales_order_row = SalesOrders(*attributes)
    sales_orders.append(sales_order_row)
    
sales_orders[1].rep
sales_orders[0].region
'''
Code to find out the total sale
of the item pencil by the Sales
Representative Jones
'''
total_pencil_sale = 0
for row in range(len(sales_orders)):
    person = sales_orders[row].rep
    item   = sales_orders[row].item
    if person == "Jones" and  item ==  "Pencil":
        total_pencil_sale += sales_orders[row].total
        
print("total pencil sale by Jones", total_pencil_sale.round(2))
def total_sale_by_person_item(person, item):
    total_sale = 0
    for row in range(len(sales_orders)):
        row_person = sales_orders[row].rep
        row_item   = sales_orders[row].item
        if row_person == person and  row_item ==  item:
            total_sale += sales_orders[row].total

    print("Total " + item + " sale by " + person, total_sale.round(2))
total_sale_by_person_item("Jones", "Pencil")
total_sale_by_person_item("Morgan", "Pencil")
import matplotlib.pyplot as plt
?plt.plot()
nums    = [x for x in range(1,11)]
squares = [x**2 for x in range(1,11)]
plt.plot(nums, squares, label="square", marker = 'o', color='blue')
plt.grid()
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12408286577470867, 'fraction_numerical': 0.014026758739749677, 'mean_word_length': 3.6676737160120845, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '440838', 'license': 'Apache-2.0', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1593, 'n_words': 423, 'path': 'Lecture-Notes/2019/PSS-2019-Day9.ipynb', 'repo_name': 'unmeshvrije/python-for-beginners', 'size': '4634'}