对Django代码的更改给出了名称错误名称(函数)未定义

2024-05-02 17:36:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Django项目,其中模型有两个类——Equity和Article。在股票类别中,我曾经有以下代码,这些代码运行得很顺利

def fundamental_list_actual(self):
        l_fund = []
        filtered_articles = Article.objects.filter(equity__industry = self.industry)
        for filtered_article in filtered_articles:
            if(filtered_article.equity.equity_name == self.equity_name):
                l_fund.append([filtered_article.get_fun_score(), filtered_article.get_date(), filtered_article.id, filtered_article.title, filtered_article.get_source()])
            else:
                if (filtered_article.read_through == -1):
                    l_fund.append([float(-1)*filtered_article.get_fun_score(), filtered_article.get_date(), filtered_article.id, filtered_article.title, filtered_article.get_source()])
                if (filtered_article.read_through == 1):
                    l_fund.append([filtered_article.get_fun_score(), filtered_article.get_date(), filtered_article.id, filtered_article.title, filtered_article.get_source()])
        return l_fund 

但是,我最近更新了代码,在模型代码中但在任何类之外包含以下内容:

filename = 'fake_nums_trial_covar'
#infile = open(filename, 'rb')
infile = open('PATH_HIDDEN_FOR_PRIVACY/fake_nums_trial_covar', 'rb')
covar_trial_nums = pickle.load(infile)
infile.close()

在权益类别中,以下各项:

def covars_abs_above_mean(row_index):
        covars = covar_trial_nums #cov_to_dataframe('Russel_1000_tickers_3.xlsx')
        stocks = covars.index 
        pos_relation_list = []
        neg_relation_list = []
        pos, neg = avg_pos_and_neg(row_index)
        for stock in stocks:
            if (covars.loc[row_index, stock] > pos):
                pos_relation_list.append(stock)
            if (covars.loc[row_index, stock] < neg):
                neg_relation_list.append(stock)
        return pos_relation_list, neg_relation_list


def fundamental_list(self):
        name = self.equity_name
        pos_related_cos, neg_related_cos = covars_abs_above_mean(name)

        #now we want to get a list of articles whose equity__equity_name matches that of ANY
        #of the equities in our pos / neg lists (though we'd like 2 separate filters for this)
        #try:
        pos_filtered = Article.objects.filter(equity__equity_name__in = pos_related_cos)
        neg_filtered = Article.objects.filter(equity__equity_name__in = neg_related_cos)
        l_fund = []
        filtered_articles_industry = Article.objects.filter(equity__industry = self.industry)
    
        for filtered_article in filtered_articles_industry:
            if (filtered_article.equity.equity_name == self.equity_name):
                l_fund.append([filtered_article.get_fun_score(), filtered_article.get_date(), filtered_article.id, filtered_article.title, filtered_article.get_source()])
            else:
                if (filtered_article.read_through == -1):
                    l_fund.append([float(-1)*filtered_article.get_fun_score(), filtered_article.get_date(), filtered_article.id, filtered_article.title, filtered_article.get_source()])
                if (filtered_article.read_through == 1):
                    l_fund.append([filtered_article.get_fun_score(), filtered_article.get_date(), filtered_article.id, filtered_article.title, filtered_article.get_source()])
        for filtered_article in pos_filtered:
            if (filtered_article.equity.industry != self.industry):
                l_fund.append([filtered_article.get_fun_score(), filtered_article.get_date(), filtered_article.id, filtered_article.title, filtered_article.get_source()])
        for filtered_article in neg_filtered:
            if (filtered_article.equity.industry != self.industry):
                l_fund.append([float(-1)*filtered_article.get_fun_score(), filtered_article.get_date(), filtered_article.id, filtered_article.title, filtered_article.get_source()])
        
        return l_fund

但是,每当我在本地服务器127.0.0.1:8000上运行代码时,有些页面工作正常,但有些页面会给我一个错误,并在底部声明:“pos_related_cos,neg_related_cos=covars_abs_over_mean(name)”并说“name‘covars_abs_over_mean’未定义。”Powershell显示类似错误。然而,我想我确实定义了它…就在上面的代码中?此外,我在桌面上用一个单独的文件“测试”协方差矩阵,效果很好,似乎只有当我开始进入Django时,错误才会出现。我不擅长Django,如果有人能帮我解决这个问题,我将不胜感激

编辑:我必须将此函数作为助手添加到我的代码中

def avg_pos_and_neg(self, row_index):
        covar = covar_trial_nums 
        row = covar[row_index]
        pos_list = []
        neg_list = []
        for ind in row.index:
            if (row[ind] > 0):
                pos_list.append(row[ind])
            if (row[ind] < 0):
                neg_list.append(row[ind])
        pos_avg = 0
        neg_avg = 0
        for item in pos_list:
            pos_avg += item
        for item in neg_list:
            neg_avg += item
        pos_avg = pos_avg / len(pos_list)
        neg_avg = neg_avg / len(neg_list)
        return pos_avg, neg_avg 

每当我加载一个页面,它就会永远加载。这个页面似乎要花很长时间才能加载,它不会给我一个错误页面,或者我想要的页面-只是加载