3

I experiment some issues from making tests in another folder than my sample folder:

When created from a different folder, the dataset object haven't the same class.

Explaining: in the test file, I create a dataset object, and launch a parse method who returns a dataset object too. So I have 2 instances of dataset in tests.py, and all of their properties are equals. But when I make a "parseDataset" == "testDataset" in test.py, we reach the first if in the Dataset.eq function because of the 2 objects are not of the same class: 'sample.dataset.Dataset' and 'dataset.Dataset'

Tree:

jojomoon at MacBook-Pro-de-Johann in /tmp/test 
$ tree .
.
├── sample
│   ├── dataset.py
│   └── parse.py
└── tests
    └── tests.py

2 directories, 3 files

dataset.py

import sys
import strings

class Dataset(object):

    def __init__(self, dataTab):
        self.nbFeat = len(dataTab[0])
        self.nbData = len(dataTab)
        # ... other cool stuff but doesn't care here

    def __eq__(self, other):
        if not isinstance(other, self.__class__):
            return NotImplemented
        return self.__dict__ == other.__dict__

parse.py

    def dataset(fileName):
        #... parse the file, and create an array
        return dataset.Dataset(array)
        # the array is the same as the one in tests.py

tests.py

import unittest
import os
# Get current path
current = os.path.dirname(os.path.abspath(__file__))
# Get parent path
parent = os.path.dirname(current)
# Add parent to python paths
sys.path.append(parent)
# Add sample to python paths
sys.path.append(parent + "/sample")
from sample import parse, dataset

class TestStringMethods(unittest.TestCase):

    def test_parse_Unit_normal(self):
        # ... opening a file, writing to it an example dataset.
        # now launching a parse on it:
        outputDS = parse.dataSet("./data/test_parse_Unit_normal.csv")
        # create the expected dataset
        expectTab = [
           ["Data1","Data2","Data3","Data4"],
           ["1","2","3","4"],
           ["5","6","7","8"]
        ]
        expectDS = dataset.Dataset(expectTab)
        # here outputDS.__class__ = <class 'sample.dataset.Dataset'>
        # and  expextDS.__class__ = <class 'dataset.Dataset'>
        # so in the Dataset.__eq__() function, isinstance(other.__class__, self.__class__) = False
        self.assertTrue(outputDS == expectDS)

How can I resolve this problem? Yes I can compare value by value in tests.py or delete the instance condition in dataset class but it's just avoiding the problem, not resolving it.

-EDIT- I understand that the problem is about my imports in tests.py. How, in tests.py can I import my sample module? relative path: ../sample

-EDIT2- Okay, I tested with the following:

tests.py

from ..sample import dataset

and get this error:

Traceback (most recent call last):
  File "tests/tests.py", line 20, in <module>
    from ..sample import dataset
ValueError: attempted relative import beyond top-level package

So with searches I found explanations, but no other solutions that will make my problem of type disappear

-EDIT3- SO when (in my example) I go to the parent directory of my project named dslr, and launch with a python3.7 -m dslr.tests.tests, the following line works: from dslr import sample. It's cool, it's working, but it's messy. I have to change all relative paths in my project. If no one has the solution I'll do it, because I'm running out of patience.

  • 2
    ``dataset`` and ``sample.dataset`` are *not* the same module, they merely share the same source code. Don't modify ``sys.path`` if you don't want duplicate modules. Why do you modify ``sys.path`` in the first place? – MisterMiyagi Apr 23 '20 at 14:19
  • Does this answer your question? [Correct import and package structure now that \_\_init\_\_.py is optional](https://stackoverflow.com/questions/51397669/correct-import-and-package-structure-now-that-init-py-is-optional) – MisterMiyagi Apr 23 '20 at 14:22
  • it was the result of many tests and searches to include my modules. because of my struct is not the same as your example. I begin from ./tests, not from ./ So how to import correctly my sample module from test module? – Johann Monnerie Apr 23 '20 at 15:01
  • how, launching python in ./tests , can I import the module ../sample/dataset.py? – Johann Monnerie Apr 23 '20 at 15:08

1 Answers1

0

So. After a lots of tryings, this post gives me the solution.

$ tree
.
├── Makefile
├── sample
│   ├── parse.py
│   └── dataset.py
└── tests
    └── tests.py

In my makefile, the rule tests launch this command: PYTHONPATH=. python3.7 tests/tests.py -v and in tests.py, we have a simple import like this: import sample.dataset

The "PYTHONPATH=." way is a good solution to avoid packing your project into a folder and launching at the root of this with a python -m myproject.tests.tests.