Skip to content

Utils

clean_dir(path_to_clean, exception)

Removes all files and directories in the given path if they don't match the exception list.

Parameters:

Name Type Description Default
path_to_clean Union[str, Path]

Directory path to clean. If it is a string, it will be converted to a Path object.

required
exception List[str]

List of files and directories to keep. If a file or directory is in this list, it will not be removed.

required
Source code in make_us_rich/utils/directory_cleaning.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def clean_dir(path_to_clean: Union[str, Path], exception: List[str]) -> None:
    """
    Removes all files and directories in the given path if they don't match the exception list.

    Parameters
    ----------
    path_to_clean : Union[str, Path]
        Directory path to clean. If it is a string, it will be converted to a Path object.
    exception : List[str]
        List of files and directories to keep. If a file or directory is in this list, it will not be removed.
    """
    if isinstance(path_to_clean, str):
        path_to_clean = Path(path_to_clean)

    items_to_remove = [item for item in path_to_clean.iterdir() if item.name not in exception]
    for item in items_to_remove:
        if item.is_dir():
            rmtree(item)
        else:
            item.unlink()

directory_cleaning

clean_dir(path_to_clean, exception)

Removes all files and directories in the given path if they don't match the exception list.

Parameters:

Name Type Description Default
path_to_clean Union[str, Path]

Directory path to clean. If it is a string, it will be converted to a Path object.

required
exception List[str]

List of files and directories to keep. If a file or directory is in this list, it will not be removed.

required
Source code in make_us_rich/utils/directory_cleaning.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def clean_dir(path_to_clean: Union[str, Path], exception: List[str]) -> None:
    """
    Removes all files and directories in the given path if they don't match the exception list.

    Parameters
    ----------
    path_to_clean : Union[str, Path]
        Directory path to clean. If it is a string, it will be converted to a Path object.
    exception : List[str]
        List of files and directories to keep. If a file or directory is in this list, it will not be removed.
    """
    if isinstance(path_to_clean, str):
        path_to_clean = Path(path_to_clean)

    items_to_remove = [item for item in path_to_clean.iterdir() if item.name not in exception]
    for item in items_to_remove:
        if item.is_dir():
            rmtree(item)
        else:
            item.unlink()

load_env

All utility functions for the make_us_rich project.

load_env(file_name, config_dir=None)

Loads the environment variables from the specified .env file. You don't need to specify .env- prefix for the file name. e.g. load_env("my_env") will load .env-my_env file.

You can also specify the directory where the .env file is located. You need to provide the full path to the directory.

Parameters:

Name Type Description Default
file_name str

Name of the .env file.

required
config_dir Optional[str]

Full path to the config directory.

None
Source code in make_us_rich/utils/load_env.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def load_env(file_name: str, config_dir: Optional[str] = None) -> Dict:
    """
    Loads the environment variables from the specified .env file.
    You don't need to specify `.env-` prefix for the file name.
    e.g. `load_env("my_env")` will load `.env-my_env` file.

    You can also specify the directory where the .env file is located. You need to provide
    the full path to the directory.

    Parameters
    ----------
    file_name: str
        Name of the .env file.
    config_dir: str
        Full path to the config directory.
    """
    env_filename = f".env-{file_name}"

    if config_dir is not None:
        return dotenv_values(config_dir.joinpath(env_filename))
    else:
        workdir = Path.cwd()
        if workdir.parts[-1] not in CORRECT_WORKDIR:
            raise ValueError(
                "Your are trying to load the environment variables from the wrong directory. "
                "Please make sure you are inside one of the following directories: "
                f"{CORRECT_WORKDIR}, "
                "or provide the full path to the config directory by adding the `config_dir` parameter."
            )
        config_path = workdir.joinpath("conf")

        if config_path.joinpath("local", env_filename).exists():
            config_file = dotenv_values(config_path.joinpath("local", env_filename))
            return config_file

        config_path = config_path.joinpath("base")
        try:
            config_file = dotenv_values(config_path.joinpath(env_filename))
            return config_file
        except FileNotFoundError:
            raise FileNotFoundError(
                f"File {env_filename} not found in {config_path}."
                "Please make sure you have the correct .env file in the `conf/base` directory or "
                "`conf/local` directory."
            )

random_string

random_string(length=25)

Generate a random string of the specified length.

Parameters:

length: int The length of the string. Default is 25.

Returns:

str: The random generated string.

Source code in make_us_rich/utils/random_string.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def random_string(length: int = 25) -> str:
    """
    Generate a random string of the specified length.

    Parameters:
    -----------
    length: int
        The length of the string. Default is 25.

    Returns:
    --------
    str:
        The random generated string.
    """
    return ''.join(random.choice(CHARACTERS) for _ in range(length))

Last update: 2022-05-04