【问题标题】:Accessing a function variable without calling the function在不调用函数的情况下访问函数变量
【发布时间】:2022-01-15 21:13:25
【问题描述】:

背景 -
我是初学者,正在编写他们的第一个严肃程序;进行了 API 调用,它返回一个响应,我将其保存为 .json 文件和数据框 (df),然后再进行一些数学和转换并保存为名为 exceptions_df 的新数据框。最后,我希望将dfexceptions_df 都写入.xlsx 文件(def xlsx_writer())。

问题 -
def xlsx_writer():exception_df 写入 .xlsx 文件没有问题。但是,我也希望它可以将df 写入同一个文件。

虽然我可以使用 df = def ownership_qc() 访问 df 变量,但该函数还调用其他函数 (df = unpack_response()),而后者本身又调用另一个名为 api_response = response_writer()) 的函数。你能看到我的问题吗?通过使用此方法,我的代码将不必要地重复自身(即再次调用相关函数)。我当前的代码可以做到这一点并且“有效”,但是重复代码似乎很愚蠢;而且速度超级慢。

相关功能 -
def xlsx_writer - 这是我想在df 中使用的功能。它目前通过df = ownership_qc() 来实现,但是根据上面的描述,它很慢:

def xlsx_writer():
    exceptions_df = ownership_exceptions()
#   This is where I call df and cause the a chain reaction of ownership_qc() calling function it requires and so forth.
    df = ownership_qc()
    
    timestr = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")
    filename = 'ownership_exceptions_'+timestr
    
    writer = pd.ExcelWriter(filename+'.xlsx', engine='xlsxwriter')
    exceptions_df.to_excel(writer, sheet_name='Ownership Exceptions Report', startrow=1, header=False, index=False)
    df.to_excel(writer, sheet_name='Raw Extract', startrow=1, header=False, index=False)
    workbook = writer.book
    worksheet = writer.sheets['Ownership Exceptions Report']
    (max_row, max_col) = exceptions_df.shape
    column_settings = [{'header': column} for column in exceptions_df.columns]
    worksheet.add_table(0, 0, max_row, max_col - 1, {'columns': column_settings})
    worksheet.set_column(0, max_col - 1, 12)
xlsx_writer()

def ownership_exceptions(): -
这是我在def xlsx_writer() 中调用以访问df 的函数。问题是这个函数调用了另一个函数,并开始了连锁反应:

# This is the function I am calling to acces `df`, however this function calls another function.
def ownership_exceptions():
    df = ownership_qc()
    df = df[(df['Entity ID %'] != 1.000000) & (df['Account # %'] != 1.000000)]
    df.drop(df.index[df['Entity ID %'] == '1'], inplace=True)
    exceptions_df = df.drop(['Model Type', 'Valuation (USD)', 'Top Level Legal Entity', 'Financial Service', 'Account Close Date'], axis=1)
    return df

我的想法

  • 有没有一种方法可以访问 df 而无需调用它的函数 (def ownership_exceptions) 并因此调用所有其他相关函数?
  • 我是否应该简单地创建一个新函数,将 API 响应 (.json) 加载回数据帧并以这种方式访问​​?

【问题讨论】:

  • 更改您的代码,让ownership_exceptionsdf 作为参数,这样您就可以将其传入,而不是让它从头开始重新加载所有内容。
  • ownership_exceptions 返回df,而不是exceptions_df - 这是故意的吗?
  • 抱歉 - 错字,应该返回 exceptions_df。

标签: python json pandas function


【解决方案1】:

我可能会让ownership_exceptions 返回(一个元组)两个值:exception_dfdf。这样,您就不需要调用两次ownership_qc

稍微更新您的ownership_exceptions,以处理exceptions_df 而不是df

def ownership_exceptions():
    df = ownership_qc()
    exceptions_df = df[(df['Entity ID %'] != 1.000000) & (df['Account # %'] != 1.000000)]
    exceptions_df = df[df['Entity ID %'] != '1']
    exceptions_df = df.drop(['Model Type', 'Valuation (USD)', 'Top Level Legal Entity', 'Financial Service', 'Account Close Date'], axis=1)
    return exceptions_df, df  # <---- Notice two values are being returned here

并将您的 xlsx_writer 函数更改为仅调用 ownership_exceptions

def xlsx_writer():
    exceptions_df, df = ownership_exceptions()
    
    # now use exceptions_df and df...

【讨论】:

  • 感谢您的意见 - 不幸的是,这似乎仍然再次调用函数。
  • 所以ownership_qc 被多次调用?'
猜你喜欢
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多