尝试运行类

时间:2015-07-13 10:10:24

标签: python class main nameerror

我在Survey_name&m;进入。但是当我尝试打印Survey_route(' mikkel')时,我收到以下消息:

<__main__.SurveyRoute object at 0x10eb0cf38>

在课程运行时,我需要更改哪些内容来打印 xcoord 情节

class SurveyRoute(SurveyPlotWidget):
    """docstring for SurveyRoute"""

    def __init__(self, survey_name):
        self.survey_name = survey_name

    def read_coordinate_file(self, survey_name):
        """
        coords  is a library with 'benchmark nr': UTM X, UTM Y, depth

        The coordinate name should be changed to the coordinate type. Here we are dealing with UTM coordinates
        The coordinate type can be found in the .xls file that contains the coordinates. e.g. mikkel.xls

        df      is the result of using the pandas package to rearrange the coords dictionary.
        """ 
        coords = station_coordinates.get_coordinates_all(survey_name)
        df = pd.DataFrame(coords,index=['UTM X','UTM Y','depth']) 
        df = DataFrame.transpose(df)
        xcoord = df['UTM X'].values.tolist() 
        ycoord = df['UTM Y'].values.tolist()

        print xcoord        

    def plot_coords(xcoord,ycoord):

        fig = plt.figure()
        plt.plot(xcoord, ycoord, marker='o', ms=10, linestyle='', alpha=1.0, color='r', picker = True)[0]
        plt.xlabel('UTM x-coordinate')
        plt.ylabel('UTM y-coordinate')

        x_legend = np.nanmax(xcoord) + 0.01*(np.nanmax(xcoord)-np.nanmin(xcoord))
        y_legend = np.nanmin(ycoord) - 0.01*(np.nanmax(ycoord)-np.nanmin(ycoord))
        map_size = np.sqrt(pow(np.nanmax(xcoord)-np.nanmin(xcoord),2)+pow(np.nanmax(ycoord)-np.nanmin(ycoord),2) )

        #legend_size = 100
        #max_val = np.nanmax(val)
        #if max_val < 50:
        #legend_size = 10
        fig.canvas.mpl_connect('pick_event', on_hover)
        self.canvas.draw()

"""
Set package_directory to the right user (e.g. DJV) and the folder where the station_coordinates are stored.  
"""
package_directory = '/Users/DJV/Desktop/quad-master/station_coordinates'                                     

#survey = SurveyRoute('mikkel')
#print SurveyRoute('mikkel')
#print survey.read_coordinate_file('mikkel') WORKS
#print survey.plot_coords(xcoord,ycoord)  DOESNT WORK

print SurveyRoute('mikkel')

2 个答案:

答案 0 :(得分:0)

  

SurveyRoute(&#39;的Mikkel&#39)

返回一个对象。所以当你打电话时

  

打印SurveyRoute(&#39; mikkel&#39;)

您正在打印整个对象。 要在类运行时打印xcoord,请将其放在__ init __函数中:

  

self.read_coordinate_file(survey_name)

并替换

  

打印SurveyRoute(&#39; mikkel&#39;)

  

SurveyRoute(&#39;的Mikkel&#39)

答案 1 :(得分:0)

当您执行 - SurveyRoute('mikkel')时 - 它会创建类SurveyRoute的实例/对象,self.survey_name等于mikkel。当您尝试打印对象本身时,它会像您观察到的那样打印 - <__main__.SurveyRoute object at 0x10eb0cf38>

另外,如果plot_coords()是一个实例方法(我相信如此,因为我可以看到你在函数中使用self),那么第一个参数应该是实例({{1 }})。您应该将其定义为 -

self

如果要调用此对象的函数,则应将其称为 -

def plot_coords(self, xcoord, ycoord):