this post was submitted on 17 Aug 2024
701 points (95.2% liked)

Programmer Humor

31997 readers
600 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[โ€“] jeena@piefed.jeena.net 1 points 3 weeks ago (1 children)

I don't get it because my phyton code is indented exactly the same as all my other code. Each block of code one tab in/out, how else would you do it?

[โ€“] uranibaba@lemmy.world 1 points 3 weeks ago

It's because there is no clear indication of where a block ends.

Here is some sample code. I find it difficult to tell how many indentations I have or where I need to write if I want to continue at a certain level.

import time
import aiohttp

"""
Retreives the data from RSS URL and return the status codes as well as the data. Return -1 if something went wrong.
"""
async def get_rss_feed(rss_url):
    async with aiohttp.ClientSession() as session:
        try:
            retry_count = 0
            while retry_count < 5:
                async with session.get(rss_url) as resp:
                    if resp.status == 200:
                        return {'status': resp.status, 'data': await resp.text()}
                    else:
                        retry_count += 1
                        time.sleep(60)
            if retry_count == 5:
                raise ValueError('To many failed connection attempts', retry_count)
        except aiohttp.InvalidURL as error:
            return {'status': -1, 'data': f"Error: {rss_url} is not a valid URL.", 'error': error}
        except aiohttp.ClientConnectorError as error:
            return {'status': -1, 'data': f"Error: Could not connect to {rss_url}.", 'error': error}
        except ValueError as error:
            return {'status': -1, 'data': f"Error: Could not connect to {rss_url} after {retry_count} attempts.", 'error': error}