-1

I'm using Python3 to call an API to get a list of trains if certain conditions are met it sends me a notification on pushover.

The list I receive looks like this:

[
    {
        "Car": "8",
        "Destination": "Vienna",
        "DestinationCode": "K08",
        "DestinationName": "Vienna/Fairfax-GMU",
        "Group": "2",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "4"
    },
    {
        "Car": "8",
        "Destination": "NewCrltn",
        "DestinationCode": "D13",
        "DestinationName": "New Carrollton",
        "Group": "1",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "6"
    },
    {
        "Car": "6",
        "Destination": "Vienna",
        "DestinationCode": "K08",
        "DestinationName": "Vienna/Fairfax-GMU",
        "Group": "2",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "BRD"
    }
]

I have the following for loop and nested if statement to narrow down trains that are going to K08, don't have an 'ARR', 'BRD', or null for the 'Mins' variable, and within 10-20 mins:

for x in list:

        if x['DestinationCode'] == "K08":

                if x['Min'] != "" or "ARR" or "BRD":

                        if int(x['Min']) > 10 and int(x['Min']) < 20:
                                #pushover script

The first if statement works on the Destination variable. However, I get the following error on any with ARR, BRD, or null:

Traceback (most recent call last):
  File "./test.py", line 23, in <module>
    if int(x['Min']) > 10 and int(x['Min']) < 15:
ValueError: invalid literal for int() with base 10: 'BRD'

This leads me to believe that my nested if is passing 'ARR', 'BRD', and Null to the final if statement which will not accept anything but integers.

blintster
  • 133
  • 1
  • 7

2 Answers2

0

This is explained very well aswell in this SO answer, thanks @MisterMiyagi

The problem is that some unexpected values for x['Min'] seem to appear, while you thought you filtered them out. In fact, one of your boolean is misleading:

if x['Min'] != "" or "ARR" or "BRD":

This is only checking for x['Min'] != "". Indeed, or "ARR" does not do anything because the boolean bool("ARR") is always True! The same applies for or "BRD" and that explains why you end up having BRD values trying to be cast as integers later on your loop. See the following, if you replace x['Min'] by a value you'd want to skip:

>>> xmin = "BRD"
>>> xmin != "" or "ARR" or "BRD"
True

While you'd want False!

If you wish to say that x['Min'] must not be either "" or ARR or BRD then you may use the following:

if x['Min'] not in ["", "ARR", "BRD"]:

Or

if x['Min'] and x['Min'] not in ["ARR", "BRD"]:

Proof:

>>> xmin = "BRD"
>>> xmin not in ["", "ARR", "BRD"]
False
arnaud
  • 3,293
  • 1
  • 10
  • 27
0

The issue was in if x['Min'] != "" or "ARR" or "BRD". You do something like this.

data_dict_list = [
    {
        "Car": "8",
        "Destination": "Vienna",
        "DestinationCode": "K08",
        "DestinationName": "Vienna/Fairfax-GMU",
        "Group": "2",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "14"
    },
    {
        "Car": "8",
        "Destination": "NewCrltn",
        "DestinationCode": "D13",
        "DestinationName": "New Carrollton",
        "Group": "1",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "12"
    },
    {
        "Car": "6",
        "Destination": "Vienna",
        "DestinationCode": "K08",
        "DestinationName": "Vienna/Fairfax-GMU",
        "Group": "2",
        "Line": "OR",
        "LocationCode": "D1",
        "LocationName": "Town",
        "Min": "BRD"
    }
]

for data_dict in data_dict_list:
    if data_dict['DestinationCode'] == "K08":
        if data_dict['Min'] not in ("", "ARR", "BRD"):
            if 10 < int(data_dict['Min']) < 20:
                print("")

krishna
  • 1,029
  • 6
  • 12