vineetbansal / wbi

0 stars 0 forks source link

Experiment class not tolerant of missing files #38

Closed vineetbansal closed 8 months ago

vineetbansal commented 9 months ago

All attributes in the Experiment class should be capable of being None in case the relevant file is not found. The code that does depend on those classes should check if these are None. This will let us transition from the current state of affairs to a point where we can start dealing with only necessary files and relying on the classes themselves to provide us relevant information.

Type annotations along with pyright will likely help here immensely.

vineetbansal commented 9 months ago

We need to do something like this. This supports wildcards, which may not be needed in all cases.

import os.path
import glob

class File:
    def __new__(cls, base_folder, *args, **kwargs):
        if path := getattr(cls, 'PATH'):
            files = glob.glob(os.path.join(base_folder, path))
            if len(files) != 1:
                return None
            else:
                instance = super().__new__(cls)
                instance.path = files[0]
                return instance
        else:
            return None

class Foo(File):
    PATH = 'foo.txt'

    def __init__(self, base_folder, x):
        self.x = x

class Bar(File):
    PATH = 'sub*/'

    def __init__(self, base_folder, x, y):
        self.x = x
        self.y = y

class Baz(File):
    PATH = 'subfolder/myfile*.txt'

    def __init__(self, base_folder, x, y):
        self.x = x
        self.y = y

# assuming there's no foo.txt
o = Foo('.', 42)
assert o is None

# as long as there's any folder starting with sub
o = Bar('.', 42, "hello")
print(o.path)
assert o.x == 42
assert o.y == "hello"

# as long as there's a subfolder/myfile*.txt
o = Baz('.', 43, "goodbye")
print(o.path)
assert o.x == 43
assert o.y == "goodbye"