Writing a method that I can call in a separate script in python
Question: I am trying to write a method that I can call in a different script, however,
I am not able to successfully call the script(s) with the way I have it
written. This is one of the scripts I am trying to call (the second is very
similar:

    
    
    #!usr/bin/env python
    
    import re
    import urllib
    from datetime import datetime
    from datetime import timedelta
    
    date = datetime.now()
    date1 = date + timedelta(days=1)
    date2 = date + timedelta(days=2)
    
    
    class city :
            def __init__(self, city_name, link) :
                    self.name = city_name
                    self.url = link
                    self.wind1 = 0
    
        def retrieveTemps(self) :
                filehandle = urllib.urlopen(self.url)
    
                # get lines from result into array
                lines = filehandle.readlines()
    
                # (for each) loop through each line in lines
                line_number = 0 # a counter for line number
                for line in lines:
                        line_number = line_number + 1 # increment counter
    
                        # find string, position otherwise position is -1
    
                        position2 = line.rfind('<ul class="stats">')
    
                        #String is found in line
    
                        if position2 > 0 :
                                self.wind0 = lines[line_number + 1].split('</strong>')[0].split('style="">')[-1]
    
                                break # done with loop, break out of it
    
                return ('c.wind0')
    
                filehandle.close()
    
    m1 = city('Mexico City', 'http://www.accuweather.com/en/mx/mexico-city/242560/daily-weather-forecast/242560?day=2')
    m3 = city('Veracruz', 'http://www.accuweather.com/en/mx/veracruz/236233/daily-weather-forecast/236233?day=2')
    m5 = city('Tampico', 'http://www.accuweather.com/en/mx/tampico/235985/daily-weather-forecast/235985?day=2')
    m7 = city('Nuevo Laredo', 'http://www.accuweather.com/en/mx/nuevo-laredo/235983/daily-weather-forecast/235983?day=2')
    m9 = city('Monterrey', 'http://www.accuweather.com/en/mx/monterrey/244681/daily-weather-forecast/244681?day=2')
    m11 = city('S. Luis Potosi', 'http://www.accuweather.com/en/mx/san-luis-potosi/245369/daily-weather-forecast/245369?day=2')
    m13 = city('Queretaro', 'http://www.accuweather.com/en/mx/queretaro/245027/daily-weather-forecast/245027?day=2')
    m15 = city('Laz. Cardenas', 'http://www.accuweather.com/en/mx/lazaro-cardenas/239054/daily-weather-forecast/239054?day=2')
    
    cities = []
    cities.append(m1)
    cities.append(m3)
    cities.append(m5)
    cities.append(m7)
    cities.append(m9)
    cities.append(m11)
    cities.append(m13)
    cities.append(m15)
    

I try to call this script and another script with this:

    
    
    #!usr/bin/env python
    
    from script import getCities
    from script2 import getWind
    
    cities = getCities()
    wind = getWind()
    
    for c in wind :
            c.retrieveTemps()
    for c in cities :
            c.retrieveTemps()
    
    print(c.name,c.high0,c.low0,c.high1,c.low1,c.weather0,c.weather1,c.wind0,c.wind1)
    

c.wind0 is found with script2, while all the other variables are found with
script1. If I import script1 second, I get the error: AttributeError: city
instance has no attribute 'wind1', which has no attribute with script2, it is
associated with script1. It seems to be ignoring the first script I import.

Any suggestions would be appreciated. Thanks!

UPDATE:

Using your suggestions, plus something else, I came up with this and it works
perfectly.

    
    
    #!usr/bin/env python
    
    import script1
    import script2
    
    wind = script2.getWind()
    cities = script.getCities()
    
    for c in cities :
            c.retrieveTemps()
    for w in wind :
            w.retrieveWind()
    
    # iterate over both lists in parallel, zip returns a tuple 
    for c, w in zip(cities, wind) :
        print(c.name,c.high0,c.low0,c.high1,c.low1,c.weather0,c.weather1,c.wind0,w.wind1)
    

Thanks everyone for your help!


Answer: Make everything below the class a function that returns `cities`, import the
function, and call it, setting `cities` to a new local variable. Then you can
run your for loop.

Disclaimer: I didn't test this at all.

Step1:

    
    
    def getCities():
        m1 = city('Mexico City', 'http://www.accuweather.com/en/mx/mexico-city/242560/daily-weather-forecast/242560?day=2')
        m3 = city('Veracruz', 'http://www.accuweather.com/en/mx/veracruz/236233/daily-weather-forecast/236233?day=2')
        m5 = city('Tampico', 'http://www.accuweather.com/en/mx/tampico/235985/daily-weather-forecast/235985?day=2')
        m7 = city('Nuevo Laredo', 'http://www.accuweather.com/en/mx/nuevo-laredo/235983/daily-weather-forecast/235983?day=2')
        m9 = city('Monterrey', 'http://www.accuweather.com/en/mx/monterrey/244681/daily-weather-forecast/244681?day=2')
        m11 = city('S. Luis Potosi', 'http://www.accuweather.com/en/mx/san-luis-potosi/245369/daily-weather-forecast/245369?day=2')
        m13 = city('Queretaro', 'http://www.accuweather.com/en/mx/queretaro/245027/daily-weather-forecast/245027?day=2')
        m15 = city('Laz. Cardenas', 'http://www.accuweather.com/en/mx/lazaro-cardenas/239054/daily-weather-forecast/239054?day=2')
    
    
        cities = []
        cities.append(m1)
        cities.append(m3)
        cities.append(m5)
        cities.append(m7)
        cities.append(m9)
        cities.append(m11)
        cities.append(m13)
        cities.append(m15)
    
        return cities
    

Step 2:

    
    
    from script1 import getCities
    

Step 3:

    
    
    cities = getCities()
    
    for c in cities :
        c.retrieveTemps()
        print(c.name,c.high0,c.low0,c.high1,c.low1,c.weather0,c.weather1,c.wind0,c.wind1)    
    

