如何使用 NamedTuple 获取键和值

How to get key and values using NamedTuple

Python 3.9.6

我一直在试图弄清楚当我给出了我想要从 NamedTuple 中获取的特定变量时如何打印键和值。

我创建了我自己的 NamedTuple,我通过读取 TOML 文件的配置导入它,可以在下面阅读:

from config import configuration

"""
[discord]
    [discord.filtered]
        [[discord.filtered.swedish]]
            eu = "https://discordapp.com/api/webhooks/1...."
            asia = "https://discordapp.com/api/webhooks/2...."
            
        [[discord.unfiltered.mixed]]
            eu = "https://discordapp.com/api/webhooks/7...."
            asia = "https://discordapp.com/api/webhooks/8..."   
            
    [discord.unfiltered]
        [[discord.unfiltered.swedish]]
            eu = "https://discordapp.com/api/webhooks/3...."
            asia = "https://discordapp.com/api/webhooks/4...."
         
        [[discord.unfiltered.mixed]]
            eu = "https://discordapp.com/api/webhooks/5...."
            asia = "https://discordapp.com/api/webhooks/6..."   
"""
filtered = True  # Or False
type_filter = "swedish"  # Or mixed

collection = configuration.discord.filtered if filtered else configuration.discord.unfiltered

我能够确定 filtered 是否为真,然后我们进入配置中的 filtered 路径,否则未过滤,但我的问题从这里开始。如果我想使用瑞典语或混合语,我不知道如何打印出区域和 URL。

我想要得到的输出是例如

filtered = True  # Or False
type_filter = "swedish"  # Or mixed

should print out

eu,  https://discordapp.com/api/webhooks/1....
asia, https://discordapp.com/api/webhooks/2....

其中 eu/asia 是一个变量,URL 是它自己的变量

我怎样才能根据筛选和 type_filter 打印区域和 url?

恩佐回答后更新:

filtered = True  # Or False
type_filter = "swedish"  # Or mixed

collection = configuration.discord.filtered if filtered else configuration.discord.unfiltered

for regions in getattr(collection, type_filter):
    for region, discord_collection in regions.items():
        print(f"Region: {region}, URL {discord_collection}")

您可以使用 toml library:

data = """
[discord]
    [discord.filtered]
        [[discord.filtered.swedish]]
            eu = "https://discordapp.com/api/webhooks/1...."
            asia = "https://discordapp.com/api/webhooks/2...."
            
        [[discord.unfiltered.mixed]]
            eu = "https://discordapp.com/api/webhooks/7...."
            asia = "https://discordapp.com/api/webhooks/8..."   
            
    [discord.unfiltered]
        [[discord.unfiltered.swedish]]
            eu = "https://discordapp.com/api/webhooks/3...."
            asia = "https://discordapp.com/api/webhooks/4...."
         
        [[discord.unfiltered.mixed]]
            eu = "https://discordapp.com/api/webhooks/5...."
            asia = "https://discordapp.com/api/webhooks/6..."   
"""

import toml

mapping = toml.loads(data)
print(mapping)

产出

{'discord': {'discord.filtered': {'discord.filtered.swedish': {'asia': '...', 'eu': '...'},
                                  'discord.unfiltered.mixed': {'asia': '...', 'eu': '...'}},
             'discord.unfiltered': {'discord.unfiltered.mixed': {'asia': '...', 'eu': '...'},
                                    'discord.unfiltered.swedish': {'asia': '...', 'eu': '...'}}}}

然后,只需创建一个函数来过滤生成的字典:

def filter_dict(dicts, name: str, filtered: bool):
    key_1st = "discord"
    key_2nd = "filtered" if filtered else "unfiltered"
    key_3rd = name
    return dicts[key_1st]['.'.join((key_1st, key_2nd))]['.'.join((key_1st, key_2nd, key_3rd))] 

print(filter_dict(mapping, name="swedish", filtered=True))
# Outputs {'eu': 'https://discordapp.com/api/webhooks/1....', 'asia': 'https://discordapp.com/api/webhooks/2....'}