【问题标题】:Passing argument to method nested in the dictionary将参数传递给嵌套在字典中的方法
【发布时间】:2020-02-09 20:18:12
【问题描述】:

回购链接: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 文件的全部内容。

【问题讨论】:

  • 每次运行程序时运行 dataframe.head(10) 的确切问题是什么?
  • 我想在每次运行此方法时将该数字作为可选参数传递。但是将它直接放在字典中会导致它无论如何都会执行,并且使用上述将 dataframe.head(int(num)) 分配给“结果”变量的方法似乎根本不起作用 - 值正在被读取pandas,但没有使用,所以此时使用 result = allowed_pa​​rams[param](int(num)) 的解决方案效率低下。

标签: python pandas dataframe dictionary


【解决方案1】:

这个(简化的例子)能解决问题吗?

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
    }

【讨论】:

  • 老实说,这是我正在考虑的想法/解决方法之一。猜测最好的解决方案是从字典中删除该方法调用并直接执行它。谢谢
【解决方案2】:

好的,事实证明,为了使其全面运作,需要进行小修改。

  • allowed_params 字典中删除了head 字段
  • 更改了 if 语句以检查参数是否仅等于 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 2021-10-22
    • 1970-01-01
    • 2016-08-27
    • 2012-10-19
    相关资源
    最近更新 更多