Question: Building upon the codes below You will update your program to take TWO required command line arguments: 1 . - - filename ( or -

Building upon the codes below
You will update your program to take TWO required command line arguments:
1.--filename (or -f)
2.--action (or -a)
In your main() method, check the value of the --action argument. If the value is "head", run your previous code that writes the first 5 objects from the CSV file to the screen. (Your program should run exactly as it did before.)
The new command to run your program is:
python index.py --filename firewall_logs_2022.csv --action head
Refactoring Suggestion!
make a new method named print_head() and move your code that writes the first 5 objects to the screen into this new method. Call print_head() if the value of the --action argument is "head".
Additional Action: Deny
New Method
make a method called deny_count().
In this method, use a list comprehension to filter ONLY the log entries whose action is "Deny" into a new list.
Print the number of log entries that were denied to the screen.
Implement the New Method
Update your program to allow the user to provide the value "deny" to the --action argument.
If the user chooses this action, call the deny_count() method.
View the Result
The command
python index.py --filename firewall_logs_2022.csv --action deny
Should result in a print() statement that says the following (the number will be different based on the data):
12,345 log entries were denied.
Additional Action: Count of Country
New Method
make a method called country_count(). It must accept an argument that contains a 2-letter country code.
In this method, use a list comprehension to filter ONLY the log entries whose country matches the country code value of the argument.
Print the number of log entries that match this country code to the screen.
New Argument
To implement this action, you will need to accept a new NON-REQUIRED command line argument --country, or -c
Implement the New Method
Update your program to allow the user to provide the value "source" to the --action argument.
If the user chooses this action, call the country_count() method and pass the value of the --country argument.
View the Result
The command
python index.py --filename firewall_logs_2022.csv --action source --country US
Should result in a print() statement that says the following (the number will be different based on the data):
12,345 log entries from the US were recorded.
And also add the following functionality to your program that displays information about a single month's worth of log entries and, optionally, copies the data into a separate CSV file.
Additional Action: Parse (up to 10 points)
New Method
make a method called parse(). It must accept an argument that contains an integer value representing the month (1-12). Hint: Command line arguments are strings by default!
In this method, use a list comprehension to filter ONLY the log entries whose event_time matches the month of the argument.
Print the number of log entries from this month to the screen.
New Argument
To implement this action, you will need to accept a new NON-REQUIRED command line argument --month, or -m
Implement the New Method
Update your program to allow the user to provide the value "parse" to the --action argument.
If the user chooses this action, verify that they have passed in a valid month (it will be an integer from 1-12).
If the month is valid, call the parse() method and pass the value of the --month argument.
View the Result
The command
python index.py --filename firewall_logs_2022.csv --action parse --month 11
Should result in a print() statement that says the following (the number will be different based on the data):
12,345 log entries were recorded in month 11.
Export Data (up to 15 points)
If a user correctly executes the command to parse a specific month of data, also add the functionality that writes ONLY the events that occurred in that month to a new CSV file.
For example, if the user chose "11" as their month, your program should produce a CSV file named 2022_11_logs.csv
Hints and Tips
You can use the csv.DictWriter module to create a CSV file
Remember that the imported data in your program is stored as a list of objects of class LogEntry. You will need to convert the filtered objects into dictionaries before using DictWriter.
Your CSV file should contain a header row that labels all the "columns" in the remaining rows of your CSV file (it should have the same structure as the firewall_logs_sample.csv file).
Here is the code:
log_analyzer.py
from log_analyzer import LogEntry
import argparse
import csv
import pytz
def parse_args():
parser = argparse.ArgumentParser(description="Analyze firewall log CSV files.")
elog_analyzer.py 10 firewall_logs_sample.csv firewall_logs_2022.csv e test_log_analyzer.py
 Building upon the codes below You will update your program to

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!