0

why does the following function only runs once a sends only one set of data. I thought for each must mean for each value in the data set.

data_checkpoint_size = cclient.samples.list(meter_name ='checkpoint.size')
data_checkpoint_length = cclient.samples.list(meter_name ='checkpoint.length')
data_checkpoint_pause = cclient.samples.list(meter_name ='checkpoint.pause')


def counterVolume(data_checkpoint_size, data_checkpoint_length, data_checkpoint_pause):
  for each in data_checkpoint_size:
    d = each.counter_volume
  for each in data_checkpoint_length:
    e = each.counter_volume
  for each in data_checkpoint_pause:
    f = each.counter_volume
  pubnub.publish(channel='channel', message= {'checkpoint_size': d, 'checkpoint_length': e, 'checkpoint_pause': f})

counterVolume(data_checkpoint_size, data_checkpoint_length, data_checkpoint_pause)

And I only get following as result instead of series of data. checkpoint_size, checkpoint_length and checkpoint_pause are three different meters, these are three different data streams

{
  checkpoint_length: 75,
  checkpoint_size: 5000,
  checkpoint_pause: 50
}
6
  • 1
    Well, for one thing, we need to see how you're calling it. Commented Aug 27, 2015 at 16:08
  • Are you talking about pubnub.publish being only called once? It will get called once, because it's outside the loop. Commented Aug 27, 2015 at 16:11
  • 1
    Your d, e, and f are re-assigned through the for loop. Commented Aug 27, 2015 at 16:12
  • @Imo Do you want to run pubnub.publish for each combination of values in the 3 lists , or like once for each 0th index in the list, second time for all 1st index, etc?? Commented Aug 27, 2015 at 16:16
  • @AnandSKumar yes I would like to have a series of data Commented Aug 27, 2015 at 16:19

2 Answers 2

2

Like @ismailsunni said, your variables are reassigned. This makes the assumption that all data is of the same length, but it should work:

def counterVolume(data_checkpoint_size, data_checkpoint_length, data_checkpoint_pause):
    for i, size in enumerate(data_checkpoint_size):
        length = data_checkpoint_length[i]
        pause = data_checkpoint_pause[i]
        message = {
            'checkpoint_size': size .counter_volume,
            'checkpoint_length': length.counter_volume,
            'checkpoint_pause': pause.counter_volume,
        }
        pubnub.publish(channel='channel', message=message)

I'd test to make sure they're all the same length first and raise a specific exception (so it's easier to debug):

size_len = len(data_checkpoint_size)
length_len = len(data_checkpoint_length)
pause_len = len(data_checkpoint_pause)

if size_len != length_len or length_len != pause_len:
    raise Exception('Custom exception message.')
Sign up to request clarification or add additional context in comments.

Comments

2

You are overriding d, e, and f each time each.counter_volume is called. If you wish to end up with lots of collections of returned data, you'll need something like this:

for i in range(data_checkpoint_size):
    d = data_checkpoint_size[i].counter_volume
    e = data_checkpoint_length[i].counter_volume
    f = data_checkpoint_pause[i].counter_volume
    pubnub.publish(channel='channel', message= {'checkpoint_size': d, 'checkpoint_length': e, 'checkpoint_pause': f})

It is worth noting that this assumes that all your data sets are of equal length. For a more in depth answer, you will need to provide a more in depth question regarding what it is that you are trying to achieve.

Comments

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.