Skip to content

Instantly share code, notes, and snippets.

@Robofied
Robofied / intermediate_python_logging2.py
Created February 10, 2019 08:18
Intermediate Python
## Function with arguments
@ company
def office_hours(start_time,finish_time):
return ('Hi! Your office hours will be ' + str((int(finish_time.split(':')[0]) - int(start_time.split(':')[0])))+' hours.')
print(office_hours('10:00','18:00'))
#[Output]:
#'Hi! Your office hours will be 8 hours.'
@Robofied
Robofied / intermediate_python_logging1.py
Last active February 10, 2019 08:24
Intermediate Python
## Now this nested function will be able to handle any no. of arguments - zero, one , so on arguments and also able to handle ## key value pair arguments as well.
def company(decorated_function):
## Importing logging module
import logging
## Creating a file to store the logging info
logging.basicConfig(filename= 'log_file.log',level=logging.INFO)
  ## *args => any no. of aruguments ## **kwargs => any no. of key value pair value or dictionary values
def wrapper_function(*args, **kwargs):
@Robofied
Robofied / intermediate_python_decorator4.py
Created February 10, 2019 08:12
Intermediate Python
## Now this nested function will be able to handle any no. of arguments - zero, one , so on arguments and also able to handle
## key value pair arguments as well.
def company(decorated_function):
## *args => any no. of aruguments
## **kwargs => any no. of key value pair value or dictionary values
def wrapper_function(*args, **kwargs):
print('Wrapper function is executed first')
return decorated_function(*args, **kwargs)
@Robofied
Robofied / intermediate_python_decorator3.py
Created February 10, 2019 08:09
Intermediate Python
@ company
def office_hours(start_time,finish_time):
return ('Hi! Your office hours will be ' + str((int(finish_time.split(':')[0]) - int(start_time.split(':')[0])))+
' hours.')
## Here it is giving the error because our wrapper function will not be able to handle any no.of arguments
print(office_hours('10:00','18:00'))
#[Output]:
#---------------------------------------------------------------------------
@Robofied
Robofied / intermediate_python_decorator2.py
Created February 10, 2019 08:07
Intermediate Python
@ company
def new_employee():
print('Hi! Welcome to Robofied team')
new_employee()
#[Output]:
#Wrapper function is executed first
#Hi! Welcome to Robofied team
@Robofied
Robofied / intermediate_python_decorator1.py
Created February 10, 2019 08:05
Intermediate Python
""" defining a nested function, in that inner function i.e, a wrapper function is calling the passed function in outer function
"""
def company(decorated_function):
def wrapper_function():
print('Wrapper function is executed first')
## calling the decorator function
return decorated_function()
return wrapper_function
def office_hours(start_time,finish_time):
return ('Hi! Your office hours will be ' + str((int(finish_time.split(':')[0]) - int(start_time.split(':')[0])))+
' hours.')
args = ('10:00','18:00')
print(office_hours(*args))
#[Output]:
#'Hi! Your office hours will be 8 hours.'
## function which is taking multiple or any no. of agruments
## **kwargs takes input in the form of dictionary only.
def multiple_args(**kwargs):
## printing key and value pair.
for key, value in kwargs.items():
print('Key is {0} and value is {1}'.format(key,value))
## creating a dictionary to be passed in the function
## Creating a function which can take multiple(any no. of ) arguments
## Here in this example it is not much necessary to give unknown number of parameters
## But in some cases we are not knowing then this *args is very useful
def office_hours(*args):
return ('Hi!'+ str(args[0]) +' Your office hours will be ' + str((int(args[2].split(':')[0]) - int(args[1].split(':')[0])))+
' hours.')
## calling the above function
office_hours('Rajesh','10:00','18:00')
@Robofied
Robofied / intermediate_python_closures2.py
Created February 10, 2019 07:54
Intermediate Python
def office_hours(start_time, finish_time):
s = start_time
f = finish_time
def cal_working_hours():
return ('Hi '+ '! Your office hours will be ' + str((int(f.split(':')[0]) - int(s.split(':')[0])))+
' hours.')
## returning the function but without ()
return cal_working_hours