-1

If been working on a twitter bot for a while now, just for fun, but I've been stuck with this single problem. Sometimes it works, sometimes it does not. Here is the code:

def teste():
    tweets = api.home_timeline(2, read_id(FILE_NAME), tweet_mode = 'extended')
    print('Setup is ok!')
    for tweet in reversed(tweets):
        if 'carente' in tweet.full_text.lower():
            print(str(tweet.id) + ' - ' + tweet.full_text)
            api.retweet(tweet.id)
            store_the_id(FILE_NAME, tweet.id)
            teste.x = 1
        elif 'carente' not in tweet.full_text.lower():
            teste.x = 0
        else:
            print('Error no sistema alguem me desconfiguro')

def tweet_timer():
    if time.gmtime().tm_min == '10':
        api.update_status(f'Farid esteve carente {y} vezes')
    else:
        return

while True:
    teste()
    with open('number.txt', 'r') as f:
        read = f.readline
        z = int(read())
    y = z + teste.x
    print(y)
    with open('number.txt', 'w') as f:
        write = f.write(str(y))
        write
    tweet_timer()
    time.sleep(40) 

I'm trying access the teste.x variable out of the function, but when I do at the y = z + teste.x it gives the following error:

Traceback (most recent call last):
  File "c:\Users\fredg\Desktop\Programação\Python\Twitter Bot\Website\Word.py", line 69, in <module>
    y = z + teste.x
AttributeError: 'function' object has no attribute 'x'
Farid
  • 1
  • 1
  • 1
  • If the `tweets` list is empty, the function never sets `teste.x`, so you'll get that error. – Barmar Feb 02 '21 at 21:30
  • 1
    Setting attributes on a function is not a good practice. Use `return` instead. – Brian McCutchon Feb 02 '21 at 21:32
  • The loop keeps updating `teste.x`, so at the end it just has the value from the last iteration (which is the first tweet because you reversed them). Is that really what you want? – Barmar Feb 02 '21 at 21:32
  • And the `else:` code can never be executed. Either `carente` is in the text or it's not, there's no other possibility. – Barmar Feb 02 '21 at 21:33
  • You need to explain what this function is supposed to do? I suspect the logic in the `teste()` function is wrong. See https://stackoverflow.com/questions/42913798/searching-array-reports-not-found-even-though-its-found/42913882#42913882 – Barmar Feb 02 '21 at 21:45

2 Answers2

0

teste.x is deleted after the function is executed by the garbage collector, you can use a global variable or pass the created outside the function, returning its value from the function for further transfer and use.

Example:

def teste(x: int) -> int:
    tweets = api.home_timeline(2, read_id(FILE_NAME), tweet_mode="extended")

    print("Setup is ok!")

    for tweet in reversed(tweets):

        if "carente" in tweet.full_text.lower():
            print(str(tweet.id) + " - " + tweet.full_text)

            api.retweet(tweet.id)
            store_the_id(FILE_NAME, tweet.id)

            x = 1

        elif "carente" not in tweet.full_text.lower():
            x = 0

        else:
            print("Error no sistema alguem me desconfiguro")

    return x


def tweet_timer():
    if time.gmtime().tm_min == "10":
        api.update_status(f"Farid esteve carente {y} vezes")
    else:
        return


x = 0

while True:
    x = teste()

    with open("number.txt", "r") as f:
        read = f.readline
        z = int(read())

    y = z + x

    print(y)

    with open("number.txt", "w") as f:
        write = f.write(str(y))
        write

    tweet_timer()

    time.sleep(40)


P.S. I have not tested the functionality of the code due to the lack of the required library, but with the exception of simple possible errors, you will get the result you need

GolangMan
  • 21
  • 4
  • Why would `teste.x` be garbage collected? – Barmar Feb 02 '21 at 21:34
  • I used google translate, maybe it translated incorrectly The garbage collector itself removes the ```teste.x``` after executing the test function teste() – GolangMan Feb 02 '21 at 21:37
  • oh, sorry. I was wrong about the garbage collector, in fact, the problem is that ```x``` attribute is not always assigned to the function. This code is still working – GolangMan Feb 02 '21 at 21:40
0

teste is a function not a class. If you want to have attributes on teste, make it a class like this:

class teste:
    def __init__():
        tweets = api.home_timeline(2, read_id(FILE_NAME), tweet_mode = 'extended')
        # ...

Call it like this:

teste_instance = teste()

Access teste_instance.x inside of teste by referencing self.x.

Seth
  • 2,214
  • 1
  • 7
  • 21