将参数传递给字典中嵌套的方法

2024-05-03 09:30:59 发布

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

链接到repo:https://github.com/jsarnowski96/pysql-console

我目前正在我的程序中研究数据分析方法。我遇到的问题是,将可选参数传递到allowed_params字典内的方法调用中,这是我以前使用此解决方案(自定义内置命令字典)后不应该遇到的问题:

def DataAnalysis(fileName = "", param = "", num=""):
try:
    filePath = ""
    if settings.global_config_array["sourceCsvFile"] != None:
        filePath = settings.global_config_array["sourceCsvFile"]
    if fileName == "" and settings.global_config_array["sourceCsvFile"] == None:
        print("You did not enter CSV file name.")
        while fileName == "" or not os.path.exists(fileName):
            fileName = str(input("Please insert the CSV filename for data analysis: "))
            if fileName == "":
                print("You did not enter the filename.\n")
            else:
                filePath = "exports/" + fileName + ".csv"
                settings.global_config_array["sourceCsvFile"] = filePath
            if os.path.exists(filePath):
                print("File " + fileName + ".csv found.\n")
                if param == "":
                    param = str(input("Insert the parameter: "))
                break
            else:
                print("File " + fileName + ".csv not found.\n")
    elif fileName == "" and settings.global_config_array["sourceCsvFile"] != None:
        if os.path.exists(settings.global_config_array["sourceCsvFile"]):
            filePath = settings.global_config_array["sourceCsvFile"]
    elif fileName != "" and settings.global_config_array["sourceCsvFile"] != None:
        filePath = "exports/" + fileName + ".csv"
        if os.path.exists(filePath):
            print("File " + fileName + ".csv found.\n")
            settings.global_config_array["sourceCsvFile"] = filePath  
        else:
            if settings.global_config_array["sourceCsvFile"] != None:
                filePath = settings.global_config_array["sourceCsvFile"]
                if os.path.exists(filePath):
                    num = param
                    param = fileName
    elif fileName != "" and settings.global_config_array["sourceCsvFile"] == None:
        filePath = "exports/" + fileName + ".csv"
        if os.path.exists(filePath):
            print("File " + fileName + ".csv found.\n")
            settings.global_config_array["sourceCsvFile"] = filePath      
    def switch_params(param, num):
        result = None
        dataframe = pd.read_csv(filePath)
        columns = []
        for col in dataframe.columns:
            columns.append(col)
        allowed_params = {
            "describe": dataframe.describe,
            "info": dataframe.info,
            "explode": dataframe.explode,
            "hist": dataframe.hist,
            "cols": dataframe.columns,
            "head": dataframe.head, # the problem begins here
            "summary": smf.ols,
            "free": None
        }

        if param in allowed_params and param == "free":
            settings.global_config_array["sourceCsvFile"] = allowed_params["free"]
        elif param in allowed_params and param != "free":
            result = allowed_params[param]
            print(result,"\n")
        elif param in allowed_params and param == "head":
            if num == "":
                num = input("Insert the amount of rows to display in 'head' statement: ")
            while num > dataframe.size:
                num = input("Provided number exceeds the size of the dataframe. Please try again: ")
            result = allowed_params[param](int(num)) # and the proper execution is here
            print(result)
        elif param in allowed_params and param == "summary":
            result = allowed_params[param](columns, dataframe).fit()
            print(result.summary())
        elif param in allowed_params:
            result = allowed_params[param]()
            print(result)
        elif param == "":
            result = allowed_params["describe"]
            print(result,"\n")
        else:
            print("Incorrect parameter inserted.\n")
    switch_params(param, num)
except KeyboardInterrupt:
    print("\nTerminating command...\n")
except IOError:
    print("File " + fileName + ".csv " + "does not exist.\n")
except Exception as e:
    print(e,"\n")
except pyodbc.Error as e:
    sqlstate = e.args[0]
    if sqlstate == "42S02":
        print("Error " + e.args[0] + ": Cannot create a temporal table - referenced object does not exist in the selected database.\n")
    else:
        print("Error",e.args[0] + ":\n",e,"\n")

在我的自定义内置命令中,使用这种间接方法执行调用非常有效,但它似乎对pandas.DataFrame对象根本不起作用,或者至少它的支持非常差

我可以看到两个主要问题:
-如果我决定将dataframe.head(int(num))直接放在字典中,每次使用da命令时都会执行它,这正是我想不惜一切代价避免的。
-如果我保持原样,head方法可以工作,但它忽略了num参数,因此它总是显示目标CSV文件的完整内容


Tags: configdataframeifsettingsparamparamsresultfilename
2条回答

这个(简化的例子)有效果吗

def switch_params(param, num=None):
    dataframe = pd.read_csv(filePath)
    if num:
        head = dataframe.head(num)
    else:
        head = None
    allowed_params = {
        "describe": dataframe.describe,
        "head": head
    }

好的,所以事实证明,为了让它完全运行,小黑客是必要的。

  • allowed_params字典中删除了head字段
  • 将if语句更改为检查param是否仅等于head,而不检查它是否存在于allowed_params
  • dataframe.head(int(num))直接在result变量中调用

通过这种方式,我能够在不牺牲太多原始概念/实现的情况下获得预期的结果

            elif param == "head":
            if num == "":
                num = input("Insert the amount of rows to display in 'head' statement: ")
            while int(num) > len(dataframe) or int(num) == 0:
                if int(num) == 0:
                    num = input("Number 0 is not allowed. Please try again: ")
                else:
                    num = input("Provided number exceeds the size of the dataframe. Please try again: ")
            result = dataframe.head(int(num))
            print("\n",result,"\n")

enter image description here

相关问题 更多 >