Как я могу обновить переменную в запланированном событии, используя Python? - PullRequest
0 голосов
/ 26 мая 2020

Я использую twilio и планировщик для ежедневной отправки случайных текстовых сообщений из списка. Я могу отправлять тексты через заданные промежутки времени, но не могу сделать так, чтобы он обновлял переменную и отправлял другое сообщение. Как я могу обновить переменную случайным образом в расписании? Вот код, который у меня есть:

import schedule
import time
from twilio.rest import Client
from Positivequotelist import choosequote    

def runjob(func, arg1, arg2):
    schedule.every(10).seconds.do(func, number=arg1, message=arg2)
    while True:
        schedule.run_pending()
        time.sleep(1)

def sendtext(number,message):

    account_sid = '(omitted)'
    auth_token = '(omitted)'
    client = Client(account_sid, auth_token)

    message = client.messages \
        .create(
             body=message,
             messaging_service_sid='(omitted)',
             to='+1'+str(number)
         )

if __name__=="__main__":
    quotelist=["Successful people don’t become that way overnight."\
           "What most people see at a glance—happiness, wealth, a great career, purpose—is the result of hard work and hustle over time",
           "Your limitation—it’s only your imagination.",
           "Push yourself, because no one else is going to do it for you.",
           "Sometimes later becomes never. Do it now.",
           " Great things never come from comfort zones.",
           "Dream it. Wish it. Do it",
           "Success doesn’t just find you. You have to go out and get it.",
           "The harder you work for something, the greater you’ll feel when you achieve it.",
           "Dream bigger. Do bigger.",
           "Don’t stop when you’re tired. Stop when you’re done.",
           "You’re off to great places, today is your day. Your mountain is waiting, so get on your way",
           "Wake up with determination. Go to bed with satisfaction.",
           "Do something today that your future self will thank you for.",
           "The Pessimist Sees Difficulty In Every Opportunity. The Optimist Sees Opportunity In Every Difficulty",
           "Don’t Let Yesterday Take Up Too Much Of Today.",
           "All our dreams can come true, if we have the courage to pursue them",
           "The secret of getting ahead is getting started",
           "The best time to plant a tree was 20 years ago. The second best time is now",
           "Only the paranoid survive.",
           "It’s hard to beat a person who never gives up",
           "I wake up every morning and think to myself, ‘how far can I push this company in the next 24 hours",
           "If people are doubting how far you can go, go so far that you can’t hear them anymore.",
           "Write it. Shoot it. Publish it. Crochet it, sauté it, whatever. MAKE",
           "You’ve gotta dance like there’s nobody watching, love like you’ll never be hurt, sing like there’s nobody listening, and live like it’s heaven on earth",
           "Fairy tales are more than true: not because they tell us that dragons exist, but because they tell us that dragons can be beaten",
           "Everything you can imagine is real",
           "Do one thing every day that scares you",
           "It’s no use going back to yesterday, because I was a different person then.",
           "Smart people learn from everything and everyone, average people from their experiences, stupid people already have all the answers",
           "Do what you feel in your heart to be right – for you’ll be criticized anyway.",
           "Happiness is not something ready made. It comes from your own actions.",
           "Whatever you are, be a good one",
           "The same boiling water that softens the potato hardens the egg. It’s what you’re made of. Not the circumstances",
           "If we have the attitude that it’s going to be a great day it usually is",
           "You can either experience the pain of discipline or the pain of regret. The choice is yours.",
           "Impossible is just an opinion.",
           "Your passion is waiting for your courage to catch up",
           "Magic is believing in yourself. If you can make that happen, you can make anything happen.",
           "If something is important enough, even if the odds are stacked against you, you should still do it.",
           "Hold the vision, trust the process.",
           "Don’t be afraid to give up the good to go for the great",
           "People who wonder if the glass is half empty or full miss the point. The glass is refillable",
           "No one is to blame for your future situation but yourself. If you want to be successful, then become Successful",
           "Things may come to those who wait, but only the things left by those who hustle",
           "Everything comes to him who hustles while he waits.",
           "Invest in your dreams. Grind now. Shine later.",
           "Hustlers don’t sleep, they nap.",
           "Greatness only comes before hustle in the dictionary",
           "Without hustle, talent will only carry you so far",
           "Work like there is someone working twenty four hours a day to take it away from you",
           "Hustle in silence and let your success make the noise."]
    chosen = choosequote(quotelist)
    runjob(sendtext, (omitted), chosen)

Он работает, но каждый раз отправляет одну и ту же цитату. Любая помощь будет оценена.

Обновление: это функция selectquote:

def choosequote(listofquotes):
    randomquote=random.randint(0, len(listofquotes)-1)

    chosenquote=listofquotes.pop(randomquote)

    return chosenquote

Ответы [ 2 ]

2 голосов
/ 26 мая 2020

Вместо этого вы можете использовать random, чтобы выбрать случайную цитату в функции отправки

import schedule
import time
from twilio.rest import Client
import random

def runjob(func, arg1, arg2):
    schedule.every(10).seconds.do(func, number=arg1, quotelist=arg2)
    while True:
        schedule.run_pending()
        time.sleep(1)

def sendtext(number, quotelist):
    # Get a random quote from the list
    random_quote = random.choice(quotelist)
    account_sid = '(omitted)'
    auth_token = '(omitted)'
    client = Client(account_sid, auth_token)

    message = client.messages.create(
        body=random_quote,
        messaging_service_sid='(omitted)',
        to='+1'+str(number)
    )

if __name__ == "__main__":
    quotelist = [
        "Successful people don’t become that way overnight."\
        "What most people see at a glance—happiness, wealth, a great career, purpose—is the result of hard work and hustle over time",
        "Your limitation—it’s only your imagination.",
        "Push yourself, because no one else is going to do it for you.",
        "Sometimes later becomes never. Do it now.",
        " Great things never come from comfort zones.",
        "Dream it. Wish it. Do it",
        "Success doesn’t just find you. You have to go out and get it.",
        "The harder you work for something, the greater you’ll feel when you achieve it.",
        "Dream bigger. Do bigger.",
        "Don’t stop when you’re tired. Stop when you’re done.",
        "You’re off to great places, today is your day. Your mountain is waiting, so get on your way",
        "Wake up with determination. Go to bed with satisfaction.",
        "Do something today that your future self will thank you for.",
        "The Pessimist Sees Difficulty In Every Opportunity. The Optimist Sees Opportunity In Every Difficulty",
        "Don’t Let Yesterday Take Up Too Much Of Today.",
        "All our dreams can come true, if we have the courage to pursue them",
        "The secret of getting ahead is getting started",
        "The best time to plant a tree was 20 years ago. The second best time is now",
        "Only the paranoid survive.",
        "It’s hard to beat a person who never gives up",
        "I wake up every morning and think to myself, ‘how far can I push this company in the next 24 hours",
        "If people are doubting how far you can go, go so far that you can’t hear them anymore.",
        "Write it. Shoot it. Publish it. Crochet it, sauté it, whatever. MAKE",
        "You’ve gotta dance like there’s nobody watching, love like you’ll never be hurt, sing like there’s nobody listening, and live like it’s heaven on earth",
        "Fairy tales are more than true: not because they tell us that dragons exist, but because they tell us that dragons can be beaten",
        "Everything you can imagine is real",
        "Do one thing every day that scares you",
        "It’s no use going back to yesterday, because I was a different person then.",
        "Smart people learn from everything and everyone, average people from their experiences, stupid people already have all the answers",
        "Do what you feel in your heart to be right – for you’ll be criticized anyway.",
        "Happiness is not something ready made. It comes from your own actions.",
        "Whatever you are, be a good one",
        "The same boiling water that softens the potato hardens the egg. It’s what you’re made of. Not the circumstances",
        "If we have the attitude that it’s going to be a great day it usually is",
        "You can either experience the pain of discipline or the pain of regret. The choice is yours.",
        "Impossible is just an opinion.",
        "Your passion is waiting for your courage to catch up",
        "Magic is believing in yourself. If you can make that happen, you can make anything happen.",
        "If something is important enough, even if the odds are stacked against you, you should still do it.",
        "Hold the vision, trust the process.",
        "Don’t be afraid to give up the good to go for the great",
        "People who wonder if the glass is half empty or full miss the point. The glass is refillable",
        "No one is to blame for your future situation but yourself. If you want to be successful, then become Successful",
        "Things may come to those who wait, but only the things left by those who hustle",
        "Everything comes to him who hustles while he waits.",
        "Invest in your dreams. Grind now. Shine later.",
        "Hustlers don’t sleep, they nap.",
        "Greatness only comes before hustle in the dictionary",
        "Without hustle, talent will only carry you so far",
        "Work like there is someone working twenty four hours a day to take it away from you",
        "Hustle in silence and let your success make the noise."
    ]
    # pass the whole list and then use random.choice to get a random quote every time
    runjob(sendtext, (omitted), quotelist)
0 голосов
/ 28 мая 2020

Мне удалось заставить его работать, отредактировав свои функции runjob и sendtext:

def sendtext(number):
#now only takes one parameter for the number
    quotelist=["Successful people don’t become that way overnight."\
           "What most people see at a glance—happiness, wealth, a great career, purpose—is the result of hard work and hustle over time",
           "Your limitation—it’s only your imagination.",
           "Push yourself, because no one else is going to do it for you.",
           "Sometimes later becomes never. Do it now.",
           " Great things never come from comfort zones.",
           "Dream it. Wish it. Do it",
           "Success doesn’t just find you. You have to go out and get it.",
           "The harder you work for something, the greater you’ll feel when you achieve it.",
           "Dream bigger. Do bigger.",
           "Don’t stop when you’re tired. Stop when you’re done.",
           "You’re off to great places, today is your day. Your mountain is waiting, so get on your way",
           "Wake up with determination. Go to bed with satisfaction.",
           "Do something today that your future self will thank you for.",
           "The Pessimist Sees Difficulty In Every Opportunity. The Optimist Sees Opportunity In Every Difficulty",
           "Don’t Let Yesterday Take Up Too Much Of Today.",
           "All our dreams can come true, if we have the courage to pursue them",
           "The secret of getting ahead is getting started",
           "The best time to plant a tree was 20 years ago. The second best time is now",
           "Only the paranoid survive.",
           "It’s hard to beat a person who never gives up",
           "I wake up every morning and think to myself, ‘how far can I push this company in the next 24 hours",
           "If people are doubting how far you can go, go so far that you can’t hear them anymore.",
           "Write it. Shoot it. Publish it. Crochet it, sauté it, whatever. MAKE",
           "You’ve gotta dance like there’s nobody watching, love like you’ll never be hurt, sing like there’s nobody listening, and live like it’s heaven on earth",
           "Fairy tales are more than true: not because they tell us that dragons exist, but because they tell us that dragons can be beaten",
           "Everything you can imagine is real",
           "Do one thing every day that scares you",
           "It’s no use going back to yesterday, because I was a different person then.",
           "Smart people learn from everything and everyone, average people from their experiences, stupid people already have all the answers",
           "Do what you feel in your heart to be right – for you’ll be criticized anyway.",
           "Happiness is not something ready made. It comes from your own actions.",
           "Whatever you are, be a good one",
           "The same boiling water that softens the potato hardens the egg. It’s what you’re made of. Not the circumstances",
           "If we have the attitude that it’s going to be a great day it usually is",
           "You can either experience the pain of discipline or the pain of regret. The choice is yours.",
           "Impossible is just an opinion.",
           "Your passion is waiting for your courage to catch up",
           "Magic is believing in yourself. If you can make that happen, you can make anything happen.",
           "If something is important enough, even if the odds are stacked against you, you should still do it.",
           "Hold the vision, trust the process.",
           "Don’t be afraid to give up the good to go for the great",
           "People who wonder if the glass is half empty or full miss the point. The glass is refillable",
           "No one is to blame for your future situation but yourself. If you want to be successful, then become Successful",
           "Things may come to those who wait, but only the things left by those who hustle",
           "Everything comes to him who hustles while he waits.",
           "Invest in your dreams. Grind now. Shine later.",
           "Hustlers don’t sleep, they nap.",
           "Greatness only comes before hustle in the dictionary",
           "Without hustle, talent will only carry you so far",
           "Work like there is someone working twenty four hours a day to take it away from you",
           "Hustle in silence and let your success make the noise."]
    message1 = choosequote(quotelist)
    account_sid = '(omitted)'
    auth_token = '(omitted)'
    client = Client(account_sid, auth_token)

    message = client.messages \
        .create(
             body=message1,
             messaging_service_sid='(omitted)',
             to='+1'+str(number)
         )

def runjob(func, arg1):
    schedule.every().day.at("14:00").do(func, number=arg1)
    while True:
        schedule.run_pending()
        time.sleep(1)

Затем я запустил его, используя:

if __name__=="__main__":
    runjob(sendtext, (number omitted))

Добавив аргументы в runjob, это позволило мне передать функцию sendtext через функцию runjob с параметром для числа. Функция runjob теперь автоматически выбирает сообщение вместо использования функции selectquote, что позволяет мне каждый раз выбирать новую цитату.

Спасибо за помощь!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...