Python Forum
A better way to limit loop execution?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A better way to limit loop execution?
#4
A set() could solve the problem.
A set is a collection of unique items, where the order is not kept.
Adding items to a set -> slow
Looking up items in a set -> blazing fast
Containment checking of a set -> also fast
You can't use index access on a set.

Here the modified code:
def attachments(attach, conv_id):
    convId = str(conv_id)
    processed = set()
    for item in attach:
        if item in processed:
            continue
        attachId = str(item['id'])
        attachParams = {'id': attachId}
        response = requests.get(attachUrl + attachId, headers=h, params=attachParams)
        response = response.content
        responseParsed = json.loads(response)
        content = b64decode(responseParsed['contents'])
        fileName = responseParsed['friendlyName']
        with open(r'attachments/' + convId + '_' + fileName + '.jpg', 'wb') as k:
            k.write(content)
        processed.add(item)
If it's not the item from attach which repeats, then use the other object.
If the conv_id is repeating, you should check this, before you call attachments.

Here is an updated version which may work with your code.
The thing is, you use the camel case names for objects and this is not very Pythonic (PEP8).
With pathlib the handling with paths is easier and compatible on all platforms.

from pathlib import Path


def attachments(attach, conv_id):
    processed = set()
    for item in attach:
        if item in processed:
            continue
        attach_id = item['id']
        attach_params = {'id': attach_id}
        response = requests.get(attachUrl + attach_id, headers=h, params=attach_params)
        response = response.content
        response_parsed = json.loads(response)
        content = b64decode(response_parsed['contents'])
        friendly_name = responseParsed['friendlyName']
        file_name = f"convId_{friendly_name}.jpg"
        attachment = "attachments" / Path(file_name)
        with attachment.open("wb") as fd:
            fd.write(content)
        processed.add(item)
If you use an IDE like PyCharm, the names which have no definition, will be red underlined.
He also complains about naming.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: A better way to limit loop execution? - by DeaD_EyE - Feb-26-2020, 08:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to measure execution time of a multithread loop spacedog 2 2,981 Apr-24-2021, 07:52 AM
Last Post: spacedog
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,089 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  Time execution of a "for loop" with zip different in 2 equivalent context sebastien 1 1,972 Oct-11-2019, 11:07 AM
Last Post: sebastien
  for loop execution sumandas89 5 3,899 Nov-27-2017, 09:04 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020