如何使用字符串格式化动态分配变量 [英] How to use string formatting to dynamically assign variables

查看:128
本文介绍了如何使用字符串格式化动态分配变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我填充的目的是通过rsyslog现在守护进程(this_instance_number)的许多实例rsyslog现在在本地机器上的相关(RHEL 6.4的Python 2.6.4)I环的配置,环境等方面的建模,许多属性(属性)所定义的配置文件

使用这些值填充我的目标。

鉴于以下变量:

  this_instance_number = 5
属性=startupscript

code的以下行,

 打印('rsyslog.instance%(INSTANCE_NUMBER)■%(属性)S'%\\
      {INSTANCE_NUMBER:this_instance_number,属性:属性})

将打印这样的文字:

  rsyslog.instance5.startupscript

我怎样才能然后分配该文本将提及到的值基于该格式字符串属性?

例如,如果硬$ C $的CD,我会分配:

  rsyslog.instance5.startupscript =/等/ init.d /的样本

不过,我想它分配是这样的:

 ('rsyslog.instance%(INSTANCE_NUMBER)■%(属性)S'%\\
      {INSTANCE_NUMBER:this_instance_number,属性:属性})=变量


解决方案

您会使用 GETATTR()动态检索属性:

 实例= GETATTR(rsyslog现在,实例{}。格式(this_instance_number))
打印GETATTR(例如,属性)

SETATTR()来分配:

 实例= GETATTR(rsyslog现在,实例{}。格式(this_instance_number))
SETATTR(例如,属性,变量)

,或为任意深度的更通用的方法:

 高清get_deep_attr(OBJ,*路径):
    回报减少(GETATTR,路径,OBJ)高清set_deep_attr(OBJ,价值,*路径)
    SETATTR(get_deep_attr(OBJ,路径[: - 1]),路径[-1],值)打印get_deep_attr(rsyslog现在,实例{}。格式(this_instance_number),属性)
set_deep_attr(rsyslog现在,变量实例{}。格式(this_instance_number),属性)

In Python, I am populating an object to model the configuration, environment and other aspects related to rsyslog on the local machine (RHEL 6.4 Python 2.6.4) I loop through many instances of the rsyslog daemon (this_instance_number), and many attributes (attribute) defined in a configuration file.

With those value I populate the object.

Given the following variables:

this_instance_number=5
attribute="startupscript"

The following line of code,

print ('rsyslog.instance%(instance_number)s.%(attribute)s' %\
      {"instance_number": this_instance_number, "attribute": attribute})

will print this text:

rsyslog.instance5.startupscript

How can I then assign the attribute that text would refer to to a value based on that format string?

For example, if hard coded, I would assign:

rsyslog.instance5.startupscript="/etc/init.d/sample"

But, I want to assign it something like this:

('rsyslog.instance%(instance_number)s.%(attribute)s' %\
      {"instance_number": this_instance_number, "attribute": attribute}) = variable

解决方案

You'd use getattr() to dynamically retrieve attributes:

instance = getattr(rsyslog, 'instance{}'.format(this_instance_number))
print getattr(instance, attribute)

and setattr() to assign:

instance = getattr(rsyslog, 'instance{}'.format(this_instance_number))
setattr(instance, attribute, variable)

or, for a more generic approach with arbitrary depth:

def get_deep_attr(obj, *path):
    return reduce(getattr, path, obj)

def set_deep_attr(obj, value, *path)
    setattr(get_deep_attr(obj, path[:-1]), path[-1], value)

print get_deep_attr(rsyslog, 'instance{}'.format(this_instance_number), attribute)
set_deep_attr(rsyslog, variable, 'instance{}'.format(this_instance_number), attribute)

这篇关于如何使用字符串格式化动态分配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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