2

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!

4

3 回答 3

1

在您的主脚本中,您使用该变量c来引用“风”,然后是“城市”。在循环结束时,变量c现在将引用城市列表中的最后一个城市。因此它不会有任何“风”属性。

于 2013-09-13T20:46:52.063 回答
1

将类下面的所有内容都设置为返回cities的函数,导入该函数并调用它,并将其设置cities为一个新的局部变量。然后你可以运行你的 for 循环。

免责声明:我根本没有对此进行测试。

步骤1:

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

第2步:

from script1 import getCities

第 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)    
于 2013-09-13T18:39:18.097 回答
0

如果是我,我会将方法从第一个文件复制并粘贴到您现在正在使用的文件中,因为据我所知,没有办法从不同的脚本调用方法,不确定,也许有,但对我来说最简单的方法是复制和粘贴该方法。希望我帮助... :)

于 2013-09-13T18:32:24.213 回答