Viv
Viv

Reputation: 1584

Python calling a function from different module using a variable defined in current module

There is a module in helpers directory called helper_a.py. It has all classes and functions defined here.

Now I want to call a function from here into another module (not in helpers directory) but one step (cd ..) behind. (init.py) is in helpers directory.

Code and error is as below :

   from helpers.helper_a import *
   import pandas as pd

   query_train_data = "select * from train;"
   df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)

   query_test_data = "select * from test;"
   df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)

   df_train_data = df_train_dataset
   df_test_data = df_test_dataset

   data_prep_steps() # This function is defined in helpers_a


   Error:

    ---------------------------------------------------------------------------
   NameError                                 Traceback (most recent call last)
   <ipython-input-12-3c88b46f341a> in <module>
   ----> 1 data_prep_steps()

   ~\Desktop\Notebooks\helpers\helper_a.py in data_prep_steps()
   ---> 89     # STEP 1 : CONVERT REQUIRED COLS TO CATEGORIES
        90     for df_name in [df_train_data, df_test_data]:
        91         data_prep_class = data_prep(df_name)

NameError: name 'df_train_data' is not defined

Question is that the variable df_train data is defined in the current module and i want to use it in the function defined in helpers_a by calling it also in the current module, but why is it not recognizing this variable??

Note : Also tried assigning global variable status but it still doesnt solve the issue

Upvotes: 0

Views: 131

Answers (1)

CodeSamurai777
CodeSamurai777

Reputation: 3355

There is a solution to create non existing attributes,methods or functions in other modules. It comes from unit testing.


from unittest.mock import patch, PropertyMock

from helpers.helper_a import *
import pandas as pd

query_train_data = "select * from train;"
df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)

query_test_data = "select * from test;"
df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)

df_train_data = df_train_dataset
df_test_data = df_test_dataset

with patch('helpers.helper_a.df_test_data',create=True,new_callable=PropertyMock) as df_test_data_mock: #Create tells to create attribute if it does not exist
    with patch('helpers.helper_a.df_train_data', create=True, new_callable=PropertyMock) as df_train_data_mock:  # PropertyMock is used to mock properties 
        df_test_data_mock.return_value = df_test_data
        df_train_data_mock.return_value = df_train_data
        data_prep_steps()  # This function is defined in helpers_a

Although I agree with comments that passing those values would be way simpler. Also due to python dynamic nature you can just simply set those attributes on the module itself. This method is way simpler but you need to remember to clean up after your done which previous method does for you with context mananger.

   import helpers.helper_a
   import pandas as pd

   query_train_data = "select * from train;"
   df_train_dataset = pd.read_sql(query_train_data, con=engDps1000)

   query_test_data = "select * from test;"
   df_test_dataset = pd.read_sql(query_test_data, con=engDps1000)

   helpers.helper_a.df_train_data = df_train_dataset
   helpers.helper_a.df_test_data = df_test_dataset

   helpers.helper_a.data_prep_steps() # This function is defined in helpers_a

Upvotes: 1

Related Questions