Method to collates all errors for Conditions Checking

15 views
Skip to first unread message

likage

unread,
Oct 23, 2018, 4:18:52 PM10/23/18
to Python Programming for Autodesk Maya
Hi all, I am trying to see what is the best way that I can collate results into a QMessageBox.

This is my code:
test_dict = defaultdict(list)

# Old : msg = ""
msgA = msgB = msgC = None

# Inputs
if all((input_01, input_02)) == False:
    msgA = "One or both of the Input fields is/ are empty!"
elif input_01 == input_02:
    msgA = "Inputs used in both fields are the same!"
else:
    msgA = validate_inputs(
        (input_01, input_02)
    )
test_dict["Input Issue"].append(msgA)

# Frame Ranges
start = int(start_frame)
end = int(end_frame)
if (start > end) or (end < start):
    msgB = (
        "Value of Start/Min Frame is larger than the value of "
        "End/Max Frame."
    )
elif start == end:
    msgB = (
        "Frame Values are the same"
    )
test_dict["Frame Range Issue"].append(msgB)

# Selections
if user_selections:
    msgC = validate_user_selections(
        input_01,
        user_selections
    )
test_dict["Selections Issue"].append(msgC)

# Iterates and prints out all error at a go
if test_dict:
    err_popup = QtGui.QMessageBox()
    err_popup.setIcon(QtGui.QMessageBox.Critical)
    err_popup.setWindowTitle("Errors found!")
    err_popup.setText("Please rectify the following errors found.")

    err_popup.setDetailedText(
        "\n".join("{}\n * {}".format(k, '\n\t'.join(v)) for k, v in test_dict.items())
    )
    """
    # This will prints out in the following format if there are values found in each key

    Input Issue
        xxx
        xxx
    Frame Range Issue
        xxx
    Selections Issues
        xxx

    # If there are no values (no errors) in Frame Range, it will be ouputted as
    Input Issue
        xxx
        xxx
    Selections Issues
        xxx
    """

    err_popup.setStandardButtons(QtGui.QMessageBox.Ok)
    err_popup.exec_()

"""
# Old - Only prints one error that it found in a top-down manner
if msg:
    err_popup = QtGui.QMessageBox()
    err_popup.setIcon(QtGui.QMessageBox.Critical)
    err_popup.setWindowTitle("Errors found!")
    err_popup.setText(msg)
    err_popup.setStandardButtons(QtGui.QMessageBox.Ok)
    err_popup.exec_()
"""


In my Gui, there are a bunch of inputs, mainly QLineEdits and I have factored in some conditions checking so that if something is incorrectly inputted, it will prompts up a window.
In my old code, as you have seen, it will only prints and prompts the QMessageBox, one condition at a time. And so, say if there are 2 errors - "Inputs" and "Selections", it will only shows "Inputs"

Whereas, I am now trying to implement and have it iterated all the conditions and shows all errors at one go.

As such, is there a better way that I can go about doing this? While I am no Python expert but the use of `msgA`. `msgB`, `msgC` does not looks very nice in this context...

Many thanks in advance for any replies :)

Justin Israel

unread,
Oct 23, 2018, 4:38:49 PM10/23/18
to python_in...@googlegroups.com
The overall design of formatting detailed output for a QMessageBox seems normal to me. Although based just on this code example I am not really sure why you even need discrete msg{A,B,C} variables when you could just be appending string literals to the dictionary directly, or reusing the same msg variable. Still, not much to say about this since you are just building up error messages in a dictionary and formatting them. All good to me. 
If you are asking about the QMessageBox approach in general, I suppose there are other ways to represent invalid forms, such as coloring the bad input fields and enabling error text around them. You could even write a validating QLineEdit that can be set into an 'error' state with a message and knows how to display itself, and to clear itself once the value changes again.
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/d4a69a96-087e-489c-b294-0a07d609c67f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

likage

unread,
Oct 23, 2018, 5:09:09 PM10/23/18
to Python Programming for Autodesk Maya
Hi Justin, thanks for the reply.
I use msg{A,B,C} because if any one of the condition passed, the msg will get appended with the next in-line error.

Eg. If only frame range fails, my `test_dict` will return as follows:
{'Frame Range Issue': [None,
                       'Value of Start/Min Frame is larger than the value of End/Max Frame.'],
 'Namespace Issue': [None, None],
 'Selections Issue': [None,
                      'Value of Start/Min Frame is larger than the value of End/Max Frame.']}

And hence my usage of A,B,C to differentiate them as I can't reuse the same variable in which I am using defaultdict to differentiate them in this case.

appending string literals to the dictionary directly
Could you kindly elaborate more on this?

In general, I am trying to find out what is the best practice to go about doing them :)
 






Justin Israel

unread,
Oct 23, 2018, 5:30:15 PM10/23/18
to python_in...@googlegroups.com
On Wed, Oct 24, 2018 at 10:09 AM likage <dissid...@gmail.com> wrote:
Hi Justin, thanks for the reply.
I use msg{A,B,C} because if any one of the condition passed, the msg will get appended with the next in-line error.

Eg. If only frame range fails, my `test_dict` will return as follows:
{'Frame Range Issue': [None,
                       'Value of Start/Min Frame is larger than the value of End/Max Frame.'],
 'Namespace Issue': [None, None],
 'Selections Issue': [None,
                      'Value of Start/Min Frame is larger than the value of End/Max Frame.']}

And hence my usage of A,B,C to differentiate them as I can't reuse the same variable in which I am using defaultdict to differentiate them in this case.

I can only base my reply on your code example, which shows msg{A,B,C} being defaulted to  None, conditionally set to a message, and then always being appended immediately. So this translates in my brain to a single temp var or a string literally, conditionally being appended to each category key if there is a message to report. The code example doesn't prove the need for multiple variables.


appending string literals to the dictionary directly
Could you kindly elaborate more on this?

# Example 1

if all((input_01, input_02)) == False
:
    msg = "One or both of the Input fields is/ are empty!"
elif input_01 == input_02:
    msg = "Inputs used in both fields are the same!"
else:
    msg = validate_inputs((input_01, input_02))
if msg:
    test_dict["Input Issue"].append(msg)

if next_test():
    msg = 'error'
if msg:
    test_dict["Frame Range Issue"].append(msg)

# Example 2
append = test_dict["Input Issue"].append
if all((input_01, input_02)) == False
:
    append("One or both of the Input fields is/ are empty!")
elif input_01 == input_02:
    append("Inputs used in both fields are the same!")
else:
    append(validate_inputs((input_01, input_02)))


append = test_dict["Frame Range Issue"].append
if next_test():
    append('error')


In general, I am trying to find out what is the best practice to go about doing them :)
 






--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Justin Israel

unread,
Oct 23, 2018, 5:31:54 PM10/23/18
to python_in...@googlegroups.com
That last reply didn't format nicely:

likage

unread,
Oct 23, 2018, 6:25:46 PM10/23/18
to Python Programming for Autodesk Maya
I was not aware that you could use `append = test_dict["Input Issue"].append` as you have demonstrated in Example #2.
Learnt something new today :D

Justin Israel

unread,
Oct 23, 2018, 6:36:13 PM10/23/18
to python_in...@googlegroups.com
On Wed, Oct 24, 2018 at 11:25 AM likage <dissid...@gmail.com> wrote:
I was not aware that you could use `append = test_dict["Input Issue"].append` as you have demonstrated in Example #2.
Learnt something new today :D

Hey, its Python. You can do anything you want :-)
 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

likage

unread,
Oct 23, 2018, 6:46:51 PM10/23/18
to Python Programming for Autodesk Maya
I have 2 questions, should have done more testing on my end..

1. I am trying to understand the use of `.append'. I know that it adds things to list, but when I was looking thru the documentation page - https://docs.python.org/2/library/collections.html, even in the example, it is mostly `dict_name['key name'].append(value)` but nowhere near the method you have used.. Am I looking at the wrong doc?

2. For Method #2 you have proposed, instead of using a defaultdict(list), I used a normal dictionary {} and I got the KeyError message.
Is this because when using normal dictionary, generally it should be `dict_name['key name'] = value`, and since we are not doing this way and hence the error?

Pardon the noob questions as I am trying to make sense of things

Justin Israel

unread,
Oct 23, 2018, 6:58:46 PM10/23/18
to python_in...@googlegroups.com
On Wed, Oct 24, 2018 at 11:46 AM likage <dissid...@gmail.com> wrote:
I have 2 questions, should have done more testing on my end..

1. I am trying to understand the use of `.append'. I know that it adds things to list, but when I was looking thru the documentation page - https://docs.python.org/2/library/collections.html, even in the example, it is mostly `dict_name['key name'].append(value)` but nowhere near the method you have used.. Am I looking at the wrong doc?

It isn't actually part of the collections.defaultdict class. You have constructed:

    dict_name = defaultdict(list)

 That means when you do  
    
    dict_name['key name']

you get a list returned to you. Because python treats everything as first class objects, you can assign the list.append function to a variable

    aList = dict_name['key name']
    append = aList.append

And its actually a documented performance note:
https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Avoiding_dots...
 

2. For Method #2 you have proposed, instead of using a defaultdict(list), I used a normal dictionary {} and I got the KeyError message.
Is this because when using normal dictionary, generally it should be `dict_name['key name'] = value`, and since we are not doing this way and hence the error?

I don't recall proposing that you stop using defaultdict. My example expected that it would be a defaultdict.
 

Pardon the noob questions as I am trying to make sense of things

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

likage

unread,
Oct 23, 2018, 8:57:53 PM10/23/18
to Python Programming for Autodesk Maya
Got it, thank you.

I did not read the post correctly and thought it was a case of just dict type instead of defaultdict.
Reply all
Reply to author
Forward
0 new messages