简体   繁体   English

使用 Python 即时存储数据

[英]Storing data on the fly with Python

I have a program where I want to record the function name, parameters passed to the function and the result.我有一个程序,我想记录 function 名称、传递给 function 的参数和结果。 I am currently recording this using a decorator.我目前正在使用装饰器记录此内容。 However I am unsure how to store this.但是我不确定如何存储它。 Currently I am just appending to a file each time.目前我只是每次都附加到一个文件中。

I want to just be able to add my decorators to any function in any python program and record the result.我只想能够将我的装饰器添加到任何 python 程序中的任何 function 并记录结果。 As such I need to be able to store the data with each time.因此,我需要能够每次都存储数据。 This makes it difficult to use XML to store the data as the document would have to be parsed, added to and rewritten each time.这使得很难使用 XML 来存储数据,因为每次都必须解析、添加和重写文档。

Any suggestions on how I can save this information?关于如何保存这些信息的任何建议?

wrap all those inside a class and then use pickle module将所有这些包装在 class 中,然后使用泡菜模块

I have a project that do something very close to what you want: coopy我有一个项目做的事情非常接近你想要的: coopy

Use the logging module from the standard library.使用标准库中的logging模块。 It is designed to provide a flexible framework for logging information from a running application (across modules and source files).它旨在提供一个灵活的框架来记录来自正在运行的应用程序的信息(跨模块和源文件)。

In particular, it supports the notion of handlers , allowing you to send logging information into various sinks: files, sockets, email, syslog daemon (on Unix) and so on.特别是,它支持处理程序的概念,允许您将日志信息发送到各种接收器:文件、sockets、email、syslog 守护程序(在 Unix 上)等等。 If neither of the existing handlers answers your needs, it isn't difficult to write a custom one and plug it in. For instance, you can implement a logger that writes into a database.如果现有的处理程序都不能满足您的需求,那么编写自定义处理程序并将其插入并不难。例如,您可以实现一个写入数据库的记录器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM