Posts

Showing posts from October, 2020

Mini Project

  #Question # Create a library class # display book # lend book - (who owns the book if not present) # add book # return book # HarryLibrary = Library(listofbooks, library_name) #dictionary (books-nameofperson) # create a main function and run an infinite while loop asking # users for their input # Plan # Make a class named libary. It should have 4 functions # display book # lend book - (who owns the book if not present) # add book # return book # Add book and make a list display books if asked and check the avibility of the book asked # if anyone wants to add a book so take his/her name and name of book and if anyone lends a # book so ask name of person, name of book and save the time at which book was witdrawed and # make a .txt file to save that details. I anyone returns the book take name of person, book # and save time for the same !! # Program: import time class Libary (): def displaybook ( self ): with open ( "books.txt" ) as op: for i in op: ...

Object Introspection

  # Types to inspect an object are : # type(object) # id(object) # Next 2 line are on function only !! # o = MyClass() # print(dir(o)) # Main Code: class Employee : def __init__ ( self , fname, lname): self .fname = fname self .lname = lname # self.email = f"{fname}.{lname}@codewithharry.com" def explain ( self ): return f"This employee is { self .fname } { self .lname } " @property def email ( self ): if self .fname== None or self .lname == None : return "Email is not set. Please set it using setter" return f" { self .fname } . { self .lname } @codewithharry.com" @email.setter def email ( self , string): print ( "Setting now..." ) names = string.split( "@" )[ 0 ] self .fname = names.split( "." )[ 0 ] self .lname = names.split( "." )[ 1 ] @email.deleter def email ( self ): self...

Setter And Property Decorators

  @property  #def getter method Use @property along with getter method to access the value of the attribute @function_name.setter #def function   @function_name.setter is a setter method with which we can set the value of the attribute  # Deleter method  @function_name.deleter @function_name.deleter  is a deleter method which can delete the assigned value by the setter method #Main Code class Employee : def __init__ ( self , fname, lname): self .fname = fname self .lname = lname # self.email = f"{fname}.{lname}@codewithharry.com" def explain ( self ): return f"This employee is { self .fname } { self .lname } " @property def email ( self ): if self .fname== None or self .lname == None : return "Email is not set. Please set it using setter" return f" { self .fname } . { self .lname } @codewithharry.com" @email.setter def email ( self , string): print ( ...

Abstract Base Class (ABC) And @abstractmthod

  # Main Syntax for importing: # from abc import ABC, abstractmethod # Full Code : # from abc import ABCMeta, abstractmethod from abc import ABC, abstractmethod class Shape (ABC): @abstractmethod def printarea ( self ): return 0 class Rectangle (Shape): type = "Rectangle" sides = 4 def __init__ ( self ): self .length = 6 self .breadth = 7 def printarea ( self ): return self .length * self .breadth rect1 = Rectangle() print (rect1.printarea()) # Output: # 42

Quiz Dunder Methods And Overriding

 # Main Code class Employee:     no_of_leaves = 89     def __init__(self, aname, asalary, arole):         self.name = aname         self.salary = asalary         self.role = arole     def printdetails(self):         return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"     @classmethod     def change_leaves(cls, newleaves):         cls.no_of_leaves = newleaves     def __pow__(self, other ):         return self.salary ** other.salary     def __floordiv__(self, other):         return self.salary//other.salary     def __mul__(self, other):         return self.salary * other.salary emp1 =Employee("Harry", 345, "Programmer") emp2 =Employee("Rohan", 55, "Cleaner") #1 print(emp1 ** emp2) #2 print(emp1//emp2) #3 print(emp1*em...

Operator Overloading And Dunder Methods

Image
  # Main Code class Employee : no_of_leaves = 89 def __init__ ( self , aname, asalary, arole): self .name = aname self .salary = asalary self .role = arole def printdetails ( self ): return f"The Name is { self .name } . Salary is { self .salary } and role is { self .role } " @classmethod def change_leaves ( cls , newleaves): cls .no_of_leaves = newleaves @classmethod def from_str ( cls , string): return cls (*string.split( "-" )) # harry = Employee("Harry", 255, "Instructor") # rohan = Employee("Rohan", 85, "Cleaner") def __add__ ( self , other): return self .salary + other.salary def __truediv__ ( self , other): return self .salary / other.salary def __repr__ ( self ): return f"Employee(' { self .name } ', { self .salary } , ' { self .role } ')" # def __str__(se Name is {self.name}. S...

Diamond Shape Problem

Image
  # Diamond Shape Problem # Main Code : class A : def met ( self ): print ( "This is a method from class A" ) class B (A): def met ( self ): print ( "This is a method from class B" ) class C (A): def met ( self ): print ( "This is a method from class C" ) class D (C, B): def met ( self ): print ( "This is a method from class D" ) a = A() b = B() c = C() d = D() d.met()

Super() And Overrriding

  #Super # Main Code: class Parent_Class ( object ): def __init__ ( self ): pass class Child_Class (Parent_Class): def __init__ ( self ): super (). __init__ () # Harry's Code class A : classvar1 = "I am a class variable in class A" def __init__ ( self ): self .var1 = "I am inside class A's constructor" self .classvar1 = "Instance var in class A" self .special = "Special" class B (A): classvar1 = "I am in class B" def __init__ ( self ): self .var1 = "I am inside class B's constructor" self .classvar1 = "Instance var in class B" super (). __init__ () print ( super ().classvar1) a = A() b = B() print (b.special, b.var1, b.classvar1)

Public, Private, And Protected Access Specifiers

  # no underscore(_) means it is public - anyone can access it # # with one underscore(_) means it is protected - only the one in the class and the ones who are accessing # the class can access it # # with 2 underscore(_) means it is private - means can be used only by the class using a special code # if we try to access it without that code python will error as no attribute found and the code is in the code. # code to access private variable : # print(emp._Employee__pr) # Code for public is class employee : def __init__ ( self , name, age): self .name = name self .age = age # # Code for protected is class employee : def __init__ ( self , name, age): self ._name = name # protected attribute self ._age = age # protected attribute #Code For private is class employee : def __init__ ( self , name, age): self .__name = name # private attribute self .__age = age # private attribute # Name ...

Multilevel Inheritance And Exercise

Exercise Given In Video, Code of Exercise : # electronic device # pocket gadget # phone class Electronic_device : device1 = "camera" device2 = "Battery" class Pocket_gadget (Electronic_device): device1 = "pocket camera" device2 = "Powerbank" def cameratype ( self ): f"I have a { self .device2 } type of battery which can recharge a discharged device (mobile,etc)." class Phone (Pocket_gadget): device1 = "Phone Camera" device2 = "Ion Battery" def cameratype ( self ): return f"I have a { self .device2 } type of battery which can keep running a mobile device." ecd = Electronic_device() pog = Pocket_gadget() pho = Phone() print (pho.device2) # Output: # Ion Battery   Code For Topic  : class Dad:     basketball =6 class Son(Dad):     dance =1     basketball = 9     def isdance(self):         return f"Yes I dance {self.dance} no of times" class Grandson(S...

Multiple Inheritance

Main Syntax: class CoolProgramer (Player, Employee): Full Code: class Employee : no_of_leaves = 8 var = 8 def __init__ ( self , aname, asalary, arole): self .name = aname self .salary = asalary self .role = arole def printdetails ( self ): return f"The Name is { self .name } . Salary is { self .salary } and role is { self .role } " @classmethod def change_leaves ( cls , newleaves): cls .no_of_leaves = newleaves @classmethod def from_dash ( cls , string): return cls (*string.split( "-" )) @staticmethod def printgood (string): print ( "This is good " + string) class Player : var = 9 no_of_games = 4 def __init__ ( self , name, game): self .name = name self .game =game def printdetails ( self ): return f"The Name is { self .name } . Game is { self .game } " class CoolProgramer (Player, Employee): language = ...

Single Inheritance

 Main Syntax : class Programmer (Employee): def __init__ ( self , aname, asalary, arole, alanguages): self .name = aname self .salary = asalary self .role = arole self .languages = alanguages def printprog ( self ): print ( f"Name of programmer is { self .name } . Salary is { self .salary } . Role is { self .role } . Languages known are { self .languages } ." ) harry = Employee( "Harry" , 255 , "Instructor" ) rohan = Employee( "Rohan" , 455 , "Student" ) shubham = Programmer( "Shubham" , 555 , "Programmer" , [ 'Python, C++' ]) karan = Programmer( "Karan" , 777 , "Programmer" , "Python, Java" ) print (karan.printprog()) Full Code: class Employee : no_of_leaves = 89 def __init__ ( self , aname, asalary, arole): self .name = aname self .salary = asalary self .role = arole def printdetails ( self ): ...

Static Methods

Syntax: @staticmethod def print_good (string): print ( "This is good " + string) print (karan.print_good( "Bala" )) Ouput: This is good Bala None Code:  class Employee : no_of_leaves = 89 def __init__ ( self , aname, asalary, arole): self .name = aname self .salary = asalary self .role = arole def printdetails ( self ): return f"The Name is { self .name } . Salary is { self .salary } and role is { self .role } " @classmethod def change_leaves ( cls , newleaves): cls .no_of_leaves = newleaves @classmethod def from_str ( cls , string): return cls (*string.split( "-" )) @staticmethod def print_good (string): print ( "This is good " + string) harry = Employee( "Harry" , 255 , "Instructor" ) rohan = Employee( "Rohan" , 455 , "Student" ) karan = Employee.from_str( "Karan-480-Student" ) ...

Class Method As Alternative Constructors

class Employee : no_of_leaves = 89 def __init__ ( self , aname, asalary, arole): self .name = aname self .salary = asalary self .role = arole def printdetails ( self ): return f"The Name is { self .name } . Salary is { self .salary } and role is { self .role } " @classmethod def change_leaves ( cls , newleaves): cls .no_of_leaves = newleaves @classmethod def from_str ( cls , string): return cls (*string.split( "-" )) harry = Employee( "Harry" , 255 , "Instructor" ) rohan = Employee( "Rohan" , 455 , "Student" ) karan = Employee.from_str( "Karan-480-Student" ) print (karan.printdetails())

Class Methods

  class Employee : no_of_leaves = 89 def __init__ ( self , aname, asalary, arole): self .name = aname self .salary = asalary self .role = arole def printdetails ( self ): return f"The Name is { self .name } . Salary is { self .salary } and role is { self .role } " @classmethod def change_leaves ( cls , newleaves): cls .no_of_leaves = newleaves harry = Employee( "Harry" , 255 , "Instructor" ) rohan = Employee( "Rohan" , 4544 , "Student" ) harry.change_leaves( 34 ) print (harry.no_of_leaves)

__int__(self)

  class Employee : no_of_leaves = 8 def __init__ ( self , aname, asalary, arole): self .name = aname self .salary = asalary self .role = arole def printdetails ( self ): return f"The Name is { self .name } . Salary is { self .salary } and role is { self .role } " harry = Employee( "Harry" , 255 , "Instructor" ) # rohan = Employee() # harry.name = "Harry" # harry.salary = 455 # harry.role = "Instructor" # # rohan.name = "Rohan" # rohan.salary = 4554 # rohan.role = "Student" print (harry.salary)

Self For Class

  class Employee (): no_of_leaves= 8 def printdetails ( self ): return f"Name is { self .name } . Salary is { self .salary } . Role is { self .role } . " harry = Employee() rohan = Employee() harry.name = "Harry" harry.salary = 455 harry.role = "Instructor" rohan.name = "Rohan" rohan.salary = 4554 rohan.role = "Student" print (rohan.printdetails())

Class, Instance, Object, Class Variables

  class Student (): pass harry= Student() larry= Student() harry.name = "Harry" harry.std = 9 harry.sec = 1 print (harry.std) # By Harry class Employee : no_of_leaves = 8 pass harry = Employee() rohan = Employee() harry.name = "Harry" harry.salary = 455 harry.role = "Instructor" rohan.name = "Rohan" rohan.salary = 4554 rohan.role = "Student" print (Employee.no_of_leaves) print (Employee. __dict__ ) rohan.no_of_leaves = 9 print (rohan. __dict__ ) print (Employee.no_of_leaves)

Decorators In Python

  # @dec1 # def function1(): # print("Subscribe Now !!") # # # # func2 = function1 # # del function1 # # func2() # By Harry # def function1(): # print("Subscribe now") # # func2 = function1 # del function1 # func2() # def funcret(num): # if num==0: # return print # if num==1: # return sum # # print and sum are 2 functions in python # a = funcret(1) # print(a) # def executor(func): # func("this") # # # executor(print) # By Me def dec1 (func1): def nowexec (): print ( "Now Excecuting" ) func1() print ( "Executed" ) return nowexec() @dec1 def who_is_harry (): print ( "Harry is a good boy" ) # who_is_harry = dec1(who_is_harry) who_is_harry

Exercise 7

  from pygame import mixer import threading import datetime import time def get_time (): return datetime.datetime.now() def drink_water_log (text): """ This the function for record and append the log :param text: as input Drank :return: appended text file """ with open ( "logs/water-drank-log.txt" , "a" ) as f: a = f.write( f" { get_time() } - { text }\n " ) return a def eye_exercise_log (text): """ This the function for record and append the log :param text: as input Eydone :return: appended text file """ with open ( "logs/eye-exercise-log.txt" , "a" ) as f: a = f.write( f" { get_time() } - { text }\n " ) return a def physical_exercise_log (text): """ This the function for record and append the log :param text: as input Eydone :return: appended text fi...