diff --git a/OOP/dog.py b/OOP/dog.py index 49ba9f5..bcce89e 100644 --- a/OOP/dog.py +++ b/OOP/dog.py @@ -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) @@ -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")) diff --git a/OOP/dog_inheritance.py b/OOP/dog_inheritance.py deleted file mode 100644 index 41e56a2..0000000 --- a/OOP/dog_inheritance.py +++ /dev/null @@ -1,40 +0,0 @@ -# Parent class -class Dog: - - # Class attribute - species = 'mammal' - - # Initializer / Instance attributes - def __init__(self, name, age): - self.name = name - self.age = age - - # instance method - def description(self): - return "{} is {} years old".format(self.name, self.age) - - # instance method - def speak(self, sound): - return "{} says {}".format(self.name, sound) - - -# Child class (inherits from Dog class) -class RussellTerrier(Dog): - def run(self, speed): - return "{} runs {}".format(self.name, speed) - - -# Child class (inherits from Dog class) -class Bulldog(Dog): - def run(self, speed): - return "{} runs {}".format(self.name, speed) - - -# Child classes inherit attributes and -# behaviors from the parent class -jim = Bulldog("Jim", 12) -print(jim.description()) - -# Child classes have specific attributes -# and behaviors as well -print(jim.run("slowly")) \ No newline at end of file diff --git a/OOP/dog_sol.py b/OOP/dog_sol.py deleted file mode 100644 index cf43ca6..0000000 --- a/OOP/dog_sol.py +++ /dev/null @@ -1,27 +0,0 @@ -## Solution to Exercise "The Oldest Dog" - -class Dog: - - # Class Attribute - species = 'mammal' - - # Initializer / Instance Attributes - def __init__(self, name, age): - self.name = name - self.age = age - - -# Instantiate the Dog object -jake = Dog("Jake", 7) -doug = Dog("Doug", 4) -william = Dog("William", 5) - - -# Determine the oldest dog -def get_biggest_number(*args): - return max(args) - - -# Output -print("The oldest dog is {} years old.".format( - get_biggest_number(jake.age, doug.age, william.age))) \ No newline at end of file diff --git a/OOP/encapsulation.py b/OOP/encapsulation.py new file mode 100644 index 0000000..571f172 --- /dev/null +++ b/OOP/encapsulation.py @@ -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() \ No newline at end of file diff --git a/OOP/inheritance.py b/OOP/inheritance.py new file mode 100644 index 0000000..999d770 --- /dev/null +++ b/OOP/inheritance.py @@ -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() \ No newline at end of file diff --git a/OOP/polymorphism.py b/OOP/polymorphism.py new file mode 100644 index 0000000..677c233 --- /dev/null +++ b/OOP/polymorphism.py @@ -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) \ No newline at end of file diff --git a/exeption_logging/exeption_logging.py b/exeption_logging/exeption_logging.py index c7b9704..e5ddaef 100644 --- a/exeption_logging/exeption_logging.py +++ b/exeption_logging/exeption_logging.py @@ -30,7 +30,7 @@ ## 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!") @@ -38,6 +38,7 @@ # except Exception as e: # add and show as e, print(e) # print("Something went wrong!") # print(e) +# print(type(e)) ## Else block # try: @@ -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") \ No newline at end of file +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") \ No newline at end of file diff --git a/files_os_module/test2.txt b/files_os_module/test2.txt new file mode 100644 index 0000000..8318c86 --- /dev/null +++ b/files_os_module/test2.txt @@ -0,0 +1 @@ +Test \ No newline at end of file diff --git a/files_os_module/working_with_files.py b/files_os_module/working_with_files.py index 00dfc40..3d90e62 100644 --- a/files_os_module/working_with_files.py +++ b/files_os_module/working_with_files.py @@ -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) @@ -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: @@ -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) diff --git a/function/functions.py b/function/functions.py index 65fee51..1870d70 100644 --- a/function/functions.py +++ b/function/functions.py @@ -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) \ No newline at end of file +student_info(courses, info) +print ("---") +student_info(*courses, **info) \ No newline at end of file diff --git a/generators/generator.py b/generators/generator.py index e17749d..6b31be3 100644 --- a/generators/generator.py +++ b/generators/generator.py @@ -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)) @@ -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) \ No newline at end of file +# 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)) \ No newline at end of file diff --git a/if_else_elif/if_then_2.py b/if_else_elif/if_then_2.py index 16a5161..7d561fc 100644 --- a/if_else_elif/if_then_2.py +++ b/if_else_elif/if_then_2.py @@ -1,6 +1,9 @@ # Prompt user to enter number / test if even or odd + + + def main(): number = int(input("Please enter an integer: ")) diff --git a/iterators/iterators.py b/iterators/iterators.py new file mode 100644 index 0000000..b1d4ade --- /dev/null +++ b/iterators/iterators.py @@ -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)) diff --git a/modules/__pycache__/mymath.cpython-38.pyc b/modules/__pycache__/mymath.cpython-38.pyc deleted file mode 100644 index e70db86..0000000 Binary files a/modules/__pycache__/mymath.cpython-38.pyc and /dev/null differ diff --git a/scripts/words_count.py b/scripts/words_count.py index 72af679..2acbabb 100644 --- a/scripts/words_count.py +++ b/scripts/words_count.py @@ -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() diff --git a/working_with_json/json_demo.py b/working_with_json/json_demo.py index 1b2581f..8f28749 100644 --- a/working_with_json/json_demo.py +++ b/working_with_json/json_demo.py @@ -18,4 +18,7 @@ ## Simple deserialization example with open("demo.json","r") as read_file: data = json.load(read_file) - print(data) \ No newline at end of file + print(data) + print(type(data)) + +