How to store and build paths that depend on user input

51 views Asked by At

I am working on a project that will manipulate data stored somewhere else in the computer. This data is organized in case folders and each case folder have the same structure with mostly the same files in the same folders. For example:

CaseDirectory
|- Case1 
|   |- file1.dat 
|   |- Folder1 
|      |- file2.dat 
|- Case2
|   |- file1.dat 
|   |- Folder1 
|      |- file2.dat 

So I want to store the paths for file1, file2, and Folder1 so I know where to read or write the necessary files (in reality there are at least a couple of doze of these paths). Right now I have a file where this paths are stored in string form inside a dictionary and then I use format to substitute the CaseDirectory path and the Case name to obtain the actual path to the files I need.

This works fine so far but have the disadvantage of needing to find the specific key I need for the file I want and is prone to misspellings when accessing the information stored in the dict.

So this is what I have:

from pathlib import Path

paths_storage = {}
paths_storage["FILE1"] = "{case_directory_path}/{case_name}/file1.dat"
paths_storage["FOLDER1"] = "{case_directory_path}/{case_name}/Folder1/"
paths_storage["FILE2"] = "{case_directory_path}/{case_name}/Folder1/file2.dat"

parameters = {
  "case_directory_path": ".",   # The user will select this
  "case_name": "Case2",         # Same
}

file1_path = Path(paths_storage["FILE1"].format(**parameters))

What I want is to wrap this in a way that I can instead do: file1_path = path_generator.FILE1 And obtain the path to the file. In order to do this I am thinking of separating the path string storage and the path generation into 2 classes:

from dataclasses import dataclass
from pathlib impor Path
from typing import ClassVar

@dataclass(frozen=True)
class PathTemplates:
  FILE1: ClassVar[str] = "{case_directory_path}/{case_name}/file1.dat"
  FOLDER1: ClassVar[str] = "{case_directory_path}/{case_name}/Folder1/"
  FILE2: ClassVar[str] = "{case_directory_path}/{case_name}/Folder1/file2.dat"

class PathGenerator
  def __init__():
    self._parameters = {}

  def add_parameter(key:str, value:str):
    self._parameter[key] = value

  def _build_path(path_string: str) -> Path:
    return Path(path_string.format(**self._parameters))


path_generator = PathGenerator()
path_generator.add_parameter("case_directory_path", ".")
path_generator.add_parameter("case_name", "Case2")

file1_path = path_generator.FILE1

Now I am missing an important part that would be how to generate the properties in PathGenerator so they have the same name as the constants in PathTemplates and make them call the _build_path function to generate the path.

I am choosing to use 2 classes giving that the number of string constants in PathTemplates can keep expanding and I didnt want to clutter the PathGenerator class. In addition I can just add the PathTemplates dataclass to my constant.py file.

I am also open to a different way of doing things but ideally I want to end up having a namespace for these paths so I have an easier time working with them.

0

There are 0 answers