Question: Create a new file named log _ analyzer.py in the directory you created for this application. This file will be a module that you will
Create a new file named loganalyzer.py in the directory you created for this application. This file will be a module that you will include into your main directive code.
In the loganalyzer.py file, create a new Python class named LogEntry. Each object of class LogEntry will represent one line in the CSV file.
The LogEntry class will have the following attributes: eventtime, internalip portnumber, protocol, action, ruleid sourceip country, and countryname. The other fields are optional.
The CSV file contains a string with the date and time information. In the initializer for the LogEntry class, use the Python datetime package to ensure that the value for eventdate is stored as a datetime object. This will be important later.
Add a property to the LogEntry class called ipvclass that uses a regular expression to examine the sourceip and returns the string value of the IPv address class which will be either ABC or D based on the public IP address range. For example, the sourceip will return A and the sourceip will return C
Think: How can you find just the first component of an IPv address?
Write Unit Tests for the LogEntry Class
Create a new file named testloganalyzer.py in the directory you created for this application. Write several unit tests that evaluate the following functionality of your LogEntry class:
Ensure that the eventtime string is properly converted to a datetime object.
a For example, you could create a new instance of LogEntry with a string value of :: UTC for the eventtime. You could then assert that the month of the resulting datetime object
b You could also assert that the hour of the resulting datetime object
Ensure that the ipvclass returns the correct IPv address class
a For example, you can assert that the ipvclass D for the sourceip
Write several tests asserts for each of the above scenarios to ensure your class is behaving correctly. Make sure all your tests pass before moving on
THINK: What else could go wrong when converting the CSV to objects of class LogEntry?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
