'How to minimise many if conditions in a program to use DRY concept?
I have a program that has a few if blocks. I want to use DRY concept as am using same code repeating in those blocks, but different conditions.
Please check the below blocks. I'm using functions in each block that are repeating too. Any idea to code once instead of so many blocks?
if today.day == 2: #chose the date until the code is ready
img_1, img_2, filename = grab_images(images = images, movie_title = movie_title)
tweet(status = "Will this be a good watch❓ ... Read the plot👇🧵.", show_update = show_update, img_1= img_1, img_2 = img_2, movie_title = movie_title,api = api)
time.sleep(2)
##Delete images
os.remove("watchmovies/Images/"+"01"+filename+".jpg")
os.remove("watchmovies/Images/"+"02"+filename+".jpg")
tweet_reply(status = "w", story = final_story, api = api)
time.sleep(2)
second_tweet_reply(status = "w🕰: ", rating_comment = rating_comment, movie_duration = movie_duration, api = api)
time.sleep(2)
third_reply_reviews(status = "w", liners = liners, api = api)
time.sleep(2)
if release_date.day == today.day: #thinking to take this if block out of the program, as release_date if not available will cause issue.
img_1, img_2, filename = grab_images(images = images, movie_title = movie_title)
tweet(status = "-- Is releasing today. Are you excited to watch❓. Here is the plot and rating👇", img_1= img_1, img_2 = img_2, movie_title = movie_title, api = api)
time.sleep(2)
##Delete images
os.remove("watchmovies/Images/"+"01"+filename+".jpg")
os.remove("watchmovies/Images/"+"02"+filename+".jpg")
tweet_reply(status = "", story = final_story, api = api)
time.sleep(2)
second_tweet_reply(status = "🕰 ", rating_comment = rating_comment, movie_duration = movie_duration, api = api)
time.sleep(2)
movie_releasing, todays_date = strip_date_add(edit_date = str(release_date), day = 1)
if movie_releasing == todays_date:
img_1, img_2, filename = grab_images(images = images, movie_title = movie_title)
tweet(status = "-- Was released yesterday. Have you watched it❓ ... read the plot 👇.", img_1= img_1, img_2 = img_2, movie_title = movie_title, api = api)
time.sleep(2)
##Delete images
os.remove("watchmovies/Images/"+"01"+filename+".jpg")
os.remove("watchmovies/Images/"+"02"+filename+".jpg")
tweet_reply(status = "", story = final_story, api = api)
time.sleep(2)
second_tweet_reply(status = "🕰 ", rating_comment = rating_comment, movie_duration = movie_duration, api = api)
time.sleep(2)
movie_releasing, todays_date = strip_date_minus(edit_date = str(release_date), day = 7)
if movie_releasing == todays_date:
img_1, img_2, filename = grab_images(images = images, movie_title = movie_title)
tweet(status = "-- Is releasing next week. Read plot and rating 👇.", img_1= img_1, img_2 = img_2, movie_title = movie_title, api = api)
time.sleep(2)
##Delete images
os.remove("watchmovies/Images/"+"01"+filename+".jpg")
os.remove("watchmovies/Images/"+"02"+filename+".jpg")
tweet_reply(status = "", story = final_story, api = api)
time.sleep(2)
second_tweet_reply(status = "🕰 ", rating_comment = rating_comment, movie_duration = movie_duration, api = api)
time.sleep(2)
movie_releasing, todays_date = strip_date_minus(edit_date = str(release_date), day = 1)
if movie_releasing == todays_date:
img_1, img_2, filename = grab_images(images = images, movie_title = movie_title)
tweet(status = "-- Is releasing tomorrow. Do you plan to watch❓ Here is the plot and rating 👇.", img_1= img_1, img_2 = img_2, movie_title = movie_title, api = api)
time.sleep(2)
##Delete images
os.remove("watchmovies/Images/"+"01"+filename+".jpg")
os.remove("watchmovies/Images/"+"02"+filename+".jpg")
tweet_reply(status = "", story = final_story, api = api)
time.sleep(2)
second_tweet_reply(status = "🕰 ", rating_comment = rating_comment, movie_duration = movie_duration, api = api)
time.sleep(2)
The program needs to execute all the tweets in all blocks after meeting its condition.
Solution 1:[1]
You did not show the implementation for your tweet functions but maybe you can make a Tweet class to encapsulate the Tweet details. Something along these lines:
class Tweet:
status = None
story = None
img_1 = None
img_2 = None
def process(list_of_tweets):
img_1, img_2, filename = grab_images(images = images, movie_title = movie_title)
tweet(list_of_tweets[0])
os.remove("watchmovies/Images/"+"01"+filename+".jpg")
os.remove("watchmovies/Images/"+"02"+filename+".jpg")
tweet_reply(list_of_tweets[1]))
second_tweet_reply(list_of_tweets[2]))
third_reply_reviews(list_of_tweets[3]))
if today.day == 2: #chose the date until the code is ready
tweets = [
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
]
process(tweets)
if release_date.day == today.day: #thinking to take this if block out of the program, as release_date if not available will cause issue.
tweets = [
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
]
process(tweets)
movie_releasing, todays_date = strip_date_add(edit_date = str(release_date), day = 1)
if movie_releasing == todays_date:
tweets = [
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
]
process(tweets)
movie_releasing, todays_date = strip_date_minus(edit_date = str(release_date), day = 7)
if movie_releasing == todays_date:
tweets = [
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
]
process(tweets)
movie_releasing, todays_date = strip_date_minus(edit_date = str(release_date), day = 1)
if movie_releasing == todays_date:
tweets = [
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
Tweet(status=..., story=...),
]
process(tweets)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amer |
