Random Quote Generator with Date and Theme

  • Share this:

Code introduction


This function generates a random quote based on the provided date and an optional theme.


Technology Stack : Typer, Rich, Random

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
from typer import Typer, Argument, Option
from rich.console import Console
from random import choice
import datetime

def random_quote():
    quotes = [
        "The only way to do great work is to love what you do.",
        "Success is not final, failure is not fatal: It is the courage to continue that counts.",
        "The best way to predict the future is to create it.",
        "The future belongs to those who believe in the beauty of their dreams."
    ]
    return choice(quotes)

def xxx(date, theme=None):
    """
    This function generates a random quote based on a given date and an optional theme.
    """
    app = Typer()
    @app.command()
    def generate_quote(date: str, theme: str = None):
        quote = random_quote()
        if theme:
            quote += f" - {theme}"
        print(f"Today's quote for {date}: {quote}")

    app.run(date, theme)

    # Output for Rich Console
    console = Console()
    console.print(f"Today's quote for {date}: {quote}")

    # JSON Output
    json_output = {
        "type": "Python Function",
        "hard": "中级",
        "explain": "这个函数根据输入的日期和可选的主题生成一条随机引用。",
        "tench": "Typer, Rich, Random",
        "explain_en": "This function generates a random quote based on the provided date and an optional theme.",
        "tench_en": "Typer, Rich, Random"
    }

    return json_output

# Example usage:
# xxx("2023-04-01", "Innovation")                
              
Tags: