Search code examples
pythonpython-class

Create my own Custom Methods in seperate file


I would like to place all my methods\functions is a Custom Folder. That way I can import it, it will have all my commonly used functions.

Let go through my current setup. Folder Name: CustomMetrics (lives in my site-packages folder in working environment)

Inside Folder: File 1 main_func.py : This file has all the methods and it code at the bottom I placed this code to make sure file runs when imported

print("before __name__ guard")
if __name__ == '__main__':
    missing_zero_values_table()
    barplots()   
    bool_histograms()  
    univariate_kdeplots()   
    get_mostProbable()
    KDE_feature_vs_Multiclass()
    get_outliers_IQRmethod()
    outlier_filter()
    get_clf_name()
    train_classifier_ensemble_CV()
    plot_mean_feature_importances()
    get_RFE_rankings()  
    get_optimal_n_components() 
    compare_optimal_n_components() 
    tune_classifier_ensemble()
    get_validation_curve()  
    get_learning_curve()
print("after __name__ guard")

File 2: init.py: this file imports all the methods that exists in main file

from CustomMetrics.main_func import missing_zero_values_table
from CustomMetrics.main_func import barplots
from CustomMetrics.main_func import bool_histograms
from CustomMetrics.main_func import univariate_kdeplots
from CustomMetrics.main_func import get_mostProbable
from CustomMetrics.main_func import KDE_feature_vs_Multiclass
from CustomMetrics.main_func import get_outliers_IQRmethod
from CustomMetrics.main_func import outlier_filter
from CustomMetrics.main_func import get_clf_name
from CustomMetrics.main_func import train_classifier_ensemble_CV
from CustomMetrics.main_func import plot_mean_feature_importances
from CustomMetrics.main_func import get_RFE_rankings
from CustomMetrics.main_func import get_optimal_n_components
from CustomMetrics.main_func import compare_optimal_n_components
from CustomMetrics.main_func import tune_classifier_ensemble
from CustomMetrics.main_func import get_validation_curve
from CustomMetrics.main_func import get_learning_curve
from CustomMetrics.main_func import pandas_entropy

File 3: EDA.py in this file I import CustomMetrics. I get no error and I get a print out that the file was complied. I know this because on import I get this print out from the bottom of my main file

before __name__ guard
after __name__ guard  

But Once I try to use one of the functions I get an error function does not exists

call

missing_zero_values_table(df)

error

----> 19 missing_zero_values_table(df)
NameError: name 'missing_zero_values_table' is not defined

How defined in main file

def missing_zero_values_table(df):
    zero_val = df.eq(0).sum() #(df == 0).astype(int).sum(axis=0)
    mis_val = df.isnull().sum() #df.eq(-999).sum() #
    zero_val_pct = 100 * df.eq(0).sum() / len(df)
    mis_val_percent = 100 * df.eq(-999).sum() / len(df)
    mz_table = pd.concat([zero_val, zero_val_pct ,mis_val, mis_val_percent], axis=1)
    mz_table = mz_table.rename(columns = {0 : 'Zero Values',1: '% of Zero Values' ,2 : 'Missing Values', 3 : '% of Total Values'})
    mz_table['Total Zero Missing Values'] = mz_table['Zero Values'] + mz_table['Missing Values']
    mz_table['% Total Zero Missing Values'] = 100 * mz_table['Total Zero Missing Values'] / len(df)
    mz_table['Data Type'] = df.dtypes
    mz_table = mz_table[
        mz_table.iloc[:,2] != 0 ].sort_values( #Missing Values Col#2
    '% of Total Values', ascending=False).round(1)
    print ("Your selected dataframe has " + str(df.shape[1]) + " columns and " + str(df.shape[0]) + " Rows.\n"      
        "There are " + str(mz_table.shape[0]) +
          " columns that have missing values.")
    return mz_table

How or what should I do to see why my import is not working?


Solution

  • Just import everything in the EDA.py file with the line:

    from CustomMetrics.main_func import *

    Using * with import imports everything in the module / file.