0

Im trying to code a script where I check a certain page of names. Before I continue summarizing the problem. The page is known to flick - meaning that once you go to the page it can list you names. The next refresh page you do, it will return empty name list and the next one again it will list you the names again. (That's the further idea I am trying to do) however I have created a little own script where we as user can test it easier.

Instead of requests, I have created a txt file to easier run the program

What I am trying to do is following:

I want to make the script so it opens the txt every loop, it checks if there is names in the list, then we print it out only once. And if there is no names - Then I want to make a counter that checks if the names actually are empty or not, meaning in that case I want to create a sort of counter that confirms that, and declare that there is no names in the list. Meaning that after 5 opens of txt file and there haven't been any names in the list after those 5 in the row opening. then we can declare it as its actually empty.

If counter have confirmed that it is empty, then we loop until we find names and print it once again and then we start over as previous.

What I have tried is that I think there is slight problem when I am coding where I might confuse myself or over-complicate myself more than I actually should.

count = 0
last_names = []
names_checker = False

while True:

    with open('./test.txt') as f:
        new_product_values = json.load(f)

    # {'name': 'Random names', 'url': 'www.stackoverflow.com', 'names': []}

    if names_checker == False:

        if not new_product_values['sizes']:
            count += 1
            time.sleep(1)

        if count == 5:
            names_checker = True
            count = 0
            logger.success('Declare value as No names')

        else:
            names_checker = True

    elif names_checker == True:

        while True:


            if new_product_values['names'] != last_names:

                print("NEW NAMES!")
                print(new_product_values['names'])
                last_names = new_product_values['names']
                logger.status(last_names)
                names_checker = False
                break

            else:
                logger.warn("Nothing found - sleep 1")
                time.sleep(1)

text file:

{'name': 'Random names', 'url': 'www.stackoverflow.com', 'names': []}

My expected results in that case is that:

If there is no names in the list, we add one in the counter, if the next loop still continues to give us empty name then we add another one in the counter until it hits the counter 5, when it hits counter 5 I want it to declare it as the list is empty. Whenever it is empty, we want to loop until we find names. And once we find the names we want to declare that the list is not empty and print out the names once and start over with the counter over again.

6
  • First of all, there is no question. It is unclear what you are asking, and even if it were clearer, the question is too broad. I suggest the following. First, split your problem into independent parts: 1) fetching the file (form web, from disk whatever), 2) extracting the names from the file, 3) error handling & retries. Write some functions. Separate the code. Then, if you have a question about a specific problem, ask that. Commented Jan 13, 2019 at 10:46
  • If you start looking for names again after you declare the list empty, why do you need to count? If the counter is only for logging or sleeping longer if the list was empty, put it all into one loop -- it actually seems quite confusing. Commented Jan 13, 2019 at 10:46
  • @user24343 Thats because as I mentioned from the beginning of the text. that my idea was to use requests in the future and the site that I want to check does this flickering where it sometimes adds names in one requests and the very next requests it says no names listed. So the reason I thought was to add a count to declare if its actually empty or not. But as you said, I might have overthought all this. Commented Jan 13, 2019 at 10:49
  • @zvone Oh alright, I thought also that I might have asked a question way to board as you mentioned. I will do it right away in that case. Im sorry for all this aswell. Commented Jan 13, 2019 at 10:49
  • @Hellosiroverthere the point is you don't stop requesting the page as soon as your counter reaches 5; you only say "list empty, checking if it fills again" instead of saying "possible flicker, checking if I get values next time" => there is no difference on the request-making side. Do you want me to write an answer on how to split up process? Commented Jan 13, 2019 at 10:54

1 Answer 1

1

You are right in your analysis: you are overthinking the problem.

First, split what you are doing up into simple steps

                +--------------------------+
  +-----------> |Get the data from the page| <-----------------+
  |             +-------------+------------+                   |
  |                           |                                |
  |                           v                                |
  |                                                            |
  |          +-------------------------------------------+     |
  |          |Get the data into a nice format (e.g. list)|     |
  |          +------------------+------------------------+     +------+
  |                             |                              |      |
  |                             |                     +--------+      |
  |                          +--+                     |               |
  |                          |                        |         +-------------+
  |                          v                        |         |wait for data|
  |                                                   |         +-------------+
  |         +--------------------------+              |
  |         |Do we actually have data? |              |                 ^
  |         +------+-----+-------------+              |flick            |no data
  |                |     |                            |                 |
+-+------+         |     |        +-------------------+-----------------+-----+
|do stuff|    <----+     +---->   |Is this a flick or is there really no data?|
+--------+     yes         no     +-------------------------------------------+

You see both flick and no data eventually return to get data.

if you put the above steps into python code, you will get something like this:

def get_data():  # just an example
    r = requests.get(...)
    r.raise_for_status()
    return r

def parse_data(text, _old=[]):  # list is mutable => preserved across calls
    """convert the text to a list of names. If there are no (new) names, the list is empty"""
    ...
    if new_data not in _old:
        _old.append(new_data)
        return new_data
    else:
        return []

def do_stuff(names):
    ...

Splitting up like this will not only allow your code to be read and understood better, but also provide you with a way change individual parts: If you want to use local files/data for testing, you can just rewrite get_data without changing anything else:

def get_data():
    if random.randint(0, 1):  # simulate flicks
        return empty_sample_data
    else:
        return random.choice(sample_data_with_names)

Following the structure diagram above, you can now first get data, check it and perform the right action before getting new data by looking at counter in one loop:

WAIT_TIME = 10*60
counter = 0
while True:
    data = parse_data(get_data())
    if data:
        counter = 0
        do_stuff(data)
        print("I got the data and did {stuff}. Now I'll wait and repeat with new names")
        time.sleep(WAIT_TIME)
    else:
        counter += 1
        if counter >= 5:  # cosmic rays may increase ``counter`` by 2 instead of 1
            counter = 0
            print("I think there actually isn't any data. I'll wait and then check again")
            time.sleep(WAIT_TIME)

If new data comes in quickly and you decide you don't need that much logging, you can also just take counter out.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.