0

im new to learning python just so you guys know. Im trying to make a function that uses regex to filter out the timestamp from a message log. Im trying to get my test cases to work but im getting an assertion error.

def get_hr_min(row):
    time_match = re.search(r'(\d{2}):(\d{2})', row)
    hour = time_match.group(1)
    minute = time_match.group(2)
    dhour = {}
    dhour['hour'] = hour
    dmin = {}
    dmin['minute'] = minute
    return dhour, dmin

here is what I have so far. as far as I can tell the function should return the minutes and hours in dictionary format(but I may have formatted it wrong as my professor did not tell me what a dictionary actually is)

log_open_row = '--- Log opened Tue Sep 20 00:01:49 2016'
join_quit_row = '00:01 -!- Guest40341 [[email protected]] has quit [Quit: Bye]'
message_row = '00:25 < ice231> anyone good with exploiting cisco asa with extrabacon?'
        
#assert get_hr_min(log_open_row) == {}
assert get_hr_min(join_quit_row) == {'hour': 0, 'minute': 1}
assert get_hr_min(message_row) == {'hour': 0, 'minute': 25}

here are my test cases. I get an assertion error when I run these test cases. I need to make an if statement to filter out the first assertion but I wanted to get the dictionary to work first(im assuming that is where my mistake is?). Any help or tips are appreciated.

1 Answer 1

1

A few problems with your code.

First of all, the functino you have there returns a set of dictionaries, rather than one dictionary. This means the value given is effectively a dictionary of dictionaries rather than a single dictionary. Second problem is that you are trying to compare and integer to a string in your assertion. Despite the fact that regex tells you that '01' is a number, its not. '01' is a string. To fix this, turn it into a number using int().

Here is the code fixed up:

def get_hr_min(row):
    time_match = re.search(r'(\d{2}):(\d{2})', row)
    hour = time_match.group(1)
    minute = time_match.group(2)
    rvalue = {} # return value
    rvalue['hour'] = int(hour) #give the hour as an integer to the "hour" value of rvalue
    rvalue['minute'] = int(minute) #give the minute as an integer to the "minute" value of rvalue
    return rvalue # return the dictionary, as one complete dictinary rather than a set.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. Wish I could give you an upvote!
@Aiden, if you think it is correct, just mark it as the correct answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.