I need help how to fix assignment error

Error Handling

Write all your code in python

Hints:

A callable is a generic type for a function. This means that anything that is of type callable, can be called or invoked, in other words, its a function. And remember to invoke a function we put parentheses “()” at the end of the variable name. For example, let var0 be a callable, to invoke it we say the following, var0().

To get the current time in a milliseconds we do the following:

import time

ms_time_stamp = int(round(time.time() * 1000))

def raiseIndexError()

This function raises an IndexError.

def raiseZeroDivisionErrorWithMessage(message:str)

This function raises a ZeroDivisionError with the specified message.

def raiseThisException(exception)

This function will receive an exception as an argument. It should raise the exception it receives.

def catchAndReturnMessage(message:str, main_function:callable) → str:

This function will receive a string message and a callable as an argument. The callable has a chance of raising an Exception. This function should invoke the callable, if the callable does not raise an exception it should return the message it received as an argument. If the callable raises an exception, it should handle the exception and return the message of the raised exception.

def catchCleanupAndThrow(main_supplier:callable, index_supplier:callable, zero_supplier:callable, cleanup:callable) → str:

This function will receive four callables as arguments. The function should invoke the main_supplier, main_supplier will return a string, if main_supplier does not raise an exception this function will return the value returned by main supplier. main_supplier has the possibility of raising an IndexError and a ZeroDivisionError. If main_supplier raises an IndexError this function will call the index_supplier function and return the value returned by index_supplier. If main_supplier raises a ZeroDivisionError this function will call the zero_supplier function and return the value returned by zero_supplier. Before the function returns regardless if an exception is thrown or not this function should invoke the cleanup function.

class Timer()

Create a context manager that times the execution duration of a function. You need to implement 4 methods in this class.

def init(self, time_out:int)

This is the constructor of the class the time_out time is the total amount of time that the context manager will allow the code in the context to execute before raising a TimeoutError upon leaving the context. The variable time out will be a whole number in milliseconds.

def get_total_time(self) → int:

This function is used to figure out how long it took for the code to execute within the context manager. This function returns the total time in milliseconds it took for the code to execute within the context manager. If this function is invoked before the code has left the context manager it should return -1.

def enter(self) → Timer:

This function should start the timer used to time the execution of the code in the context block. Then it should return the current instance of the context manager, in other words, it should return its self.

def exit(self, exc_type, exc_value, traceback):

This function should calculate the total time it took for the code to enter and exit the context block in milliseconds. If the total time is greater than the time_out time received at the time of the construction of this class then this function should raise a TimeoutError.

here is my work code and error
import time

def raiseIndexError():
raise IndexError

def raiseZeroDivisionErrorWithMessage(message:str):
raise ZeroDivisionError(message)

def raiseThisException(exception):
raise exception

def catchAndReturnMessage(message:str, main_function:callable) → str:
try:
main_function()
except Exception as e:
return str(e)
else:
return message

def catchCleanupAndThrow(main_supplier:callable, index_supplier:callable, zero_supplier:callable, cleanup:callable) → str:

try:
    result = main_supplier()
except IndexError:
    result = index_supplier()
except ZeroDivisionError:
    result = zero_supplier()
finally:
    cleanup()

return result

class Timer:
def init(self, time_out:int):
self.time_out = time_out
self.start_time = None
self.end_time = None
def get_total_time(self):
if self.end_time == None:
return -1
return self.end_time - self.start_time
def enter(self):
self.start_time = int(round(time.time() * 1000))
return self
def exit(self, exc_type, exc_value, traceback):
self.end_time = time.time()
if self.get_total_time() > self.time_out:
raise TimeoutError

Hi There,

This code looks very challenging! I’ll do my best to help you out. One thing I noticed is the (‘__ init __’) constructor for the Timer class is incorrect; it should be:


class Timer:
    def __init__(self, time_out: int):
        self.time_out = time_out
        self.start_time = None
        self.end_time = None

Another issue is with the enter() function:

def enter(self):
    self.start_time = int(round(time.time() * 1000))
    return self

This will return the object id of self,which would look like this:

<__main__.Timer object at 0x000002741AA5CA90>

Since you are using this method to set an self.start_time you can make this a property method like so:

@property
def start_time (self):
    return int(round(time.time() * 1000))  

This will make start_time and attribute of the Timer class, similar to defining it like : self.start_timer. Another issue I noticed is self.end_time is initially set to None; however, in the exit() function self.end_time is set to just time.time() ~ which means your start_time will always be greater than your end_time :

self.start_time = int(round(time.time() * 1000))
self.end_time = time.time()

Is this your desired intent? Try to implement the few changes I suggested, and let me know what the results were. I do not believe it will resolve all of your issue, so if you need further assistance feel free to ask.

Best regards,

um where do I put <main.Timer object at 0x000002741AA5CA90> and can you show me how.

and where do I put self.start_time = int(round(time.time() * 1000))
self.end_time = time.time() and can you show me how too.

is it like this?

I apologize for the confusion, I was explaining what your function would return. Your code was not formatted, so I believe this is what your code looked liked, correct?

    def enter(self):
        self.start_time = int(round(time.time() * 1000))
            return self

If so, this would return something like this.

<__main__.Timer object at 0x000002741AA5CA90>

I do not believe this is what you want, so I suggested that you change enter to a property method since it is setting the value for self.start_time. So you can do this:

class Timer:
    def __init__(self, time_out:int):
        self.time_out = time_out
        self.end_time = None

    @property
    def start_time(self):
        return int(round(time.time() * 1000))

or do this:

class Timer:
    def __init__(self, time_out:int):
        self.time_out = time_out
        self.start_time = int(round(time.time() * 1000))
        self.end_time = None

This accomplishes the same thing. Next, you have to fix that your start_time will always be greater than your end_time. Because according to your code self.start_time = int(round(time.time() * 1000)) and self.end_time = time.time(). This doesn’t seem to make sense to me. However, I could be wrong in my assessment only because I do not have access to the source material.

This may not work for your case; however,consider the following code:

class Timer:

    def __init__(self, time_out: int):
        self.start_time = time.time()
        self.time_out = time_out
        self.end_time = None

    def get_total_time(self):
        return time.time() - self.start_time

    def exit(self):
        if self.get_total_time() > self.time_out:
            raise TimeoutError

Now, lets create a Timer instance and give it a time_out of 5 seconds, and then let the program sleep for 7 seconds, and then call get_total_time.


# create a Timer instance with a time_out time of 5 seconds
t = Timer(5)

# Let the program sleep for 7 seconds
time.sleep(7)

# Now get total_time
print(t.get_total_time())

# evaluate if a time error occurs
print(t.exit())

This will raise a TimeoutError because self.get_total_time is greater than self.time (screenshot below).

I believe this is how you should approach your problem. Hopefully this will help you understand how to resolve your problem. If you have any further questions, don’t hesitate to ask.

like this?

In my previous response I mentioned that code wouldn’t work for your case. So remove the following:

t = Timer(5)
time.sleep(7)
print(t.get_total_time())

print(t.exit())

Again, I was just showing you an example of the direction you should go.

it does not work it gives me an error like this
File “/var/gt_test_envs/tLfsH8QS56s8dSP0/homework.py”, line 48
return self<main.Timer object at 0x000002741AA5CA90>
^
SyntaxError: invalid syntax
line 48

um my code looks like this