import python files, with same function names, in different directories

Viewed 48

I have different .py files across different directories.

However, each class has majority same function names, just with a different code approach.

I want to test each of their performances together, using pytest.

test_CompositeKey.py:

import os
import pandas as pd

import pytest
from dagster import execute_solid
from CompositeKey.modules.Gordian.Gordian import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from CompositeKey.modules.Assisted_Gordian.Assisted_Gordian import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning.Assisted_Gordian_with_Cardinality_Pruning import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from pwmf.pipeline.utils import local_mode

df = pd.read_csv('../data/OPIC-scraped-portfolio-public.csv')
df_sample = df.sample(10)

@pytest.mark.unit
def test_add_to_non_key_set():
    curNonKey = {0, 1, 4, 8, 21}
    global NonKeySet
    NonKeySet = {(0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
    
# Changes
    observed_value_G = CompositeKey.modules.Gordian.Gordian.add_to_non_key_set(curNonKey, NonKeySet)
    observed_value_AG = CompositeKey.modules.Assisted_Gordian.Assisted_Gordian.add_to_non_key_set(curNonKey, NonKeySet)
    observed_value_AGC = CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning.Assisted_Gordian_with_Cardinality_Pruning.add_to_non_key_set(curNonKey, NonKeySet)

    expected_output = {(0, 1, 4, 21, 8), (0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
    
    assert (expected_output == observed_value_G) and (expected_output == observed_value_AG) and (expected_output == observed_value_AGC)

test_add_to_non_key_set()

Error:

Traceback (most recent call last):
  File "test_CompositeKey.py", line 135, in <module>
    test_add_to_non_key_set()
  File "test_CompositeKey.py", line 21, in test_add_to_non_key_set
    observed_value_AG = CompositeKey.modules.Gordian.Assisted_Gordian.add_to_non_key_set(curNonKey, NonKeySet)
NameError: name 'CompositeKey' is not defined

I have checked the folder paths and they are correct. In fact, I have various other classes that each import one of these lines each, without problem.

Creating empty __init__.py files in each import files' directories didn't help.


@blhsing 's Solution:

import os
import pandas as pd

import pytest
from dagster import execute_solid
from CompositeKey.modules.Gordian import Gordian as G
from CompositeKey.modules.Assisted_Gordian import Assisted_Gordian as AG
from CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning import Assisted_Gordian_with_Cardinality_Pruning as AGC
from pwmf.pipeline.utils import local_mode

df = pd.read_csv('../data/OPIC-scraped-portfolio-public.csv')
df_sample = df.sample(10)

@pytest.mark.unit
def test_add_to_non_key_set():
    curNonKey = {0, 1, 4, 8, 21}
    global NonKeySet
    NonKeySet = {(0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
    
    observed_value_G = G.add_to_non_key_set(curNonKey, NonKeySet)
    observed_value_AG = AG.add_to_non_key_set(curNonKey, NonKeySet)
    observed_value_AGC = AGC.add_to_non_key_set(curNonKey, NonKeySet)

    expected_output = {(0, 1, 4, 21, 8), (0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
    
    assert (expected_output == observed_value_G) and (expected_output == observed_value_AG) and (expected_output == observed_value_AGC)
1 Answers

When you use:

from CompositeKey.modules.Gordian.Gordian import (add_to_non_key_set ...)

to import add_to_non_key_set from the CompositeKey.modules.Gordian.Gordian module, only the name add_to_non_key_set itself is imported into the namespace of the current module, but not the module CompositeKey.modules.Gordian.Gordian or the package CompositeKey.

You should therefore refer to add_to_non_key_set directly by this name, as you do with add_to_non_key_set(curNonKey, NonKeySet).

If you would rather refer to it through the package CompositeKey, as you have attempted with:

CompositeKey.modules.Gordian.Assisted_Gordian.add_to_non_key_set(curNonKey, NonKeySet)

you should import the package directly into the namespace of the current module with:

import CompositeKey
Related