Divide list into chunks and do a job for a given condition

Viewed 27

I'm a newbie. Maybe my question is silly but i hope we can find a solution.

Let's say my calc function in my code do some maths with data which given in data as a list. Also texter function queues the messages that calc function produced. Here is the tricky part:

What i desire is I need to create a function to send messages which queued in texter func but there is a flood limit for 7 messages per minute. So basically i need to divide messages into a list of 7 messages and then after sending 7 messages one by one, i need to wait for 60 seconds to process the other messages.

A few important points:

1- Calc function defined simple for creating an example. Some elements in data, may not create a message to send because it is not suitable for the formula in the calc function.

2- The messages that are going to be send should not be repeated. I don't prefer it to be a random through the list but even it has to be random, i need to make sure it is not repeated.

3- When we divide messages into new lists builded 7 elements max, as in this example 13 elements given in data, so first list must be 7 and the second list has to be 6 and finally in case it divides into seven again with a "None" / "Null" item in it, it should not go through the message.

4- Also changing the calc function into process 7 items in the list and then wait for 60 seconds is not an option. In the realtime run, datafeed might be hundreds of items which will take too much time and won't work as desired.

You may find my code below. hopefully there is an easy way to do it. I figured out how to divide them into new lists. But calc function may produce different amount of messages in each run. Therefore i can not create function to process them manually. Because sometimes there are too many lists that can cause index out of range error depending on less messages queued unfortunately. Sorry for the code by the way, i am trying to improve myself with online courses but i know still not pythonic enough to express myself :)

import requests

data = [1,2,3,4,5,6,7,8,9,10,11,12,13]


messages = []


def texter(message):

    messages.append(message)

    return messages



def sender():

    endpoint = 'https://api.telegram.org/TOKENHERE/sendMessage?chat_id=IDHERE&text='

    sender =  requests.get(f'{endpoint}+{message}').json() #message comes from the messages in texter function.
    texter(sender)





def calc(data):
    result = data * 5
    return result


mresult = list(map(calc, data))
sender()
1 Answers

Not sure if that answers your whole question. (Please try to break it down a little bit more or comment below :))

You divide your data into smaller chunks like this:

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
chunk_size = 7
chunks = [data[i: i+chunk_size] for i in range(0, len(data), chunk_size)]

output:

[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13]]

if you set chunk_size = 3, you get

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13]]

and then do something like

for chunk in chunks: 
    your_code()
Related