我如何获取金字塔中的ini数据? [英] how can i get the ini data in pyramid?

查看:92
本文介绍了我如何获取金字塔中的ini数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在金字塔项目中有一个development.ini或production.ini.我在其中添加自己的配置数据 ini文件如下:

There is a development.ini or production.ini in a pyramid project. I add my own config data in the ini files like:

[thrift]
host = 0.0.0.0
port = 8080

,我想在项目中的一个py文件中使用配置数据. 没有请求对象的情况下如何获取数据? (我已经看到了使用请求的解决方案.)

and I want to use the config data in one of py files in the project. How can I get the data without the request object? (I've seen a solution which uses request.)

推荐答案

您可以在request.registry.settingspyramid.threadlocal.get_current_registry().settings上访问设置.它的行为就像字典.

You can access the settings at request.registry.settings or pyramid.threadlocal.get_current_registry().settings. It behaves like dictionary.

如果要使用第二个,即在没有请求的情况下获取设置,我必须警告您.根据文档:

If you want to use the second one, that is getting the settings without having the request, I have to warn you. According to the doc :

此功能应极其谨慎地使用,通常仅在单元中使用 测试代码.通常几乎总是使用错误 get_current_registry在测试上下文之外,因为它的用法使得 可以编写既不容易测试也不可以测试的代码 脚本化.

This function should be used extremely sparingly, usually only in unit testing code. it’s almost always usually a mistake to use get_current_registry outside a testing context because its usage makes it possible to write code that can be neither easily tested nor scripted.

您需要访问设置的代码的任何部分都是Web应用程序的一部分,并且应该访问该请求,而不是某些不可访问的全局变量.您可能认为您有充分的理由,但是可能有更好的方法.如果您想对此进行讨论(如果很复杂,也许会再问另一个问题,因为某些代码会有所帮助),我很乐意帮助您找到一种干净的方法.

Any part of your code that needs access to the settings is part of the web application, and should access the request, not some magically accessible global. You might think you have a good reason, but there's probably a better way. If you want to discuss that (maybe in another question if it's complex, as some code would help), I'll be glad to help you find a clean way to do it.

这篇关于我如何获取金字塔中的ini数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆