Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions OOP/dog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def __init__(self, name, age):
self.name = name
self.age = age

def speak(self, sound):
return f"{self.name} says {sound}"


# Instantiate the Dog object
philo = Dog("Philo", 5)
Expand All @@ -27,3 +30,6 @@ def __init__(self, name, age):
# Is Philo a mammal?
if philo.species == "mammal":
print(f"{philo.name} is a {philo.species}!")

print(philo.speak("Woof Woof"))
print(mikey.speak("Waf Waf"))
40 changes: 0 additions & 40 deletions OOP/dog_inheritance.py

This file was deleted.

27 changes: 0 additions & 27 deletions OOP/dog_sol.py

This file was deleted.

25 changes: 25 additions & 0 deletions OOP/encapsulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Using OOP in Python, we can restrict access to methods and variables.
# This prevents data from direct modification which is called encapsulation.
# In Python, we denote private attributes using underscore as the prefix i.e single _ or double __.

class Computer:

def __init__(self):
self.__maxprice = 900

def sell(self):
print("Selling Price: {}".format(self.__maxprice))

def setMaxPrice(self, price):
self.__maxprice = price

c = Computer()
c.sell()

# change the price
c.__maxprice = 1000
c.sell()

# using setter function
c.setMaxPrice(1000)
c.sell()
30 changes: 30 additions & 0 deletions OOP/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# parent class
class Bird:

def __init__(self):
print("Bird is ready")

def whoisThis(self):
print("Bird")

def swim(self):
print("Swim faster")

# child class
class Penguin(Bird):

def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")

def whoisThis(self):
print("Penguin")

def run(self):
print("Run faster")

peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()
27 changes: 27 additions & 0 deletions OOP/polymorphism.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Parrot:

def fly(self):
print("Parrot can fly")

def swim(self):
print("Parrot can't swim")

class Penguin:

def fly(self):
print("Penguin can't fly")

def swim(self):
print("Penguin can swim")

# common interface
def flying_test(bird):
bird.fly()

#instantiate objects
blu = Parrot()
peggy = Penguin()

# passing the object
flying_test(blu)
flying_test(peggy)
27 changes: 14 additions & 13 deletions exeption_logging/exeption_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@

## Let's add more code , with differnt broken line
# try:
# f = open('test.txt','r') # file open fixed
# # f = open('test.txt','r') # file open fixed
# var = bad_var # bad varibale
# except FileNotFoundError as e: # add and show as e, print(e)
# print("Sorry this file isn't exist!")
# print(e)
# except Exception as e: # add and show as e, print(e)
# print("Something went wrong!")
# print(e)
# print(type(e))

## Else block
# try:
Expand All @@ -61,17 +62,17 @@

## Log module

# import logging
import logging

# logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.basicConfig(filename='example.log',level=logging.DEBUG)

# try:
# f = open('test.txt','r')
# except Exception as e:
# logging.error(e)
# else:
# print(f.read())
# logging.info("end file {} manipulation".format(f.name))
# f.close()
# finally:
# logging.info("from finally block of exception")
try:
f = open('test_.txt','r')
except Exception as e:
logging.error(e)
else:
print(f.read())
logging.info("end file {} manipulation".format(f.name))
f.close()
finally:
logging.info("from finally block of exception")
1 change: 1 addition & 0 deletions files_os_module/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
14 changes: 7 additions & 7 deletions files_os_module/working_with_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
# print(f.read())

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

## read and print line by line file:
# with open('test.txt','r') as f:
# with open('test.txt','r') as f:
# with open('test.txt','r') as fa:
# # f_contents = f.readlines()
# f_contents = f.readlines()
# f_contents = fa.readlines()
# print(f_contents)

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

## More control for reading , explanation of read buffer
# with open('test.txt','r') as f:
# f_contents = f.read()
# f_contents = f.read(100) # copy and explain
# print(f_contents, end='')
# # f_contents = f.read()
# f_contents = f.read(100) # copy and explain
# print(f_contents, end='')

## More contr eol for big files with while and EOF condition in while loop, also f.tell() and f.seek()
# with open('test.txt','r') as f:
Expand Down Expand Up @@ -88,7 +88,7 @@
# suppose you want to make a subfolder under your home folder: $HOMEPATH/python-project/project1/temp
# folder_name = os.path.join(os.environ['HOMEPATH'],'python-project','project1','temp')
# os.makedirs(folder_name)

# class
## Walking throught directories and files :
# for dirpath, dirname, filename in os.walk(os.environ['HOMEPATH']):
# print('Current path',dirpath)
Expand Down
22 changes: 11 additions & 11 deletions function/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
#####

# Function with args and kwargs
# def student_info(*args, **kwargs):
# print(args)
# print(kwargs)
# #
# student_info('Math', 'Art', name='Jhon', age=22)
# print ("---")
# courses = ['Math', 'Art']
# info = {'name':'Jhon', 'age':22}
def student_info(*args, **kwargs):
print(args)
print(kwargs)
#
student_info('Math', 'Art', name='Jhon', age=22)
print ("---")
courses = ['Math', 'Art']
info = {'name':'Jhon', 'age':22}

# student_info(courses, info)
# print ("---")
# student_info(*courses, **info)
student_info(courses, info)
print ("---")
student_info(*courses, **info)
20 changes: 16 additions & 4 deletions generators/generator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# def square_numbers(nums):
# def square_numbers(*nums):
# for i in nums:
# yield (i*i)


# my_nums = square_numbers([1, 2, 3, 4, 5])
# my_nums = square_numbers(*[1, 2, 3, 4, 5])


# print(next(my_nums))
Expand All @@ -12,11 +12,23 @@
# print(next(my_nums))
# print(next(my_nums))

##

# my_nums = square_numbers(*[1, 2, 3, 4, 5])
# for num in my_nums:
# print(num)

# list comprehention advance
## Generator expressions

# my_nums = list(x*x for x in [1,2,3,4,5])
# my_sum = sum([x*x for x in range(1,10)])

# print (my_nums)
# print (my_sum)

# print (my_nums)
# jobtext = '/administrator/ HTTP/1.1'
# all_lines = (line for line in open('..\\scripts\\access.log', 'r') )
# job = ( line for line in all_lines if line.find(jobtext) != -1)
# print(next(job))
# print(next(job))
# print(next(job))
3 changes: 3 additions & 0 deletions if_else_elif/if_then_2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Prompt user to enter number / test if even or odd





def main():
number = int(input("Please enter an integer: "))

Expand Down
28 changes: 28 additions & 0 deletions iterators/iterators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Counter():
def __init__(self, low, high):
self.current = low
self.high = high

def __iter__(self):
'Returns itself as an iterator object'
return self

def __next__(self):
'Returns the next value till current is lower than high'
if self.current > self.high:
raise StopIteration
else:
self.current += 1
return self.current - 1
##
c = Counter(5,10)

for i in c:
print(i, end=' ')
print('\n')
##
c = Counter(5,6)

print(next(c))
print(next(c))
# print(next(c))
Binary file removed modules/__pycache__/mymath.cpython-38.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion scripts/words_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def print_dictionary(dic, file_name):
print('{:>20} {:>20}'.format(key, dic[key]))

def main():
# file_name = input("Please insert file name to work with: ")
file_name = input("Please insert file name to work with: ")
alpha_statistic = {}
with open(file_name,'r') as f:
lines = f.readlines()
Expand Down
5 changes: 4 additions & 1 deletion working_with_json/json_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@
## Simple deserialization example
with open("demo.json","r") as read_file:
data = json.load(read_file)
print(data)
print(data)
print(type(data))