簡體   English   中英

Python在__init__中創建ifif以獲取包和函數

[英]Python create if elif in __init__ for package and function

我將所有已定義的函數組合到一個class並使用ifelif進行操作。
我將在下面解釋。

首先,我有3種類型的plot, combolinebar
我知道如何為這三個圖分別定義函數。

其次,我想使用if將這3個圖組合到一個包中。
我試過的代碼是:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt


class AP(object):

    def __init__(self, dt, date, group, value, value2, value3, value4, value5, value6, TYPE):
        self.dt = dt
        self.date = date
        self.group= carrier
        self.value = value
        self.col1 = col1
        self.col2 = col2
        self.col3 = col3
        self.col4 = col4
        self.TYPE = TYPE

        if self.TYPE == "combo":

            def ComboChart(self, dt, date, group, value, TYPE):
                dataset = pd.read_csv(dt)
                dataset['date'] = pd.to_datetime(dataset[date])
                dataset['yq'] = pd.PeriodIndex(dataset['date'], freq='Q')
                dataset['qtr'] = dataset['date'].dt.quarter
                dataset = dataset.groupby([carrier, 'yq', 'qtr'])[value].sum().reset_index()
                dataset['total.YQGR'] = dataset[value] / dataset.groupby(['qtr', carrier])[value].transform('shift') - 1
                dataset = dataset[np.isfinite(dataset['total.YQGR'])]
                dataset['total.R'] = dataset[value] / dataset.groupby(group)[value].transform('first')
                dataset.yq = dataset.yq.astype(str)

                fig, ax1 = plt.subplots(figsize=(12,7))
                ax2=ax1.twinx()
                sns.lineplot(x='yq',y='total.R', data=dataset, hue=group, ax=ax1, legend = None, palette = ('navy', 'r'), linewidth=5)
                ax1.set_xticklabels(ax1.get_xticks(), rotation=45, fontsize=15, weight = 'heavy')
                ax1.set_xlabel("", fontsize=15)
                ax1.set_ylabel("")
                ax1.set_ylim((0, max(dataset['total.R']) + 0.05))
                sns.barplot(x='yq', y='total.YQGR', data=dataset, hue=group, ax=ax2, palette = ('navy', 'r'))
                ax2.set_yticklabels(['{:.1f}%'.format(a*100) for a in ax2.get_yticks()])
                ax2.set_ylabel("")
                ax2.set_ylim((min(dataset['total.YQGR']) - 0.01, max(dataset['total.YQGR']) + 0.2))
                ax2.get_legend().remove()
                ax2.legend(bbox_to_anchor=(-0.35, 0.5), loc=2, borderaxespad=0., fontsize = 'xx-large')
                for groups in ax2.containers:
                    for bar in groups:
                        if bar.get_height() >= 0:
                            ax2.text(
                                    bar.get_xy()[0] + bar.get_width()/1.5,
                                    bar.get_height() + 0.003, 
                                '{:.1f}%'.format(round(100*bar.get_height(),2)), 
                                    color='black',
                                    horizontalalignment='center',
                                    fontsize = 12, weight = 'heavy'
                                    )
                        else:
                            ax2.text(
                                    bar.get_xy()[0] + bar.get_width()/1.5,
                                    bar.get_height() - 0.008, 
                                '{:.1f}%'.format(round(100*bar.get_height(),2)), 
                                    color='black',
                                    horizontalalignment='center',
                                    fontsize = 12, weight = 'heavy'
                                    )
                ax1.yaxis.set_visible(False)
                ax2.yaxis.set_visible(False)
                ax2.xaxis.set_visible(False)
                ax1.spines["right"].set_visible(False)
                ax1.spines["left"].set_visible(False)
                ax1.spines["top"].set_visible(False)
                ax1.spines["bottom"].set_visible(False)
                ax2.spines["right"].set_visible(False)
                ax2.spines["left"].set_visible(False)
                ax2.spines["top"].set_visible(False)
                ax2.spines["bottom"].set_visible(False)
                ax1.set_title(TYPE, fontsize=20)
                plt.show()

                fig.savefig(TYPE, bbox_inches='tight', dpi=600)

        elif self.TYPE == "line":

            def line(self, dt, date, carrier, value, value2, TYPE):
                dataset = pd.read_csv(dt)
                dataset['date'] = pd.to_datetime(dataset[date])
                dataset['yq'] = pd.PeriodIndex(dataset['date'], freq='Q')
                dataset = dataset.groupby([group, 'yq'])[value, value2].sum().reset_index()
                dataset['Arate'] = dataset[value2] / dataset[value]
                dataset.yq = dataset.yq.astype(str)

                fig, ax1 = plt.subplots(figsize=(12,7))
                sns.lineplot(x='yq', y='Arate', data=dataset, hue=group, ax=ax1, linewidth=5)
                ax1.set_xticklabels(dataset['yq'], rotation=45, fontsize = 15)
                ax1.set_xlabel("")
                ax1.set_ylabel("")
                ax1.set_ylim((min(dataset['Arate']) - 0.05, max(dataset['Arate']) + 0.05))
                ax1.set_yticklabels(['{:.1f}%'.format(a*100) for a in ax1.get_yticks()], fontsize = 18, weight = 'heavy')
                ax1.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=2, borderaxespad=0., ncol = 6)
                ax1.yaxis.grid(True)
                ax1.spines["right"].set_visible(False)
                ax1.spines["left"].set_visible(False)
                ax1.spines["top"].set_visible(False)
                ax1.spines["bottom"].set_visible(False)
                ax1.set_title(TYPE, fontsize = 20)
                plt.show()

                fig.savefig(TYPE, bbox_inches='tight', dpi=600)

        elif self.TYPE == "bar":

            def Bar(self, dt, date, group, value3, value4, value5, value6, TYPE):
                dataset = pd.read_csv(dt, sep = '|')
                dataset['date'] = pd.to_datetime(dataset[date])
                dataset['yq'] = pd.PeriodIndex(dataset['date'], freq='Q')
                dataset = dataset.groupby([group, 'yq'])[value3, value4, value5, value6].sum().reset_index()
                dataset = dataset.groupby([group]).tail(4)
                dataset.yq = dataset.yq.astype(str)
                dataset = pd.melt(dataset, id_vars = [group, 'yq'], value_vars = [value3, value4, value5, value6])
                dataset = dataset.groupby(['variable', group]).value.sum().reset_index()
                dataset['L4Qtr'] = dataset.value / dataset.groupby([group]).value.transform('sum')

                fig, ax1 = plt.subplots(figsize=(12,7))
                sns.barplot(x='variable', y='L4Qtr', data=dataset, hue=group, ax=ax1)
                ax1.set_xticklabels(ax1.get_xticklabels(), fontsize=17.5, weight = 'heavy')
                ax1.set_xlabel("", fontsize=15)
                ax1.set_ylabel("")
                ax1.yaxis.set_ticks(np.arange(0, max(dataset['L4Qtr']) + 0.1, 0.05), False)
                ax1.set_yticklabels(['{:.1f}%'.format(a*100) for a in ax1.get_yticks()], fontsize = 18, weight = 'heavy')
                ax1.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=2, borderaxespad=0., ncol = 6)
                for groups in ax1.containers:
                    for bar in groups:
                        ax1.text(
                                bar.get_xy()[0] + bar.get_width()/2,
                                bar.get_height() + 0.005, 
                            '{:.1f}%'.format(round(100*bar.get_height(),2)), 
                                color=bar.get_facecolor(),
                                horizontalalignment='center',
                                fontsize = 16, weight = 'heavy'
                                    )
                ax1.spines["right"].set_visible(False)
                ax1.spines["left"].set_visible(False)
                ax1.spines["top"].set_visible(False)
                ax1.spines["bottom"].set_visible(False)
                ax1.set_title(TYPE, fontsize=20)
                plt.show()

                fig.savefig(TYPE, bbox_inches='tight', dpi=600)

第三,我希望其他人可以簡單地使用此模塊,如下所示:

import sys
sys.path.append(r'\\users\desktop\module')
from AP import AP as ap

最后,當有人分配TYPE ,它將自動打印並保存它。

# This will plot combo chart
ap(r'\\users\desktop\dataset.csv', date = 'DATEVALUE', group = 'GRPS', value = 'total', TYPE = 'combo')

以上是理想的想法。 我不需要在其中傳遞value2 ~ value6 ,因為combo不使用它們。
當我想要bar

# This will plot bar chart
ap(r'\\users\desktop\dataset.csv', date = 'DATEVALUE', group = 'GRPS', value3 = 'col1', value4 = 'col2', value5 = 'col3', value6 = 'col4', TYPE = 'combo')

由於發生錯誤,我的代碼不正確。 似乎我需要在其中傳遞所有參數。

但是,即使我在其中傳遞了所有參數。 沒有錯誤,但沒有輸出。

有什么建議嗎?

您能解釋一下,為什么不只為類型創建子類嗎? 那不是更直接嗎?

1.)一種方法是使子類對用戶可見,如果您不喜歡,

2.)您可以只創建一種接口類(例如,AP,該類隱藏在幕后使用的類,例如,在設置類型后立即實例化)。

3.)您可以一開始就可以工作,但是我想您必須使這些方法對用戶可見,因為我想您實現該方法的方式是,這些函數僅在init方法中可見(也許您的縮進不可見)非常正確)。 例如,如果您的if語句由init方法執行,則可以將這些方法分配給實例變量,例如self.ComboChart = ComboChart,以便能夠從外部調用該方法。 但是,恕我直言,這不是非常pythonic,而是更hacky /更少面向對象。

因此,我建議1.),如果由於某種原因無法實現,那么我將尋求解決方案2。這兩種解決方案還允許您形成一個干凈的類結構並以這種方式重用代碼,同時您仍然可以如果願意,可以構建簡化的接口類。

方法1的示例(偽代碼)如下所示。 請注意,我尚未對其進行測試,它僅是為了向您提供有關以面向對象的方式拆分邏輯的想法。 我沒有檢查您的整個解決方案,因此例如,您是否始終以相同的方式對數據進行分組,就不知道了。 我可能還會將表示邏輯與數據邏輯分開。 如果您打算以更多方式顯示相同的數據,那將是一個特別好的主意,因為使用當前的邏輯,您每次需要進行另一次重新呈現時,都將重新讀取csv文件並重新處理數據。 因此,在我只想解釋基本原理時,不要使其變得更加復雜,而忽略了這一點,並給出了基類“ Chart”和子類“ ComboChart”的示例。 “ ComboChart”類知道如何讀取/分組數據,因為它繼承了“ Chart”的方法,因此您只需實施一次即可,因此,如果您發現錯誤或以后想對其進行增強,則只需要在一個地方做。 然后,draw_chart方法僅需要根據所選擇的表示做不同的事情。 用戶必須根據要顯示的圖表類型創建子類的實例,然后調用display_chart()。

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt


class Chart(object):
    def __init__(self, dt, date, group, value, value2, value3, value4, value5, value6):
        self.dt = dt
        self.date = date
        self.group= carrier
        self.value = value
        self.col1 = col1
        self.col2 = col2
        self.col3 = col3
        self.col4 = col4
        self.TYPE = TYPE
        self.dataset= None

    def _read_data_(self)        
        dataset = pd.read_csv(dt)
        dataset['date'] = pd.to_datetime(dataset[self.date])
        dataset['yq'] = pd.PeriodIndex(dataset['date'], freq='Q')
        dataset['qtr'] = dataset['date'].dt.quarter
        dataset = dataset.groupby([carrier, 'yq', 'qtr'])[value].sum().reset_index()
        dataset['total.YQGR'] = dataset[value] / dataset.groupby(['qtr', carrier])[value].transform('shift') - 1
        dataset = dataset[np.isfinite(dataset['total.YQGR'])]
        dataset['total.R'] = dataset[value] / dataset.groupby(group)[value].transform('first')
        dataset.yq = dataset.yq.astype(str)
        self.dataset= dataset
        return dataset

    def get_data(self):
        if self.dataset is None:
            self._read_data_()
        return self.dataset

    def group_data(self):
        dataset= self.get_data()
        dataset = dataset.groupby([carrier, 'yq', 'qtr'])[value].sum().reset_index()
        dataset['total.YQGR'] = dataset[value] / dataset.groupby(['qtr', carrier])[value].transform('shift') - 1
        dataset = dataset[np.isfinite(dataset['total.YQGR'])]
        dataset['total.R'] = dataset[value] / dataset.groupby(group)[value].transform('first')
        dataset.yq = dataset.yq.astype(str)
        return dataset

    def draw_chart(self):
        pass


class ComboChart(Chart):
    def draw_chart(self):
        dataset = self.group_data()
        fig, ax1 = plt.subplots(figsize=(12,7))
        ax2=ax1.twinx()
        sns.lineplot(x='yq',y='total.R', data=dataset, hue=group, ax=ax1, legend = None, palette = ('navy', 'r'), linewidth=5)
        ax1.set_xticklabels(ax1.get_xticks(), rotation=45, fontsize=15, weight = 'heavy')
        ax1.set_xlabel("", fontsize=15)
        ax1.set_ylabel("")
        ax1.set_ylim((0, max(dataset['total.R']) + 0.05))
        sns.barplot(x='yq', y='total.YQGR', data=dataset, hue=group, ax=ax2, palette = ('navy', 'r'))
        ax2.set_yticklabels(['{:.1f}%'.format(a*100) for a in ax2.get_yticks()])
        ax2.set_ylabel("")
        ax2.set_ylim((min(dataset['total.YQGR']) - 0.01, max(dataset['total.YQGR']) + 0.2))
        ax2.get_legend().remove()
        ax2.legend(bbox_to_anchor=(-0.35, 0.5), loc=2, borderaxespad=0., fontsize = 'xx-large')
        for groups in ax2.containers:
            for bar in groups:
                if bar.get_height() >= 0:
                    ax2.text(
                            bar.get_xy()[0] + bar.get_width()/1.5,
                            bar.get_height() + 0.003, 
                        '{:.1f}%'.format(round(100*bar.get_height(),2)), 
                            color='black',
                            horizontalalignment='center',
                            fontsize = 12, weight = 'heavy'
                            )
                else:
                    ax2.text(
                            bar.get_xy()[0] + bar.get_width()/1.5,
                            bar.get_height() - 0.008, 
                        '{:.1f}%'.format(round(100*bar.get_height(),2)), 
                            color='black',
                            horizontalalignment='center',
                            fontsize = 12, weight = 'heavy'
                            )
        ax1.yaxis.set_visible(False)
        ax2.yaxis.set_visible(False)
        ax2.xaxis.set_visible(False)
        ax1.spines["right"].set_visible(False)
        ax1.spines["left"].set_visible(False)
        ax1.spines["top"].set_visible(False)
        ax1.spines["bottom"].set_visible(False)
        ax2.spines["right"].set_visible(False)
        ax2.spines["left"].set_visible(False)
        ax2.spines["top"].set_visible(False)
        ax2.spines["bottom"].set_visible(False)
        ax1.set_title(TYPE, fontsize=20)
        plt.show()

        fig.savefig(TYPE, bbox_inches='tight', dpi=600)

第二種方法(帶有接口類)看起來是一樣的,只是您擁有用戶已知的第四類,並且知道如何調用真正的實現。 像這樣:

class YourInterface:
    def __init__(self, your_arguments, TYPE):
        if TYPE == __ 'ComboChart':
            self.client= ComboChart(your_arguments)
        elif TYPE == ....

    def display_chart(self):
        self.client.display_chart()

但這是一個很無聊的課,不是嗎? 如果您的類層次結構非常技術性,並且如果您想避免庫的用戶建立對真實類層次結構的依賴關系,而這種依賴關系可能會在您更改層次結構后立即破壞,那么我只會這樣做。 我猜在大多數情況下,類層次結構保持相對穩定,因此您不需要接口類創建的額外抽象級別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM