Python Forum
Need some help with strange situation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help with strange situation
#1
I have been working through writing a program for about a week now. Suddenly my inputs changed. I'm not sure why, but it has taken me down a different path. How can I write a test that gives my program both input types then will let me code to satisfy both requirements?

I guess this is like unit testing but I have not done any coding like that since college, and that was with Ruby on Rails which gave a continuous output and pass or fail message.

--- Yeah I don't even remember how I made that.

Any nudging in the right direction would be greatly appreciated. I would also be willing to pay a little if someone would spend some time with me walking me through my problem.

Thanks
Reply
#2
Can't provide an answer without code?
Quote:Suddenly my inputs changed
what inputs?
please post code.
list inputs and outputs including full verbatim error tracebacks
Reply
#3
This is it.

from imapclient import IMAPClient
import pyzmail
import base64
import re
import time
import os

test_num = []
happy_days = []
attachment_all = []

hostname = "mail.server.com"
username = "[email protected]"
mail_password = "xxxxxxxxxxxxxxxxxxx"
server = IMAPClient(hostname, use_uid=True, ssl=False)


def server_login():
    try:
        server.login(username, mail_password)
        server.select_folder('Inbox')

        
    except server.Error as e:
        print('Could not log in: ', e)


def get_messages_processed():
    message_ids = server.search(u'UNSEEN')
    if message_ids is None:
        return
    else:
        for messages in message_ids:
            raw_message = server.fetch([messages], ['BODY[]', 'FLAGS'])
            message = pyzmail.PyzMessage.factory(raw_message[messages][b'BODY[]'])
            temp_message = str(message.get_payload(1))
            test_num.append(temp_message)
        check_name = [x for x in test_num if "name=stockreport.txt" in x]
        for attachment in check_name:
            bingo = str(attachment.split('\n\n', 1)[1])
            happy_days.append(bingo)
        return happy_days


def prep_print():
    for item in happy_days:
        the_real_deal = str(base64.b64decode(item).decode("utf8"))
        # print(the_real_deal)
        testing_the_split = re.split('[\t\r\n]+', the_real_deal)
        # print(testing_the_split)
        # list_of_lists = [list(elem) for elem in the_real_deal]
        # print(list_of_lists)
        header_list = testing_the_split[:3]
        parts_list = testing_the_split[7:]
        print("_" * 45, file=open("output.txt", "a"))
        print("| Job Number: " + header_list[0],
              " " * (25 - len(header_list[0])) + "| ",
              file=open("output.txt", "a", encoding="utf-8"))
        print("| Job Name: " + header_list[1],
              " " * (18 - len(header_list[1])) + "| ",
              file=open("output.txt", "a"))
        print("| Job Location: " + header_list[2],
              " " * (17 - len(header_list[2])) + "| ",
              file=open("output.txt", "a"))
        print("*" * 45, file=open("output.txt", "a"))
        print("| Loc  | Part #", " " * (17) + "| Qty   |", file=open("output.txt", "a"))
        while parts_list:
            for p in parts_list:
                number_parts = parts_list[0]
                part_number = parts_list[1]
                part_qty = parts_list[3]
                part_dec = parts_list[2]
                print("| " + number_parts,
                      " " * (4 - len(number_parts)) + "| "
                      + part_number,
                      " " * (23 - len(part_number)) + "| "
                      + part_qty,
                      " " * (10 - len(part_qty)) + "|",
                      file=open("output.txt", "a+"))
                print("| Dec: " + part_dec, file=open("output.txt", "a+"))
                del parts_list[:3]
                print("there was " + header_list[0])
                return
    # os.system("C:/Users/Kingdel/PycharmProjects/ScannerMailHandler/printanddelete.exe")
    time.sleep(5)
Reply
#4
Your still not being specific about errors, nor showing them
see: https://python-forum.io/misc.php?action=help&hid=19
Reply
#5
Sorry

At this point I don't get any errors. Just wrong output or no output.

I have just figured out some emails come through as base64 and some do not. I currently filter out the ones that are not but I have no easy way to make the ones that are base64... Which is odd. So I'm stuck in my testing till I figure that part out.

(Oct-16-2018, 07:15 PM)Larz60+ Wrote: Your still not being specific about errors, nor showing them
see: https://python-forum.io/misc.php?action=help&hid=19

After many hours of back and forth on trying to figure out different aspects of the data I am working with, I have gotten part of it sorted out just a little bit ago and am now ready to make some changes to my approach.

Currently, the program processes the last email over and over 1 time for itself and 1 time for each new email. So if I send 10 emails I get 10 outputs of the 1st email.

The same goes for the parts. It keeps processing the last part that was processed over and over.

so my plan for change is as follows.

1. convert all messages to the same format
2. export all the data to JSON
3. Do my processing of the data for output

I'm not exactly sure where to start yet. As far as the actual how of what I will do. but to work, I go!!!

Again any help is greatly appreciated as this process has been very frustrating.
Reply
#6
Quote:
        while parts_list:
            for p in parts_list:
                # processing
                return
Why loop over the parts_list at all, if you're just going to process the first thing in it?

This seems like a good opportunity for you to try mocking the server's response, so you can test it without needing to log into the server every time. Besides making testing easier, that also means it's easier for us to help, since we don't have to guess what your input is.
Reply
#7
So what is the best way to do that? I'm guessing I could save the responses to some files. Then I just open the files up to test each part?

That seems to make sense to me right now.

I guess I should be able to do that pretty easy too as I just ask for the response and then feed it into a file. Not sure why I didn't think of that before!

Awesome thank you! I will let you know how it results
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020