Python Bootcamp 100 Days of Code

Honda.putha

Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    if coffee_maker.is_resource_sufficient(drink):
    me method eka danna.
    Thank you. But I have already used that.

    else:
    drink = menu.find_drink(user_choice)
    if coffee_maker.is_resource_sufficient(drink):
    if money_machine.make_payment(drink.cost):
    coffee_maker.make_coffee(drink)

    But
    make_coffee(order)
    and
    make_payment(cost)

    don't reduce the available ingredients and addition of the profit as described in the DB.
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    Thank you. But I have already used that.

    else:
    drink = menu.find_drink(user_choice)
    if coffee_maker.is_resource_sufficient(drink):
    if money_machine.make_payment(drink.cost):
    coffee_maker.make_coffee(drink)

    But
    make_coffee(order)
    and
    make_payment(cost)

    don't reduce the available ingredients and addition of the profit as described in the DB.
    coffee_maker.is_resource_sufficient(drink):
    oya method eke code eka danna kiyala ban kiwwe.
     
    • Like
    Reactions: Honda.putha

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    ==================================================================================

    Gotcha!

    The problem is I create objects inside the while loop, then every time the operation come to the first lines it creates new objects.
    So the values, the resources dictionary is always a new one.

    Erroneous code.
    Python:
    is_on = True
    
    while is_on:
        menu = Menu()
        coffee_maker = CoffeeMaker()
        money_machine = MoneyMachine()
        user_choice = input(f"What item do you want to order? {menu.get_items()} : ")
    
        if user_choice == 'report':
            coffee_maker.report()
            money_machine.report()
        elif user_choice == 'off':
            is_on = False
        else:
            drink = menu.find_drink(user_choice)
            if coffee_maker.is_resource_sufficient(drink):
                if money_machine.make_payment(drink.cost):
                    coffee_maker.make_coffee(drink)

    Corrected code.

    Python:
    menu = Menu()
    coffee_maker = CoffeeMaker()
    money_machine = MoneyMachine()
    is_on = True
    
    while is_on:
    
        user_choice = input(f"What item do you want to order? {menu.get_items()} : ")
    
        if user_choice == 'report':
            coffee_maker.report()
            money_machine.report()
        elif user_choice == 'off':
            is_on = False
        else:
            drink = menu.find_drink(user_choice)
            if coffee_maker.is_resource_sufficient(drink):
                if money_machine.make_payment(drink.cost):
                    coffee_maker.make_coffee(drink)

    #Day 18 finished.
    ------ Post added on Jul 15, 2021 at 9:23 AM
     
    • Like
    Reactions: MihiCherub

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    #Day_26 - Intermediate
    List Comprehension and the NATO Alphabet (1hr 2min)

    මේක නම් ගොඩක් වටිනව. මම card deck එක හදල තිබ්බෙ මේ විදියට. මේක ඊයෙ කලේ. update කරන්න බැරි උනා.


    #Day_27 - Intermediate
    *args, **kwargs

    Rest skipped..


    #Day_28 - Intermediate
    Tkinter, Dynamic Typing and the Pomodoro GUI Application

    Skipped

    Day 29 - Intermediate
    Building a Password Manager GUI App with Tkinter

    Skipped


    Tkinter එච්චර වැදගත් නෑ. ඒ නිසා skip කලා lesson ඔක්කොම.. Tkinter වඩා PyQt5 හොදයි..


    #Day_30 - Intermediate
    Errors, Exceptions and JSON Data

    Skipped Password Improving GUI
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    ekata Qt danaganna one neda?
    Yes. Qt5 designer tool එක ඕනෙ. හැබැයි python GUI ඉගෙන ගන්න ඕනෙ නෑ බන්. python වලින් UI development ඉගෙනගෙන තේරුමක් නෑ. පට්ට අමරුයි. හෙන වදයක් විදින්න ඕනෙ. ඊට වඩා ලේසියෙන් GUI හදන්න පුලුවන් වෙන ලැන්ග්වෙජ්ස් වලින්.
     

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    Yes. Qt5 designer tool එක ඕනෙ. හැබැයි python GUI ඉගෙන ගන්න ඕනෙ නෑ බන්. python වලින් UI development ඉගෙනගෙන තේරුමක් නෑ. පට්ට අමරුයි. හෙන වදයක් විදින්න ඕනෙ. ඊට වඩා ලේසියෙන් GUI හදන්න පුලුවන් වෙන ලැන්ග්වෙජ්ස් වලින්.
    ඒ කියන්නෙ Qt වලින් GUI එක ඩිසයින් කරලා පයිතන් එකෙන් බැක් එන්ඩ් එක ගහනවා වගේ දෙයක්ද?

    එහෙමනම් PyQt ඉගෙන ගන්න ඕනෙ. මුලින් පයිතන් ටික ඉවර කරලා ඒක පටන් ගන්නවා.

    ඒ අතර දවස ගානෙ Qt ටිකක් කරගන්න තිබ්බා නම් හරි.
     
    • Like
    Reactions: shenat

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    ඒ කියන්නෙ Qt වලින් GUI එක ඩිසයින් කරලා පයිතන් එකෙන් බැක් එන්ඩ් එක ගහනවා වගේ දෙයක්ද?

    එහෙමනම් PyQt ඉගෙන ගන්න ඕනෙ. මුලින් පයිතන් ටික ඉවර කරලා ඒක පටන් ගන්නවා.

    ඒ අතර දවස ගානෙ Qt ටිකක් කරගන්න තිබ්බා නම් හරි.
    ඔවු. වැඩේ තියෙන්නෙ එකම ide එකේ ඕක කරන්න බෑ. pyqt5 වලින් මුලින් ඩිසයින් කරල ෆයිල් එක generate කරගන්න ඕනෙ. ඊට පස්සෙ ඒකට action listener events වෙනම ලියන්න ඕනෙ. ඔය අතරෙ ui එකේ edit එකක් ගහන්න ඕනෙ උනොත් ආයිත් වෙනම qt5 open කරල ui file එක edit කරල generate කරල pycharm දාගෙන code කරන්න ඕනෙ. ඕක මල ඇනයක්.
    එකම ide එකක ui design කරන්නයි කෝඩ් කරන්නයි පුලුවන් කියල තිබ්බ. මම වැඩිය හෙව්වෙ නෑ.
     

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    Finished the Day 19.

    The turtle race.

    1626482579868.jpeg
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    #Day_31 - Intermediate
    Flash Card App Capstone Project (51min)
    Skipped



    #Day_32
    Intermediate+ Send Email (smtplib) & Manage Dates (datetime) (1hr 2min)

    Gmail 2 step verification enable කරල නම් app password එකක් හදා ගන්න පුලුවන් yahoo email වගේම.
    manage your google account --> security --> 2 step verification settings වල තියෙනව
     
    • Like
    Reactions: Honda.putha

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    #Day_31 - Intermediate
    Flash Card App Capstone Project (51min)
    Skipped



    #Day_32
    Intermediate+ Send Email (smtplib) & Manage Dates (datetime) (1hr 2min)

    Gmail 2 step verification enable කරල නම් app password එකක් හදා ගන්න පුලුවන් yahoo email වගේම.
    manage your google account --> security --> 2 step verification settings වල තියෙනව
    Seems like you are skipping a lot. Any reason?
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    Seems like you are skipping a lot. Any reason?
    tkinter වැඩක් නෑ බන්. ඕව පුරුදු උනා කියල practically use කරන්න බෑ. මොකද practical projects way beyond. heavy GUI තියෙද්දි ඒව editor එකේ code කර කර හදන්න බෑ. drag and drop with properties ඕනෙ. ඉතින් කාලෙ කන එක තේරුමක් නෑ.

    උදාහරනෙකට ගත්තොත් දැන් මන් Java වලින් email දහස් ගානක් commercially යවල ඇති. හැබැයි මට java වලින් දැන් scratch වල ඉදන් code එකක් ලියල email එකක් යවන්න කිව්වොත් පටන් ගන්නවත් දන්නෙ නෑ. හිතාගන්නත් බෑ. මම කරන්නෙ modules හදාගන්න එක. ඕනෙ වෙලාවට class එක හදල method එක call කරනව. සරලයි. Dr.Angela ගෙ code exercise වල උනත් මම එකම දේ කරන්නෙ නෑ. වැඩක් නෑ බන්. අපි Java එක්ක එක මාසයක් ඔට්ටු උන ගමන් python ඔක්කොම අමතක වෙනව. ඒ නිසා පුලුවන් තරම් modules හදල method call එක තමා ලේසිම වැඩේ.

    ex-- අද lesson එකෙන් මම මේ basic email module එකක් හදා ගත්ත. දැන් google කරකර මේකට attachment ගොඩක් දාන්නෙ කොහොමද, recipients ගොඩක් දාන්නෙ කොහොමද කියල බලනව. ඒව lesson එකේ නෑ. ඊට පස්සෙ ඒ ඔක්කොම method ටික හදාගෙන module එක built කරගන්නව. පස්සෙ දවසක email එකක් python වලින් යවන්න ඕනෙ උනොත් attachment එකක් කරලද, නැතුවද recipients ගොඩක්ද, එකෙක්ද එ ඔක්කොටම methods ටික තියෙනව. 1min වලින් වැඩේ ගොඩ.
    Python:
    import smtplib
    
    def send_gmail(user, password, recepient, subject, msg):
        with smtplib.SMTP("smtp.gmail.com") as connection:
            connection.starttls()
            connection.login(user=user, password=password)
            connection.sendmail(from_addr=user, to_addrs=recepient,
                                msg=f"Subject:{subject}\n\n{msg}")
     

    Honda.putha

    Well-known member
  • Dec 26, 2017
    17,242
    29,818
    113
    tkinter වැඩක් නෑ බන්. ඕව පුරුදු උනා කියල practically use කරන්න බෑ. මොකද practical projects way beyond. heavy GUI තියෙද්දි ඒව editor එකේ code කර කර හදන්න බෑ. drag and drop with properties ඕනෙ. ඉතින් කාලෙ කන එක තේරුමක් නෑ.

    උදාහරනෙකට ගත්තොත් දැන් මන් Java වලින් email දහස් ගානක් commercially යවල ඇති. හැබැයි මට java වලින් දැන් scratch වල ඉදන් code එකක් ලියල email එකක් යවන්න කිව්වොත් පටන් ගන්නවත් දන්නෙ නෑ. හිතාගන්නත් බෑ. මම කරන්නෙ modules හදාගන්න එක. ඕනෙ වෙලාවට class එක හදල method එක call කරනව. සරලයි. Dr.Angela ගෙ code exercise වල උනත් මම එකම දේ කරන්නෙ නෑ. වැඩක් නෑ බන්. අපි Java එක්ක එක මාසයක් ඔට්ටු උන ගමන් python ඔක්කොම අමතක වෙනව. ඒ නිසා පුලුවන් තරම් modules හදල method call එක තමා ලේසිම වැඩේ.

    ex-- අද lesson එකෙන් මම මේ basic email module එකක් හදා ගත්ත. දැන් google කරකර මේකට attachment ගොඩක් දාන්නෙ කොහොමද, recipients ගොඩක් දාන්නෙ කොහොමද කියල බලනව. ඒව lesson එකේ නෑ. ඊට පස්සෙ ඒ ඔක්කොම method ටික හදාගෙන module එක built කරගන්නව. පස්සෙ දවසක email එකක් python වලින් යවන්න ඕනෙ උනොත් attachment එකක් කරලද, නැතුවද recipients ගොඩක්ද, එකෙක්ද එ ඔක්කොටම methods ටික තියෙනව. 1min වලින් වැඩේ ගොඩ.
    Python:
    import smtplib
    
    def send_gmail(user, password, recepient, subject, msg):
        with smtplib.SMTP("smtp.gmail.com") as connection:
            connection.starttls()
            connection.login(user=user, password=password)
            connection.sendmail(from_addr=user, to_addrs=recepient,
                                msg=f"Subject:{subject}\n\n{msg}")
    Practical නැත්නම් කෝස් එකට දාලා තියෙන්නෙ බේසික් කියා දෙන්න හින්දා වෙන්න පුලුවන්. මම නම් ස්කිප් කරන්න අදහසක් නෑ. බලමු. කොහොමත් මෙතෙක් තිබ්බ කෝඩ් වලත් Dr. Angela ලියනවට වඩා කෙටියෙන් කෝඩ් එක ගහන්න පුලුවන් තැන් තිබුනා.

    මොඩියුල් හදන එක නම් හොඳ වැඩක්. වැඩේ තියෙන්නෙ මොඩියුල් ටිකත් මතක තියෙයිද කියන එක. :lol::lol:

    මාත් බලන්න ඕනෙ මොඩියුල් හදන වැඩේ ටිකක් ඉස්සරහට ගියාම.

    මම කලින් Java කලා කලා. ඊටපස්සෙ .net පටන් ගත්තා. දැන් C++ . කලින් එව්වල(Java, .Net) මෙලෝ මල දානයක් මතක නෑ.
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    email module එකක් හැදුව. කට්ටිය තව improve කරල ගන්න.
    Python:
    import smtplib
    import ssl
    from email import encoders
    from email.mime.base import MIMEBase
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    port = 465  # For SSL
    
    # Create a secure SSL context
    context = ssl.create_default_context()
    
    
    def send_gmail(user, password, recipients, subject, msg):
        # with smtplib.SMTP("smtp.gmail.com") as connection:
        with smtplib.SMTP_SSL("smtp.gmail.com", port=port, context=context) as connection:
            # connection.starttls()
            connection.login(user=user, password=password)
            connection.sendmail(from_addr=user, to_addrs=recipients,
                                msg=f"Subject:{subject}\n\n{msg}")
    
    
    def send_gmail_rich_text(user, password, recipients, subject, text="", html=""):
        message = MIMEMultipart()
        message["Subject"] = subject
        message["From"] = user
    
        if isinstance(recipients, str):
            message["To"] = recipients
        else:
            message["To"] = ", ".join(recipients)
    
        # Turn these into plain/html MIMEText objects
        text_part = MIMEText(text, "plain")
        html_part = MIMEText(html, "html")
    
        # Add HTML/plain-text parts to MIMEMultipart message
        # The email client will try to render the last part first
        message.attach(text_part)
        message.attach(html_part)
    
        with smtplib.SMTP_SSL("smtp.gmail.com", port=port, context=context) as connection:
            connection.login(user=user, password=password)
            connection.sendmail(from_addr=user, to_addrs=recipients, msg=message.as_string())
    
    
    def send_gmail_attachments(user, password, recipients, subject, file_name, text="", html=""):
        message = MIMEMultipart()
        message["Subject"] = subject
        message["From"] = user
        if isinstance(recipients, str):
            message["To"] = recipients
            message["Bcc"] = recipients  # Recommended for mass emails
        else:
            receivers = ", ".join(recipients)
            message["To"] = receivers
            message["Bcc"] = receivers  # Recommended for mass emails
    
        text_part = MIMEText(text, "plain")
        html_part = MIMEText(html, "html")
    
        message.attach(text_part)
        message.attach(html_part)
    
        with open(file_name, "rb") as attachment:
            # Add file as application/octet-stream
            # Email client can usually download this automatically as attachment
            part = MIMEBase("application", "octet-stream")
            part.set_payload(attachment.read())
    
        # Encode file in ASCII characters to send by email
        encoders.encode_base64(part)
    
        # Add header as key/value pair to attachment part
        part.add_header(
            "Content-Disposition",
            f"attachment; filename= {file_name}",
        )
    
        # Add attachment to message
        message.attach(part)
    
        with smtplib.SMTP_SSL("smtp.gmail.com", port=port, context=context) as connection:
            connection.login(user=user, password=password)
            connection.sendmail(from_addr=user, to_addrs=recipients, msg=message.as_string())

    repl එක fork කරගන්න ඕන අය
    https://replit.com/@tharindumihi/emailhandler#email_handler.py
     
    • Like
    Reactions: shenat

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    #Day_34
    Intermediate+ API Practice - Creating a GUI Quiz App (55min)
    Skipped Class based Tkinter UI

    python වල ලොකුම අවුල compile time එකේ errors අහුවෙන්නෙ නැති එකනෙ. ගොඩක් අහුවෙන්නෙ runtime එකේ. මන් මේ කිරියෙම ගොඩ දෙනෙක්ගෙන් ඇහුව මේ ප්‍රශ්නෙ විසඳගන්න හැටි. කාලගවත් හොඳ උත්තරයක් තිබ්බෙ නෑ. ගොඩක් අය කිව්වෙ එහෙම බෑ දන්නෙ නෑ වගේ උත්තර.

    මේකට solution එකක් අද lesson එකේ තියෙනව. data hint.
    දැන් වැඩේ හරි java වගේම තමා. complex codes ලියන අවස්තාවල ඒ විදියට කරානම් ලේසි වෙනව. compile time එකේම දැන් ගොඩක් ප්‍රශන් විසඳෙනව.
    දෙයක් පිලිවෙලට ඉගෙන ගන්න එකේ වටිනාකම. ගොඩක් ප්‍රශ්න විසදෙනව වගේම ලේසි ක්‍රම අහුවෙනව.