将3个变量清理为1个stru

2024-05-20 19:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个struct,其中包含stats\u user、stats\u password和stats\u ui的数据,而不是像这样单独使用它们。我该怎么清洗这个?你知道吗

def config(apps, groups, stats_user, stats_password, stats_uri,
           bind_http_https, ssl_certs, templater):
    logger.info("generating config")
    config = templater.haproxy_head.format(
        statsUser=stats_user,
        statsPassword=stats_password,
        statsURI=stats_uri
    )

Tags: apps数据httpsconfighttpuibinddef
3条回答
  1. 在名为config的函数中给config赋值是一个非常糟糕的主意。

  2. Python没有所谓的“struct”,但是如果我理解正确的话,您可以将它放在字典中,然后在对.format:

    的调用中展开它

data = {'statsUser': 'foo', 'statsPassword': 'bar', 'statsURI': 'bat'}

以及

def config(apps, groups, data, bind_http_https, ssl_certs, templater):
    logger.info("generating config")
    config = templater.haproxy_head.format(**data)

有很多方法可以做到这一点。可以创建class,也可以使用dictionary,也可以使用named tuple。你知道吗

您的问题并不完全清楚您打算如何使用它们,但是通过所有这些解决方案(类、字典、命名元组),您可以将对象作为单个实体传递。你知道吗

自定义类

类是最具表现力的解决方案。可以定义具有所需属性的类,但也可以附加处理类中数据的方法。类也可以继承其他类或由其他类组成。你知道吗

# accept stats as a single argument, but then use each
# piece of data separately
def config(..., stats, ...):
    templater.haproxy_head.format(
        statsUser = stats.username,
        statsPassword=stats.password,
        statsURI = stats.uri
    )

# define the "Stats" class
class Stats(object):
   def __init__(self, username, password, uri):
        self.username = username
        self.password = password
        self.uri = uri

# create a stats object
stats = Stats("homer", "doh", "www.example.com")
...

# pass the object to config
config(..., stats, ...)

命名元组

命名元组与类非常相似,但设置起来要简单一些。您只需定义名称和属性列表。你得到的是一个自定义类。你知道吗

另一个重要的区别是它们是不变的。一旦创建了命名元组,就不能更改它包含的值。你知道吗

import collections

# accept stats as a single argument, but then use each
# piece of data separately
def config(..., stats, ...):
    templater.haproxy_head.format(
        statsUser = stats.username,
        statsPassword=stats.password,
        statsURI = stats.uri
    )
# define the Stats named tuple
Stats = collections.namedtuple("Stats", ["username", "password", "uri"])

# create a stats object
stats = Stats("homer", "doh", "www.example.com")
...

# pass the object to config
config(..., stats, ...)

注意,在实际使用中,类和命名元组是相同的。在这两种情况下,您都使用“点符号”来访问对象的元素(例如:stats.用户名). 你知道吗

字典

字典的开销最少。它只是名称到值的映射。你知道吗

# accept stats as a single argument, but then use each
# piece of data separately
def config(..., stats, ...):
    templater.haproxy_head.format(
        statsUser = stats["username",
        statsPassword=stats["password"],
        statsURI = stats["uri"]
    )
# define the stats
stats = {
    "username": "homer",
    "password": "doh",
    "uri": "http://www.example.com"
}
# pass them to the config function as a single object
config(..., stats, ...)

字典与类和命名元组的不同之处在于,通过将项名称作为键(例如:stats[“username”])来引用元素。你知道吗

您可以使用namedtuple。请看这里:https://docs.python.org/3/library/collections.html#collections.namedtuple

比如:

from collections import namedtuple
Stats = namedtuple('Stats', ['user', 'password', 'uri'])

然后可以使用位置参数、关键字参数或混合参数创建Stats对象:

s1 = stats(stats_user, stats_password, stats_uri) # positional
s2 = stats(user=stats_user, password=stats_password, uri=stats_uri) # keyword
s3 = stats(stats_user, stats_password, uri=stats_uri)

可以像访问任何其他对象一样访问成员(例如s1.user

在代码中,可以使用以下对象之一:

config = templater.haproxy_head.format(**s1._asdict())

相关问题 更多 >