Sr. Python Developer Test
Sr. Python Developer Test
Q1.Write a decorator for logging the function. The decorator also consumes an optional
argument which denotes the file path to write logs. If it is not provided, print it to
console.
It should log below details related to the function it was attached:
a. function name
b. function arguments
c. output of the function
d. time took to execute the function
E.g.
@mydecorator('/tmp/output.log')
def subtract(a, b):
return a - b
result = subtract(10 , 2)
In the '/tmp/output.log', you should see below details:
Method - subtract
arguments - [10, 2]
kwargs - None
Output - 8
Time Taken - 0.2086548805236816 seconds
@mydecorator()
def add(a, b=1):
return a + b
result = add(a=10)
In the output console, you should see below details:
Method - add
arguments - None
kwargs - {'a': 10}
Output - 11
Time Taken - 0.2086548805236816 seconds
Q2.Write unit tests (no integration tests) for singleton design pattern implementation in
python.
allData = {}
def doAction(data):
if(data['action'] == 'create'):
temp = {}
temp[data] = data['data']
while(1):
randInt = random.randint()
if randInt not in allData:
break
temp['id'] = randInt
allData[randInt] = temp
return temp
if(data['action'] == 'replace'):
key = data['id']
if(key not in allData):
print("Key does not exist for {}".format(key))
else:
allData[key] = data
if(action['action'] == 'update'):
key = data['id']
if(key not in allData):
print("Key does not exist for {}".format(key))
else:
f