2

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']
2
  • 4
    Not sure what you're asking. It's calling the _mw_action method, getting the 'StatisticSeries' value from the returned data, then returning that. Commented Dec 21, 2016 at 16:41
  • you can always elucidate the data type of the returned value by using type(my_object.my_method(arguments)) Commented Dec 21, 2016 at 16:49

1 Answer 1

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"}
Sign up to request clarification or add additional context in comments.

11 Comments

awesome thanks for the quick response, so if i use a loop to iterate kwargs i should be able to see the values?
Exactly. for k in kwargs: print k, kwargs[k]
so i can iterate the keys:values of the dictionary i input but how would I iterate the dictionary that comes back ?
def _mws_action(self, action, method='GET', **kwargs): response_key = '%sResponse' % action return self.api_call(method=method, Action=action, **kwargs)[response_key]
you've been a great help, happy holidays
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.