【问题标题】:Write a program to generate a log编写程序生成日志
【发布时间】:2020-03-10 02:54:54
【问题描述】:

我的文本文件

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"

用户预期的输入: 1) 生成的行数

2) 输出到文件或控制台

3) 当程序没有给出参数时提供帮助。

4) 确保程序生成的行数是该程序用户输入的数字。 (考虑大数)

python3 test.py --help(它应该显示帮助选项)

python3 test.py -N 20 -type console(它应该在控制台打印日志)

python3 test.py -N 10 -type log -name abc.log(它应该在文件中打印日志)

我的伪代码

 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))

还有比argument passing这样更好的方法吗

【问题讨论】:

  • 您可以准备一个INIYAML文件,并用它们控制程序。
  • 当然,参数必须以某种方式从控制台传到程序。因此,争论的通过是不可避免的。但是,有些库可以处理这些用户输入的进一步处理。我立刻想到了三个可以很好地处理 解析 参数的工具:argparsedocoptclick

标签: python logging arguments parameter-passing


【解决方案1】:

对于命令行参数,请使用 Python 标准库argparse

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("type")
args = parser.parse_args()

if args.type == "console":
    print("log to console here")
elif args.type == "log":
    # log to file here
    pass

【讨论】:

    【解决方案2】:

    如果未指定 output_file,此程序将默认在控制台中打印行

    def parsing_arguments():
        parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                         description="""Write what your tool is doing here""")
        parser.add_argument("-n",
                            "--number_of_lines",
                            type=int,
                            help="Number of line to be generated",
                            required=True)
        parser.add_argument("-o",
                            "--output_file",
                            default=None,
                            help="output file used for saving logs")
        args = parser.parse_args()
    
        output_to_console = args.output_file is None 
    
        return {"number_of_lines": args.number_of_lines,
                "output_file": args.output_file
                "output_to_console":args.output_to_console}
    
    def line_generator_function(number_of_lines,output_file,output_to_console):
        lines = ... # custom logic here # assuming that lines is a list of strings 
        if output_to_console is True:
            print(*lines, sep="\n")
        else:
            with open(output_file, "wt") as out:
                print(*lines, sep="\n", file=out)
    
    
    if __name__ == "__main__":
        arguments = line_generator_function()
        line_generator_function(**arguments)
    

    --help 由 argparse 自动提供。它将打印提供给参数描述的主要描述和帮助。以及它被称为没有任何参数。

    如果您需要 dekstop GUI,您可以查看此工具。 Gooey

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      相关资源
      最近更新 更多