CodeStack82
CodeStack82

Reputation: 23

Error while calling method defined inside a class

I am trying to create a class and I can't seem to get it to work? I'm fairly new to Python, so any assistance would be appreciated. Also, not sure if this is the most efficient way to create and use an object. I am trying to build a well model and this is one piece of that model, once I get this simple issue figured out the rest should be fairly easy. Thanks.

import sys
import os
import csv
import pyodbc 
import pandas as pd
import pandas.io.sql as psql
from pandas import Series, DataFrame
from time import gmtime, strftime



#Drill Pipe Class
class DP:
    #Properties
    DP_ID = 1.00
    DP_OD = 1.00
    DP_Name = 'Drill Pipe'
    #test global
    idwel = '6683AFCEA5DF429CAC123213F85EB9B3' 

    #Constructor <- Accepts idwell to get info
    def __init__(self,idwell):
        self.id = idwell
    #..

    #WV DB connecton Function  -> return as dataframe -Updated 7/5/17
    def WV_Read_Query(_query):
        try:
            cnxn = pyodbc.connect("DSN=SQL_R_WV") 
            cur = cnxn.cursor()
            df = psql.read_sql(_query, cnxn)
            cnxn.close()
            #print(df)
            return df
        except "Error":
            return "Query Error...!"
    #..
    def get_DP_Data(_id):
        _id = str(_id)
        DP_Query = """Select Top 1 
                            DS.des as 'dp_name',DS.SZIDNOM as 'dp_id',
                            DS.SZODNOM as 'dp_od',DS.SYSCREATEDATE as 'date'
                        From [dbo].[US_WVJOBDRILLSTRINGCOMP] DS
                        Where IDWELL = '""" + _id +"""' 
                        AND Des = 'Drill Pipe' Order by SYSCREATEDATE Desc"""
        mud_Data = WV_Read_Query(DP_Query)
        return mud_Data
    #..


    DP_Table = get_DP_Data(id) 

    def get_DP_ID(self, DP_Table):
        dp_id = DP_Table['dp_id']
        return dp_id
    #..

    def get_DP_OD(self, DP_Table):
        dp_od = DP_Table['dp_od']
        return dp_od
    #..

    def get_Date(self, DP_Table):
        u_date = DP_Table['date']
        return u_date
    #..

    def get_Des(self, DP_Table):
        des = DP_Table['dp_name']
        return des
    #..

    #Print DP Info
    def DP_Info(self):
        Des = get_Des()
        ID = get_DP_ID()
        OD = get_DP_OD()
        Updated = strftime("%Y-%m-%d %H:%M:%S", gmtime())
        return Des + "\nDP Id:\t" + ID + "\nDP Id:\t" + OD + "\nUpdated:\t" + Updated
    #..
#...

dp = DP('6683AFCEA5DF429CAC123213F85EB9B3')
dp_info = dp.DP_Info()
print(dp_info)  

Traceback (most recent call last): File "u:\Development\Python Scripts\HCP\CUC Export Files 8_7_17\Well_Model.py", line 71, in class DP: File "u:\Development\Python Scripts\HCP\CUC Export Files 8_7_17\Well_Model.py", line 108, in DP DP_Table = get_DP_Data(id) File "u:\Development\Python Scripts\HCP\CUC Export Files 8_7_17\Well_Model.py", line 104, in get_DP_Data mud_Data = WV_Read_Query(DP_Query) NameError: name 'WV_Read_Query' is not defined

Upvotes: 0

Views: 62

Answers (2)

cs95
cs95

Reputation: 402363

If you are defining non-static, non-class methods within a function, the first argument is always an instance of that class. We usually call this argument self:

def WV_Read_Query(self, _query):
    ...

And,

def get_DP_Data(self, _id):

Furthermore, you call a these methods on the object self:

self.WV_Read_Query(DP_Query)

You might wonder why the function is defined with 2 arguments, but only 1 passed to. That's because the instance is implicitly passed as the first parameter, automatically.

This is equivalent to

DP.WV_Read_Query(self, DP_Query)

Where you call the method on the class, but explicitly pass the instance to it.


Further reading:

  1. Python classes

  2. What is the difference between class and instance methods?

Upvotes: 2

Cory Madden
Cory Madden

Reputation: 5193

You need to access it with self. So

def get_DP_Data(self, _id):
    _id = str(_id)
    DP_Query = """Select Top 1 
                        DS.des as 'dp_name',DS.SZIDNOM as 'dp_id',
                        DS.SZODNOM as 'dp_od',DS.SYSCREATEDATE as 'date'
                    From [dbo].[US_WVJOBDRILLSTRINGCOMP] DS
                    Where IDWELL = '""" + _id +"""' 
                    AND Des = 'Drill Pipe' Order by SYSCREATEDATE Desc"""
    mud_Data = self.WV_Read_Query(DP_Query)
    return mud_Data

You also need to add self to several of your methods. The class instance will always be the first parameter in a method unless you define it as a staticmethod using the decorator staticmethod.

Upvotes: 1

Related Questions