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



我有一个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_lated_cos=covars_abs_above_m均值(名称("并说";未定义名称"covars_abs_above_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 

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

它不是函数,而是一个类方法,所以应该正确地调用它

self.covars_abs_above_mean(name)

相关内容

最新更新