Tuesday, 26 November 2019

Write a program to generate a log

My Text file

192.168.10.20 - - [18/Jul/2017:08:41:37 +0000] "PUT /search/tag/list HTTP/1.0" 200 5042 "http://cooper.com/homepage/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/5342 (KHTML, like Gecko) Chrome/14.0.870.0 Safari/5342" 10.30.24.3 - - [18/Jul/2017:08:45:15 +0000] "POST /search/tag/list HTTP/1.0" 200 4939 "http://www.cole-brown.net/category/main/list/privacy/" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/5322 (KHTML, like Gecko) Chrome/14.0.843.0 Safari/5322" 98.5.45.3 - - [18/Jul/2017:08:45:49 +0000] "GET /apps/cart.jsp?appID=8471 HTTP/1.0" 200 4958 "http://knight-chase.com/post.jsp" "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_7_3; rv:1.9.6.20) Gecko/2013-11-03 17:44:01 Firefox/3.8" 94.5.6.3 - - [18/Jul/2017:08:48:56 +0000] "GET /list HTTP/1.0" 200 4891 "http://thomas.com/explore/wp-content/homepage/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_4; rv:1.9.5.20) Gecko/2013-02-19 05:36:22 Firefox/3.6.15"

Input from user expected: 1) Number of lines to generate

2) output to file or console

3) Provide a help when no arguments to the program is given.

4) Ensure that number of lines generated by the program is the number entered by the user of this program. (Think large numbers)

python3 test.py --help (it should display help options)

python3 test.py -N 20 -type console (it should print the log in console)

python3 test.py -N 10 -type log -name abc.log (it should print the log in a file)

My Psedo Code

 import sys
 from itertools import islice

 args = sys.argv
 print (args)
 #['file.py', 'datafile', '-N', '10']
 if args[1] == '-h':
     print ("-N for printing the number of lines: python file.py datafile -N 10")
 if args[2] == '-N':
     datafile = args[1]
     number = int(args[3])
     with open(datafile) as myfile:
         head = list(islice(myfile, number))
         head = [item.strip() for item in head]
         print (head)
         print ('\n'.join(head))

Is there any better way of approach than this like argument passing



from Write a program to generate a log

No comments:

Post a Comment