Python函数返回字典? [英] Python function return dictionary?

查看:34
本文介绍了Python函数返回字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python初学者,遇到了我不理解的函数的输出.我不能提供所有代码,因为其中有些是我公司的IP.

I am a beginner Python user and I have come across an output to a function that I don't understand. I can't give all the code because some of it is IP at my company.

我基本上是使用我们的一位开发人员编写的库来从数据仓库中提取指标.然后,我想在另一个应用程序中使用该指标值,当我获得该值时,我会将其传递给我自己的数据库.

I am basically using a library written by one of our devs to pull a metric from out data warehouse. I want to then use this metric value in another application to when i get the value i will pass it to my own DB.

我的问题是我不了解我用来实际推断所需值的函数的输出.

My issue is I dont understand the output of the function I am using to actually extrapolate the value I want.

如果有更多Python经验的人可以告诉我函数的返回是做什么的,那么我可以告诉我它是在构建字典,但是我不完全了解如何以及在哪里.我必须添加这是来自lib内部的函数

If someone with more Python experience could tell me what the return of the function is doing as the best I can tell it is building a dict, but I don't fully understand how and where. I must add this is the function from inside the lib

def get(self, **kwargs):
    if 'SchemaName' not in kwargs:
        kwargs['SchemaName'] = self.find_schema_by_params(**kwargs)

    if 'Stat' in kwargs and kwargs['Stat'] not in MWS.VALID_Stat:
        raise MWSException("Incorrect Stat value: %s" % kwargs['Stat'])

    if 'Period' in kwargs and kwargs['Period'] not in MWS.VALID_Period:
        raise MWSException("Incorrect Period value: %s" % kwargs['Period'])

    self._validate_schema(kwargs, MWS.DEFAULT_GET_PARAMETERS)
    self._encode_start_time(kwargs)

    if 'EndTime' not in kwargs:
    if kwargs['StartTime'].startswith('-P'):
            kwargs['EndTime'] = '-P00D'
        else:
            kwargs['EndTime'] = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z")

    return self._mws_action('GetMetricData', **kwargs)['StatisticSeries']

推荐答案

显然, _mws_action()是一个传递字符串,'GetMetricData'和与get方法相同的关键字参数的方法(进行一些修改). _mws_action()返回一个字典,然后您返回该字典的'StatisticSeries'元素.

Apparently, _mws_action() is a method that is passed a string, 'GetMetricData' and the same keyword arguments as your get method (with a few modifications). _mws_action() returns a dictionary, and you return the 'StatisticSeries' element of that dictionary.

** kwargs 将字典与关键字参数进行转换.这样您就可以调用as as

**kwargs converts a dictionary to/from keyword arguments. So you can call get as

get(SchemaName='schema', Stat='somestat', EndTime="-P00D")

和kwargs将是:

{'SchemaName': 'schema', 'Stat':'somestat', 'EndTime':"-P00D"}

这篇关于Python函数返回字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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