Skip to content

Commit b974192

Browse files
author
lev epshtein
committed
oop, iterator, decorator
1 parent 313ac5c commit b974192

16 files changed

Lines changed: 174 additions & 105 deletions

File tree

OOP/dog.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def __init__(self, name, age):
1616
self.name = name
1717
self.age = age
1818

19+
def speak(self, sound):
20+
return f"{self.name} says {sound}"
21+
1922

2023
# Instantiate the Dog object
2124
philo = Dog("Philo", 5)
@@ -27,3 +30,6 @@ def __init__(self, name, age):
2730
# Is Philo a mammal?
2831
if philo.species == "mammal":
2932
print(f"{philo.name} is a {philo.species}!")
33+
34+
print(philo.speak("Woof Woof"))
35+
print(mikey.speak("Waf Waf"))

OOP/dog_inheritance.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

OOP/dog_sol.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

OOP/encapsulation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Using OOP in Python, we can restrict access to methods and variables.
2+
# This prevents data from direct modification which is called encapsulation.
3+
# In Python, we denote private attributes using underscore as the prefix i.e single _ or double __.
4+
5+
class Computer:
6+
7+
def __init__(self):
8+
self.__maxprice = 900
9+
10+
def sell(self):
11+
print("Selling Price: {}".format(self.__maxprice))
12+
13+
def setMaxPrice(self, price):
14+
self.__maxprice = price
15+
16+
c = Computer()
17+
c.sell()
18+
19+
# change the price
20+
c.__maxprice = 1000
21+
c.sell()
22+
23+
# using setter function
24+
c.setMaxPrice(1000)
25+
c.sell()

OOP/inheritance.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# parent class
2+
class Bird:
3+
4+
def __init__(self):
5+
print("Bird is ready")
6+
7+
def whoisThis(self):
8+
print("Bird")
9+
10+
def swim(self):
11+
print("Swim faster")
12+
13+
# child class
14+
class Penguin(Bird):
15+
16+
def __init__(self):
17+
# call super() function
18+
super().__init__()
19+
print("Penguin is ready")
20+
21+
def whoisThis(self):
22+
print("Penguin")
23+
24+
def run(self):
25+
print("Run faster")
26+
27+
peggy = Penguin()
28+
peggy.whoisThis()
29+
peggy.swim()
30+
peggy.run()

OOP/polymorphism.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Parrot:
2+
3+
def fly(self):
4+
print("Parrot can fly")
5+
6+
def swim(self):
7+
print("Parrot can't swim")
8+
9+
class Penguin:
10+
11+
def fly(self):
12+
print("Penguin can't fly")
13+
14+
def swim(self):
15+
print("Penguin can swim")
16+
17+
# common interface
18+
def flying_test(bird):
19+
bird.fly()
20+
21+
#instantiate objects
22+
blu = Parrot()
23+
peggy = Penguin()
24+
25+
# passing the object
26+
flying_test(blu)
27+
flying_test(peggy)

exeption_logging/exeption_logging.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030

3131
## Let's add more code , with differnt broken line
3232
# try:
33-
# f = open('test.txt','r') # file open fixed
33+
# # f = open('test.txt','r') # file open fixed
3434
# var = bad_var # bad varibale
3535
# except FileNotFoundError as e: # add and show as e, print(e)
3636
# print("Sorry this file isn't exist!")
3737
# print(e)
3838
# except Exception as e: # add and show as e, print(e)
3939
# print("Something went wrong!")
4040
# print(e)
41+
# print(type(e))
4142

4243
## Else block
4344
# try:
@@ -61,17 +62,17 @@
6162

6263
## Log module
6364

64-
# import logging
65+
import logging
6566

66-
# logging.basicConfig(filename='example.log',level=logging.DEBUG)
67+
logging.basicConfig(filename='example.log',level=logging.DEBUG)
6768

68-
# try:
69-
# f = open('test.txt','r')
70-
# except Exception as e:
71-
# logging.error(e)
72-
# else:
73-
# print(f.read())
74-
# logging.info("end file {} manipulation".format(f.name))
75-
# f.close()
76-
# finally:
77-
# logging.info("from finally block of exception")
69+
try:
70+
f = open('test_.txt','r')
71+
except Exception as e:
72+
logging.error(e)
73+
else:
74+
print(f.read())
75+
logging.info("end file {} manipulation".format(f.name))
76+
f.close()
77+
finally:
78+
logging.info("from finally block of exception")

files_os_module/test2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test

files_os_module/working_with_files.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
# print(f.read())
1515

1616
## read and print whole file :
17-
# with open('test.txt','r') as f:
17+
# with open('C:\\Users\\lev\\Documents\\OpsGuru\\projects\\Devops-course\\devops-python\\files_os_module\\test.txt','r') as f:
1818
# f_contents = f.read()
1919
# print(f_contents)
2020

2121
## read and print line by line file:
2222
# with open('test.txt','r') as f:
23-
# with open('test.txt','r') as f:
23+
# with open('test.txt','r') as fa:
2424
# # f_contents = f.readlines()
25-
# f_contents = f.readlines()
25+
# f_contents = fa.readlines()
2626
# print(f_contents)
2727

2828
## read and print single line, first one every time we call function readline will read next line (copy and show)
@@ -39,9 +39,9 @@
3939

4040
## More control for reading , explanation of read buffer
4141
# with open('test.txt','r') as f:
42-
# f_contents = f.read()
43-
# f_contents = f.read(100) # copy and explain
44-
# print(f_contents, end='')
42+
# # f_contents = f.read()
43+
# f_contents = f.read(100) # copy and explain
44+
# print(f_contents, end='')
4545

4646
## More contr eol for big files with while and EOF condition in while loop, also f.tell() and f.seek()
4747
# with open('test.txt','r') as f:
@@ -88,7 +88,7 @@
8888
# suppose you want to make a subfolder under your home folder: $HOMEPATH/python-project/project1/temp
8989
# folder_name = os.path.join(os.environ['HOMEPATH'],'python-project','project1','temp')
9090
# os.makedirs(folder_name)
91-
91+
# class
9292
## Walking throught directories and files :
9393
# for dirpath, dirname, filename in os.walk(os.environ['HOMEPATH']):
9494
# print('Current path',dirpath)

function/functions.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
#####
3434

3535
# Function with args and kwargs
36-
# def student_info(*args, **kwargs):
37-
# print(args)
38-
# print(kwargs)
39-
# #
40-
# student_info('Math', 'Art', name='Jhon', age=22)
41-
# print ("---")
42-
# courses = ['Math', 'Art']
43-
# info = {'name':'Jhon', 'age':22}
36+
def student_info(*args, **kwargs):
37+
print(args)
38+
print(kwargs)
39+
#
40+
student_info('Math', 'Art', name='Jhon', age=22)
41+
print ("---")
42+
courses = ['Math', 'Art']
43+
info = {'name':'Jhon', 'age':22}
4444

45-
# student_info(courses, info)
46-
# print ("---")
47-
# student_info(*courses, **info)
45+
student_info(courses, info)
46+
print ("---")
47+
student_info(*courses, **info)

0 commit comments

Comments
 (0)