Python function return dictionary?

无人久伴 提交于 2020-08-05 07:02:53

问题


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.

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']

回答1:


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 converts a dictionary to/from keyword arguments. So you can call get as

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

and kwargs will be:

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


来源:https://stackoverflow.com/questions/41267719/python-function-return-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!