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']
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
pyth0nBen
  • 91
  • 1
  • 10

1 Answers1

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"}
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
mlv
  • 580
  • 4
  • 10
  • awesome thanks for the quick response, so if i use a loop to iterate kwargs i should be able to see the values? – pyth0nBen Dec 21 '16 at 16:53
  • Exactly. for k in kwargs: print k, kwargs[k] – mlv Dec 21 '16 at 17:27
  • so i can iterate the `keys:values` of the dictionary i input but how would I iterate the dictionary that comes back ? – pyth0nBen Dec 22 '16 at 11:13
  • `def _mws_action(self, action, method='GET', **kwargs): response_key = '%sResponse' % action return self.api_call(method=method, Action=action, **kwargs)[response_key] ` – pyth0nBen Dec 22 '16 at 11:16
  • do i need to iterate `[response_key]` from `_mws_action` to key the values? – pyth0nBen Dec 22 '16 at 11:18
  • If you need to look at all the keys that _mws_action returns (not just 'StatisticsSeries'), then yes, you can iterate through them. From what you said, api_call returns a dictionary with a key of 'GetMetricDataResponse', that contains another dictionary with a key of 'StatisticsSeries'. But I don't know what api_ret['GetMetricDataResponse']['StatisticsSeries'] is. My recommendation is to: `ret = xxx.get(SchemaName='schema', Stat='somestat', EndTime="-P00D"); print("get returned: {}".format(repr(ret)))` Then you'll know what you're dealing with. – mlv Dec 22 '16 at 13:30
  • i can't tell you how much you've helped me. So now I can see the return. The first few keys are: `{'attributes': {}, u'Datapoint': {'attributes': {}, u'Val': u'11.0', u'StartTime': u'2016-12-22T14:18:00z'}` so from what i understand the key i want is `Datapoint` as that contains the value and timestamp that i am after, and i can dump the rest, but i am struggling with the syntax to return just this key and value, or even to just `print` it – pyth0nBen Dec 22 '16 at 14:32
  • It would be just `ret[u'Datapoint']` or `ret[u'Datapoint'][u'Val']` but since the Val is a string, you'll want to do `float(ret[u'Datapoint'][u'Val'])` – mlv Dec 22 '16 at 14:37
  • `format(repr(xxx))` is a great tool i wasn't aware of it – pyth0nBen Dec 22 '16 at 14:38
  • format is just a method of strings (replacement for the depricated % notation). It replaces {}s in the string with it's arguments. – mlv Dec 22 '16 at 14:54
  • 1
    you've been a great help, happy holidays – pyth0nBen Dec 22 '16 at 17:19