Starting from:

$35

Homework 2- log decorator

COP 4521
Homework - 2
Total Points: 10

1 Objective
The objective for this assignment is to make sure
• You are familiar with native Python and Python syntax.
• You can write small programs that demonstrate Python’s capabilities.
• You can use Python to solve small problems.
• You are comfortable with Python’s Object Oriented Programming concepts.
• You can do some research on a particular problem and learn about its general background and then
use Python to solve the problem. You will be expected to do this at any Software Engineering job,
and it is good to practice while you’re still in school.
2 log decorator.py (5 points)
Your goal is to create a decorator that extends a function with some logging statements. The logging
decorator must be called log and must be defined in the module log decorator.py. The decorator may
optionally take a single argument that indicates the file to which the statements should be appended. If no
argument is provided, the statements are written to stdout. If the filename provided cannot be opened for
whatever reason, logging should be directed to stdout. The logging decorator should extend the function
by printing the following items:
• The name of the function being called. (Hint: Make use of the name attribute available on every
function object).
• A list of the arguments supplied to the function as well as the type of the argument. If no arguments
are supplied, you must report this. You might want to make use of name here as well!
• The output of the function.
• The execution time of the function (elapsed wall clock time) in seconds. The time should be rounded
to 5 decimal places.
• The return value and the type of the return value. Note that even when multiple values are returned,
the return value is a single tuple object. If there is no return value, you must report this.
To demonstrate the behavior of the log decorator, take a look at the following example log.py, which
defines (but does not show!) the log decorator:
@log ( )
d e f f a c t o r i a l (∗ n um li s t ) :
r e s u l t s = [ ]
f o r number i n n um li s t :
r e s = number
1
f o r i i n r an ge ( number −1 ,0 , −1):
r e s = i ∗ r e s
r e s u l t s . append ( r e s )
r e t u r n r e s u l t s
@log ( ” l o g g e r . t x t ” )
d e f w a s t e tim e ( a , b , c ) :
p r i n t ( ” Wasting time . ” )
time . s l e e p ( 5 )
r e t u r n a , b , c
@log ( ” l o g g e r . t x t ” )
d e f gcd ( a , b ) :
p r i n t ( ” The GCD o f ” , a , ”and ” , b , ” i s ” , end=””)
w hil e a!=b :
i f a b :
a −= b
e l s e :
b −= a
p r i n t ( abs ( a ) )
r e t u r n abs ( a )
@log ( )
d e f p r i n t h e l l o ( ) :
p r i n t ( ” H ell o ! ” )
@log ( 1 0 )
d e f p ri n t g o o d b y e ( ) :
p r i n t ( ” Goodbye ! ” )
i f n am e == ” m ai n ” :
f a c t o r i a l ( 4 , 5 )
w a s t e tim e ( ” one ” , 2 , ” 3 ” )
gcd ( 1 5 , 9 )
p r i n t h e l l o ( )
p ri n t g o o d b y e ( )
The results of executing this module are given below:
$ python l o g . py
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
C alli n g f u n c ti o n f a c t o r i a l .
Arguments :
− 4 o f type i n t .
− 5 o f type i n t .
Output :
Execu ti on time : 0. 0 0 0 0 2 s .
Return v al u e : [ 2 4 , 1 2 0] o f type l i s t .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
C alli n g f u n c ti o n p r i n t h e l l o .
No arguments .
Output :
H ell o !
Execu ti on time : 0. 0 0 0 0 2 s .
No r e t u r n v al u e .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
C alli n g f u n c ti o n p ri n t g o o d b y e .
No arguments .
Output :
Goodbye !
Execu ti on time : 0. 0 0 0 3 7 s .
No r e t u r n v al u e .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
$ more l o g g e r . t x t
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
C alli n g f u n c ti o n w a s t e tim e .
Arguments :
− one o f type s t r .
− 2 o f type i n t .
− 3 o f type s t r .
Output :
Wasting time .
Execu ti on time : 5. 0 0 5 3 9 s .
Return v al u e : ( ’ one ’ , 2 , ’ 3 ’ ) o f type t u pl e .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
C alli n g f u n c ti o n gcd .
Arguments :
− 15 o f type i n t .
− 9 o f type i n t .
Output :
The GCD o f 15 and 9 i s 3
Execu ti on time : 0. 0 0 0 1 0 s .
Return v al u e : 3 o f type i n t .
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
3 oop.py (5 points)
Define a student class. A student has the following attributes: firstname, lastname, gender which can be
male or female, status which can be equal to freshman, sophomore, junior, and senior and gpa.
Then define the following methods for the student class.
• The show myself method will simply print all the attribute variables when called upon the object.
This method has no input arguments.
• The study time method will increment the gpa of the student according to the following formula: ‘
gpa = gpa + log(study time)’. The only input argument of this method is the variable study time.
In addition make sure that the gpa variable never exceeds 4.0 even if the student studies for a very
long time.
Create 5 student objects and store the objects in a list called student list. The five students are: Mike
Barnes, Jim Nickerson, Jack Indabox, Jane Miller and Mary Scott. Mike is a freshman, Jim a sophomore,
Jack a junior, Jane and Mary are seniors. Their respective GPAs are: 4, 3, 2.5, 3.6 and 2.7. Make sure
you assign the gender when you instantiate the objects.
Then call the show myself method on all of them. I suggest you use a loop for making the objects and a
separate loop for showing the objects.
3
4 Submission
• For this homework, Make sure your python code can be run on Python version ¿= 3.8.
• Please name your file log decorator.py and oop.py
• Please add your name and FSUID as a comment at the top of your programs.
• Please turn in the program through Canvas.
• We are aware that solutions to these problems might exist on the internet. Please turn in your
solution to the problem, and not someone else’s. We will be running your submission through
plagiarism detection software.
• You are also responsible for making sure your submission on Canvas contains the right file and that
the file has not been corrupted in any way.
4

More products