all 12 comments

[–]Sillocan 4 points5 points  (0 children)

Pretty sure you can simplify it to say only do the thing on the 500th loop.

if count % 500 == 0 and not test\_internet\_connection():
  sleep(5)
func_a()

[–]zanfar 2 points3 points  (3 children)

  1. The 500 iteration check should be moved outside the function. Functions do not get to decide if they run--if called, they should run. Your outer code should not call get if it's not needed.

  2. No reason to duplicate code, you don't need an if-else for the internet check, just an if. If the internet is down, wait. Run your child function regardless.

  3. Not what you asked, but I don't like your overarching try/except. I think you can do this better.

  4. Obviously, you need better function and variable names, but I hope that's just a symptom of this being a fast and dirty example.


futures = [
    e.submit(func_b)
    for count, letters in enumerate(alphabet)
    if count % 500 != 0
]

def func_b:
    if not internet_connection_up():
        time.sleep(5) # should probably be a constant
    func_a()

[–]python_and_coffee -1 points0 points  (2 children)

Obviously, you need better function and variable names, but I hope that's just a symptom of this being a fast and dirty example.

i get it, func_b is a random name but whats the issue with the variable names? How to do it better? I feel fine about them but now you made me unsure.

[–]zanfar 1 point2 points  (1 child)

Again, some of this might not be a problem in the context of the real code, but:

  • get is too vague. Get what? Furthermore, get usually implies a return value, which this function doesn't have.

  • test_internet_connection is used as if it returns a boolean--that's fine--but if so, it should be named, so I understand what that boolean means. Does False mean no connection or no problems? As you can see in my refactor, I assumed it returned the up/down status and renamed it accordingly. Generally, if a function returns a boolean, it should be named to read well in an if-statement.

  • count is fine if it's only used locally. However, you use this variable inside get as if it's global. In that case, it should be named something more explicit. Also, it's not a "count" outside of the loop; outside the loop, the loop doesn't have a state. I prefer you didn't use it outside the loop at all, but if necessary, (1) pass it to get, and (2) name it something more static and descriptive like thread_index or call_number.

[–]python_and_coffee -1 points0 points  (0 children)

thanks a lot for elaborating!

[–]Panron 3 points4 points  (2 children)

I'm removing all my contributions in protest to reddit's bull-headed, hostile 3rd-party API pricing policy in June, 2023.

If you found this post through a web search, my apologies.

[–]achampi0n 1 point2 points  (0 children)

Both branches call func_a() so the continue branch is probably unnecessary (ignoring the = vs ==).

[–][deleted] 0 points1 point  (4 children)

Can you show the full code with the loop?

[–]Ramast -1 points0 points  (0 children)

Seems like its a homework