Python Forum
if conditional not firing - unable to understand why
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if conditional not firing - unable to understand why
#1
Hi Folks -
I'm running into a weird issue where when I add two if conditionals, the portion of code following the second if conditional when true does not execute.

For instance, this code works fine:

def sendemail(email, data):
    ret = ''
    try:
        me = '[email protected]'  # You can update the helpdesk mail address.
        my_password = 'pass'  # Mailbox Password
        you = email
        msg = MIMEMultipart('alternative')
        msg['Subject'] = 'WARNING: Unassigned Request Type(s) and/or Catagory(ies) and/or Subcategory(ies)  etc : ' + time.strftime('%c')
        msg['From'] = me
        msg['To'] = email

        html = data
        part2 = MIMEText(html, 'html')

        msg.attach(part2)

        s = smtplib.SMTP_SSL('server')  # Mail Server Host name
        s.login(me, my_password)

        s.sendmail(me, you, msg.as_string())
        s.quit()
        ret = 'success'
    except smtplib.SMTPException as err:
        print ('Unable to Send emails : ', err)
        ret = 'failure'
    return ret


reportJson = str(sys.argv[1])
json_data = open(reportJson).read()
data = json.loads(json_data)
counter = 0
techEmail = ''
message = \
    """<html><html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr>"""
length = len(data)

for dObj in data:
    requestID = dObj.get('requestid')
    site = dObj.get('site')
    RequestType = dObj.get('RequestType')
    status = dObj.get('status')
    requester = dObj.get('requester')
    subject = dObj.get('subject')
    Violated = dObj.get('Violated')
    ApproachingSLA = dObj.get('ApproachingSLA')
    TechnicianUpdated = dObj.get('TechnicianUpdated')
    email = dObj.get('email')

    if RequestType is None:
        RequestType = 'Not Assigned'
            if counter == 0:
                techEmail = email
            counter += 1
            if techEmail == email:
                message += \
                    """<tr><td>""" \
                    + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
                    + status + """</td><td>""" + requester + """</td><td>""" \
                    + subject + """</td></tr>"""
                if counter == length:
                    message += '</table></body></html>'
                    r = sendemail(techEmail, message)
                    print(techEmail)
            else:
                message += '</table></body></html>'
                r = sendemail(techEmail, message)
                print(techEmail)
                techEmail = email
                message = \
                    """<html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr><tr><td>""" \
                    + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
                    + status + """</td><td>""" + requester + """</td><td>""" \
                    + subject + """</td></tr>"""
                if counter == length:
                    message = \
                        """<html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr><tr><td>""" \
                        + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
                        + status + """</td><td>""" + requester + """</td><td>""" \
                        + subject + """</td></tr></table></body></html>"""
                    r = sendemail(techEmail, message)
                    print(techEmail)

if(r=="success"):
	print("Reports sent successfully to the respective techs")
else:
	print("Unable to send reports")
However when I add (2) if conditionals to the beginning of the loop, even though the second if conditional is true, it doesn't continue to execute.

In the interest of space, I've only copied int he portion where I added the if conditionals (Violated & ApproachingSLA).

    if RequestType is None:
        RequestType = 'Not Assigned'
    if Violated == 'No':
        if ApproachingSLA == 'Yes':
            if counter == 0:
                techEmail = email
            counter += 1
            if techEmail == email:
Any idea why? Thanks!
Reply
#2
what is the output when you print RequestType, Violated and ApproachingSLA just before the if blocks
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
What have you tried to debug this. Have you printed the variables to see what they contain, RequestType, Violated, etc? The Python syntax looks OK to me and that is all we can do because we don't have any of the values for the variables.
Reply
#4
Hi Folks -

Yes I have printed the variables and all show correctly. That's the weird issue.
Reply
#5
I see indentation errors, which wouldn't cause your problem, but it does make me wonder if the code you've posted is indented identically. Could you please post the version with the problematic conditions implemented? Otherwise, we're all just guessing at the logical flow of the code.
Reply
#6
(Oct-20-2018, 01:25 AM)stullis Wrote: I see indentation errors, which wouldn't cause your problem, but it does make me wonder if the code you've posted is indented identically. Could you please post the version with the problematic conditions implemented? Otherwise, we're all just guessing at the logical flow of the code.

Sure thing, here you go!

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
import sys
import json
import datetime
import smtplib
import time
from pprint import pprint
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def sendemail(email, data):
    ret = ''
    try:
        me = 'email'  # You can update the helpdesk mail address.
        my_password = 'password'  # Mailbox Password
        you = email
        msg = MIMEMultipart('alternative')
        msg['Subject'] = 'WARNING: Unassigned Request Type(s) and/or Catagory(ies) and/or Subcategory(ies)  etc : ' + time.strftime('%c')
        msg['From'] = me
        msg['To'] = email

        html = data
        part2 = MIMEText(html, 'html')

        msg.attach(part2)

        s = smtplib.SMTP_SSL('mail')  # Mail Server Host name
        s.login(me, my_password)

        s.sendmail(me, you, msg.as_string())
        s.quit()
        ret = 'success'
    except smtplib.SMTPException as err:
        print ('Unable to Send emails : ', err)
        ret = 'failure'
    return ret


reportJson = str(sys.argv[1])
json_data = open(reportJson).read()
data = json.loads(json_data)
counter = 0
techEmail = ''
message = \
    """<html><html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr>"""
length = len(data)

for dObj in data:
    requestID = dObj.get('requestid')
    site = dObj.get('site')
    RequestType = dObj.get('RequestType')
    status = dObj.get('status')
    requester = dObj.get('requester')
    subject = dObj.get('subject')
    email = dObj.get('email')
    if RequestType is None:
        RequestType = 'Not Assigned'
    if Violated == 'No':
        if ApproachingSLA == 'Yes':
            if counter == 0:
                techEmail = email
            counter += 1
            if techEmail == email:
                message += \
                    """<tr><td>""" \
                    + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
                    + status + """</td><td>""" + requester + """</td><td>""" \
                    + subject + """</td></tr>"""
                if counter == length:
                    message += '</table></body></html>'
                    r = sendemail(techEmail, message)
                    print(techEmail)
            else:
                message += '</table></body></html>'
                r = sendemail(techEmail, message)
                print(techEmail)
                techEmail = email
                message = \
                    """<html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr><tr><td>""" \
                    + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
                    + status + """</td><td>""" + requester + """</td><td>""" \
                    + subject + """</td></tr>"""
                if counter == length:
                    message = \
                        """<html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 100%;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #dddddd;}</style></head><body><table><tr><th>RequestID</th><th>Site</th><th>RequestType</th><th>Status</th><th>Requester</th><th>Subject</th></tr><tr><td>""" \
                        + requestID + """</td><td>""" + site + """</td><td>""" + RequestType + """</td><td>""" \
                        + status + """</td><td>""" + requester + """</td><td>""" \
                        + subject + """</td></tr></table></body></html>"""
                    r = sendemail(techEmail, message)
                    print(techEmail)

if(r=="success"):
	print("Reports sent successfully to the respective techs")
else:
Reply
#7
Is it possible to post an example of the JSON data you are using?
Reply
#8
I see two problems:

1. Line 66 needs to be indented more.
2. The code does not set Violated or ApproachingSLA before testing them, which should cause an exception.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 268 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,645 Jan-28-2021, 05:22 PM
Last Post: snippsat
  .Set - Unable to understand the statement ateestructural 5 2,307 Aug-02-2020, 08:24 PM
Last Post: deanhystad
  Unable to understand a statement in an existing code ateestructural 1 2,190 Aug-01-2020, 09:38 PM
Last Post: deanhystad
  Help with calling the function as the code keep firing chris0147 8 3,869 May-04-2018, 09:00 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

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