python 在我的情况下,如何写代码更美观,可以加快程序?

goqiplq2  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(117)

我有一个类“数据处理”是处理与 Dataframe “df”。
处理df有这样的一些步骤:
首先,从csv文件中获取 Dataframe “df”。
第二,concat df和constant df。
第三,用df计算一些指标。
第四,用df数据判断策略。
第五步,多处理器第四步。
下面是我使用函数“data_processing”编写的代码:

class DataProcessing:
    def __init__(self, constant_df):
        self.constant_df = constant_df 

    # First, get dataframe "df" from csv file. 
    def data_processing(self, code):
        self.df = pd.read_csv(...)

        # Second, concat df and constant df.
        self.df = pd.concat([self.df, self.constant_df]) # Should I naming a new variable?

        # Third,  calculate some indicators with df. 
        self.df = do some calculating...

        # Fourth, judge strategy with df data. 
        if self.df do some judge...

    # Fifth, multiprocessing fourth step.
    def multiprocessing(self, code):
        # use multiprocessing Pool...

或者我可以像这样使用多个函数

class DataProcessing: 
    def __init__(self, constant_df):
        self.constant_df = constant_df 

    # First, get dataframe "df" from csv file. 
    def get_df(self, code):
        self.df = pd.read_csv(...)
        return 

    # Second, concat df and constant df.
    def concat_df(self, code):
        self.get_df(code) # Need run get_df function to get self.df first 
        self.df = pd.concat([self.df, self.constant_df])
        return 
    
    # Third,  calculate some indicators with df. 
    def calculate_indicators(self, code):
        self.concat_df(code) # Need run concat_df function to get self.df that concated 

    # Fourth, judge strategy with df data. 
    def judge_strateg(self, code):
        self.calculate_indicators(code) # Need run calculate_indicators function first 
        if ... # do some judge 

    # Fifth, multiprocessing fourth step.
    def multiprocessing(self):
        # use multiprocessing Pool...

但如果我使用多个函数,我需要先运行函数中的前一个函数,并传递相同的arg“代码”,这让我怀疑是否有另一种方法可以将代码写得漂亮?
谢啦,谢啦

r3i60tvu

r3i60tvu1#

为什么不将代码保存为示例变量呢?

def __init__(self, constant_df, code):
        self.constant_df = constant_df 
        self.code = code
... 
    def judge_strateg(self):
        self.calculate_indicators(self.code)    # use it there

或者你有一个可以完成所有任务的overall-function(如果你因为一些不明显的原因不想把代码作为示例变量)

def __init__(self, constant_df):
        self.constant_df = constant_df 

    # Overall
    def process_all(self, code):
        df.self = get_df()
        df.self = concat_df()
        #...
        df.self = calculate_indicators(self, code)
        #...

    # First, get dataframe "df" from csv file. 
    def get_df(self):
        self.df = pd.read_csv(...)
        return 

    # Second, concat df and constant df.
    def concat_df(self):
        self.get_df(code) # Need run get_df function to get self.df first 
        self.df = pd.concat([self.df, self.constant_df])
        return 
    
    # Third,  calculate some indicators with df. 
    def calculate_indicators(self, code):
        self.concat_df(code) # Need run concat_df function to get self.df that concated 

    # Fourth, judge strategy with df data. 
    def judge_strateg(self, code):
        self.calculate_indicators(code) # Need run calculate_indicators function first 
        if ... # do some judge 

    # Fifth, multiprocessing fourth step.
    def multiprocessing(self):
        # use multiprocessing Pool...

分割代码绝对是个好主意,但传递变量就不是了

相关问题