The pythonic way out of the loop

Total python beginners.

I have the following function that checks if a string obtained from certain inputs exists in a text file. It scans each line of the text file to see if an exact match is found.

I have to exit the loop right after the match is found to avoid an unnecessary loop.

Here is the code:

def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):    #   function to check if a given DateZoneCity
                                                                    #   combination had already been completely downloaded
    string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
    with open(Record_File) as download_status:
        DateZoneCity_exists = False
        for line in download_status:
            if string_to_match in line:
                DateZoneCity_exists = True                      #   if match found, then set "DateZoneCity_exists" to True
                break                                           #   and break out from the [for line in download_status:] loop
        if DateZoneCity_exists: return True
    download_status.close()

I am looking for a more concise, pythonic way to structure code. Is there something I can do to make it better? Somehow eliminate the need for "DateZoneCity_exists" and the second If statement?

+4
source share
5 answers

, any :

# Function to check if a given DateZoneCity
def DateZoneCity_downloaded_previously(Order_Date, ZoneCity):
    # Combination had already been completely downloaded
    string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0]
                                                      + "-" + ZoneCity[1]
    with open(Record_File) as download_status:
        return any((string_to_match in line) for line in download_status)

, False , , None, , , , .

+6

return break:

def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):
    """Check if a given DataZoneCity combination had already been downloaded."""
    string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
    with open(Record_File) as download_status:
        for line in download_status:
            if string_to_match in line:
                return True
    return False  # No match found.
+4

, , ( , ):

if string_to_match in open(Record_File).read():
    return True

:

def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):                   
    string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
    if string_to_match in open(Record_File).read():
        return True
+3

for , . .

def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):
    """Check if a given DataZoneCity combination had already been downloaded."""
    string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
    with open(Record_File) as download_status:
        if string_to_match in download_status.read():
            return True
    return False  # No match found.
+2

, in if, : -

def DateZoneCity_downloaded_previously(Order_Date,ZoneCity):                   
    string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0] + "-" + ZoneCity[1]
    if string_to_match in open(Record_File).read():
        return True  

while, record_file , , : -

def DateZoneCity_downloaded_previously(Order_Date, ZoneCity):
    string_to_match = Order_Date.strftime('%Y/%m/%d') + "-" + ZoneCity[0]  + "-" + ZoneCity[1]
    with open(Record_File) as download_status:
        return is_empty((string_to_match in line) for line in download_status)  

since reading line by line will save the memory used to save the line.

0
source

All Articles