Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Break out of nested loops
#11
Why write this?
def get_html(BASE_URL, current_page, base_session):
    #Get request
    response = base_session.get(BASE_URL.format(current_page), headers=HEADERS)
    return response
What benefit is derived by writing this function that just calls another function? Does it make the code easier to understand? Does it introduce an important abstraction? I don't think it does either, and adding a function just adds code. How about doing this instead:
def get_soup(session, page):
    '''Return steamy bowl of soup for BASE_URL page.  Return None if request fails'''
    session = session.get(BASE_URL.format(page), headers=HEADERS)
    if session.status_code == 200:
        return BeautifulSoup(session.text, 'lxml')
    return None
This barely meets my minimum requirements for making a function but at least it does something interesting. It is a soup factory. Give it a URL and a page and it returns soup. Notice I removed the URL argument. I assume the URL and the HEADERS argument are closely tied. If so either pass both as arguments or pass neither. If you pass the URL as an argument, use lower case. Save all upper case for global variables.

Now you can write your code like this:
def get_authors():  # Use meaningful function names, not Parse
    '''Loop through BASE_URL pages collecting author tags.  Return list of authors'''
    authors = []
    page = 1
    session = requests.Session()
   
    while True:
        soup = get_soup(session, page)
        if soup is None:
            print("I'm hungry")  # Should probably raise an exception
            break

        for name in soup.select('.author'):
            authors.append(name.text)
             
        if not(soup.select_one('li.next')):  
            break      
        page += 1
    return authors

if __name__ == '__main__':
    save_csv(get_authors(), 'example.csv')
You still get to have functions, but now each does something useful and has a purpose you can describe using one sentence. get_soup() retruns soup for a page, save_csv() will eventually save a list to a csv file, and get_authors returns a list of authors from a URL. If you parameterized the URL, HEADER and tag these might all be reusable.
Reply
#12
I get what you are saying but this was the exercise I had to follow. I realize the get_html function is useless. Remember, I am following an exercise with specific criteria.

The instructor wanted us to do it this way and said, "This is not really the best example, etc...." He also said, that it is best to pass global parameters as an argument to the function, even if the global variable can be seen from inside the function. This allows for more self-expressing code.

You use a lot of shortcuts which is fine but for a better understanding, I would prefer to add a few extra lines of code. You did things like, call the function, then take the return into a list comprehension, etc... It works but it's a little difficult to understand. Well, I understand it but if someone uses your code they may not.

We can take our code and constantly refactor it to make it better but there comes a time when you must abandon it. You have much more experience with Python and coding in general than I do.

I was a web developer which is different. My strong points are SQL, rationalizing a database, optimizing it for speed, letting the database layer do most of the heavy lifting with triggers, constraints, etc... Session and cookie management, authentication and authorization, CSS, HTML 5, Javascript, etc...

I want to be as thorough as possible with Python. I have to understand the packages that are included with it such as Math, Collections, Sys, Os, etc.. Then I will move onto building a test website with no framework.

I mainly used VB.net with asp.net then I came across PHP and frameworks. The PHP frameworks aren't as difficult as Django. I briefly looked it over and it's overwhelming because it's not really a MVC.


Question:

How would someone go about creating instances of a class on the fly? or is it even necessary. Let's say it's for a GUI windows program.

A class called Hand which creates a new player for a Black Jack game. Let's say it looks something like this: How would I create instances on the fly to avoid name collision, a GUID?

class Player:
    def __init__(self, name):
        self.name = name
        self.all_cards = []

    def remove_one(self):
        return self.all_cards.pop(0)

    def add_cards(self, new_cards):

        if type(new_cards) == type([]):
            self.all_cards.extend(new_cards)
        else:
            self.all_cards.append(new_cards)

    def __str__(self):
        return f"Player {self.name} has {len(self.all_cards)} cards."


p1 = Player("Matt")
p2 = Player("Paul") # How would you create more players and create instances on the fly?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 826 Sep-18-2023, 07:52 AM
Last Post: buran
  reduce nested for-loops Phaze90 11 1,762 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,641 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,175 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to break out of nested loops pace 11 5,264 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 7,321 Jan-09-2021, 06:48 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,332 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,926 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  Python beginner - nested while loops mikebarden 1 1,838 Jun-01-2020, 01:04 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

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